|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +input_path = ARGV.fetch(0) |
| 4 | +output_path = ARGV.fetch(1) |
| 5 | + |
| 6 | +def ignored_lines_for(source_path) |
| 7 | + return [] unless File.file?(source_path) |
| 8 | + |
| 9 | + ignored = [] |
| 10 | + active = false |
| 11 | + |
| 12 | + File.readlines(source_path).each_with_index do |line, index| |
| 13 | + line_number = index + 1 |
| 14 | + |
| 15 | + if line.include?("coverage:ignore-line") |
| 16 | + ignored << line_number |
| 17 | + next |
| 18 | + end |
| 19 | + |
| 20 | + if line.include?("coverage:ignore-start") |
| 21 | + active = true |
| 22 | + ignored << line_number |
| 23 | + next |
| 24 | + end |
| 25 | + |
| 26 | + ignored << line_number if active |
| 27 | + |
| 28 | + active = false if line.include?("coverage:ignore-end") |
| 29 | + end |
| 30 | + |
| 31 | + ignored |
| 32 | +end |
| 33 | + |
| 34 | +def flush_record(output, record, ignored_lines) |
| 35 | + covered = 0 |
| 36 | + found = 0 |
| 37 | + |
| 38 | + filtered = [] |
| 39 | + |
| 40 | + record.each do |line| |
| 41 | + if line.start_with?("DA:") |
| 42 | + line_number, hit_count = line.delete_prefix("DA:").split(",", 2) |
| 43 | + next if ignored_lines.include?(line_number.to_i) |
| 44 | + |
| 45 | + found += 1 |
| 46 | + covered += 1 if hit_count.to_i.positive? |
| 47 | + filtered << line |
| 48 | + elsif line.start_with?("LF:") || line.start_with?("LH:") |
| 49 | + next |
| 50 | + else |
| 51 | + filtered << line |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + filtered.insert(-2, "LF:#{found}\n", "LH:#{covered}\n") if filtered.last == "end_of_record\n" |
| 56 | + output.concat(filtered) |
| 57 | +end |
| 58 | + |
| 59 | +source_file = nil |
| 60 | +ignored_lines = [] |
| 61 | +record = [] |
| 62 | +output = [] |
| 63 | + |
| 64 | +File.readlines(input_path).each do |line| |
| 65 | + if line.start_with?("SF:") |
| 66 | + flush_record(output, record, ignored_lines) unless record.empty? |
| 67 | + source_file = line.delete_prefix("SF:").strip |
| 68 | + ignored_lines = ignored_lines_for(source_file) |
| 69 | + record = [line] |
| 70 | + else |
| 71 | + record << line |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +flush_record(output, record, ignored_lines) unless record.empty? |
| 76 | + |
| 77 | +File.write(output_path, output.join) |
0 commit comments