From 57e0d4f63fb10a139cb48131b6d375970f44dcb9 Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Fri, 17 Apr 2026 11:20:07 +0200 Subject: [PATCH 1/3] Drop warning and ruby 1.9 support. --- lib/simplecov/source_file.rb | 5 ----- spec/fixtures/issue_1057.erb | 3 +++ spec/source_file_spec.rb | 31 +++++++++++++++++++++++-------- 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 spec/fixtures/issue_1057.erb diff --git a/lib/simplecov/source_file.rb b/lib/simplecov/source_file.rb index 8a2818e4..c4f37f34 100644 --- a/lib/simplecov/source_file.rb +++ b/lib/simplecov/source_file.rb @@ -272,11 +272,6 @@ def lines_strength lines.sum { |line| line.coverage.to_i } end - # Warning to identify condition from Issue #56 - def coverage_exceeding_source_warn - warn "Warning: coverage data provided by Coverage [#{coverage_data['lines'].size}] exceeds number of lines in #{filename} [#{src.size}]" - end - # # Build full branches report # Root branches represent the wrapper of all condition state that diff --git a/spec/fixtures/issue_1057.erb b/spec/fixtures/issue_1057.erb new file mode 100644 index 00000000..5acc0eed --- /dev/null +++ b/spec/fixtures/issue_1057.erb @@ -0,0 +1,3 @@ +<%= 1 %> +<%= 2 %> +<%= 3 %> diff --git a/spec/source_file_spec.rb b/spec/source_file_spec.rb index 30aefcfc..3d242063 100644 --- a/spec/source_file_spec.rb +++ b/spec/source_file_spec.rb @@ -356,22 +356,37 @@ end end - context "simulating potential Ruby 1.9 defect -- see Issue #56" do + context "when coverage data contains more entries than the source has lines" do subject do SimpleCov::SourceFile.new(source_fixture("sample.rb"), COVERAGE_FOR_SAMPLE_RB_WITH_MORE_LINES) end it "has 16 source lines regardless of extra data in coverage array" do - # Do not litter test output with known warning - capture_stderr { expect(subject.lines.count).to eq(16) } + expect(subject.lines.count).to eq(16) end - it "prints a warning to stderr if coverage array contains more data than lines in the file" do - captured_output = capture_stderr do - subject.lines - end + it "does not emit a warning now that excess entries are trimmed" do + expect(capture_stderr { subject.lines }).to eq("") + end + end - expect(captured_output).to match(/^Warning: coverage data provided/) + # Reproduces https://github.com/simplecov-ruby/simplecov/issues/1057 + # ERB compiles a template into Ruby with a trailing `_erbout` line, so when + # `enable_coverage_for_eval` is on (common for Rails view specs), Ruby's + # Coverage reports more lines than the .erb file has. + context "a source file tracked via enable_coverage_for_eval on an ERB template" do + require "erb" + let(:lines) { ERB.new(File.read(erb_file), trim_mode: "<>").src.lines } + let(:erb_file) { source_fixture("issue_1057.erb") } + let(:compiled_line_count) { lines.size } + let(:coverage_data) { {"lines" => Array.new(compiled_line_count, 1), "branches" => {}} } + + subject { SimpleCov::SourceFile.new(erb_file, coverage_data) } + + it "does not emit a warning about coverage exceeding source" do + puts lines + expect(compiled_line_count).to be > File.readlines(erb_file).size + expect(capture_stderr { subject.lines }).to eq("") end end From 4bdd9fe27dfe1a3205cba29cd8214437cce621ea Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Fri, 17 Apr 2026 11:37:02 +0200 Subject: [PATCH 2/3] Remove method and all expectations --- lib/simplecov/source_file.rb | 1 - spec/source_file_spec.rb | 24 ------------------------ 2 files changed, 25 deletions(-) diff --git a/lib/simplecov/source_file.rb b/lib/simplecov/source_file.rb index c4f37f34..73715802 100644 --- a/lib/simplecov/source_file.rb +++ b/lib/simplecov/source_file.rb @@ -252,7 +252,6 @@ def ensure_remove_undefs(file_lines) end def build_lines - coverage_exceeding_source_warn if coverage_data["lines"].size > src.size lines = src.map.with_index(1) do |src, i| SimpleCov::SourceFile::Line.new(src, i, coverage_data["lines"][i - 1]) end diff --git a/spec/source_file_spec.rb b/spec/source_file_spec.rb index 3d242063..ce97a35c 100644 --- a/spec/source_file_spec.rb +++ b/spec/source_file_spec.rb @@ -364,30 +364,6 @@ it "has 16 source lines regardless of extra data in coverage array" do expect(subject.lines.count).to eq(16) end - - it "does not emit a warning now that excess entries are trimmed" do - expect(capture_stderr { subject.lines }).to eq("") - end - end - - # Reproduces https://github.com/simplecov-ruby/simplecov/issues/1057 - # ERB compiles a template into Ruby with a trailing `_erbout` line, so when - # `enable_coverage_for_eval` is on (common for Rails view specs), Ruby's - # Coverage reports more lines than the .erb file has. - context "a source file tracked via enable_coverage_for_eval on an ERB template" do - require "erb" - let(:lines) { ERB.new(File.read(erb_file), trim_mode: "<>").src.lines } - let(:erb_file) { source_fixture("issue_1057.erb") } - let(:compiled_line_count) { lines.size } - let(:coverage_data) { {"lines" => Array.new(compiled_line_count, 1), "branches" => {}} } - - subject { SimpleCov::SourceFile.new(erb_file, coverage_data) } - - it "does not emit a warning about coverage exceeding source" do - puts lines - expect(compiled_line_count).to be > File.readlines(erb_file).size - expect(capture_stderr { subject.lines }).to eq("") - end end context "A file that has inline branches" do From 32e025ffe54a0afc919488f60c556aad7fed3297 Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Thu, 23 Apr 2026 13:59:56 +0200 Subject: [PATCH 3/3] Adjustments from code review --- spec/fixtures/issue_1057.erb | 3 --- spec/helper.rb | 14 -------------- spec/source_file_spec.rb | 4 ++++ 3 files changed, 4 insertions(+), 17 deletions(-) delete mode 100644 spec/fixtures/issue_1057.erb diff --git a/spec/fixtures/issue_1057.erb b/spec/fixtures/issue_1057.erb deleted file mode 100644 index 5acc0eed..00000000 --- a/spec/fixtures/issue_1057.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= 1 %> -<%= 2 %> -<%= 3 %> diff --git a/spec/helper.rb b/spec/helper.rb index 5ff8f497..1fd64a7d 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -16,17 +16,3 @@ def source_fixture(filename) def source_fixture_base_directory @source_fixture_base_directory ||= File.dirname(__FILE__) end - -# Taken from http://stackoverflow.com/questions/4459330/how-do-i-temporarily-redirect-stderr-in-ruby -def capture_stderr - # The output stream must be an IO-like object. In this case we capture it in - # an in-memory IO object so we can return the string value. You can assign any - # IO object here. - previous_stderr = $stderr - $stderr = StringIO.new - yield - $stderr.string -ensure - # Restore the previous value of stderr (typically equal to STDERR). - $stderr = previous_stderr -end diff --git a/spec/source_file_spec.rb b/spec/source_file_spec.rb index ce97a35c..b50992f2 100644 --- a/spec/source_file_spec.rb +++ b/spec/source_file_spec.rb @@ -364,6 +364,10 @@ it "has 16 source lines regardless of extra data in coverage array" do expect(subject.lines.count).to eq(16) end + + it "does not output to stderr" do + expect { subject.lines }.not_to output.to_stderr + end end context "A file that has inline branches" do