From b9e36172d9eb16571f44c1e2dd1e60d8f8527f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20F=C3=A9lizard?= Date: Mon, 25 Mar 2013 20:42:12 +0100 Subject: [PATCH 1/2] Remove Ruby warning `lib/simplecov/jruby16_fix.rb:31: warning: assigned but unused variable - e` The previous catch-all `rescue` was used to swallow this exception: `Errno::ENOENT: No such file or directory - test/fixtures/generated_buddha.rb` We now make sure the file is present with `File.file?`. Also reverts 6f711098dbe3dccac067f1a128fc33ef2f845079 and moves the fix into jruby_fix.rb to keep JRuby fixes in the same file. --- lib/simplecov.rb | 2 +- lib/simplecov/{jruby16_fix.rb => jruby_fix.rb} | 11 +++++------ lib/simplecov/result.rb | 12 ++---------- 3 files changed, 8 insertions(+), 17 deletions(-) rename lib/simplecov/{jruby16_fix.rb => jruby_fix.rb} (81%) diff --git a/lib/simplecov.rb b/lib/simplecov.rb index 8bde740e..7df6daab 100644 --- a/lib/simplecov.rb +++ b/lib/simplecov.rb @@ -109,7 +109,7 @@ def usable? @usable = begin require 'coverage' - require 'simplecov/jruby16_fix' + require 'simplecov/jruby_fix' true rescue LoadError false diff --git a/lib/simplecov/jruby16_fix.rb b/lib/simplecov/jruby_fix.rb similarity index 81% rename from lib/simplecov/jruby16_fix.rb rename to lib/simplecov/jruby_fix.rb index 5a447a83..ba2d2a73 100644 --- a/lib/simplecov/jruby16_fix.rb +++ b/lib/simplecov/jruby_fix.rb @@ -1,8 +1,8 @@ -if defined?(JRUBY_VERSION) && JRUBY_VERSION.to_f < 1.7 +if defined?(JRUBY_VERSION) require 'jruby' java_import 'org.jruby.ast.NodeType' - # Coverage for JRuby < 1.7.0 does not work correctly + # Coverage for JRuby does not work correctly # # - does not distinguish lines that cannot be executed # - does (partial) coverage for files loaded before `Coverage.start`. @@ -16,6 +16,8 @@ class << self def result fixed = {} __broken_result__.each do |path, executed_lines| + next unless File.file? path + covered_lines = executed_lines.dup process = lambda do |node| @@ -26,10 +28,7 @@ def result node.child_nodes.each(&process) end - begin - process[JRuby.parse(File.read(path), path)] - rescue => e - end + process[JRuby.parse(File.read(path), path)] if (first = covered_lines.detect { |x| x }) && first > 0 fixed[File.expand_path(path)] = covered_lines diff --git a/lib/simplecov/result.rb b/lib/simplecov/result.rb index 55268100..668fff4c 100644 --- a/lib/simplecov/result.rb +++ b/lib/simplecov/result.rb @@ -19,18 +19,10 @@ class Result # Initialize a new SimpleCov::Result from given Coverage.result (a Hash of filenames each containing an array of # coverage data) def initialize(original_result) - @original_result = original_result.dup - - # Squeeze filepaths (i.e. "/a/b/../c" becomes "/a/c") - @original_result.keys.each do |filename| - expanded_filename = File.expand_path filename - @original_result[expanded_filename] = @original_result.delete filename - end - - @files = SimpleCov::FileList.new(@original_result.map do |filename, coverage| + @original_result = original_result.freeze + @files = SimpleCov::FileList.new(original_result.map do |filename, coverage| SimpleCov::SourceFile.new(filename, coverage) if File.file?(filename) end.compact.sort_by(&:filename)) - filter! end From 221a74ebcbd99d1dab0fe4c73b2835003eeeb560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20F=C3=A9lizard?= Date: Mon, 25 Mar 2013 20:52:09 +0100 Subject: [PATCH 2/2] Remove Ruby warning `lib/simplecov/defaults:57: warning: instance variable @exit_status not initialized` We now ensure that @exit_status is always initialized. Also did some cosmetic arrangements with no functional changes. --- lib/simplecov/defaults.rb | 25 ++++++++++++------------- lib/simplecov/exit_codes.rb | 1 + 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/simplecov/defaults.rb b/lib/simplecov/defaults.rb index 5b85de20..bec4e194 100644 --- a/lib/simplecov/defaults.rb +++ b/lib/simplecov/defaults.rb @@ -42,10 +42,12 @@ at_exit do # Store the exit status of the test run since it goes away after calling the at_exit proc... - if $! #was an exception thrown? - #if it was a SystemExit, use the accompanying status - #otherwise set a non-zero status representing termination by some other exception - #(see github issue 41) + @exit_status = SimpleCov::ExitCodes::SUCCESS + + if $! # was an exception thrown? + # if it was a SystemExit, use the accompanying status + # otherwise set a non-zero status representing termination by some other exception + # (see github issue 41) @exit_status = $!.is_a?(SystemExit) ? $!.status : SimpleCov::ExitCodes::EXCEPTION end @@ -54,12 +56,12 @@ if SimpleCov.result? # Result has been computed covered_percent = SimpleCov.result.covered_percent.round(2) - if @exit_status.to_i == 0 # No other errors - @exit_status = if covered_percent < SimpleCov.minimum_coverage + if @exit_status == SimpleCov::ExitCodes::SUCCESS # No other errors + if covered_percent < SimpleCov.minimum_coverage $stderr.puts "Coverage (%.2f%%) is below the expected minimum coverage (%.2f%%)." % \ [covered_percent, SimpleCov.minimum_coverage] - SimpleCov::ExitCodes::MINIMUM_COVERAGE + @exit_status = SimpleCov::ExitCodes::MINIMUM_COVERAGE elsif (last_run = SimpleCov::LastRun.read) diff = last_run['result']['covered_percent'] - covered_percent @@ -67,18 +69,15 @@ $stderr.puts "Coverage has dropped by %.2f%% since the last time (maximum allowed: %.2f%%)." % \ [diff, SimpleCov.maximum_coverage_drop] - SimpleCov::ExitCodes::MAXIMUM_COVERAGE_DROP + @exit_status = SimpleCov::ExitCodes::MAXIMUM_COVERAGE_DROP end end end - metrics = { - :result => { :covered_percent => covered_percent } - } - SimpleCov::LastRun.write(metrics) + SimpleCov::LastRun.write(:result => {:covered_percent => covered_percent}) end - exit @exit_status if @exit_status # Force exit with stored status (see github issue #5) + exit @exit_status # Force exit with stored status (see github issue #5) end # Autoload config from ~/.simplecov if present diff --git a/lib/simplecov/exit_codes.rb b/lib/simplecov/exit_codes.rb index 04f3ab81..670e2deb 100644 --- a/lib/simplecov/exit_codes.rb +++ b/lib/simplecov/exit_codes.rb @@ -1,4 +1,5 @@ module SimpleCov::ExitCodes + SUCCESS = 0 EXCEPTION = 1 MINIMUM_COVERAGE = 2 MAXIMUM_COVERAGE_DROP = 3