|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require "helper" |
| 4 | +require "coverage" |
| 5 | +require "tempfile" |
4 | 6 |
|
5 | 7 | RSpec.describe SimpleCov::SimulateCoverage do |
6 | 8 | describe ".call" do |
7 | 9 | let(:fixture) { source_fixture("sample.rb") } |
8 | 10 |
|
| 11 | + # TruffleRuby doesn't implement Coverage.line_stub at all, and JRuby's |
| 12 | + # implementation returns the wrong length for multi-line statements. |
| 13 | + # The contexts below assert the exact shape line_stub produces and are |
| 14 | + # gated accordingly. |
| 15 | + has_line_stub = Coverage.respond_to?(:line_stub) |
| 16 | + line_stub_handles_multiline = has_line_stub && RUBY_ENGINE == "ruby" |
| 17 | + |
9 | 18 | it "produces a hash with lines/branches/methods keys" do |
10 | 19 | result = described_class.call(fixture) |
11 | 20 | expect(result.keys).to contain_exactly("lines", "branches", "methods") |
12 | 21 | end |
13 | 22 |
|
14 | | - it "classifies the file's lines via LinesClassifier" do |
| 23 | + it "classifies the file's lines as an Array" do |
15 | 24 | result = described_class.call(fixture) |
16 | 25 | expect(result["lines"]).to be_an(Array) |
17 | 26 | expect(result["lines"]).not_to be_empty |
|
22 | 31 | expect(result["branches"]).to eq({}) |
23 | 32 | expect(result["methods"]).to eq({}) |
24 | 33 | end |
| 34 | + |
| 35 | + # Regression for https://github.com/simplecov-ruby/simplecov/issues/654. |
| 36 | + # A multi-line statement (method chain, hash literal, etc.) used to count |
| 37 | + # every continuation line as relevant when the file was tracked but not |
| 38 | + # loaded — even though Ruby's Coverage module marks the continuations as |
| 39 | + # nil for a loaded file. The two paths now agree. |
| 40 | + context "with a multi-line method chain", if: line_stub_handles_multiline do |
| 41 | + let(:source) { <<~RUBY } |
| 42 | + def show |
| 43 | + @product = base_scope |
| 44 | + .includes(colors_products: :color) |
| 45 | + .find(params[:id]) |
| 46 | + end |
| 47 | + RUBY |
| 48 | + |
| 49 | + it "returns the same line classification Coverage produces for a loaded file" do |
| 50 | + with_tmp_source(source) do |path| |
| 51 | + # Coverage.line_stub is what Ruby would have produced if the file |
| 52 | + # were required — the def + first assignment line are relevant, |
| 53 | + # the chained calls and `end` are not. |
| 54 | + expect(described_class.call(path)["lines"]).to eq([0, 0, nil, nil, nil]) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # Coverage.line_stub doesn't understand SimpleCov's `# :nocov:` toggles, |
| 60 | + # so the overlay step must demote those lines to nil. |
| 61 | + context "with a :nocov: block", if: has_line_stub do |
| 62 | + let(:source) { <<~RUBY } |
| 63 | + def shown |
| 64 | + 1 |
| 65 | + end |
| 66 | + # :nocov: |
| 67 | + def hidden |
| 68 | + 2 |
| 69 | + end |
| 70 | + # :nocov: |
| 71 | + RUBY |
| 72 | + |
| 73 | + it "demotes the :nocov: lines (and the toggles themselves) to nil" do |
| 74 | + with_tmp_source(source) do |path| |
| 75 | + # `def shown` + `1` + `end` for the visible method are relevant; |
| 76 | + # everything from the opening :nocov: through the closing one is nil. |
| 77 | + expect(described_class.call(path)["lines"]).to eq([0, 0, nil, nil, nil, nil, nil, nil]) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + # Same overlay path, but with the new `# simplecov:disable line` directive. |
| 83 | + context "with a simplecov:disable line range", if: has_line_stub do |
| 84 | + let(:source) { <<~RUBY } |
| 85 | + def shown |
| 86 | + 1 |
| 87 | + end |
| 88 | + # simplecov:disable line |
| 89 | + def hidden |
| 90 | + 2 |
| 91 | + end |
| 92 | + # simplecov:enable line |
| 93 | + RUBY |
| 94 | + |
| 95 | + it "demotes the disabled range to nil" do |
| 96 | + with_tmp_source(source) do |path| |
| 97 | + expect(described_class.call(path)["lines"]).to eq([0, 0, nil, nil, nil, nil, nil, nil]) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + context "when the file does not exist" do |
| 103 | + it "returns the empty-shape hash without raising" do |
| 104 | + expect(described_class.call("/no/such/file.rb")) |
| 105 | + .to eq("lines" => [], "branches" => {}, "methods" => {}) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + context "when Coverage.line_stub raises SyntaxError" do |
| 110 | + it "falls back to LinesClassifier's raw output" do |
| 111 | + allow(Coverage).to receive(:line_stub).and_raise(SyntaxError, "boom") |
| 112 | + # With the fallback, every non-blank/non-comment line is relevant — |
| 113 | + # the historical (pre-#654) behavior. |
| 114 | + with_tmp_source("a = 1\nb = 2\n") do |path| |
| 115 | + expect(described_class.call(path)["lines"]).to eq([0, 0]) |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + # Simulates JRuby / TruffleRuby, where Coverage.line_stub doesn't exist. |
| 121 | + # Runs on every engine so the fallback branch stays exercised on MRI. |
| 122 | + context "when Coverage doesn't expose line_stub" do |
| 123 | + it "falls back to LinesClassifier's raw output" do |
| 124 | + allow(Coverage).to receive(:respond_to?).and_call_original |
| 125 | + allow(Coverage).to receive(:respond_to?).with(:line_stub).and_return(false) |
| 126 | + with_tmp_source("a = 1\nb = 2\n") do |path| |
| 127 | + expect(described_class.call(path)["lines"]).to eq([0, 0]) |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def with_tmp_source(content) |
| 133 | + Tempfile.create(["sc654", ".rb"]) do |f| |
| 134 | + f.write(content) |
| 135 | + f.close |
| 136 | + yield f.path |
| 137 | + end |
| 138 | + end |
25 | 139 | end |
26 | 140 | end |
0 commit comments