rubycritic 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf087d756a9bf0d14b016b82d75b675870614854
4
- data.tar.gz: 5c17c62548734bd39a5ca405f764473b96cdd24e
3
+ metadata.gz: 1099d50ed1beb525a791af3127cfae40dc525d4e
4
+ data.tar.gz: 2be5a299cd4a4839ca9c096ffd613a3ae0e38647
5
5
  SHA512:
6
- metadata.gz: d0cfeef76646512463c61140b93959f5de1bc6654bc505005f74f5d560c1e5518d5cda6af6cfbcc18b81285c594aad88ae15fce0cd9ef1c143d56ee49300dc81
7
- data.tar.gz: 957ce873afee75b3c4ae67b43d4fd2402bdc3317f7c78f0f2d36d93a0e252eb3d47101a48b83b398df38e887169f1aeafc151231217d2dd580f14316bc6527f9
6
+ metadata.gz: 11c74550a23f8fdcf3d7bcb579d72ab8cf584552bf8ee412bc3c41be13f565d21a5ed73159c615dfc177724c9d73a20eaa9efd6f081da363f2ab7b33f3278ec3
7
+ data.tar.gz: dcfa39fc7b30b9d2ba6a2a3d02d93eacb1f1616cfb3c78dad9f790841172d8b202d93c4c312db95798090f7a733152837d38ce95222df0545cf5fbeadc970657
data/README.md CHANGED
@@ -1,33 +1,55 @@
1
- Ruby Critic
1
+ RubyCritic
2
2
  ===========
3
3
 
