lib/rspec_tracer/coverage_reporter.rb in rspec-tracer-0.9.3 vs lib/rspec_tracer/coverage_reporter.rb in rspec-tracer-1.0.0

- old
+ new

@@ -86,12 +86,10 @@ @coverage = @coverage.slice(*all_files) all_files.each do |file_path| @coverage[file_path] ||= line_stub(file_path).freeze end - - generate_final_coverage_stat end private def existing_file_diff_coverage(example_id, file_path, coverage_stats) @@ -129,35 +127,10 @@ end all_files.sort end - def generate_final_coverage_stat - total_loc = 0 - covered_loc = 0 - - @coverage.each_pair do |_file_path, line_coverage| - line_coverage.each do |strength| - next if strength.nil? - - total_loc += 1 - covered_loc += 1 if strength.positive? - end - end - - @coverage_stat = { - total_lines: total_loc, - covered_lines: covered_loc, - missed_lines: total_loc - covered_loc, - covered_percent: 0.0 - } - - return if total_loc.zero? - - @coverage_stat[:covered_percent] = (100.0 * covered_loc / total_loc).round(2) - end - def peek_coverage data = ::Coverage.peek_result.select do |file_path, _| file_path.start_with?(RSpecTracer.root) end @@ -165,18 +138,48 @@ data.transform_values { |stats| stats[:lines] } end def line_stub(file_path) + case RUBY_ENGINE + when 'ruby' + ruby_line_stub(file_path) + when 'jruby' + jruby_line_stub(file_path) + end + end + + def ruby_line_stub(file_path) lines = File.foreach(file_path).map { nil } - iseqs = [RubyVM::InstructionSequence.compile_file(file_path)] + iseqs = [::RubyVM::InstructionSequence.compile_file(file_path)] until iseqs.empty? iseq = iseqs.pop iseq.trace_points.each { |line_number, type| lines[line_number - 1] = 0 if type == :line } iseq.each_child { |child| iseqs << child } end + + lines + end + + def jruby_line_stub(file_path) + lines = File.foreach(file_path).map { nil } + root_node = ::JRuby.parse(File.read(file_path)) + + visitor = org.jruby.ast.visitor.NodeVisitor.impl do |_name, node| + if node.newline? + if node.respond_to?(:position) + lines[node.position.line] = 0 + else + lines[node.line] = 0 + end + end + + node.child_nodes.each { |child| child&.accept(visitor) } + end + + root_node.accept(visitor) lines end end end