simplecov 0.22.0 → 1.0.0.rc2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +89 -1
- data/LICENSE +1 -1
- data/README.md +1009 -511
- data/doc/alternate-formatters.md +0 -5
- data/doc/commercial-services.md +5 -5
- data/exe/simplecov +11 -0
- data/lib/minitest/simplecov_plugin.rb +13 -5
- data/lib/simplecov/autostart.rb +11 -0
- data/lib/simplecov/cli/clean.rb +47 -0
- data/lib/simplecov/cli/coverage.rb +91 -0
- data/lib/simplecov/cli/diff.rb +151 -0
- data/lib/simplecov/cli/dotfile.rb +100 -0
- data/lib/simplecov/cli/merge.rb +116 -0
- data/lib/simplecov/cli/open.rb +50 -0
- data/lib/simplecov/cli/report.rb +84 -0
- data/lib/simplecov/cli/run.rb +36 -0
- data/lib/simplecov/cli/serve.rb +139 -0
- data/lib/simplecov/cli/uncovered.rb +107 -0
- data/lib/simplecov/cli.rb +150 -0
- data/lib/simplecov/color.rb +74 -0
- data/lib/simplecov/combine/branches_combiner.rb +3 -2
- data/lib/simplecov/combine/files_combiner.rb +7 -1
- data/lib/simplecov/combine/lines_combiner.rb +19 -17
- data/lib/simplecov/combine/methods_combiner.rb +26 -0
- data/lib/simplecov/combine/results_combiner.rb +5 -4
- data/lib/simplecov/command_guesser.rb +46 -32
- data/lib/simplecov/configuration/coverage.rb +171 -0
- data/lib/simplecov/configuration/coverage_criteria.rb +156 -0
- data/lib/simplecov/configuration/filters.rb +197 -0
- data/lib/simplecov/configuration/formatting.rb +119 -0
- data/lib/simplecov/configuration/ignored_entries.rb +63 -0
- data/lib/simplecov/configuration/merging.rb +74 -0
- data/lib/simplecov/configuration/thresholds.rb +174 -0
- data/lib/simplecov/configuration.rb +86 -407
- data/lib/simplecov/coverage_statistics.rb +12 -9
- data/lib/simplecov/coverage_violations.rb +148 -0
- data/lib/simplecov/defaults.rb +27 -20
- data/lib/simplecov/deprecation.rb +47 -0
- data/lib/simplecov/directive.rb +162 -0
- data/lib/simplecov/exit_codes/exit_code_handling.rb +8 -2
- data/lib/simplecov/exit_codes/maximum_coverage_drop_check.rb +19 -57
- data/lib/simplecov/exit_codes/maximum_overall_coverage_check.rb +45 -0
- data/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb +17 -27
- data/lib/simplecov/exit_codes/minimum_coverage_by_group_check.rb +41 -0
- data/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb +38 -21
- data/lib/simplecov/exit_codes.rb +3 -0
- data/lib/simplecov/exit_handling.rb +158 -0
- data/lib/simplecov/file_list.rb +61 -17
- data/lib/simplecov/filter.rb +69 -24
- data/lib/simplecov/formatter/base.rb +101 -0
- data/lib/simplecov/formatter/html_formatter/public/application.css +1 -0
- data/lib/simplecov/formatter/html_formatter/public/application.js +18 -0
- data/lib/simplecov/formatter/html_formatter/public/favicon_green.png +0 -0
- data/lib/simplecov/formatter/html_formatter/public/favicon_red.png +0 -0
- data/lib/simplecov/formatter/html_formatter/public/favicon_yellow.png +0 -0
- data/lib/simplecov/formatter/html_formatter/public/index.html +56 -0
- data/lib/simplecov/formatter/html_formatter.rb +79 -0
- data/lib/simplecov/formatter/json_formatter/errors_formatter.rb +84 -0
- data/lib/simplecov/formatter/json_formatter/result_hash_formatter.rb +127 -0
- data/lib/simplecov/formatter/json_formatter/source_file_formatter.rb +99 -0
- data/lib/simplecov/formatter/json_formatter.rb +77 -0
- data/lib/simplecov/formatter/multi_formatter.rb +4 -5
- data/lib/simplecov/formatter/simple_formatter.rb +9 -11
- data/lib/simplecov/formatter.rb +4 -0
- data/lib/simplecov/last_run.rb +10 -3
- data/lib/simplecov/lines_classifier.rb +26 -13
- data/lib/simplecov/load_global_config.rb +9 -4
- data/lib/simplecov/parallel_adapters/base.rb +51 -0
- data/lib/simplecov/parallel_adapters/generic.rb +42 -0
- data/lib/simplecov/parallel_adapters/parallel_tests.rb +77 -0
- data/lib/simplecov/parallel_adapters.rb +83 -0
- data/lib/simplecov/parallel_coordination.rb +95 -0
- data/lib/simplecov/process.rb +26 -14
- data/lib/simplecov/profiles/bundler_filter.rb +1 -1
- data/lib/simplecov/profiles/hidden_filter.rb +1 -1
- data/lib/simplecov/profiles/rails.rb +24 -10
- data/lib/simplecov/profiles/root_filter.rb +6 -5
- data/lib/simplecov/profiles/strict.rb +32 -0
- data/lib/simplecov/profiles/test_frameworks.rb +1 -4
- data/lib/simplecov/profiles.rb +32 -3
- data/lib/simplecov/result/missing_source_files_reporter.rb +49 -0
- data/lib/simplecov/result/source_file_builder.rb +51 -0
- data/lib/simplecov/result.rb +97 -19
- data/lib/simplecov/result_adapter.rb +68 -6
- data/lib/simplecov/result_merger/legacy_format_adapter.rb +28 -0
- data/lib/simplecov/result_merger/resultset_file.rb +38 -0
- data/lib/simplecov/result_merger/resultset_store.rb +50 -0
- data/lib/simplecov/result_merger.rb +54 -90
- data/lib/simplecov/result_processing.rb +162 -0
- data/lib/simplecov/simulate_coverage.rb +54 -8
- data/lib/simplecov/source_file/branch.rb +1 -3
- data/lib/simplecov/source_file/branch_builder.rb +114 -0
- data/lib/simplecov/source_file/builder_context.rb +28 -0
- data/lib/simplecov/source_file/line.rb +7 -2
- data/lib/simplecov/source_file/line_builder.rb +43 -0
- data/lib/simplecov/source_file/method.rb +52 -0
- data/lib/simplecov/source_file/method_builder.rb +58 -0
- data/lib/simplecov/source_file/ruby_data_parser.rb +88 -0
- data/lib/simplecov/source_file/skip_chunks.rb +77 -0
- data/lib/simplecov/source_file/source_loader.rb +63 -0
- data/lib/simplecov/source_file/statistics.rb +57 -0
- data/lib/simplecov/source_file.rb +66 -232
- data/lib/simplecov/static_coverage_extractor/visitor.rb +193 -0
- data/lib/simplecov/static_coverage_extractor.rb +111 -0
- data/lib/simplecov/useless_results_remover.rb +16 -7
- data/lib/simplecov/version.rb +1 -1
- data/lib/simplecov-html.rb +4 -0
- data/lib/simplecov.rb +148 -377
- data/lib/simplecov_json_formatter.rb +4 -0
- data/schemas/coverage-v1.0.schema.json +300 -0
- data/schemas/coverage.schema.json +300 -0
- metadata +89 -56
- data/lib/simplecov/default_formatter.rb +0 -20
|
@@ -1,46 +1,64 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "directive"
|
|
4
|
+
require_relative "static_coverage_extractor"
|
|
5
|
+
require_relative "source_file/ruby_data_parser"
|
|
6
|
+
require_relative "source_file/source_loader"
|
|
7
|
+
require_relative "source_file/skip_chunks"
|
|
8
|
+
require_relative "source_file/builder_context"
|
|
9
|
+
require_relative "source_file/line_builder"
|
|
10
|
+
require_relative "source_file/branch_builder"
|
|
11
|
+
require_relative "source_file/method_builder"
|
|
12
|
+
require_relative "source_file/statistics"
|
|
13
|
+
|
|
3
14
|
module SimpleCov
|
|
4
15
|
#
|
|
5
16
|
# Representation of a source file including it's coverage data, source code,
|
|
6
17
|
# source lines and featuring helpers to interpret that data.
|
|
7
18
|
#
|
|
8
19
|
class SourceFile
|
|
20
|
+
include BuilderContext
|
|
21
|
+
|
|
9
22
|
# The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
|
|
10
23
|
attr_reader :filename
|
|
11
24
|
# The array of coverage data received from the Coverage.result
|
|
12
25
|
attr_reader :coverage_data
|
|
13
26
|
|
|
14
|
-
def initialize(filename, coverage_data)
|
|
27
|
+
def initialize(filename, coverage_data, loaded: true)
|
|
15
28
|
@filename = filename
|
|
16
29
|
@coverage_data = coverage_data
|
|
30
|
+
@loaded = loaded
|
|
17
31
|
end
|
|
18
32
|
|
|
19
33
|
# The path to this source file relative to the projects directory
|
|
20
34
|
def project_filename
|
|
21
|
-
@filename.delete_prefix(SimpleCov.root)
|
|
35
|
+
@filename.delete_prefix(SimpleCov.root).sub(%r{\A[/\\]}, "")
|
|
22
36
|
end
|
|
23
37
|
|
|
24
|
-
# The source code for this file. Aliased as :source
|
|
38
|
+
# The source code for this file. Aliased as :source.
|
|
39
|
+
# Intentionally read lazily to suppress reading unused source code.
|
|
25
40
|
def src
|
|
26
|
-
|
|
27
|
-
# suppress reading unused source code.
|
|
28
|
-
@src ||= load_source
|
|
41
|
+
@src ||= SourceLoader.call(filename)
|
|
29
42
|
end
|
|
30
43
|
alias source src
|
|
31
44
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
45
|
+
# Returns a hash keyed by every supported coverage criterion. Each
|
|
46
|
+
# value is a CoverageStatistics, even for criteria that weren't
|
|
47
|
+
# enabled during the run — those collapse to 0/0/0. Consumers
|
|
48
|
+
# (FileList, formatters) decide which keys to surface based on
|
|
49
|
+
# `SimpleCov.coverage_criterion_enabled?`.
|
|
50
|
+
# The per-criterion coverage statistics for this file. With no argument
|
|
51
|
+
# returns the `{line:, branch:, method:}` Hash; pass a criterion symbol
|
|
52
|
+
# (`:line` / `:branch` / `:method`) to get that one CoverageStatistics.
|
|
53
|
+
def coverage_statistics(criterion = nil)
|
|
54
|
+
@coverage_statistics ||= Statistics.new(self).call
|
|
55
|
+
criterion ? @coverage_statistics[criterion] : @coverage_statistics
|
|
38
56
|
end
|
|
39
57
|
|
|
40
58
|
# Returns all source lines for this file as instances of SimpleCov::SourceFile::Line,
|
|
41
59
|
# and thus including coverage data. Aliased as :source_lines
|
|
42
60
|
def lines
|
|
43
|
-
@lines ||=
|
|
61
|
+
@lines ||= LineBuilder.new(self).call
|
|
44
62
|
end
|
|
45
63
|
alias source_lines lines
|
|
46
64
|
|
|
@@ -68,7 +86,7 @@ module SimpleCov
|
|
|
68
86
|
|
|
69
87
|
# Returns the number of relevant lines (covered + missed)
|
|
70
88
|
def lines_of_code
|
|
71
|
-
coverage_statistics[:line]&.total
|
|
89
|
+
coverage_statistics[:line]&.total || 0
|
|
72
90
|
end
|
|
73
91
|
|
|
74
92
|
# Access SimpleCov::SourceFile::Line source lines by line number
|
|
@@ -76,65 +94,60 @@ module SimpleCov
|
|
|
76
94
|
lines[number - 1]
|
|
77
95
|
end
|
|
78
96
|
|
|
79
|
-
# The coverage for this file in percent
|
|
80
|
-
|
|
81
|
-
|
|
97
|
+
# The coverage for this file in percent, for the given criterion (line by
|
|
98
|
+
# default). Returns nil if the criterion was not measured.
|
|
99
|
+
def covered_percent(criterion = :line)
|
|
100
|
+
coverage_statistics(criterion)&.percent
|
|
82
101
|
end
|
|
83
102
|
|
|
84
|
-
def covered_strength
|
|
85
|
-
coverage_statistics
|
|
103
|
+
def covered_strength(criterion = :line)
|
|
104
|
+
coverage_statistics(criterion)&.strength
|
|
86
105
|
end
|
|
87
106
|
|
|
88
107
|
def no_lines?
|
|
89
|
-
lines.
|
|
108
|
+
lines.empty? || (lines.length == never_lines.size)
|
|
90
109
|
end
|
|
91
110
|
|
|
92
111
|
def relevant_lines
|
|
93
112
|
lines.size - never_lines.size - skipped_lines.size
|
|
94
113
|
end
|
|
95
114
|
|
|
96
|
-
#
|
|
97
115
|
# Return all the branches inside current source file
|
|
98
116
|
def branches
|
|
99
|
-
@branches ||=
|
|
117
|
+
@branches ||= BranchBuilder.new(self).call
|
|
100
118
|
end
|
|
101
119
|
|
|
102
120
|
def no_branches?
|
|
103
121
|
total_branches.empty?
|
|
104
122
|
end
|
|
105
123
|
|
|
124
|
+
# DEPRECATED: use `covered_percent(:branch)`.
|
|
106
125
|
def branches_coverage_percent
|
|
107
|
-
|
|
126
|
+
SimpleCov::Deprecation.warn("`SimpleCov::SourceFile#branches_coverage_percent` is deprecated. " \
|
|
127
|
+
"Use `covered_percent(:branch)`.")
|
|
128
|
+
covered_percent(:branch)
|
|
108
129
|
end
|
|
109
130
|
|
|
110
|
-
#
|
|
111
131
|
# Return the relevant branches to source file
|
|
112
132
|
def total_branches
|
|
113
133
|
@total_branches ||= covered_branches + missed_branches
|
|
114
134
|
end
|
|
115
135
|
|
|
116
|
-
#
|
|
117
136
|
# Return hash with key of line number and branch coverage count as value
|
|
118
137
|
def branches_report
|
|
119
|
-
@branches_report ||=
|
|
138
|
+
@branches_report ||=
|
|
139
|
+
branches.reject(&:skipped?).group_by(&:report_line).transform_values { |bs| bs.map(&:report) }
|
|
120
140
|
end
|
|
121
141
|
|
|
122
|
-
#
|
|
123
|
-
#
|
|
124
|
-
#
|
|
125
|
-
#
|
|
126
|
-
#
|
|
127
|
-
# @return [Array]
|
|
128
|
-
#
|
|
142
|
+
# Select the covered branches. We use a tree schema here because
|
|
143
|
+
# some conditions like `case` may have an additional `else` that
|
|
144
|
+
# isn't declared in code but is given by default by the coverage
|
|
145
|
+
# report.
|
|
129
146
|
def covered_branches
|
|
130
147
|
@covered_branches ||= branches.select(&:covered?)
|
|
131
148
|
end
|
|
132
149
|
|
|
133
|
-
#
|
|
134
150
|
# Select the missed branches with coverage equal to zero
|
|
135
|
-
#
|
|
136
|
-
# @return [Array]
|
|
137
|
-
#
|
|
138
151
|
def missed_branches
|
|
139
152
|
@missed_branches ||= branches.select(&:missed?)
|
|
140
153
|
end
|
|
@@ -143,213 +156,34 @@ module SimpleCov
|
|
|
143
156
|
branches_report.fetch(line_number, [])
|
|
144
157
|
end
|
|
145
158
|
|
|
146
|
-
#
|
|
147
159
|
# Check if any branches missing on given line number
|
|
148
|
-
#
|
|
149
|
-
# @param [Integer] line_number
|
|
150
|
-
#
|
|
151
|
-
# @return [Boolean]
|
|
152
|
-
#
|
|
153
160
|
def line_with_missed_branch?(line_number)
|
|
154
|
-
branches_for_line(line_number).
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
private
|
|
158
|
-
|
|
159
|
-
# no_cov_chunks is zero indexed to work directly with the array holding the lines
|
|
160
|
-
def no_cov_chunks
|
|
161
|
-
@no_cov_chunks ||= build_no_cov_chunks
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
def build_no_cov_chunks
|
|
165
|
-
no_cov_lines = src.map.with_index(1).select { |line_src, _index| LinesClassifier.no_cov_line?(line_src) }
|
|
166
|
-
|
|
167
|
-
# if we have an uneven number of nocovs we assume they go to the
|
|
168
|
-
# end of the file, the source doesn't really matter
|
|
169
|
-
# Can't deal with this within the each_slice due to differing
|
|
170
|
-
# behavior in JRuby: jruby/jruby#6048
|
|
171
|
-
no_cov_lines << ["", src.size] if no_cov_lines.size.odd?
|
|
172
|
-
|
|
173
|
-
no_cov_lines.each_slice(2).map do |(_line_src_start, index_start), (_line_src_end, index_end)|
|
|
174
|
-
index_start..index_end
|
|
175
|
-
end
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
def load_source
|
|
179
|
-
lines = []
|
|
180
|
-
# The default encoding is UTF-8
|
|
181
|
-
File.open(filename, "rb:UTF-8") do |file|
|
|
182
|
-
current_line = file.gets
|
|
183
|
-
|
|
184
|
-
if shebang?(current_line)
|
|
185
|
-
lines << current_line
|
|
186
|
-
current_line = file.gets
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
read_lines(file, lines, current_line)
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
SHEBANG_REGEX = /\A#!/.freeze
|
|
194
|
-
def shebang?(line)
|
|
195
|
-
SHEBANG_REGEX.match?(line)
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
def read_lines(file, lines, current_line)
|
|
199
|
-
return lines unless current_line
|
|
200
|
-
|
|
201
|
-
set_encoding_based_on_magic_comment(file, current_line)
|
|
202
|
-
lines.concat([current_line], ensure_remove_undefs(file.readlines))
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX = /\A#\s*(?:-\*-)?\s*(?:en)?coding:\s*(\S+)\s*(?:-\*-)?\s*\z/.freeze
|
|
206
|
-
def set_encoding_based_on_magic_comment(file, line)
|
|
207
|
-
# Check for encoding magic comment
|
|
208
|
-
# Encoding magic comment must be placed at first line except for shebang
|
|
209
|
-
if (match = RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX.match(line))
|
|
210
|
-
file.set_encoding(match[1], "UTF-8")
|
|
211
|
-
end
|
|
161
|
+
branches_for_line(line_number).any? { |_type, count| count.zero? }
|
|
212
162
|
end
|
|
213
163
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
# simplecov-html to have encoding shenaningans in one place. See #866
|
|
218
|
-
# also setting these option on `file.set_encoding` doesn't seem to work
|
|
219
|
-
# properly so it has to be done here.
|
|
220
|
-
file_lines.each do |line|
|
|
221
|
-
if line.encoding == Encoding::UTF_8
|
|
222
|
-
line
|
|
223
|
-
else
|
|
224
|
-
line.encode!("UTF-8", invalid: :replace, undef: :replace)
|
|
225
|
-
end
|
|
226
|
-
end
|
|
164
|
+
# Return all methods detected in this source file
|
|
165
|
+
def methods
|
|
166
|
+
@methods ||= MethodBuilder.new(self).call
|
|
227
167
|
end
|
|
228
168
|
|
|
229
|
-
def
|
|
230
|
-
|
|
231
|
-
lines = src.map.with_index(1) do |src, i|
|
|
232
|
-
SimpleCov::SourceFile::Line.new(src, i, coverage_data["lines"][i - 1])
|
|
233
|
-
end
|
|
234
|
-
process_skipped_lines(lines)
|
|
169
|
+
def covered_methods
|
|
170
|
+
@covered_methods ||= methods.select(&:covered?)
|
|
235
171
|
end
|
|
236
172
|
|
|
237
|
-
def
|
|
238
|
-
|
|
239
|
-
# chunks are 1-based and are expected to be like this in other parts (and it's also
|
|
240
|
-
# arguably more understandable)
|
|
241
|
-
no_cov_chunks.each { |chunk| lines[(chunk.begin - 1)..(chunk.end - 1)].each(&:skipped!) }
|
|
242
|
-
|
|
243
|
-
lines
|
|
244
|
-
end
|
|
245
|
-
|
|
246
|
-
def lines_strength
|
|
247
|
-
lines.sum { |line| line.coverage.to_i }
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
# Warning to identify condition from Issue #56
|
|
251
|
-
def coverage_exceeding_source_warn
|
|
252
|
-
warn "Warning: coverage data provided by Coverage [#{coverage_data['lines'].size}] exceeds number of lines in #{filename} [#{src.size}]"
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
#
|
|
256
|
-
# Build full branches report
|
|
257
|
-
# Root branches represent the wrapper of all condition state that
|
|
258
|
-
# have inside the branches
|
|
259
|
-
#
|
|
260
|
-
# @return [Hash]
|
|
261
|
-
#
|
|
262
|
-
def build_branches_report
|
|
263
|
-
branches.reject(&:skipped?).each_with_object({}) do |branch, coverage_statistics|
|
|
264
|
-
coverage_statistics[branch.report_line] ||= []
|
|
265
|
-
coverage_statistics[branch.report_line] << branch.report
|
|
266
|
-
end
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
#
|
|
270
|
-
# Call recursive method that transform our static hash to array of objects
|
|
271
|
-
# @return [Array]
|
|
272
|
-
#
|
|
273
|
-
def build_branches
|
|
274
|
-
coverage_branch_data = coverage_data.fetch("branches", {})
|
|
275
|
-
branches = coverage_branch_data.flat_map do |condition, coverage_branches|
|
|
276
|
-
build_branches_from(condition, coverage_branches)
|
|
277
|
-
end
|
|
278
|
-
|
|
279
|
-
process_skipped_branches(branches)
|
|
280
|
-
end
|
|
281
|
-
|
|
282
|
-
def process_skipped_branches(branches)
|
|
283
|
-
return branches if no_cov_chunks.empty?
|
|
284
|
-
|
|
285
|
-
branches.each do |branch|
|
|
286
|
-
branch.skipped! if no_cov_chunks.any? { |no_cov_chunk| branch.overlaps_with?(no_cov_chunk) }
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
branches
|
|
290
|
-
end
|
|
291
|
-
|
|
292
|
-
# Since we are dumping to and loading from JSON, and we have arrays as keys those
|
|
293
|
-
# don't make their way back to us intact e.g. just as a string
|
|
294
|
-
#
|
|
295
|
-
# We should probably do something different here, but as it stands these are
|
|
296
|
-
# our data structures that we write so eval isn't _too_ bad.
|
|
297
|
-
#
|
|
298
|
-
# See #801
|
|
299
|
-
#
|
|
300
|
-
def restore_ruby_data_structure(structure)
|
|
301
|
-
# Tests use the real data structures (except for integration tests) so no need to
|
|
302
|
-
# put them through here.
|
|
303
|
-
return structure if structure.is_a?(Array)
|
|
304
|
-
|
|
305
|
-
# rubocop:disable Security/Eval
|
|
306
|
-
eval structure
|
|
307
|
-
# rubocop:enable Security/Eval
|
|
308
|
-
end
|
|
309
|
-
|
|
310
|
-
def build_branches_from(condition, branches)
|
|
311
|
-
# the format handed in from the coverage data is like this:
|
|
312
|
-
#
|
|
313
|
-
# [:then, 4, 6, 6, 6, 10]
|
|
314
|
-
#
|
|
315
|
-
# which is [type, id, start_line, start_col, end_line, end_col]
|
|
316
|
-
_condition_type, _condition_id, condition_start_line, * = restore_ruby_data_structure(condition)
|
|
317
|
-
|
|
318
|
-
branches.map do |branch_data, hit_count|
|
|
319
|
-
branch_data = restore_ruby_data_structure(branch_data)
|
|
320
|
-
build_branch(branch_data, hit_count, condition_start_line)
|
|
321
|
-
end
|
|
322
|
-
end
|
|
323
|
-
|
|
324
|
-
def build_branch(branch_data, hit_count, condition_start_line)
|
|
325
|
-
type, _id, start_line, _start_col, end_line, _end_col = branch_data
|
|
326
|
-
|
|
327
|
-
SourceFile::Branch.new(
|
|
328
|
-
start_line: start_line,
|
|
329
|
-
end_line: end_line,
|
|
330
|
-
coverage: hit_count,
|
|
331
|
-
inline: start_line == condition_start_line,
|
|
332
|
-
type: type
|
|
333
|
-
)
|
|
173
|
+
def missed_methods
|
|
174
|
+
@missed_methods ||= methods.select(&:missed?)
|
|
334
175
|
end
|
|
335
176
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
missed: missed_lines.size
|
|
342
|
-
)
|
|
343
|
-
}
|
|
177
|
+
# DEPRECATED: use `covered_percent(:method)`.
|
|
178
|
+
def methods_coverage_percent
|
|
179
|
+
SimpleCov::Deprecation.warn("`SimpleCov::SourceFile#methods_coverage_percent` is deprecated. " \
|
|
180
|
+
"Use `covered_percent(:method)`.")
|
|
181
|
+
covered_percent(:method)
|
|
344
182
|
end
|
|
345
183
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
covered: covered_branches.size,
|
|
350
|
-
missed: missed_branches.size
|
|
351
|
-
)
|
|
352
|
-
}
|
|
184
|
+
# Whether this file was added via track_files but never loaded/required.
|
|
185
|
+
def not_loaded?
|
|
186
|
+
!@loaded
|
|
353
187
|
end
|
|
354
188
|
end
|
|
355
189
|
end
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SimpleCov
|
|
4
|
+
module StaticCoverageExtractor
|
|
5
|
+
# `Prism::IfNode#subsequent` was renamed from `consequent` in Prism
|
|
6
|
+
# 1.3 (Dec 2024). Ruby 3.3's stdlib still ships an older Prism that
|
|
7
|
+
# only exposes `consequent`; 3.4+ and any project that's done
|
|
8
|
+
# `gem install prism` exposes `subsequent`. Resolve the method name
|
|
9
|
+
# ONCE here so the per-node hot path stays branch-free. The
|
|
10
|
+
# not-taken arm on whichever Prism version we're on can't be
|
|
11
|
+
# exercised by our own dogfood (we only run on one Prism at a time).
|
|
12
|
+
# simplecov:disable
|
|
13
|
+
IF_NODE_SUBSEQUENT_METHOD =
|
|
14
|
+
if ::Prism::IfNode.method_defined?(:subsequent)
|
|
15
|
+
:subsequent
|
|
16
|
+
else
|
|
17
|
+
:consequent
|
|
18
|
+
end
|
|
19
|
+
# simplecov:enable
|
|
20
|
+
|
|
21
|
+
# Prism visitor that accumulates branch and method tuples in the
|
|
22
|
+
# shape Ruby's `Coverage` reports. Tuple ids are sequential across
|
|
23
|
+
# the file — `Coverage` uses sequential ids too, so this matches the
|
|
24
|
+
# conventional shape. Only defined when Prism is loadable;
|
|
25
|
+
# `StaticCoverageExtractor.available?` is the runtime gate.
|
|
26
|
+
class Visitor < ::Prism::Visitor
|
|
27
|
+
attr_reader :branches, :methods
|
|
28
|
+
|
|
29
|
+
def initialize
|
|
30
|
+
super
|
|
31
|
+
@branches = {}
|
|
32
|
+
@methods = {}
|
|
33
|
+
@next_id = 0
|
|
34
|
+
@class_stack = []
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# `if` / `unless` / postfix-if / postfix-unless / ternary all parse
|
|
38
|
+
# as IfNode (or UnlessNode). Both carry a `then` arm (the
|
|
39
|
+
# statements body) and an optional `subsequent` (an ElseNode for
|
|
40
|
+
# `else`, another IfNode for `elsif`). When the subsequent is
|
|
41
|
+
# missing, Coverage synthesizes a `:else` arm attributed to the
|
|
42
|
+
# whole condition's range — we do the same.
|
|
43
|
+
def visit_if_node(node)
|
|
44
|
+
emit_if_like(node)
|
|
45
|
+
super
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def visit_unless_node(node)
|
|
49
|
+
emit_if_like(node)
|
|
50
|
+
super
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# `case`/`when` and `case`/`in` (pattern matching) parse as CaseNode
|
|
54
|
+
# and CaseMatchNode respectively. When there's no explicit `else`,
|
|
55
|
+
# Coverage synthesizes one at the case's range.
|
|
56
|
+
def visit_case_node(node)
|
|
57
|
+
emit_case_like(node, :when)
|
|
58
|
+
super
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def visit_case_match_node(node)
|
|
62
|
+
emit_case_like(node, :in)
|
|
63
|
+
super
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# `while` / `until` loops get a single `:body` arm. No synthetic
|
|
67
|
+
# else (the loop either runs the body or doesn't).
|
|
68
|
+
def visit_while_node(node)
|
|
69
|
+
emit_loop(node, :while)
|
|
70
|
+
super
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def visit_until_node(node)
|
|
74
|
+
emit_loop(node, :until)
|
|
75
|
+
super
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Track class/module nesting so method tuples carry the lexical
|
|
79
|
+
# class name. Module + Class are both treated as namespaces here
|
|
80
|
+
# since `Coverage` reports both as the constant.
|
|
81
|
+
def visit_class_node(node)
|
|
82
|
+
with_class(constant_name(node.constant_path)) { super }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def visit_module_node(node)
|
|
86
|
+
with_class(constant_name(node.constant_path)) { super }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# `def name(...)` and `def self.name(...)` both produce DefNode.
|
|
90
|
+
# The class context is the surrounding lexical class/module (or
|
|
91
|
+
# `Object` at the top level, matching `Coverage`'s convention).
|
|
92
|
+
def visit_def_node(node)
|
|
93
|
+
loc = node.location
|
|
94
|
+
class_name = @class_stack.last || "Object"
|
|
95
|
+
key = [class_name, node.name, loc.start_line, loc.start_column, loc.end_line, loc.end_column]
|
|
96
|
+
@methods[key] = 0
|
|
97
|
+
super
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
# IfNode and UnlessNode share a shape (predicate + then body +
|
|
103
|
+
# optional else/elsif) but expose the trailing arm under different
|
|
104
|
+
# accessors. `if_like_else_location` hides that split.
|
|
105
|
+
def emit_if_like(node)
|
|
106
|
+
then_loc = arm_location(node.statements, node.location)
|
|
107
|
+
else_loc = if_like_else_location(node)
|
|
108
|
+
@branches[build_tuple(:if, node.location)] = {
|
|
109
|
+
build_tuple(:then, then_loc) => 0,
|
|
110
|
+
build_tuple(:else, else_loc) => 0
|
|
111
|
+
}
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Resolve the source range Coverage attributes to a real-or-synthetic
|
|
115
|
+
# `:else` arm of an if-like construct. IfNode uses
|
|
116
|
+
# `subsequent` / `consequent` depending on Prism version (resolved
|
|
117
|
+
# to `IF_NODE_SUBSEQUENT_METHOD` at load time); UnlessNode uses
|
|
118
|
+
# `else_clause`. When neither is present, the synthesized else
|
|
119
|
+
# inherits the whole condition's range (matches Coverage's
|
|
120
|
+
# convention).
|
|
121
|
+
def if_like_else_location(node)
|
|
122
|
+
sub = node.is_a?(::Prism::IfNode) ? node.public_send(IF_NODE_SUBSEQUENT_METHOD) : node.else_clause
|
|
123
|
+
return node.location unless sub
|
|
124
|
+
|
|
125
|
+
arm_location(else_body_of(sub), sub.location)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def emit_case_like(node, when_type)
|
|
129
|
+
arms = node.conditions.to_h do |when_node|
|
|
130
|
+
loc = arm_location(when_node.statements, when_node.location)
|
|
131
|
+
[build_tuple(when_type, loc), 0]
|
|
132
|
+
end
|
|
133
|
+
arms[build_tuple(:else, else_arm_location(node))] = 0
|
|
134
|
+
@branches[build_tuple(:case, node.location)] = arms
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Resolve the source range Coverage attributes to a synthetic-or-real
|
|
138
|
+
# `:else` arm of a case construct: the body of an explicit else,
|
|
139
|
+
# or the case's full range when no else is present.
|
|
140
|
+
def else_arm_location(node)
|
|
141
|
+
return node.location unless node.else_clause
|
|
142
|
+
|
|
143
|
+
arm_location(else_body_of(node.else_clause), node.else_clause.location)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def emit_loop(node, type)
|
|
147
|
+
cond_tuple = build_tuple(type, node.location)
|
|
148
|
+
body_loc = arm_location(node.statements, node.location)
|
|
149
|
+
@branches[cond_tuple] = {build_tuple(:body, body_loc) => 0}
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Body location for an arm. Prism's `statements` is a StatementsNode
|
|
153
|
+
# whose span covers the contained expressions; fall back to the
|
|
154
|
+
# parent when the arm body is empty (e.g., `if cond then end`).
|
|
155
|
+
def arm_location(statements, fallback_location)
|
|
156
|
+
statements&.location || fallback_location
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# simplecov:disable branch
|
|
160
|
+
# The `else_node` fallback is defensive: every Prism node passed
|
|
161
|
+
# in here in practice responds to `:statements`.
|
|
162
|
+
def else_body_of(else_node)
|
|
163
|
+
else_node.respond_to?(:statements) ? else_node.statements : else_node
|
|
164
|
+
end
|
|
165
|
+
# simplecov:enable branch
|
|
166
|
+
|
|
167
|
+
def build_tuple(type, location)
|
|
168
|
+
id = @next_id
|
|
169
|
+
@next_id += 1
|
|
170
|
+
[type, id, location.start_line, location.start_column, location.end_line, location.end_column]
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Render a constant path (e.g., `Foo::Bar`) as its source-form
|
|
174
|
+
# string. Defensive nil / to_s fallbacks: ClassNode and ModuleNode
|
|
175
|
+
# always carry a constant_path in practice.
|
|
176
|
+
# simplecov:disable
|
|
177
|
+
def constant_name(node)
|
|
178
|
+
return "<anonymous>" if node.nil?
|
|
179
|
+
return node.slice if node.respond_to?(:slice)
|
|
180
|
+
|
|
181
|
+
node.to_s
|
|
182
|
+
end
|
|
183
|
+
# simplecov:enable
|
|
184
|
+
|
|
185
|
+
def with_class(name)
|
|
186
|
+
@class_stack.push(name)
|
|
187
|
+
yield
|
|
188
|
+
ensure
|
|
189
|
+
@class_stack.pop
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|