4
- Ruby Critic is a tool that detects and reports smells in Ruby code.
4
+ RubyCritic is a gem that wraps around static analysis gems such as [Reek][1]
5
+ and [Flay][2] to provide a quality report of your Ruby code.
6
+
7
+ For example, given the following code:
8
+
9
+ ```ruby
10
+ class Dirty
11
+ def awful(x, y, offset = 0, log = false)
12
+ puts @screen.title
13
+ @screen = widgets.map {|w| w.each {|key| key += 3}}
14
+ puts @screen.contents
15
+ end
16
+ end
17
+ ```
18
+
19
+ It turns something like this:
20
+
21
+ ![Reek output screenshot](http://i.imgur.com/xLtEDOb.png)
22
+
23
+ Into this:
24
+
25
+ ![RubyCritic output screenshot](http://i.imgur.com/SpZ2SJN.png)
5
26
 
6
27
  Installation
7
28
  ------------
8
29
 
9
- Add this line to your application's Gemfile:
30
+ RubyCritic can be installed with the following command:
10
31
 
11
- ```ruby
12
- gem "rubycritic"
32
+ ```bash
33
+ $ gem install rubycritic
13
34
  ```
14
35
 
15
- And then execute:
36
+ If you'd rather install RubyCritic using Bundler, add this line to your
37
+ application's Gemfile:
16
38
 
17
- ```bash
18
- $ bundle
39
+ ```ruby
40
+ gem "rubycritic", :require => false
19
41
  ```
20
42
 
21
- Or just install it yourself:
43
+ And then execute:
22
44
 
23
45
  ```bash
24
- $ gem install rubycritic
46
+ $ bundle
25
47
  ```
26
48
 
27
49
  Usage
28
50
  -----
29
51
 
30
- Running `rubycritic` with no arguments will check all Ruby source files in the
52
+ Running `rubycritic` with no arguments will analyse all the Ruby files in the
31
53
  current directory:
32
54
 
33
55
  ```bash
@@ -39,3 +61,12 @@ Alternatively you can pass `rubycritic` a list of files and directories to check
39
61
  ```bash
40
62
  $ rubycritic app lib/foo.rb
41
63
  ```
64
+
65
+ For a full list of the command-line options run:
66
+
67
+ ```bash
68
+ $ rubycritic --help
69
+ ```
70
+
71
+ [1]: https://github.com/troessner/reek
72
+ [2]: https://github.com/seattlerb/flay
@@ -1,7 +1,9 @@
1
1
  require "rubycritic/active_support/methods"
2
2
  require "rubycritic/analysers/flay"
3
- require "rubycritic/smell_adapters/flay"
3
+ require "rubycritic/analysers/flog"
4
4
  require "rubycritic/analysers/reek"
5
+ require "rubycritic/smell_adapters/flay"
6
+ require "rubycritic/smell_adapters/flog"
5
7
  require "rubycritic/smell_adapters/reek"
6
8
 
7
9
  module Rubycritic
@@ -9,7 +11,7 @@ module Rubycritic
9
11
  class AnalysersRunner
10
12
  include ActiveSupport
11
13
 
12
- ANALYSERS = ["Flay", "Reek"]
14
+ ANALYSERS = ["Flay", "Flog", "Reek"]
13
15
 
14
16
  def initialize(paths)
15
17
  @paths = paths
@@ -6,7 +6,11 @@ module Rubycritic
6
6
  OptionParser.new do |opts|
7
7
  opts.banner = "Usage: rubycritic [options] [paths]"
8
8
 
9
- opts.on_tail("-v", "--version", "Show this version") do
9
+ opts.on("-p", "--path [PATH]", "Set path where report will be saved (tmp/rubycritic by default)") do |path|
10
+ configuration.root = path
11
+ end
12
+
13
+ opts.on_tail("-v", "--version", "Show gem's version") do
10
14
  require "rubycritic/version"
11
15
  puts VERSION
12
16
  exit 0
@@ -0,0 +1,17 @@
1
+ module Rubycritic
2
+ def self.configuration
3
+ @configuration ||= Configuration.new
4
+ end
5
+
6
+ class Configuration
7
+ attr_reader :root
8
+
9
+ def initialize
10
+ self.root = "tmp/rubycritic"
11
+ end
12
+
13
+ def root=(path)
14
+ @root = File.expand_path(path)
15
+ end
16
+ end
17
+ end
@@ -7,7 +7,7 @@ module Rubycritic
7
7
 
8
8
  def initialize(path, line)
9
9
  @pathname = Pathname.new(path)
10
- @line = line
10
+ @line = line.to_i
11
11
  end
12
12
 
13
13
  def file_name
@@ -4,7 +4,6 @@ require "rubycritic/report_generators/view_helpers"
4
4
  module Rubycritic
5
5
 
6
6
  class BaseGenerator
7
- REPORT_DIR = File.expand_path("tmp/rubycritic", Dir.getwd)
8
7
  TEMPLATES_DIR = File.expand_path("../templates", __FILE__)
9
8
  LAYOUT_TEMPLATE = ERB.new(File.read(File.join(TEMPLATES_DIR, "layouts", "application.html.erb")))
10
9
 
@@ -19,7 +18,7 @@ module Rubycritic
19
18
  end
20
19
 
21
20
  def file_directory
22
- raise NotImplementedError.new("The #{self.class} class must implement the #{__method__} method.")
21
+ root_directory
23
22
  end
24
23
 
25
24
  def file_name
@@ -37,7 +36,7 @@ module Rubycritic
37
36
  private
38
37
 
39
38
  def root_directory
40
- REPORT_DIR
39
+ ::Rubycritic.configuration.root
41
40
  end
42
41
  end
43
42
 
@@ -10,10 +10,6 @@ module Rubycritic
10
10
  @file_generators = file_generators.sort { |a, b| a.analysed_file_name <=> b.analysed_file_name }
11
11
  end
12
12
 
13
- def file_directory
14
- REPORT_DIR
15
- end
16
-
17
13
  def file_name
18
14
  "code_index.html"
19
15
  end
@@ -14,7 +14,7 @@ module Rubycritic
14
14
  end
15
15
 
16
16
  def file_directory
17
- File.join(REPORT_DIR, @pathname.dirname)
17
+ File.join(root_directory, @pathname.dirname)
18
18
  end
19
19
 
20
20
  def file_name
@@ -22,7 +22,7 @@ module Rubycritic
22
22
  file.write(generator.render)
23
23
  end
24
24
  end
25
- FileUtils.cp_r(ASSETS_DIR, BaseGenerator::REPORT_DIR)
25
+ FileUtils.cp_r(ASSETS_DIR, ::Rubycritic.configuration.root)
26
26
  code_index_generator.file_href
27
27
  end
28
28
 
@@ -10,10 +10,6 @@ module Rubycritic
10
10
  @smells = smells.sort { |a, b| a.type <=> b.type }
11
11
  end
12
12
 
13
- def file_directory
14
- REPORT_DIR
15
- end
16
-
17
13
  def file_name
18
14
  "smells_index.html"
19
15
  end
@@ -7,7 +7,7 @@ require "rubycritic/smells_status_setter"
7
7
  module Rubycritic
8
8
 
9
9
  class RevisionComparator
10
- SNAPSHOTS_DIR = File.expand_path("tmp/rubycritic/snapshots", Dir.getwd)
10
+ SNAPSHOTS_DIR_NAME = "snapshots"
11
11
 
12
12
  def initialize(smells, source_control_system)
13
13
  @smells_now = smells
@@ -36,7 +36,11 @@ module Rubycritic
36
36
  end
37
37
 
38
38
  def revision_file
39
- @revision_file ||= File.join(SNAPSHOTS_DIR, @source_control_system.head_reference)
39
+ @revision_file ||= File.join(
40
+ ::Rubycritic.configuration.root,
41
+ SNAPSHOTS_DIR_NAME,
42
+ @source_control_system.head_reference
43
+ )
40
44
  end
41
45
 
42
46
  def paths_of_tracked_files
@@ -4,14 +4,18 @@ module Rubycritic
4
4
  module SmellAdapter
5
5
 
6
6
  class Flog
7
+ HIGH_COMPLEXITY_SCORE_THRESHOLD = 25
8
+ VERY_HIGH_COMPLEXITY_SCORE_THRESHOLD = 60
9
+
7
10
  def initialize(flog)
8
11
  @flog = flog
9
12
  end
10
13
 
11
14
  def smells
12
15
  smells = []
13
- @flog.each_by_score do |class_method, score|
14
- smells << create_smell(class_method, score)
16
+ @flog.each_by_score do |class_method, original_score|
17
+ score = original_score.round
18
+ smells << create_smell(class_method, score) if score >= HIGH_COMPLEXITY_SCORE_THRESHOLD
15
19
  end
16
20
  smells
17
21
  end
@@ -20,14 +24,20 @@ module Rubycritic
20
24
 
21
25
  def create_smell(context, score)
22
26
  location = smell_location(context)
23
- message = "has a complexity of #{score.round}"
27
+ message = "has a flog score of #{score}"
28
+ type =
29
+ if score >= VERY_HIGH_COMPLEXITY_SCORE_THRESHOLD
30
+ "VeryHighComplexity"
31
+ else
32
+ "HighComplexity"
33
+ end
24
34
 
25
35
  Smell.new(
26
36
  :locations => [location],
27
37
  :context => context,
28
38
  :message => message,
29
39
  :score => score,
30
- :type => "Complexity"
40
+ :type => type
31
41
  )
32
42
  end
33
43
 
@@ -3,7 +3,7 @@ module Rubycritic
3
3
  class SmellsStatusSetter
4
4
  def initialize(smells_before, smells_now)
5
5
  @smells_before = smells_before || []
6
- @smells_now = smells_now
6
+ @smells_now = smells_now || []
7
7
  end
8
8
 
9
9
  def smells
@@ -1,3 +1,3 @@
1
1
  module Rubycritic
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/lib/rubycritic.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "rubycritic/configuration"
1
2
  require "rubycritic/source_locator"
2
3
  require "rubycritic/analysers_runner"
3
4
  require "rubycritic/smells_aggregator"
@@ -0,0 +1,17 @@
1
+ require "test_helper"
2
+ require "rubycritic/configuration"
3
+
4
+ describe Rubycritic::Configuration do
5
+ describe "#root" do
6
+ it "has a default" do
7
+ Rubycritic.configuration.root.wont_be_empty
8
+ end
9
+
10
+ it "can be configured" do
11
+ default = Rubycritic.configuration.root
12
+ Rubycritic.configuration.root = "foo"
13
+ Rubycritic.configuration.root.must_equal File.expand_path("foo")
14
+ Rubycritic.configuration.root = default
15
+ end
16
+ end
17
+ end
@@ -5,7 +5,7 @@ describe Rubycritic::Location do
5
5
  describe "attribute readers" do
6
6
  before do
7
7
  @path = "./foo.rb"
8
- @line = 42
8
+ @line = "42"
9
9
  @location = Rubycritic::Location.new(@path, @line)
10
10
  end
11
11
 
@@ -14,7 +14,7 @@ describe Rubycritic::Location do
14
14
  end
15
15
 
16
16
  it "has a line number" do
17
- @location.line.must_equal @line
17
+ @location.line.must_equal @line.to_i
18
18
  end
19
19
 
20
20
  it "has a file name" do
@@ -15,7 +15,7 @@ describe Rubycritic::SmellAdapter::Flog do
15
15
 
16
16
  it "has smells with messages" do
17
17
  smell = @adapter.smells.first
18
- smell.message.must_equal "has a complexity of 8"
18
+ smell.message.must_be_kind_of String
19
19
  end
20
20
 
21
21
  it "has smells with scores" do
@@ -3,7 +3,7 @@ require "rubycritic/source_locator"
3
3
 
4
4
  describe Rubycritic::SourceLocator do
5
5
  before do
6
- @original_dir = Dir.pwd
6
+ @original_dir = Dir.getwd
7
7
  Dir.chdir("test/samples/location")
8
8
  end
9
9
 
@@ -1,6 +1,11 @@
1
1
  class AllTheMethods
2
2
  def method_missing(method, *args, &block)
3
- define_method(method) { "I did not exist, but now I do." }
4
- send(method)
3
+ message = "I"
4
+ eval "message = ' did not'"
5
+ eval "message << ' exist,'"
6
+ eval "message << ' but now'"
7
+ eval "message << ' I do.'"
8
+ self.class.send(:define_method, method) { "I did not exist, but now I do." }
9
+ self.send(method)
5
10
  end
6
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubycritic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Simoes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-29 00:00:00.000000000 Z
11
+ date: 2014-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -147,6 +147,7 @@ files:
147
147
  - lib/rubycritic/analysers/reek.rb
148
148
  - lib/rubycritic/analysers_runner.rb
149
149
  - lib/rubycritic/cli.rb
150
+ - lib/rubycritic/configuration.rb
150
151
  - lib/rubycritic/location.rb
151
152
  - lib/rubycritic/report_generators/assets/javascripts/application.js
152
153
  - lib/rubycritic/report_generators/assets/javascripts/jquery-2.1.0.js
@@ -180,6 +181,7 @@ files:
180
181
  - lib/rubycritic/version.rb
181
182
  - rubycritic.gemspec
182
183
  - test/lib/rubycritic/analysers_runner_test.rb
184
+ - test/lib/rubycritic/configuration_test.rb
183
185
  - test/lib/rubycritic/location_test.rb
184
186
  - test/lib/rubycritic/metric_adapters/flay_adapter_test.rb
185
187
  - test/lib/rubycritic/metric_adapters/flog_adapter_test.rb
@@ -226,6 +228,7 @@ specification_version: 4
226
228
  summary: Ruby code smell detector
227
229
  test_files:
228
230
  - test/lib/rubycritic/analysers_runner_test.rb
231
+ - test/lib/rubycritic/configuration_test.rb
229
232
  - test/lib/rubycritic/location_test.rb
230
233
  - test/lib/rubycritic/metric_adapters/flay_adapter_test.rb
231
234
  - test/lib/rubycritic/metric_adapters/flog_adapter_test.rb