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 a788804d..24cde434 100644 --- a/test/json/test_helper.rb +++ b/test/json/test_helper.rb @@ -1,14 +1,27 @@ $LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__)) -require 'coverage' -Coverage.start +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) + + # 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'