Skip to content

Commit d334dbf

Browse files
authored
Merge pull request #856 from robinetmiller/add-branch-coverage
Branch coverage & Explicit File Inclusion
2 parents db89486 + 08b9eb0 commit d334dbf

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

Rakefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ include RbConfig
66
require 'rake/testtask'
77
class UndocumentedTestTask < Rake::TestTask
88
def desc(*) end
9+
10+
def ruby(...)
11+
env_was = ENV["JSON_COVERAGE"]
12+
ENV["JSON_COVERAGE"] = "1"
13+
ret = super
14+
ENV["JSON_COVERAGE"] = env_was
15+
ret
16+
end
917
end
1018

1119
PKG_VERSION = File.foreach(File.join(__dir__, "lib/json/version.rb")) do |line|

test/json/test_helper.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
$LOAD_PATH.unshift(File.expand_path('../../../ext', __FILE__), File.expand_path('../../../lib', __FILE__))
22

3-
require 'coverage'
4-
Coverage.start
3+
if ENV["JSON_COVERAGE"]
4+
# This test helper is loaded inside Ruby's own test suite, so we try to not mess it up.
5+
require 'coverage'
6+
7+
branches_supported = Coverage.respond_to?(:supported?) && Coverage.supported?(:branches)
8+
9+
# Coverage module must be started before SimpleCov to work around the cyclic require order.
10+
# Track both branches and lines, or else SimpleCov misleadingly reports 0/0 = 100% for non-branching files.
11+
Coverage.start(lines: true,
12+
branches: branches_supported)
513

6-
begin
714
require 'simplecov'
8-
rescue LoadError
9-
# Don't fail Ruby's test suite
10-
else
11-
SimpleCov.start
15+
SimpleCov.start do
16+
# Enabling both coverage types to let SimpleCov know to output them together in reports
17+
enable_coverage :line
18+
enable_coverage :branch if branches_supported
19+
20+
# Can't always trust SimpleCov to find files implicitly
21+
track_files 'lib/**/*.rb'
22+
23+
add_filter 'lib/json/truffle_ruby' unless RUBY_ENGINE == 'truffleruby'
24+
end
1225
end
1326

1427
require 'json'

0 commit comments

Comments
 (0)