Skip to content

Commit 5bcaeaf

Browse files
committed
Refactor to simplify .rubocop.yml and fix offenses
1 parent 92f336b commit 5bcaeaf

4 files changed

Lines changed: 181 additions & 259 deletions

File tree

.rubocop.yml

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ AllCops:
1313
- "vendor/**/*"
1414
- ".git/**/*"
1515

16-
Bundler/DuplicatedGem:
17-
Enabled: false
18-
19-
Gemspec/RequiredRubyVersion:
20-
Enabled: false
21-
2216
Layout/AccessModifierIndentation:
2317
EnforcedStyle: outdent
2418

@@ -29,19 +23,18 @@ Metrics/BlockNesting:
2923
Max: 2
3024

3125
Metrics/ClassLength:
32-
Enabled: false
26+
CountAsOne:
27+
- array
28+
- hash
29+
30+
Metrics/ModuleLength:
31+
CountAsOne:
32+
- array
33+
- hash
3334

3435
Layout/LineLength:
3536
AllowURI: true
36-
Enabled: false
37-
38-
Metrics/MethodLength:
39-
CountComments: false
40-
Max: 10
41-
42-
Metrics/ParameterLists:
43-
Max: 4
44-
CountKeywordArgs: true
37+
Max: 160
4538

4639
Naming/FileName:
4740
Exclude:
@@ -55,34 +48,12 @@ Style/CollectionMethods:
5548
find: "detect"
5649
find_all: "select"
5750

58-
Style/Documentation:
59-
Enabled: false
60-
61-
Style/DoubleNegation:
62-
Enabled: false
63-
64-
Style/ExpandPathArguments:
65-
Enabled: false
66-
6751
Style/FrozenStringLiteralComment:
6852
Enabled: true
6953

70-
Style/MutableConstant:
71-
Exclude:
72-
- "lib/simplecov-html/version.rb"
73-
74-
Style/NumericPredicate:
75-
Enabled: false
76-
77-
Style/RegexpLiteral:
78-
Enabled: false
79-
8054
Style/StringLiterals:
8155
EnforcedStyle: double_quotes
8256

83-
Style/SymbolArray:
84-
Enabled: false
85-
8657
Style/TrailingCommaInHashLiteral:
8758
EnforcedStyleForMultiline: "comma"
8859

lib/simplecov-html.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
# Ensure we are using a compatible version of SimpleCov
99
major, minor, patch = SimpleCov::VERSION.scan(/\d+/).first(3).map(&:to_i)
10-
if major < 0 || minor < 9 || patch < 0
10+
if major < 0 || minor < 9 || patch < 0 # rubocop:disable Style/NumericPredicate
1111
raise "The version of SimpleCov you are using is too old. " \
1212
"Please update with `gem install simplecov` or `bundle update simplecov`"
1313
end
1414

