Skip to content

Commit b742504

Browse files
Fix Codecov 0%: fix coverage.json, rm gem codecov (#2857)
Our earlier commit e2de938 created the file coverage/codecov-result.json fixing the "Found 0 coverage files" error, but the site still showed 0%. Codecov's backend rejected the upload as "Unusable report ... incorrect data format": the abandoned codecov 0.6.0 gem's formatter emits the deprecated v2 "network" report format (a filename list, a "<<<<<< network" separator, then one line of coverage JSON) which isn't even valid JSON despite the .json name. The pinned Codecov CLI uploaded it without complaint because `--plugin noop` means it never parses the file, so the upload step looked healthy while the backend silently discarded the data. This commit changes the approach for recording coverage results. Now we write coverage/coverage.json with SimpleCov::Formatter::JSONFormatter (from simplecov_json_formatter), whose output Codecov parses natively, and point the CLI --file at it. As before this runs in test:coverage_gaps rather than test_helper.rb, because CI runs with DEFER_COVERAGE set (swapping in SimpleFormatter) and the merged result is only complete once all parallel + system runs finish. This removes the abandoned codecov gem entirely and drops its dead formatter reference from test_helper.rb (it never ran in CI anyway, since DEFER_COVERAGE overrode it). The codecov gem was the only thing pulling in simplecov, including its <0.22 pin, so we now list simplecov and simplecov_json_formatter explicitly: both are required directly (in test_helper.rb and test:coverage_gaps respectively), so declaring them prevents a future simplecov release that stops bundling the JSON formatter from breaking coverage reporting in a surprising way. Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 2fc9ac1 commit b742504

5 files changed

Lines changed: 29 additions & 28 deletions

File tree

.circleci/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ jobs:
177177
# use. This matches the dependency-pinning policy stated at the top of this
178178
# file and mirrors the Heroku CLI install in the deploy job: we never run
179179
# an unpinned, unverified binary fetched over the network. The coverage
180-
# JSON itself is produced by SimpleCov::Formatter::Codecov; see
181-
# test/test_helper.rb. --disable-search + --file restrict the upload to
180+
# JSON itself is produced by SimpleCov::Formatter::JSONFormatter in the
181+
# test:coverage_gaps rake task. --disable-search + --file restrict the upload to
182182
# exactly that file, and --plugin noop skips Codecov's coverage-generation
183183
# plugins (we already have the report). We do NOT pass --fail-on-error, so
184184
# a Codecov outage cannot break an otherwise-green build. The upload token,
@@ -208,7 +208,7 @@ jobs:
208208
echo "** Hash verified. Uploading coverage."
209209
chmod +x codecov
210210
./codecov upload-process --disable-search --plugin noop \
211-
--file coverage/codecov-result.json
211+
--file coverage/coverage.json
212212
rm -f codecov
213213
214214
# I haven't found a reliable way to calculate HEROKU_APP just once,

Gemfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ end
183183

184184
group :test do
185185
gem 'capybara-slow_finder_errors', require: false # ID slow Capybara finders
186-
gem 'codecov', require: false # Report test code coverage
187186
# Pin minitest < 6.0 until minitest-reporters supports it.
188187
# Minitest 6.0 introduced breaking changes that cause minitest-reporters 1.7.1
189188
# to fail silently (tests don't run). Remove this constraint when
@@ -196,7 +195,12 @@ group :test do
196195
# Note: Updating 'rails-controller-testing' to '1.0.5' causes failures
197196
gem 'rails-controller-testing', '~> 1.0' # for `assigns` and `assert_template`
198197
gem 'selenium-webdriver' # Automates browser i/f for Rails system testing
199-
# We don't list "simplecov"; code depends on it & brings it in
198+
# Test coverage. We require both directly: simplecov in test_helper.rb, and
199+
# simplecov_json_formatter in test:coverage_gaps to write coverage/coverage.json
200+
# for the Codecov upload. List both so a future simplecov release that stops
201+
# bundling the JSON formatter can't break us in a surprising way.
202+
gem 'simplecov', require: false
203+
gem 'simplecov_json_formatter', require: false
200204
gem 'webmock', '~> 3.0', require: false # Mock HTTP requests for testing
201205
end
202206

Gemfile.lock

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ GEM
138138
concurrent-ruby
139139
code_analyzer (0.5.5)
140140
sexp_processor
141-
codecov (0.6.0)
142-
simplecov (>= 0.15, < 0.22)
143141
coderay (1.1.3)
144142
commonmarker (2.6.3)
145143
rb_sys (~> 0.9)
@@ -532,7 +530,7 @@ GEM
532530
concurrent-ruby (~> 1.0, >= 1.0.2)
533531
logger
534532
sexp_processor (4.17.5)
535-
simplecov (0.21.2)
533+
simplecov (0.22.0)
536534
docile (~> 1.1)
537535
simplecov-html (~> 0.11)
538536
simplecov_json_formatter (~> 0.1)
@@ -637,7 +635,6 @@ DEPENDENCIES
637635
bundler-audit
638636
capybara-slow_finder_errors
639637
chartkick (~> 5.2)
640-
codecov
641638
commonmarker (~> 2.6.2)
642639
dotenv (~> 3.0)
643640
eslintrb
@@ -690,6 +687,8 @@ DEPENDENCIES
690687
selenium-webdriver
691688
sentry-rails
692689
sentry-ruby
690+
simplecov
691+
simplecov_json_formatter
693692
solid_queue (~> 1.1)
694693
spring (~> 4.1)
695694
sprockets-rails (= 3.5.2)

lib/tasks/default.rake

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -963,20 +963,22 @@ task 'test:coverage_gaps' do
963963

964964
puts "Combined Coverage: #{result.covered_percent.round(2)}%"
965965

966-
# In CI, write coverage/codecov-result.json from the merged result so the
966+
# In CI, write coverage/coverage.json from the merged result so the
967967
# Codecov CLI (see .circleci/config.yml) has a file to upload. This MUST
968968
# happen here, not in test_helper.rb: the test workers run with
969-
# DEFER_COVERAGE set, which swaps in the minimal SimpleFormatter, so the
970-
# Codecov formatter never runs during the test processes. The merged
969+
# DEFER_COVERAGE set, which swaps in the minimal SimpleFormatter, so a
970+
# report formatter never runs during the test processes. The merged
971971
# result is only complete once all parallel + system test runs finish.
972972
#
973-
# We call the gem's inner Codecov::SimpleCov::Formatter directly rather
974-
# than SimpleCov::Formatter::Codecov, because the latter also invokes the
975-
# gem's deprecated built-in network uploader. We upload separately with a
976-
# pinned, hash-verified CLI, so we want ONLY the file-writing half here.
973+
# We use SimpleCov::Formatter::JSONFormatter (from simplecov_json_formatter,
974+
# a dependency of simplecov) because Codecov's backend natively parses its
975+
# output. We previously used the abandoned 'codecov' gem's formatter, but
976+
# it emits the deprecated "network" report format that the current Codecov
977+
# backend rejects as "unusable report ... incorrect data format", silently
978+
# yielding 0% coverage on the site even though the upload itself succeeds.
977979
if ENV['CI']
978-
require 'codecov'
979-
Codecov::SimpleCov::Formatter.new.format(result)
980+
require 'simplecov_json_formatter'
981+
SimpleCov::Formatter::JSONFormatter.new.format(result)
980982
end
981983

982984
# Iterate through files using SimpleCov's own 'missed_lines' logic

test/test_helper.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@
1515
# *MUST* state VERY EARLY that we're in the test environment.
1616
ENV['RAILS_ENV'] ||= 'test'
1717

18-
# Configure SimpleCov formatting before we start it
19-
if ENV['CI']
20-
require 'codecov'
21-
SimpleCov.formatters = [
22-
SimpleCov::Formatter::HTMLFormatter,
23-
SimpleCov::Formatter::Codecov
24-
]
25-
else
26-
SimpleCov.formatters = SimpleCov::Formatter::HTMLFormatter
27-
end
18+
# Configure SimpleCov formatting before we start it.
19+
# The HTML report is for humans; the machine-readable JSON report uploaded to
20+
# Codecov is written separately by the test:coverage_gaps rake task, since CI
21+
# runs with DEFER_COVERAGE set (which swaps in SimpleFormatter below) and the
22+
# merged result is only complete once all parallel + system runs finish.
23+
SimpleCov.formatters = SimpleCov::Formatter::HTMLFormatter
2824

2925
# Start SimpleCov to track coverage
3026
# NOTE: If you change SimpleCov configuration (used locally), you may also

0 commit comments

Comments
 (0)