diff --git a/Gemfile b/Gemfile index e93bf8f88..2f577f267 100644 --- a/Gemfile +++ b/Gemfile @@ -15,5 +15,6 @@ group :test do gem 'simplecov-rcov-text' end gem "fakefs", :require => "fakefs/safe" + gem 'json' end gemspec :path => File.expand_path('..', __FILE__) diff --git a/lib/metric_fu.rb b/lib/metric_fu.rb index b893a5204..4c27225da 100644 --- a/lib/metric_fu.rb +++ b/lib/metric_fu.rb @@ -35,4 +35,13 @@ def self.tasks_load(tasks_relative_path) end LOADER.setup + + def self.reset + # TODO Don't like how this method needs to know + # all of these class variables that are defined + # in separate classes. + @configuration = nil + @graph = nil + @result = nil + end end diff --git a/spec/metric_fu/formatter/html_spec.rb b/spec/metric_fu/formatter/html_spec.rb index b2df2593e..89462cdcb 100644 --- a/spec/metric_fu/formatter/html_spec.rb +++ b/spec/metric_fu/formatter/html_spec.rb @@ -12,6 +12,10 @@ # for some platforms. @metric_with_graph = MetricFu.configuration.mri? ? :cane : :flay @metric_without_graph = :hotspots + MetricFu.configuration.configure_metrics.each do |metric| + metric.enabled = true if [@metric_with_graph, @metric_without_graph].include?(metric.name) + end + MetricFu.result.add(@metric_with_graph) # metric w/ graph MetricFu.result.add(@metric_without_graph) # metric w/out graph end @@ -67,7 +71,8 @@ def directory(name) context 'when on OS X' do before do - MetricFu.configuration.stub(:platform).and_return('darwin') + MetricFu.configuration.stub(:osx?).and_return(true) + MetricFu.configuration.stub(:is_cruise_control_rb?).and_return(false) end it "can open the results in the browser" do @@ -117,7 +122,8 @@ def directory(name) context 'when on OS X' do before do - MetricFu.configuration.stub(:platform).and_return('darwin') + MetricFu.configuration.stub(:osx?).and_return(true) + MetricFu.configuration.stub(:is_cruise_control_rb?).and_return(false) end it "can open the results in the browser from the custom output directory" do diff --git a/spec/metric_fu/metrics/hotspots/hotspots_spec.rb b/spec/metric_fu/metrics/hotspots/hotspots_spec.rb index 402319de5..f04859262 100644 --- a/spec/metric_fu/metrics/hotspots/hotspots_spec.rb +++ b/spec/metric_fu/metrics/hotspots/hotspots_spec.rb @@ -71,7 +71,7 @@ hotspots.instance_variable_set(:@analyzer, analyzer) result = hotspots.analyze expected = MultiJson.load("{\"methods\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":\"Client#client_requested_sync\",\"file_path\":\"lib/client/client.rb\",\"hash\":7919384682,\"simple_method_name\":\"#client_requested_sync\"},\"details\":{\"reek\":\"found 1 code smells\",\"flog\":\"complexity is 37.9\"}}],\"classes\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":7995629750},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\"}}],\"files\":[{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":-5738801681},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\",\"churn\":\"detected high level of churn (changed 54 times)\"}},{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/foo.rb\",\"hash\":-7081271905},\"details\":{\"churn\":\"detected high level of churn (changed 52 times)\"}}]}") - compare_hashes(MultiJson.load(hotspots.to_h[:hotspots].to_json), expected) + compare_hashes(MultiJson.load(MultiJson.dump(hotspots.to_h[:hotspots])), expected) end it "should put the changes into a hash" do @@ -80,7 +80,7 @@ hotspots.instance_variable_set(:@analyzer, analyzer) hotspots.analyze expected = MultiJson.load("{\"methods\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":\"Client#client_requested_sync\",\"file_path\":\"lib/client/client.rb\",\"hash\":7919384682,\"simple_method_name\":\"#client_requested_sync\"},\"details\":{\"reek\":\"found 1 code smells\",\"flog\":\"complexity is 37.9\"}}],\"classes\":[{\"location\":{\"class_name\":\"Client\",\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":7995629750},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\"}}],\"files\":[{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/client.rb\",\"hash\":-5738801681},\"details\":{\"reek\":\"found 2 code smells\",\"flog\":\"complexity is 37.9\",\"churn\":\"detected high level of churn (changed 54 times)\"}},{\"location\":{\"class_name\":null,\"method_name\":null,\"file_path\":\"lib/client/foo.rb\",\"hash\":-7081271905},\"details\":{\"churn\":\"detected high level of churn (changed 52 times)\"}}]}") - compare_hashes(MultiJson.load(hotspots.to_h[:hotspots].to_json), expected) + compare_hashes(MultiJson.load(MultiJson.dump(hotspots.to_h[:hotspots])), expected) end # really testing the output of analyzed_problems#worst_items it "should return the worst item granularities: files, classes, methods" do diff --git a/spec/run_spec.rb b/spec/run_spec.rb index b2682c3c0..200f25ff4 100644 --- a/spec/run_spec.rb +++ b/spec/run_spec.rb @@ -161,11 +161,7 @@ def data_directory MetricFu::Configuration.run do |config| config.formatters.clear end - - cleanup_fs end - - end context "given other options" do @@ -187,6 +183,10 @@ def data_directory end + after do + cleanup_fs + end + def metric_fu(options = "--no-open") MfDebugger::Logger.capture_output { begin diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 04245e06a..44a92a2a7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,7 @@ require 'rspec/autorun' require 'date' require 'construct' +require 'json' # add lib to the load path just like rubygems does $:.push File.expand_path("../../lib", __FILE__) @@ -29,4 +30,8 @@ def mf_log(msg); mf_debug(msg); end config.after(:suite) do cleanup_test_files end + + config.after(:each) do + MetricFu.reset + end end diff --git a/spec/support/suite.rb b/spec/support/suite.rb index a34454bfb..724440089 100644 --- a/spec/support/suite.rb +++ b/spec/support/suite.rb @@ -35,6 +35,8 @@ def setup_fs MetricFu::Io::FileSystem.stub(:directory).with('base_directory').and_return("tmp/metric_fu/test") MetricFu::Io::FileSystem.stub(:directory).with('output_directory').and_return("tmp/metric_fu/test/output") MetricFu::Io::FileSystem.stub(:directory).with('data_directory').and_return("tmp/metric_fu/test/_data") + MetricFu::Io::FileSystem.stub(:directory).with('code_dirs').and_return(%w(lib)) + MetricFu::Io::FileSystem.stub(:directory).with('scratch_directory').and_return('tmp/metric_fu/test/scratch') end end