rubycrap 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db809cb564df163b709e7d1c7ae2114161644f32
4
+ data.tar.gz: 3d3cf3be27dc9382a1db7835f868848532281f18
5
+ SHA512:
6
+ metadata.gz: c393f12154ab04189f87a9a32d7ee28d0426161f99f798cc74c12b1e13a8fae8384f00102680a5d44519333ca63225b26b5a634bb6cc32409bf6381e941ef59f
7
+ data.tar.gz: 5af210175f7635e59fcb91b8d44714b73075868eecfd5ac3698094933540858abc1d9e12ec8ea3b035a57be281872f396ad583b912667cd293f9cc43bc535f1f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at igj@certi.org.br. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubycrap.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ingo Gerhard Jauch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Rubycrap
2
+
3
+ This is an implementation/interpretation of Alberto Savoia's C.R.A.P. Metric for RUBY (https://www.artima.com/weblogs/viewpost.jsp?thread=215899).
4
+ Rubycrap works with the "flog score" instead of the cyclomatic complexity because be belive it is more meaningful for Ruby.
5
+ It should work with Ruby 1.9+
6
+
7
+
8
+ ## Installation
9
+
10
+ To build the gem you need to do:
11
+ ```
12
+ $ gem build rubycrap.gemspec
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Requirements:
18
+
19
+ Simplecov (https://github.com/colszowka/simplecov with 'simplecov-json' gem) to generate a coverage.json file.
20
+ e.g.
21
+ ```
22
+ $ cd test/testapp;bundle install
23
+ $ COVERAGE=true rspec spec
24
+ ```
25
+ => Should result in coverage/coverage.json
26
+
27
+ To generate the rubycrap metric you need to pass a simplecov .json file as the 1st argument
28
+ ```
29
+ $ ruby -Ilib ./bin/rubycrap test/testapp/coverage/coverage.json
30
+ ```
31
+
32
+ ## Development
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ingojauch/rubycrap. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
39
+
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
data/bin/rubycrap ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubycrap"
4
+ require 'logger'
5
+
6
+ read_filename = ARGV[0]
7
+
8
+ if read_filename == nil
9
+ puts "Please specify an Simplecov coverage.json file as argument"
10
+ exit 1
11
+ end
12
+
13
+ case ARGV[1]
14
+ when '-d','--debug'
15
+ mode = Logger::DEBUG
16
+ else
17
+ mode = Logger::WARN
18
+ end
19
+
20
+ Rubycrap.run(read_filename,mode)
data/lib/rubycrap.rb ADDED
@@ -0,0 +1,169 @@
1
+ require 'parser/current'
2
+ require 'flog_cli'
3
+ require 'json'
4
+ require 'builder'
5
+ require 'logger'
6
+
7
+ class Rubycrap
8
+ @simplecov_information=[]
9
+ @crap_methods=[]
10
+ def self.minfo(object)
11
+ puts ">supports: #{(object.methods - Object.methods).inspect}\n"
12
+ end
13
+
14
+ def self.logger
15
+ @logger ||= Logger.new(STDOUT)
16
+ end
17
+ #
18
+ # => reads the sourcefile with an ast parser to get all the methods, then calculate the method coverage
19
+ #
20
+ def self.process_simplecov_file(file)
21
+ #get filename with its coverage information
22
+ filename = file["filename"]
23
+ logger.debug(filename)
24
+ parse_method_coverage(file,filename)
25
+ end
26
+
27
+ def self.parse_method_coverage(file,filename)
28
+ ast = Parser::CurrentRuby.parse(File.open(filename, "r").read)
29
+ recursive_search_ast(file,ast)
30
+ end
31
+
32
+ def self.calculate_method_coverage(file,startline,lastline)
33
+ # first we get the coverage lines from simplecov
34
+ # start position -1 and number of total lines (-1 if you dont want the end)
35
+ total_lines = lastline-startline
36
+ logger.debug("startline #{startline}")
37
+ logger.debug("latline #{lastline}")
38
+ coveragelinestotal = file["coverage"]
39
+
40
+ logger.debug( "total_lines #{total_lines}")
41
+ coveragelines = coveragelinestotal.slice(startline-1,total_lines)
42
+ logger.debug("coveragelines: #{coveragelines}")
43
+ covered_lines = 0
44
+ coveragelines.each do |line|
45
+ if !(line.to_s.eql? "0" or line.to_s.eql? "")
46
+ covered_lines = covered_lines + 1
47
+ end
48
+ end
49
+ method_coverage = covered_lines.to_f / total_lines.to_f
50
+ logger.debug("method_coverage: #{method_coverage}")
51
+ return method_coverage
52
+ end
53
+
54
+ def self.recursive_search_ast(file,ast)
55
+ begin
56
+ ast.children.each do |child|
57
+ if child.class.to_s == "Parser::AST::Node"
58
+ if (child.type.to_s == "def" or child.type.to_s == "defs")
59
+
60
+ methodname = child.children[0].to_s
61
+ startline = child.loc.line
62
+ lastline = child.loc.last_line
63
+ logger.debug("\nmethodname: #{methodname}")
64
+ method_coverage = calculate_method_coverage(file,startline,lastline)
65
+
66
+ @simplecov_information << {:name => methodname, :coverage => method_coverage , :startline => startline, :lastline => lastline}
67
+ else
68
+ recursive_search_ast(file,child)
69
+ end
70
+ end
71
+ end
72
+ rescue
73
+ # maybe empty source code
74
+ end
75
+ end
76
+
77
+ def self.crap(score,coverage)
78
+ # CRAP(m) = comp(m)^2 * (1 � cov(m)/100)^3 + comp(m)
79
+ comp = score
80
+ cov = coverage
81
+ comp ** 2 * (1 - cov) ** 3 + comp
82
+ end
83
+
84
+ def self.calcualte_crap_from_flog(file)
85
+ begin
86
+ FlogCLI.load_plugins
87
+ options = FlogCLI.parse_options "-qma"
88
+ flogger = FlogCLI.new options
89
+
90
+ flogger.flog file["filename"]
91
+ logger.debug("flogger absolute_filename: #{file["filename"]}")
92
+ flogger.each_by_score nil do |class_method, score, call_list|
93
+ startline = flogger.method_locations[class_method].split(":")[1]
94
+ absolute_filename = flogger.method_locations[class_method].split(":")[0]
95
+ logger.debug("flogger startline: #{startline}")
96
+ #match simplecov line with startine form floc
97
+ element = @simplecov_information.detect {|f| f[:startline] == startline.to_i }
98
+ if element.to_s == ""
99
+ logger.debug("no match with simplecov for logger class_method: #{class_method} startline: #{startline} ")
100
+ else
101
+ logger.debug("flogger class_method: #{class_method} simplecov: #{element}")
102
+ test_coverage = element[:coverage]
103
+ @crap_methods << {:methodname => class_method, :flog_score => score , :filepath => absolute_filename, :startline => startline, :method_coverage => test_coverage, :crap_score => crap(score,test_coverage)}
104
+ end
105
+ end
106
+ rescue
107
+ # something went wrong with flog
108
+ end
109
+ end
110
+
111
+ def self.hasharray_to_html( hashArray )
112
+ # collect all hash keys, even if they don't appear in each hash:
113
+ headers = hashArray.inject([]){|a,x| a |= x.keys ; a} # use array union to find all unique headers/keys
114
+
115
+ html = Builder::XmlMarkup.new(:indent => 2)
116
+ html.table {
117
+ html.tr { headers.each{|h| html.th(h)} }
118
+ hashArray.each do |row|
119
+ html.tr { row.values.each { |value| html.td(value) }}
120
+ end
121
+ }
122
+ return html
123
+ end
124
+
125
+ def self.run(coveragefile,mode)
126
+ logger.level = mode
127
+ coverage = JSON.parse(File.open(coveragefile, "r").read)
128
+
129
+ # file = coverage["files"].first
130
+ #
131
+ # => get all the source filenames from the coverage file
132
+ #
133
+ puts "total files: #{coverage["files"].count}"
134
+ coverage["files"].each.with_index(1) do |file, index|
135
+ logger.debug("file nr. #{index}")
136
+ process_simplecov_file(file)
137
+ calcualte_crap_from_flog(file)
138
+ end
139
+
140
+ @sorted = @crap_methods.sort_by { |k| -k[:crap_score] }
141
+
142
+ puts "\nRESULTS"
143
+ @sorted.each do |element|
144
+ puts "#{element[:crap_score].round}".ljust(6) + "#{element[:methodname]} ---> #{element[:filepath]}:#{element[:startline]}"
145
+ end
146
+
147
+ #
148
+ # buidler sucks
149
+ # it doesnt do thead and tbody
150
+ # and th: isnt accepted in datatables
151
+
152
+
153
+ # <script src="https://code.jquery.com/jquery-2.2.2.min.js" integrity="sha256-36cp2Co+/62rEAAYHLmRCPIych47CvdM+uTBJwSzWjI=" crossorigin="anonymous"></script>
154
+ # <script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.js" crossorigin="anonymous"></script>
155
+ # <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css">
156
+ # <script type="text/javascript">
157
+ # $(document).ready(function(){
158
+ # $('#myTable').DataTable();
159
+ # });
160
+ # </script>
161
+
162
+ # <table id="myTable">
163
+
164
+ File.open("CRAP.html", 'w') { |file| file.write(hasharray_to_html(@sorted)) }
165
+ puts "THE END"
166
+ end
167
+
168
+ end
169
+
@@ -0,0 +1,10 @@
1
+ require 'logger'
2
+
3
+ module Logging
4
+ def logger
5
+ Logging.logger
6
+ end
7
+ def self.logger
8
+ @logger ||= Logger.new(STDOUT)
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Rubycrap
2
+ VERSION = "0.1.0"
3
+ end
data/rubycrap.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubycrap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubycrap"
8
+ spec.version = Rubycrap::VERSION
9
+ spec.authors = ["Ingo Gerhard Jauch","Gustavo Maestri"]
10
+ spec.email = ["ingo.jauch@gmail.com","gumaestri@gmail.com"]
11
+
12
+ spec.summary = "Ruby version of CRAP4J"
13
+ spec.description = ""
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+
18
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
19
+ # delete this section to allow pushing this gem to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = "bin"
28
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_runtime_dependency "parser", '~> 2.3'
34
+ spec.add_runtime_dependency "flog", '~> 4.3'
35
+ spec.add_runtime_dependency "builder", '~> 3.2'
36
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubycrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ingo Gerhard Jauch
8
+ - Gustavo Maestri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.10'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.10'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: parser
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '2.3'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.3'
56
+ - !ruby/object:Gem::Dependency
57
+ name: flog
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '4.3'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '4.3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: builder
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.2'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.2'
84
+ description: ''
85
+ email:
86
+ - ingo.jauch@gmail.com
87
+ - gumaestri@gmail.com
88
+ executables:
89
+ - rubycrap
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - ".travis.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/rubycrap
101
+ - lib/rubycrap.rb
102
+ - lib/rubycrap/logging.rb
103
+ - lib/rubycrap/version.rb
104
+ - rubycrap.gemspec
105
+ homepage: ''
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Ruby version of CRAP4J
130
+ test_files: []
131
+ has_rdoc: