diff --git a/test/json/test_helper.rb b/test/json/test_helper.rb index cf592deb..5d230037 100644 --- a/test/json/test_helper.rb +++ b/test/json/test_helper.rb @@ -1,14 +1,5 @@ $LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__)) -begin - require 'simplecov' -rescue LoadError - # Don't fail Ruby's test suite -else - SimpleCov.start -end - -require 'json' require 'test/unit' if ENV["JSON_COMPACT"] @@ -35,3 +26,46 @@ require "core_assertions" Test::Unit::TestCase.include Test::Unit::CoreAssertions end + +# The built-in Coverage module (and therefore SimpleCov) need to be activated prior to any require statements. +# But! SimpleCov requires 'json' before activating Coverage measurement, meaning it misses several files. +# +# The solution is to defer any JSON requires from SimpleCov, which works out because we require it ourselves before +# SimpleCov actually uses it for anything. +module JSONTestPatch + def require(name) + if name == 'json' + caller_path = caller_locations.first.path + + return false if caller_path.match? %r(/simplecov/) + end + + super(name) + end +end +Kernel.prepend JSONTestPatch + +begin + require 'simplecov' +rescue LoadError + # Don't fail Ruby's test suite +else + # Override default at_exit or else it will fire when the Rake task process ends early + SimpleCov.external_at_exit = true + Test::Unit.at_exit do + SimpleCov.at_exit_behavior + end + + SimpleCov.start do + # Force SimpleCov to include all files in its report, avoiding accidental require-order misses + track_files 'lib/**/*.rb' + + add_filter 'lib/json/truffle_ruby' unless RUBY_ENGINE == 'truffleruby' + + enable_coverage :branch + primary_coverage :branch + end +end + +# Require must be after SimpleCov is started for it to see the code +require 'json'