Skip to content

Commit

Permalink
Change instance_doubles to work with new implementation of reek gen…
Browse files Browse the repository at this point in the history
…erator

Remove unused variable (`@lines`) in `generator_spec.rb`

Upgrade dependency to `reek`
  • Loading branch information
etagwerker committed Sep 13, 2019
1 parent 88f4a5b commit d0acf59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/reek/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def run!(files, config_files)
end

def analyze
@matches = @output.map(&:smells).flatten.group_by(&:source).collect do |file_path, smells|
@matches = @output.flat_map(&:smells).group_by(&:source).collect do |file_path, smells|
{ file_path: file_path,
code_smells: analyze_smells(smells) }
end
Expand Down
2 changes: 1 addition & 1 deletion metric_fu.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "flay", [">= 2.0.1", "~> 2.1"]
s.add_runtime_dependency "churn", ["~> 0.0.35"]
s.add_runtime_dependency "flog", [">= 4.1.1", "~> 4.1"]
s.add_runtime_dependency "reek", [">= 1.3.4"]
s.add_runtime_dependency "reek", [">= 5.4.0"]
s.add_runtime_dependency "cane", [">= 2.5.2", "~> 2.5"]
s.add_runtime_dependency "rails_best_practices", [">= 1.14.3", "~> 1.14"]
s.add_runtime_dependency "metric_fu-Saikuro", [">= 1.1.3", "~> 1.1"]
Expand Down
45 changes: 34 additions & 11 deletions spec/metric_fu/metrics/reek/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "spec_helper"

MetricFu.metrics_require { "reek/generator" }

describe MetricFu::ReekGenerator do
Expand Down Expand Up @@ -42,8 +43,8 @@
before :each do
MetricFu::Configuration.run {}
allow(File).to receive(:directory?).and_return(true)
@reek = MetricFu::ReekGenerator.new
@examiner = @reek.send(:examiner)
@generator = MetricFu::ReekGenerator.new
@examiner = @generator.send(:examiner)
@smell_warning = Reek.const_defined?(:SmellWarning) ? Reek.const_get(:SmellWarning) : Reek.const_get(:Smells).const_get(:SmellWarning)
if @smell_warning.instance_methods.include?(:subclass)
@smell_warning.send(:alias_method, :smell_type, :subclass)
Expand Down Expand Up @@ -99,9 +100,11 @@
smell_type: "Duplication",
lines: [6, 9])
]
@lines = instance_double(@examiner, smells: @smells)
@reek.instance_variable_set(:@output, @lines)
@matches = @reek.analyze
@examiners = [
instance_double(Reek::Examiner, smells: @smells)
]
@generator.instance_variable_set(:@output, @examiners)
@matches = @generator.analyze
end

it "should find the code smell's line numbers" do
Expand Down Expand Up @@ -143,9 +146,11 @@
subclass: "Duplication",
lines: [2, 4]),
]
@lines = instance_double(@examiner, smells: @smells)
@reek.instance_variable_set(:@output, @lines)
@matches = @reek.analyze
@examiners = [
instance_double(Reek::Examiner, smells: @smells)
]
@generator.instance_variable_set(:@output, @examiners)
@matches = @generator.analyze
end

it "uses the subclass field to find the smell type" do
Expand All @@ -154,11 +159,29 @@
end
end

context "with real output, not mocked nor doubled" do
let(:result) do
{:file_path=>"spec/support/samples/alfa.rb", :code_smells=>[{:method=>"Alfa", :message=>"takes parameters ['echo', 'foxtrot', 'golf'] to 3 methods", :type=>"DataClump", :lines=>[2, 3, 4]}, {:method=>"Alfa", :message=>"has no descriptive comment", :type=>"IrresponsibleModule", :lines=>[1]}, {:method=>"Alfa#bravo", :message=>"has unused parameter 'echo'", :type=>"UnusedParameters", :lines=>[2]}, {:method=>"Alfa#bravo", :message=>"has unused parameter 'foxtrot'", :type=>"UnusedParameters", :lines=>[2]}, {:method=>"Alfa#bravo", :message=>"has unused parameter 'golf'", :type=>"UnusedParameters", :lines=>[2]}, {:method=>"Alfa#charlie", :message=>"has unused parameter 'echo'", :type=>"UnusedParameters", :lines=>[3]}, {:method=>"Alfa#charlie", :message=>"has unused parameter 'foxtrot'", :type=>"UnusedParameters", :lines=>[3]}, {:method=>"Alfa#charlie", :message=>"has unused parameter 'golf'", :type=>"UnusedParameters", :lines=>[3]}, {:method=>"Alfa#delta", :message=>"has unused parameter 'echo'", :type=>"UnusedParameters", :lines=>[4]}, {:method=>"Alfa#delta", :message=>"has unused parameter 'foxtrot'", :type=>"UnusedParameters", :lines=>[4]}, {:method=>"Alfa#delta", :message=>"has unused parameter 'golf'", :type=>"UnusedParameters", :lines=>[4]}]}
end
before do
@generator = MetricFu::ReekGenerator.new(dirs_to_reek: ["spec/support/samples"])
@generator.emit
end

it "returns real data" do
@matches = @generator.analyze

expect(@matches.first).to eq(result)
end
end

context "without reek warnings" do
before :each do
@lines = instance_double(@examiner, smells: [])
@reek.instance_variable_set(:@output, @lines)
@matches = @reek.analyze
@examiners = [
instance_double(Reek::Examiner, smells: [])
]
@generator.instance_variable_set(:@output, @examiners)
@matches = @generator.analyze
end

it "returns empty analysis" do
Expand Down

0 comments on commit d0acf59

Please sign in to comment.