1515
module SimpleCov
1616
module Formatter
17+
# Generates an HTML coverage report from SimpleCov results.
1718
class HTMLFormatter
1819
# Only have a few content types, just hardcode them
1920
CONTENT_TYPES = {
@@ -53,18 +54,16 @@ def branchable_result?
5354
end
5455

5556
def line_status?(source_file, line)
56-
if branchable_result? && source_file.line_with_missed_branch?(line.number)
57-
"missed-branch"
58-
else
59-
line.status
60-
end
57+
branchable_result? && source_file.line_with_missed_branch?(line.number) ? "missed-branch" : line.status
6158
end
6259

6360
def output_message(result)
6461
output = "Coverage report generated for #{result.command_name} to #{output_path}."
6562
output += "\nLine Coverage: #{result.covered_percent.floor(2)}% (#{result.covered_lines} / #{result.total_lines})"
6663

67-
output += "\nBranch Coverage: #{result.coverage_statistics[:branch].percent.floor(2)}% (#{result.covered_branches} / #{result.total_branches})" if branchable_result?
64+
if branchable_result?
65+
output += "\nBranch Coverage: #{result.coverage_statistics[:branch].percent.floor(2)}% (#{result.covered_branches} / #{result.total_branches})"
66+
end
6867
output
6968
end
7069

@@ -78,11 +77,9 @@ def output_path
7877
end
7978

8079
def asset_output_path
81-
return @asset_output_path if defined?(@asset_output_path) && @asset_output_path
82-
83-
@asset_output_path = File.join(output_path, "assets", SimpleCov::Formatter::HTMLFormatter::VERSION)
84-
FileUtils.mkdir_p(@asset_output_path)
85-
@asset_output_path
80+
@asset_output_path ||= File.join(output_path, "assets", SimpleCov::Formatter::HTMLFormatter::VERSION).tap do |path|
81+
FileUtils.mkdir_p(path)
82+
end
8683
end
8784

8885
def assets_path(name)
@@ -95,10 +92,7 @@ def asset_inline(name)
9592
path = File.join(@public_assets_dir, name)
9693
# Equivalent to `Base64.strict_encode64(File.read(path))` but without depending on Base64
9794
base64_content = [File.read(path)].pack("m0")
98-
99-
content_type = CONTENT_TYPES[File.extname(name)]
100-
101-
"data:#{content_type};base64,#{base64_content}"
95+
"data:#{CONTENT_TYPES[File.extname(name)]};base64,#{base64_content}"
10296
end
10397

10498
# Returns the html for the given source_file

test/coverage_fixtures.rb

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# frozen_string_literal: true
2+
3+
# Coverage data fixtures adapted from simplecov's spec/source_file_spec.rb
4+
module CoverageFixtures
5+
BRANCHES_RB = {
6+
"lines" => [1, 1, 1, nil, 1, nil, 1, 0, nil, 1, nil, nil, nil],
7+
"branches" => {
8+
[:if, 0, 3, 4, 3, 21] => {[:then, 1, 3, 4, 3, 10] => 0, [:else, 2, 3, 4, 3, 21] => 1},
9+
[:if, 3, 5, 4, 5, 26] => {[:then, 4, 5, 16, 5, 20] => 1, [:else, 5, 5, 23, 5, 26] => 0},
10+
[:if, 6, 7, 4, 11, 7] => {[:then, 7, 8, 6, 8, 10] => 0, [:else, 8, 10, 6, 10, 9] => 1},
11+
},
12+
}.freeze
13+
14+
SAMPLE_RB = {
15+
"lines" => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil, nil, nil, nil, nil, nil, nil, nil],
16+
}.freeze
17+
18+
INLINE_RB = {
19+
"lines" => [1, 1, 1, nil, 1, 1, 0, nil, 1, nil, nil, nil, nil],
20+
"branches" => {
21+
[:if, 0, 3, 11, 3, 33] => {[:then, 1, 3, 23, 3, 27] => 1, [:else, 2, 3, 30, 3, 33] => 0},
22+
[:if, 3, 6, 6, 10, 9] => {[:then, 4, 7, 8, 7, 12] => 0, [:else, 5, 9, 8, 9, 11] => 1},
23+
},
24+
}.freeze
25+
26+
NEVER_RB = {"lines" => [nil, nil], "branches" => {}}.freeze
27+
28+
NOCOV_COMPLEX_RB = {
29+
"lines" => [nil, nil, 1, 1, nil, 1, nil, nil, nil, 1, nil, nil, 1, nil, nil, 0, nil, 1, nil, 0, nil, nil, 1, nil, nil, nil, nil],
30+
"branches" => {
31+
[:if, 0, 6, 4, 11, 7] => {[:then, 1, 7, 6, 7, 7] => 0, [:else, 2, 10, 6, 10, 7] => 1},
32+
[:if, 3, 13, 4, 13, 24] => {[:then, 4, 13, 4, 13, 12] => 1, [:else, 5, 13, 4, 13, 24] => 0},
33+
[:while, 6, 16, 4, 16, 27] => {[:body, 7, 16, 4, 16, 12] => 2},
34+
[:case, 8, 18, 4, 24, 7] => {[:when, 9, 20, 6, 20, 11] => 0, [:when, 10, 23, 6, 23, 10] => 1, [:else, 11, 18, 4, 24, 7] => 0},
35+
},
36+
}.freeze
37+
38+
NESTED_BRANCHES_RB = {
39+
"lines" => [nil, nil, 1, 1, 1, 1, 1, 1, nil, nil, 0, nil, nil, nil, nil],
40+
"branches" => {
41+
[:while, 0, 7, 8, 7, 31] => {[:body, 1, 7, 8, 7, 16] => 2},
42+
[:if, 2, 6, 6, 9, 9] => {[:then, 3, 7, 8, 8, 11] => 1, [:else, 4, 6, 6, 9, 9] => 0},
43+
[:if, 5, 5, 4, 12, 7] => {[:then, 6, 6, 6, 9, 9] => 1, [:else, 7, 11, 6, 11, 11] => 0},
44+
},
45+
}.freeze
46+
47+
CASE_RB = {
48+
"lines" => [1, 1, 1, nil, 0, nil, 1, nil, 0, nil, 0, nil, nil, nil],
49+
"branches" => {
50+
[:case, 0, 3, 4, 12, 7] => {
51+
[:when, 1, 5, 6, 5, 10] => 0, [:when, 2, 7, 6, 7, 10] => 1,
52+
[:when, 3, 9, 6, 9, 10] => 0, [:else, 4, 11, 6, 11, 11] => 0
53+
},
54+
},
55+
}.freeze
56+
57+
CASE_WITHOUT_ELSE_RB = {
58+
"lines" => [1, 1, 1, nil, 0, nil, 1, nil, 0, nil, nil, nil],
59+
"branches" => {
60+
[:case, 0, 3, 4, 10, 7] => {
61+
[:when, 1, 5, 6, 5, 10] => 0, [:when, 2, 7, 6, 7, 10] => 1,
62+
[:when, 3, 9, 6, 9, 10] => 0, [:else, 4, 3, 4, 10, 7] => 0
63+
},
64+
},
65+
}.freeze
66+
67+
ELSIF_RB = {
68+
"lines" => [1, 1, 1, 0, 1, 0, 1, 1, nil, 0, nil, nil, nil],
69+
"branches" => {
70+
[:if, 0, 7, 4, 10, 10] => {[:then, 1, 8, 6, 8, 10] => 1, [:else, 2, 10, 6, 10, 10] => 0},
71+
[:if, 3, 5, 4, 10, 10] => {[:then, 4, 6, 6, 6, 10] => 0, [:else, 5, 7, 4, 10, 10] => 1},
72+
[:if, 6, 3, 4, 11, 7] => {[:then, 7, 4, 6, 4, 10] => 0, [:else, 8, 5, 4, 10, 10] => 1},
73+
},
74+
}.freeze
75+
76+
BRANCH_TESTER_RB = {
77+
"lines" => [nil, nil, 1, 1, nil, 1, nil, 1, 1, nil, nil, 1, 0, nil, nil, 1, 0, nil, 1, nil, nil, 1, 1, 1, nil, nil, 1, 0, nil, nil, 1, 1, nil, 0, nil, 1,
78+
1, 0, 0, 1, 5, 0, 0, nil, 0, nil, 0, nil, nil, nil],
79+
"branches" => {
80+
[:if, 0, 4, 0, 4, 19] => {[:then, 1, 4, 12, 4, 15] => 0, [:else, 2, 4, 18, 4, 19] => 1},
81+
[:unless, 3, 6, 0, 6, 23] => {[:else, 4, 6, 0, 6, 23] => 0, [:then, 5, 6, 0, 6, 6] => 1},
82+
[:unless, 6, 8, 0, 10, 3] => {[:else, 7, 8, 0, 10, 3] => 0, [:then, 8, 9, 2, 9, 14] => 1},
83+
[:unless, 9, 12, 0, 14, 3] => {[:else, 10, 12, 0, 14, 3] => 1, [:then, 11, 13, 2, 13, 14] => 0},
84+
[:unless, 12, 16, 0, 20, 3] => {[:else, 13, 19, 2, 19, 13] => 1, [:then, 14, 17, 2, 17, 14] => 0},
85+
[:if, 15, 22, 0, 22, 19] => {[:then, 16, 22, 0, 22, 6] => 0, [:else, 17, 22, 0, 22, 19] => 1},
86+
[:if, 18, 23, 0, 25, 3] => {[:then, 19, 24, 2, 24, 14] => 1, [:else, 20, 23, 0, 25, 3] => 0},
87+
[:if, 21, 27, 0, 29, 3] => {[:then, 22, 28, 2, 28, 14] => 0, [:else, 23, 27, 0, 29, 3] => 1},
88+
[:if, 24, 31, 0, 35, 3] => {[:then, 25, 32, 2, 32, 14] => 1, [:else, 26, 34, 2, 34, 13] => 0},
89+
[:if, 27, 42, 0, 47, 8] => {[:then, 28, 43, 2, 45, 13] => 0, [:else, 29, 47, 2, 47, 8] => 0},
90+
[:if, 30, 40, 0, 47, 8] => {[:then, 31, 41, 2, 41, 25] => 1, [:else, 32, 42, 0, 47, 8] => 0},
91+
[:if, 33, 37, 0, 48, 3] => {[:then, 34, 38, 2, 39, 21] => 0, [:else, 35, 40, 0, 47, 8] => 1},
92+
},
93+
}.freeze
94+
95+
SINGLE_NOCOV_RB = {
96+
"lines" => [nil, 1, 1, 1, 0, 1, 0, 1, 1, nil, 0, nil, nil, nil],
97+
"branches" => {
98+
[:if, 0, 8, 4, 11, 10] => {[:then, 1, 9, 6, 9, 10] => 1, [:else, 2, 11, 6, 11, 10] => 0},
99+
[:if, 3, 6, 4, 11, 10] => {[:then, 4, 7, 6, 7, 10] => 0, [:else, 5, 8, 4, 11, 10] => 1},
100+
[:if, 6, 4, 4, 12, 7] => {[:then, 7, 5, 6, 5, 10] => 0, [:else, 8, 6, 4, 11, 10] => 1},
101+
},
102+
}.freeze
103+
104+
UNEVEN_NOCOVS_RB = {
105+
"lines" => [1, 1, nil, 1, 0, 1, 0, nil, 1, 1, nil, nil, 0, nil, nil, nil],
106+
"branches" => {
107+
[:if, 0, 9, 4, 13, 10] => {[:then, 1, 10, 6, 10, 10] => 1, [:else, 2, 13, 6, 13, 10] => 0},
108+
[:if, 3, 6, 4, 13, 10] => {[:then, 4, 7, 6, 7, 10] => 0, [:else, 5, 9, 4, 13, 10] => 1},
109+
[:if, 6, 4, 4, 14, 7] => {[:then, 7, 5, 6, 5, 10] => 0, [:else, 8, 6, 4, 13, 10] => 1},
110+
},
111+
}.freeze
112+
113+
ALL_FIXTURES = {
114+
"branches.rb" => BRANCHES_RB,
115+
"sample.rb" => SAMPLE_RB,
116+
"inline.rb" => INLINE_RB,
117+
"never.rb" => NEVER_RB,
118+
"nocov_complex.rb" => NOCOV_COMPLEX_RB,
119+
"nested_branches.rb" => NESTED_BRANCHES_RB,
120+
"case.rb" => CASE_RB,
121+
"case_without_else.rb" => CASE_WITHOUT_ELSE_RB,
122+
"elsif.rb" => ELSIF_RB,
123+
"branch_tester_script.rb" => BRANCH_TESTER_RB,
124+
"single_nocov.rb" => SINGLE_NOCOV_RB,
125+
"uneven_nocovs.rb" => UNEVEN_NOCOVS_RB,
126+
}.freeze
127+
end

0 commit comments

Comments
 (0)