diff --git a/features/support/env.rb b/features/support/env.rb index 55df4bf3..2d14d5e0 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -24,3 +24,10 @@ end step 'I cd to "project"' end + +# Workaround for https://github.com/cucumber/aruba/pull/125 +Aruba.configure do |config| + config.before_cmd do + set_env('JRUBY_OPTS', '-X-C --1.9') + end +end diff --git a/lib/simplecov.rb b/lib/simplecov.rb index c06ac106..ced71982 100644 --- a/lib/simplecov.rb +++ b/lib/simplecov.rb @@ -29,6 +29,7 @@ def start(adapter=nil, &block) return false unless SimpleCov.usable? require 'coverage' + require 'simplecov/jruby16_fix' load_adapter(adapter) unless adapter.nil? Coverage.start configure(&block) if block_given? diff --git a/lib/simplecov/jruby16_fix.rb b/lib/simplecov/jruby16_fix.rb new file mode 100644 index 00000000..5a447a83 --- /dev/null +++ b/lib/simplecov/jruby16_fix.rb @@ -0,0 +1,43 @@ +if defined?(JRUBY_VERSION) && JRUBY_VERSION.to_f < 1.7 + require 'jruby' + java_import 'org.jruby.ast.NodeType' + + # Coverage for JRuby < 1.7.0 does not work correctly + # + # - does not distinguish lines that cannot be executed + # - does (partial) coverage for files loaded before `Coverage.start`. + # - does not expand a path like `lib/../spec` to `spec`. + # + # This monkey patches Coverage to address those issues + module Coverage + class << self + alias __broken_result__ result + + def result + fixed = {} + __broken_result__.each do |path, executed_lines| + covered_lines = executed_lines.dup + + process = lambda do |node| + if node.node_type == NodeType::NEWLINENODE + pos = node.position + covered_lines[pos.line] ||= 0 + end + node.child_nodes.each(&process) + end + + begin + process[JRuby.parse(File.read(path), path)] + rescue => e + end + + if (first = covered_lines.detect { |x| x }) && first > 0 + fixed[File.expand_path(path)] = covered_lines + end + end + + fixed + end + end + end +end