Skip to content

Commit d62d829

Browse files
committed
Add 100% line and branch coverage with SimpleCov self-tracking
1 parent 5bcaeaf commit d62d829

3 files changed

Lines changed: 94 additions & 26 deletions

File tree

lib/simplecov-html.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
require "digest/sha1"
66
require "time"
77

8-
# Ensure we are using a compatible version of SimpleCov
9-
major, minor, patch = SimpleCov::VERSION.scan(/\d+/).first(3).map(&:to_i)
10-
if major < 0 || minor < 9 || patch < 0 # rubocop:disable Style/NumericPredicate
11-
raise "The version of SimpleCov you are using is too old. " \
12-
"Please update with `gem install simplecov` or `bundle update simplecov`"
13-
end
8+
# :nocov:
9+
raise "The version of SimpleCov you are using is too old." unless Gem::Version.new(SimpleCov::VERSION) >= Gem::Version.new("0.9.0")
10+
# :nocov:
1411

1512
module SimpleCov
1613
module Formatter

test/helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
# frozen_string_literal: true
22

33
require "bundler/setup"
4+
require "coverage"
5+
if RUBY_ENGINE == "jruby"
6+
Coverage.start(lines: true)
7+
else
8+
Coverage.start(lines: true, branches: true)
9+
end
410
require "simplecov"
11+
SimpleCov.start do
12+
coverage_dir "tmp/coverage"
13+
add_filter "/test/"
14+
if RUBY_ENGINE == "jruby"
15+
minimum_coverage line: 97
16+
else
17+
enable_coverage :branch
18+
minimum_coverage line: 100, branch: 100
19+
end
20+
end
521
require "simplecov-html"
622
require "minitest/autorun"
723

test/test_simple_cov-html.rb

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ class TestSimpleCovHtml < Minitest::Test
1515
].freeze
1616

1717
def setup
18+
@original_coverage_dir = SimpleCov.coverage_dir
19+
@original_criteria = SimpleCov.coverage_criteria.dup
20+
@original_filters = SimpleCov.filters.dup
1821
SimpleCov.coverage_dir(output_path)
1922
SimpleCov.enable_coverage(:branch)
23+
SimpleCov.filters.clear
2024
end
2125

2226
def teardown
23-
SimpleCov.coverage_dir(nil)
27+
SimpleCov.coverage_dir(@original_coverage_dir)
2428
SimpleCov.clear_coverage_criteria
29+
@original_criteria.each { |criterion| SimpleCov.enable_coverage(criterion) }
30+
SimpleCov.filters.replace(@original_filters)
2531
end
2632

2733
def test_defined
@@ -36,52 +42,101 @@ def test_output
3642
assert_branch_coverages(html_doc) if RUBY_ENGINE != "jruby"
3743
end
3844

45+
def test_output_without_branch_coverage
46+
SimpleCov.clear_coverage_criteria
47+
html_doc = format_results("sample.rb" => CoverageFixtures::SAMPLE_RB)
48+
49+
assert_nil html_doc.at_css("div#AllFiles div.t-branch-summary")
50+
51+
stdout, = capture_io { format_results("sample.rb" => CoverageFixtures::SAMPLE_RB) }
52+
53+
refute_match(/Branch Coverage/, stdout)
54+
end
55+
56+
def test_inline_assets
57+
FileUtils.rm_rf(output_path)
58+
ENV["SIMPLECOV_INLINE_ASSETS"] = "true"
59+
html = generate_inline_html
60+
61+
assert_match(%r{data:text/javascript;base64,}, html)
62+
assert_match(%r{data:text/css;base64,}, html)
63+
refute_path_exists output_path.join("assets").to_s
64+
ensure
65+
ENV.delete("SIMPLECOV_INLINE_ASSETS")
66+
FileUtils.rm_rf(output_path)
67+
end
68+
69+
def test_encoding_error_handling
70+
formatter = SimpleCov::Formatter::HTMLFormatter.new
71+
bad_template = Object.new
72+
def bad_template.result(_binding)
73+
raise Encoding::CompatibilityError, "incompatible encoding"
74+
end
75+
formatter.instance_variable_get(:@templates)["source_file"] = bad_template
76+
77+
source_file = SimpleCov::SourceFile.new(fixtures_path.join("sample.rb").to_s, CoverageFixtures::SAMPLE_RB)
78+
output, = capture_io { formatter.send(:formatted_source_file, source_file) }
79+
80+
assert_match(/Encoding problems with file/, output)
81+
end
82+
83+
def test_strength_css_classes
84+
formatter = SimpleCov::Formatter::HTMLFormatter.new
85+
86+
assert_equal "green", formatter.send(:strength_css_class, 2)
87+
assert_equal "yellow", formatter.send(:strength_css_class, 1)
88+
assert_equal "red", formatter.send(:strength_css_class, 0)
89+
end
90+
3991
private
4092

