From 6bded942c4fa21eb15b4e5663f0980a8f947786a Mon Sep 17 00:00:00 2001 From: Robin Miller Date: Sat, 13 Sep 2025 16:11:20 -0600 Subject: [PATCH 1/2] Add branch test coverage when available. Force track all files to prevent implicit file discovery missing files. --- test/json/test_helper.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/test/json/test_helper.rb b/test/json/test_helper.rb index a788804d..24d98170 100644 --- a/test/json/test_helper.rb +++ b/test/json/test_helper.rb @@ -1,14 +1,29 @@ $LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__)) require 'coverage' -Coverage.start + +branches_supported = Coverage.respond_to?(:supported?) && Coverage.supported?(:branches) + +# Coverage module must be started before SimpleCov to work around the cyclic require order. +# Track both branches and lines, or else SimpleCov misleadingly reports 0/0 = 100% for non-branching files. +Coverage.start(lines: true, + branches: branches_supported) begin require 'simplecov' rescue LoadError # Don't fail Ruby's test suite else - SimpleCov.start + SimpleCov.start do + # Enabling both coverage types to let SimpleCov know to output them together in reports + enable_coverage :line + enable_coverage :branch if branches_supported + + # Can't always trust SimpleCov to find files implicitly + track_files 'lib/**/*.rb' + + add_filter 'lib/json/truffle_ruby' unless RUBY_ENGINE == 'truffleruby' + end end require 'json' From 08b9eb0ee66abff31000fffae19ab74c907c4576 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 18 Sep 2025 11:20:21 +0200 Subject: [PATCH 2/2] Only enable test coverage when running the test suite standalone --- Rakefile | 8 ++++++++ test/json/test_helper.rb | 18 ++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Rakefile b/Rakefile index c43acbc6..c1768853 100644 --- a/Rakefile +++ b/Rakefile @@ -6,6 +6,14 @@ include RbConfig require 'rake/testtask' class UndocumentedTestTask < Rake::TestTask def desc(*) end + + def ruby(...) + env_was = ENV["JSON_COVERAGE"] + ENV["JSON_COVERAGE"] = "1" + ret = super + ENV["JSON_COVERAGE"] = env_was + ret + end end PKG_VERSION = File.foreach(File.join(__dir__, "lib/json/version.rb")) do |line| diff --git a/test/json/test_helper.rb b/test/json/test_helper.rb index 24d98170..24cde434 100644 --- a/test/json/test_helper.rb +++ b/test/json/test_helper.rb @@ -1,19 +1,17 @@ $LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__)) -require 'coverage' +if ENV["JSON_COVERAGE"] + # This test helper is loaded inside Ruby's own test suite, so we try to not mess it up. + require 'coverage' -branches_supported = Coverage.respond_to?(:supported?) && Coverage.supported?(:branches) + branches_supported = Coverage.respond_to?(:supported?) && Coverage.supported?(:branches) -# Coverage module must be started before SimpleCov to work around the cyclic require order. -# Track both branches and lines, or else SimpleCov misleadingly reports 0/0 = 100% for non-branching files. -Coverage.start(lines: true, - branches: branches_supported) + # Coverage module must be started before SimpleCov to work around the cyclic require order. + # Track both branches and lines, or else SimpleCov misleadingly reports 0/0 = 100% for non-branching files. + Coverage.start(lines: true, + branches: branches_supported) -begin require 'simplecov' -rescue LoadError - # Don't fail Ruby's test suite -else SimpleCov.start do # Enabling both coverage types to let SimpleCov know to output them together in reports enable_coverage :line