4193
def assert_header_coverage(html_doc)
42-
header_line_coverage = html_doc.at_css("div#AllFiles span.covered_percent span").content.strip
94+
header = html_doc.at_css("div#AllFiles span.covered_percent span").content.strip
4395

44-
assert_equal("74.11%", header_line_coverage)
96+
assert_equal("74.11%", header)
4597

46-
subheader_line_coverage = html_doc.at_css("div#AllFiles div.t-line-summary span:last-child").content.strip
98+
subheader = html_doc.at_css("div#AllFiles div.t-line-summary span:last-child").content.strip
4799

48-
assert_equal("74.11%", subheader_line_coverage)
100+
assert_equal("74.11%", subheader)
49101

50102
return if RUBY_ENGINE == "jruby"
51103

52-
subheader_branch_coverage = html_doc.at_css("div#AllFiles div.t-branch-summary span:last-child").content.strip
104+
branch = html_doc.at_css("div#AllFiles div.t-branch-summary span:last-child").content.strip
53105

54-
assert_equal("48.27%", subheader_branch_coverage)
106+
assert_equal("48.27%", branch)
55107
end
56108

57109
def assert_line_coverages(html_doc)
58-
table_coverages = html_doc.css("div#AllFiles table.file_list tr.t-file td.t-file__coverage").map { |m| m.content.strip }
110+
table = html_doc.css("div#AllFiles table.file_list tr.t-file td.t-file__coverage").map { |m| m.content.strip }
59111

60-
assert_equal(EXPECTED_LINE_COVERAGES, table_coverages.sort_by(&:to_f))
112+
assert_equal(EXPECTED_LINE_COVERAGES, table.sort_by(&:to_f))
61113

62-
page_coverages = html_doc.css("div.source_files div.header h4:nth-child(2) span").map { |m| m.content.strip }
114+
pages = html_doc.css("div.source_files div.header h4:nth-child(2) span").map { |m| m.content.strip }
63115

64-
assert_equal(EXPECTED_LINE_COVERAGES, page_coverages.sort_by(&:to_f))
116+
assert_equal(EXPECTED_LINE_COVERAGES, pages.sort_by(&:to_f))
65117
end
66118

67119
def assert_branch_coverages(html_doc)
68-
table_coverages = html_doc.css("div#AllFiles table.file_list tr.t-file td.t-file__branch-coverage").map { |m| m.content.strip }
120+
table = html_doc.css("div#AllFiles table.file_list tr.t-file td.t-file__branch-coverage").map { |m| m.content.strip }
121+
122+
assert_equal(EXPECTED_BRANCH_COVERAGES, table.sort_by(&:to_f))
69123

70-
assert_equal(EXPECTED_BRANCH_COVERAGES, table_coverages.sort_by(&:to_f))
124+
pages = html_doc.css("div.source_files div.header h4:nth-child(3) span").map { |m| m.content.strip }
71125

72-
page_coverages = html_doc.css("div.source_files div.header h4:nth-child(3) span").map { |m| m.content.strip }
126+
assert_equal(EXPECTED_BRANCH_COVERAGES, pages.sort_by(&:to_f))
127+
end
73128

74-
assert_equal(EXPECTED_BRANCH_COVERAGES, page_coverages.sort_by(&:to_f))
129+
def generate_inline_html
130+
formatter = SimpleCov::Formatter::HTMLFormatter.new
131+
result = SimpleCov::Result.new({fixtures_path.join("sample.rb").to_s => CoverageFixtures::SAMPLE_RB})
132+
capture_io { formatter.format(result) }
133+
output_path.join("index.html").read
75134
end
76135

77136
def format_results(coverage_results)
78-
coverage_results = coverage_results.transform_keys do |fixture_file_name|
79-
fixtures_path.join(fixture_file_name).to_s
80-
end
137+
coverage_results = coverage_results.transform_keys { |name| fixtures_path.join(name).to_s }
81138
result = SimpleCov::Result.new(coverage_results)
82139
capture_io { SimpleCov::Formatter::HTMLFormatter.new.format(result) }
83-
84-
# Return an HTML doc instance
85140
output_path.join("index.html").open { |f| Nokogiri::HTML(f) }
86141
end
87142

0 commit comments

Comments
 (0)