rails_tia 0.1.0
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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +180 -0
- data/docs/configuration.md +109 -0
- data/docs/github-actions.md +147 -0
- data/exe/rails-tia +5 -0
- data/ext/rails_tia/allocated_classes.c +111 -0
- data/ext/rails_tia/extconf.rb +7 -0
- data/lib/rails_tia/action_view_support.rb +67 -0
- data/lib/rails_tia/audit.rb +42 -0
- data/lib/rails_tia/boot.rb +12 -0
- data/lib/rails_tia/ci/base.rb +38 -0
- data/lib/rails_tia/ci/git.rb +49 -0
- data/lib/rails_tia/ci/github_actions.rb +237 -0
- data/lib/rails_tia/ci/gitlab.rb +199 -0
- data/lib/rails_tia/ci.rb +36 -0
- data/lib/rails_tia/cli.rb +328 -0
- data/lib/rails_tia/configuration.rb +169 -0
- data/lib/rails_tia/constant_reads.rb +84 -0
- data/lib/rails_tia/context.rb +17 -0
- data/lib/rails_tia/coverage_collector.rb +198 -0
- data/lib/rails_tia/described_class_support.rb +75 -0
- data/lib/rails_tia/fallback.rb +79 -0
- data/lib/rails_tia/formatter.rb +88 -0
- data/lib/rails_tia/http.rb +89 -0
- data/lib/rails_tia/junit_report.rb +116 -0
- data/lib/rails_tia/map.rb +178 -0
- data/lib/rails_tia/metrics.rb +71 -0
- data/lib/rails_tia/minitest.rb +243 -0
- data/lib/rails_tia/native_extension.rb +6 -0
- data/lib/rails_tia/object_allocations.rb +23 -0
- data/lib/rails_tia/predictor.rb +61 -0
- data/lib/rails_tia/rails_support.rb +86 -0
- data/lib/rails_tia/run_report.rb +116 -0
- data/lib/rails_tia/static_mapping.rb +85 -0
- data/lib/rails_tia/templates/minitest.yml +39 -0
- data/lib/rails_tia/templates/rspec.yml +45 -0
- data/lib/rails_tia/version.rb +3 -0
- data/lib/rails_tia.rb +27 -0
- data/skills/rails-tia/SKILL.md +49 -0
- metadata +145 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
require 'coverage'
|
|
2
|
+
require 'set'
|
|
3
|
+
require 'rails_tia'
|
|
4
|
+
require 'rails_tia/run_report'
|
|
5
|
+
require 'rails_tia/rails_support'
|
|
6
|
+
require 'rails_tia/action_view_support'
|
|
7
|
+
|
|
8
|
+
module RailsTia
|
|
9
|
+
class CoverageCollector
|
|
10
|
+
def initialize
|
|
11
|
+
@config = RailsTia.config
|
|
12
|
+
@root = @config.root.end_with?('/') ? @config.root : "#{@config.root}/"
|
|
13
|
+
@map = Map.new(generated_at: Time.now, commit_sha: RailsTia.commit_sha)
|
|
14
|
+
@report = RunReport.for('map_worker', @config)
|
|
15
|
+
@coverage_calls = 0
|
|
16
|
+
@example_count = 0
|
|
17
|
+
@current_spec = nil
|
|
18
|
+
@rails_support = nil
|
|
19
|
+
@action_view_support = nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def start
|
|
23
|
+
@started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) if @report
|
|
24
|
+
@rails_support ||= build_rails_support
|
|
25
|
+
@action_view_support ||= build_action_view_support
|
|
26
|
+
@tracking = { active_record: !!@rails_support, action_view: !!@action_view_support }
|
|
27
|
+
if @config.rails_enabled? && @config.constant_reads_enabled?
|
|
28
|
+
@constant_reads ||= build_constant_reads
|
|
29
|
+
end
|
|
30
|
+
@tracking[:constant_reads] = !!@constant_reads
|
|
31
|
+
baseline = coverage_result
|
|
32
|
+
@rails_support&.flush
|
|
33
|
+
@action_view_support&.flush
|
|
34
|
+
@object_allocations ||= build_object_allocations
|
|
35
|
+
@object_allocations&.start
|
|
36
|
+
@tracking[:object_allocations] = !!@object_allocations
|
|
37
|
+
baseline
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def switch_spec(spec)
|
|
41
|
+
return if spec == @current_spec
|
|
42
|
+
|
|
43
|
+
flush
|
|
44
|
+
@current_spec = spec
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def finish
|
|
48
|
+
flush
|
|
49
|
+
@map.specs.each { |spec| @map.add(spec, @suite_sources) } if @suite_sources
|
|
50
|
+
@map
|
|
51
|
+
ensure
|
|
52
|
+
unsubscribe
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def count_example
|
|
56
|
+
@example_count += 1
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def save(failed, described_class: false, **runner)
|
|
60
|
+
if failed
|
|
61
|
+
@map.mark_stale!
|
|
62
|
+
warn '[rails_tia] Test run failed; saving the generated map as stale'
|
|
63
|
+
end
|
|
64
|
+
if @report
|
|
65
|
+
save_fragment_with_report(failed, runner, described_class)
|
|
66
|
+
else
|
|
67
|
+
@map.dump(fragment_path)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Write a separate file for each parallel_tests process and CI node.
|
|
72
|
+
# Reusing a filename would let processes overwrite dependencies recorded by other processes.
|
|
73
|
+
def fragment_path
|
|
74
|
+
return ENV['RAILS_TIA_FRAGMENT_PATH'] if ENV['RAILS_TIA_FRAGMENT_PATH']
|
|
75
|
+
|
|
76
|
+
File.join(@config.map_fragment_dir, "map-#{fragment_suffix}.json.gz")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def unsubscribe
|
|
82
|
+
@object_allocations&.stop
|
|
83
|
+
@object_allocations = nil
|
|
84
|
+
@rails_support&.unsubscribe
|
|
85
|
+
@action_view_support&.unsubscribe
|
|
86
|
+
@rails_support = @action_view_support = nil
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def fragment_suffix
|
|
90
|
+
parts = [ENV['CI_NODE_INDEX'], ENV['TEST_ENV_NUMBER']].map { |v| v.to_s.empty? ? nil : v }.compact
|
|
91
|
+
parts.empty? ? Process.pid.to_s : parts.join('-')
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def coverage_result
|
|
95
|
+
return read_coverage unless @report
|
|
96
|
+
|
|
97
|
+
@coverage_calls += 1
|
|
98
|
+
@report.measure(:coverage) { read_coverage }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def read_coverage
|
|
102
|
+
Coverage.result(stop: false, clear: true)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def save_fragment_with_report(failed, runner, described_class)
|
|
106
|
+
path = fragment_path
|
|
107
|
+
json = nil
|
|
108
|
+
@report.measure(:write_fragment) { @map.dump(path) { |serialized| json = serialized } }
|
|
109
|
+
suite_seconds = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started_at
|
|
110
|
+
@report.write do
|
|
111
|
+
{
|
|
112
|
+
fragment_id: Digest::SHA256.hexdigest(json), fragment_bytes: File.size(path),
|
|
113
|
+
spec_count: @map.specs.size, example_count: @example_count, coverage_calls: @coverage_calls,
|
|
114
|
+
suite_seconds: suite_seconds, failed: !!failed,
|
|
115
|
+
ruby_version: RUBY_VERSION, **runner,
|
|
116
|
+
rails_version: Gem.loaded_specs['railties']&.version&.to_s,
|
|
117
|
+
tracking: @tracking.merge(described_class: described_class),
|
|
118
|
+
}
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def build_constant_reads
|
|
123
|
+
require 'rails_tia/constant_reads'
|
|
124
|
+
return ConstantReads.new(@config) unless @report
|
|
125
|
+
|
|
126
|
+
@report.measure(:constant_index) { ConstantReads.new(@config) }
|
|
127
|
+
rescue LoadError, StandardError => e
|
|
128
|
+
warn "[rails_tia] Cannot index constant reads: #{e.class}: #{e.message}; marking the map stale for a full run"
|
|
129
|
+
@map.mark_stale!
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def build_object_allocations
|
|
134
|
+
return unless @config.rails_enabled? && @config.object_allocations_enabled?
|
|
135
|
+
|
|
136
|
+
require 'rails_tia/object_allocations'
|
|
137
|
+
ObjectAllocations.new(@root, directories: @config.app_dirs)
|
|
138
|
+
rescue LoadError => e
|
|
139
|
+
message = "Cannot load object allocation tracking: #{e.message}"
|
|
140
|
+
raise Error, message if @config.object_allocations_required?
|
|
141
|
+
|
|
142
|
+
warn "[rails_tia] #{message}; marking the map stale for a full run"
|
|
143
|
+
@map.mark_stale!
|
|
144
|
+
nil
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def build_rails_support
|
|
148
|
+
return nil unless @config.rails_enabled? && @config.active_record_enabled?
|
|
149
|
+
return nil unless RailsSupport.available?
|
|
150
|
+
|
|
151
|
+
RailsSupport.new(root: @root).subscribe
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def build_action_view_support
|
|
155
|
+
return nil unless @config.rails_enabled? && @config.action_view_enabled?
|
|
156
|
+
return nil unless ActionViewSupport.available?
|
|
157
|
+
|
|
158
|
+
ActionViewSupport.new(root: @root).subscribe
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def relative(path)
|
|
162
|
+
absolute = File.expand_path(path.to_s.sub(%r{\A\./}, ''), @config.root)
|
|
163
|
+
absolute.start_with?(@root) ? absolute.delete_prefix(@root) : absolute
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def flush
|
|
167
|
+
allocated = @object_allocations&.flush || []
|
|
168
|
+
result = coverage_result
|
|
169
|
+
touched = (@rails_support&.flush || []) + (@action_view_support&.flush || [])
|
|
170
|
+
files = @report ? @report.measure(:scan) { project_files(result) } : project_files(result)
|
|
171
|
+
sources = files + touched + allocated
|
|
172
|
+
if @current_spec.nil?
|
|
173
|
+
# Attribute before/after(:suite) dependencies to every spec, not just the first or last.
|
|
174
|
+
(@suite_sources ||= Set.new).merge(sources)
|
|
175
|
+
else
|
|
176
|
+
Array(@current_spec).each { |file| @map.add(file, sources) }
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def append_covered_file(files, path, lines, previous = nil)
|
|
181
|
+
file = path.delete_prefix(@root)
|
|
182
|
+
files << file
|
|
183
|
+
files.concat(@constant_reads.sources(file, lines, previous)) if @constant_reads
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def project_files(result)
|
|
187
|
+
result.each_with_object([]) do |(path, coverage), files|
|
|
188
|
+
next unless path.start_with?(@root)
|
|
189
|
+
|
|
190
|
+
lines = coverage[:lines]
|
|
191
|
+
next if lines.nil?
|
|
192
|
+
next unless lines.any? { |hits| hits.to_i.positive? }
|
|
193
|
+
|
|
194
|
+
append_covered_file(files, path, lines)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
module RailsTia
|
|
2
|
+
# Resolve class identities without invoking application-defined reflection methods.
|
|
3
|
+
class DescribedClassSupport
|
|
4
|
+
UNRESOLVED = Object.new.freeze
|
|
5
|
+
MODULE_NAME = Module.instance_method(:name)
|
|
6
|
+
ANCESTORS = Module.instance_method(:ancestors)
|
|
7
|
+
AUTOLOAD = Module.instance_method(:autoload?)
|
|
8
|
+
CONST_DEFINED = Module.instance_method(:const_defined?)
|
|
9
|
+
CONST_GET = Module.instance_method(:const_get)
|
|
10
|
+
SAME_OBJECT = BasicObject.instance_method(:equal?)
|
|
11
|
+
SOURCE_LOCATION = Module.instance_method(:const_source_location)
|
|
12
|
+
|
|
13
|
+
def initialize(root:, directories: nil)
|
|
14
|
+
@root = "#{File.expand_path(root).delete_suffix('/')}/"
|
|
15
|
+
@prefixes = directories ? directories.map { |dir| File.join(File.absolute_path(dir, @root), '') }.uniq : [@root]
|
|
16
|
+
@files = {}.compare_by_identity
|
|
17
|
+
@locations = {}.compare_by_identity
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def files_for(target, include_anonymous: false)
|
|
21
|
+
return @files[target] if @files.key?(target)
|
|
22
|
+
return [] unless Module === target
|
|
23
|
+
|
|
24
|
+
named = !MODULE_NAME.bind_call(target).to_s.empty?
|
|
25
|
+
return [] unless named || include_anonymous
|
|
26
|
+
|
|
27
|
+
complete = true
|
|
28
|
+
files = ANCESTORS.bind_call(target).filter_map do |ancestor|
|
|
29
|
+
next if MODULE_NAME.bind_call(ancestor).to_s.empty?
|
|
30
|
+
|
|
31
|
+
location = @locations.fetch(ancestor) do
|
|
32
|
+
resolved = definition_file(ancestor)
|
|
33
|
+
@locations[ancestor] = resolved unless resolved.equal?(UNRESOLVED)
|
|
34
|
+
resolved
|
|
35
|
+
end
|
|
36
|
+
if location.equal?(UNRESOLVED)
|
|
37
|
+
complete = false
|
|
38
|
+
next
|
|
39
|
+
end
|
|
40
|
+
location
|
|
41
|
+
end.uniq.freeze
|
|
42
|
+
@files[target] = files if named && complete
|
|
43
|
+
files
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def definition_file(target)
|
|
49
|
+
name = MODULE_NAME.bind_call(target)
|
|
50
|
+
|
|
51
|
+
owner = Object
|
|
52
|
+
parts = name.split('::')
|
|
53
|
+
parts[0...-1].each do |part|
|
|
54
|
+
# Never trigger autoload while resolving a stale or removed constant path.
|
|
55
|
+
return UNRESOLVED unless Module === owner && CONST_DEFINED.bind_call(owner, part, false)
|
|
56
|
+
return UNRESOLVED if AUTOLOAD.bind_call(owner, part, false)
|
|
57
|
+
|
|
58
|
+
owner = CONST_GET.bind_call(owner, part, false)
|
|
59
|
+
end
|
|
60
|
+
leaf = parts.last
|
|
61
|
+
return UNRESOLVED unless Module === owner && CONST_DEFINED.bind_call(owner, leaf, false)
|
|
62
|
+
return UNRESOLVED if AUTOLOAD.bind_call(owner, leaf, false)
|
|
63
|
+
return UNRESOLVED unless SAME_OBJECT.bind_call(target, CONST_GET.bind_call(owner, leaf, false))
|
|
64
|
+
|
|
65
|
+
path = SOURCE_LOCATION.bind_call(owner, leaf, false)&.first
|
|
66
|
+
return unless path
|
|
67
|
+
|
|
68
|
+
path = File.expand_path(path)
|
|
69
|
+
path.delete_prefix(@root) if path.start_with?(*@prefixes) && File.file?(path)
|
|
70
|
+
rescue NameError, TypeError
|
|
71
|
+
# Removed constants and unresolvable names do not prevent map generation.
|
|
72
|
+
UNRESOLVED
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
module RailsTia
|
|
2
|
+
# Determine when to run every spec instead of predicting.
|
|
3
|
+
#
|
|
4
|
+
# Cover changes invisible to coverage, such as gems, CI definitions, and the execution environment,
|
|
5
|
+
# as well as situations that reduce confidence, such as stale maps or large changes.
|
|
6
|
+
# Return the reason as text for CI logs and metrics.
|
|
7
|
+
class Fallback
|
|
8
|
+
SAMPLE_SIZE = 5
|
|
9
|
+
|
|
10
|
+
def initialize(config:, map:)
|
|
11
|
+
@config = config
|
|
12
|
+
@map = map
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def reason(context) = decision(context)&.last
|
|
16
|
+
|
|
17
|
+
def decision(context)
|
|
18
|
+
not_merge_request(context) ||
|
|
19
|
+
missing_map ||
|
|
20
|
+
stale_map ||
|
|
21
|
+
matched_label(context) ||
|
|
22
|
+
matched_commit_message(context) ||
|
|
23
|
+
too_many_changes(context) ||
|
|
24
|
+
matched_full_run_path(context)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def not_merge_request(context)
|
|
30
|
+
return nil if context.merge_request?
|
|
31
|
+
|
|
32
|
+
['not_merge_request', 'Not a merge request pipeline']
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def missing_map
|
|
36
|
+
return nil unless @map.nil? || @map.empty?
|
|
37
|
+
|
|
38
|
+
['missing_map', 'No coverage map available']
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def stale_map
|
|
42
|
+
return nil unless @map.stale?(max_age_days: @config.map_max_age_days)
|
|
43
|
+
|
|
44
|
+
generated = @map.generated_at ? @map.generated_at.utc.iso8601 : 'unknown'
|
|
45
|
+
['stale_map', "Coverage map is stale (generated: #{generated} / maximum age: #{@config.map_max_age_days} days)"]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def matched_label(context)
|
|
49
|
+
hit = @config.full_run_labels & context.labels
|
|
50
|
+
return nil if hit.empty?
|
|
51
|
+
|
|
52
|
+
['label', "Full-run label matched: #{hit.join(', ')}"]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def matched_commit_message(context)
|
|
56
|
+
message = context.commit_message.to_s
|
|
57
|
+
hit = @config.full_run_commit_message_tokens.select { |token| message.include?(token) }
|
|
58
|
+
return nil if hit.empty?
|
|
59
|
+
|
|
60
|
+
['commit_message', "Full-run token found in commit message: #{hit.join(', ')}"]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def too_many_changes(context)
|
|
64
|
+
limit = @config.max_changed_files
|
|
65
|
+
return nil if limit.nil? || context.changed_files.size <= limit
|
|
66
|
+
|
|
67
|
+
['changed_file_limit', "Too many changed files (#{context.changed_files.size} > #{limit})"]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def matched_full_run_path(context)
|
|
71
|
+
hit = context.changed_files.select { |path| @config.full_run_path?(path) }
|
|
72
|
+
return nil if hit.empty?
|
|
73
|
+
|
|
74
|
+
sample = hit.first(SAMPLE_SIZE).join(', ')
|
|
75
|
+
suffix = hit.size > SAMPLE_SIZE ? " and #{hit.size - SAMPLE_SIZE} more" : ''
|
|
76
|
+
['path', "Full-run path changed: #{sample}#{suffix}"]
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
require 'rspec/core'
|
|
2
|
+
require 'rspec/core/formatters'
|
|
3
|
+
require 'rails_tia/coverage_collector'
|
|
4
|
+
require 'rails_tia/described_class_support'
|
|
5
|
+
|
|
6
|
+
module RailsTia
|
|
7
|
+
# RSpec formatter that records coverage dependencies per spec file as map fragments.
|
|
8
|
+
class Formatter < CoverageCollector
|
|
9
|
+
RSpec::Core::Formatters.register self, :start, :example_group_started, :example_group_finished, :example_started, :stop
|
|
10
|
+
|
|
11
|
+
def initialize(_output)
|
|
12
|
+
super()
|
|
13
|
+
@described_class_support = DescribedClassSupport.new(root: @root) if @config.described_class_enabled?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def start(_notification)
|
|
17
|
+
@previous_lines = project_lines(super())
|
|
18
|
+
rescue StandardError
|
|
19
|
+
# RSpec only sends :stop if all :start callbacks return successfully.
|
|
20
|
+
unsubscribe
|
|
21
|
+
raise
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Group start arrives before before(:context); group finish arrives after after(:context).
|
|
25
|
+
def example_group_started(notification)
|
|
26
|
+
switch_spec(relative(group_spec_path(notification.group)))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def example_group_finished(notification)
|
|
30
|
+
return unless notification.group.parent_groups.size == 1
|
|
31
|
+
|
|
32
|
+
switch_spec(nil)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def example_started(notification)
|
|
36
|
+
count_example if @report
|
|
37
|
+
switch_spec(relative(spec_path_of(notification.example)))
|
|
38
|
+
if @described_class_support
|
|
39
|
+
@map.add(@current_spec, @described_class_support.files_for(notification.example.example_group.described_class))
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def stop(notification)
|
|
44
|
+
finish
|
|
45
|
+
failed = notification.failed_examples.any? || RSpec.world.non_example_failure
|
|
46
|
+
save(failed, rspec_version: RSpec::Core::Version::STRING, described_class: !!@described_class_support)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def read_coverage
|
|
52
|
+
# SimpleCov needs the cumulative counters, including hits from eager loading.
|
|
53
|
+
Coverage.peek_result
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def project_lines(snapshot)
|
|
57
|
+
snapshot.each_with_object({}) do |(path, counts), files|
|
|
58
|
+
next unless path.start_with?(@root)
|
|
59
|
+
|
|
60
|
+
# Coverage.start without explicit modes returns line arrays directly.
|
|
61
|
+
lines = counts.is_a?(Array) ? counts : counts[:lines]
|
|
62
|
+
raise Error, 'RSpec mapping requires cumulative line coverage (lines: true)' unless lines
|
|
63
|
+
|
|
64
|
+
files[path] = lines
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def project_files(snapshot)
|
|
69
|
+
previous = @previous_lines
|
|
70
|
+
@previous_lines = project_lines(snapshot)
|
|
71
|
+
@previous_lines.each_with_object([]) do |(path, lines), files|
|
|
72
|
+
before = previous[path]
|
|
73
|
+
touched = before ? before != lines : lines.any? { |hits| hits.to_i.positive? }
|
|
74
|
+
append_covered_file(files, path, lines, before) if touched
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def spec_path_of(example)
|
|
79
|
+
group_spec_path(example.example_group)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def group_spec_path(group)
|
|
83
|
+
# Use the runnable top-level group’s spec, rather than the shared examples definition.
|
|
84
|
+
metadata = group.parent_groups.last.metadata
|
|
85
|
+
metadata[:absolute_file_path] || metadata[:file_path]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
require 'openssl'
|
|
4
|
+
require 'uri'
|
|
5
|
+
|
|
6
|
+
module RailsTia
|
|
7
|
+
# Minimal GET client for CI APIs.
|
|
8
|
+
class Http
|
|
9
|
+
OPEN_TIMEOUT_SECONDS = 10
|
|
10
|
+
READ_TIMEOUT_SECONDS = 30
|
|
11
|
+
MAX_REDIRECTS = 5
|
|
12
|
+
|
|
13
|
+
# Wrap transport exceptions in Error so callers can handle failures at the job level.
|
|
14
|
+
NETWORK_ERRORS = [
|
|
15
|
+
Timeout::Error, # Parent of Net::OpenTimeout and Net::ReadTimeout.
|
|
16
|
+
SystemCallError, # Parent of errors such as Errno::ECONNRESET and Errno::ECONNREFUSED.
|
|
17
|
+
SocketError,
|
|
18
|
+
IOError,
|
|
19
|
+
OpenSSL::SSL::SSLError,
|
|
20
|
+
].freeze
|
|
21
|
+
|
|
22
|
+
class Error < StandardError; end
|
|
23
|
+
|
|
24
|
+
# Indicates a missing artifact, such as an expired artifact or a nonexistent process filename.
|
|
25
|
+
# Distinguish this from transient transport failures when deciding whether to stop fetching.
|
|
26
|
+
class NotFound < Error; end
|
|
27
|
+
class Gone < Error; end
|
|
28
|
+
|
|
29
|
+
def initialize(base_url:, headers: {})
|
|
30
|
+
@base_url = base_url.to_s.chomp('/')
|
|
31
|
+
@headers = headers
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Report errors with the requested URI, not a redirect target, to keep signed CDN URLs out of CI logs.
|
|
35
|
+
def get(path)
|
|
36
|
+
uri = URI.parse("#{@base_url}#{path}")
|
|
37
|
+
response = follow_redirects(uri)
|
|
38
|
+
raise NotFound, "GET #{uri} not found: #{response.code} #{response.message}" if response.is_a?(Net::HTTPNotFound)
|
|
39
|
+
raise Gone, "GET #{uri} gone: #{response.code} #{response.message}" if response.is_a?(Net::HTTPGone)
|
|
40
|
+
raise Error, "GET #{uri} failed: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
|
|
41
|
+
|
|
42
|
+
response.body
|
|
43
|
+
rescue *NETWORK_ERRORS => e
|
|
44
|
+
raise Error, "GET #{uri} failed: #{e.class}: #{e.message}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def get_json(path)
|
|
48
|
+
JSON.parse(get(path))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# GitLab.com may redirect artifact downloads through a CDN.
|
|
54
|
+
def follow_redirects(uri)
|
|
55
|
+
target = uri
|
|
56
|
+
headers = @headers
|
|
57
|
+
(MAX_REDIRECTS + 1).times do
|
|
58
|
+
response = perform(target, headers)
|
|
59
|
+
return response unless response.is_a?(Net::HTTPRedirection)
|
|
60
|
+
|
|
61
|
+
location = response['location']
|
|
62
|
+
raise Error, "GET #{uri} failed: #{response.code} without a Location header" if location.to_s.empty?
|
|
63
|
+
|
|
64
|
+
next_target = URI.join(target.to_s, location)
|
|
65
|
+
raise Error, "GET #{uri} failed: redirected to a non-HTTP location" unless next_target.is_a?(URI::HTTP)
|
|
66
|
+
|
|
67
|
+
# Never forward the API token to another origin, such as object storage with signed URLs.
|
|
68
|
+
headers = {} unless origin(next_target) == origin(target)
|
|
69
|
+
target = next_target
|
|
70
|
+
end
|
|
71
|
+
raise Error, "GET #{uri} failed: more than #{MAX_REDIRECTS} redirects"
|
|
72
|
+
rescue URI::Error => e
|
|
73
|
+
raise Error, "GET #{uri} failed: invalid redirect location: #{e.message}"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def origin(uri) = [uri.scheme, uri.host, uri.port]
|
|
77
|
+
|
|
78
|
+
def perform(uri, headers)
|
|
79
|
+
request = Net::HTTP::Get.new(uri)
|
|
80
|
+
headers.each { |key, value| request[key] = value }
|
|
81
|
+
options = {
|
|
82
|
+
use_ssl: uri.scheme == 'https',
|
|
83
|
+
open_timeout: OPEN_TIMEOUT_SECONDS,
|
|
84
|
+
read_timeout: READ_TIMEOUT_SECONDS,
|
|
85
|
+
}
|
|
86
|
+
Net::HTTP.start(uri.host, uri.port, **options) { |http| http.request(request) }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require 'rexml/document'
|
|
2
|
+
require 'set'
|
|
3
|
+
require 'pathname'
|
|
4
|
+
|
|
5
|
+
module RailsTia
|
|
6
|
+
# Read JUnit XML produced by rspec_junit_formatter.
|
|
7
|
+
#
|
|
8
|
+
# Used for two purposes:
|
|
9
|
+
# - Estimate execution time savings by summing testcase durations per spec file.
|
|
10
|
+
# - Audit missed predictions by collecting spec files from failed testcases.
|
|
11
|
+
class JunitReport
|
|
12
|
+
FAILURE_ELEMENTS = %w(failure error).freeze
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def load_dir(dir, expected_files: [])
|
|
16
|
+
files = Dir.glob('**/*.xml', base: dir).sort
|
|
17
|
+
paths = files.map { |name| File.join(dir, name) }
|
|
18
|
+
expected = expected_files.map { |name| Pathname.new(name).cleanpath.to_s }.uniq
|
|
19
|
+
new(paths, missing_files: expected - files, expected_paths: expected.map { |name| File.join(dir, name) })
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :missing_files
|
|
24
|
+
|
|
25
|
+
def initialize(paths, missing_files: [], expected_paths: [])
|
|
26
|
+
@paths = paths
|
|
27
|
+
@missing_files = missing_files
|
|
28
|
+
@expected_paths = expected_paths
|
|
29
|
+
@durations = Hash.new(0.0)
|
|
30
|
+
@failed_files = Set.new
|
|
31
|
+
@parsed = false
|
|
32
|
+
@validation_errors = missing_files.map { |name| "Missing expected JUnit file: #{name}" }
|
|
33
|
+
@parsed_paths = Set.new
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def durations
|
|
37
|
+
parse
|
|
38
|
+
@durations
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def failed_files
|
|
42
|
+
parse
|
|
43
|
+
@failed_files
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Metrics can use partial aggregates, but Audit must not interpret missing input as success.
|
|
47
|
+
def validation_errors
|
|
48
|
+
parse
|
|
49
|
+
@validation_errors
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def parsed_file_count
|
|
53
|
+
parse
|
|
54
|
+
@parsed_paths.size
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def complete?
|
|
58
|
+
return nil if @expected_paths.empty?
|
|
59
|
+
|
|
60
|
+
parse
|
|
61
|
+
@missing_files.empty? && @expected_paths.all? { |path| @parsed_paths.include?(path) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def empty?
|
|
65
|
+
parse
|
|
66
|
+
@durations.empty?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def parse
|
|
72
|
+
return if @parsed
|
|
73
|
+
|
|
74
|
+
@parsed = true
|
|
75
|
+
@validation_errors << 'No JUnit XML files found' if @paths.empty?
|
|
76
|
+
@paths.each { |path| parse_file(path) }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def parse_file(path)
|
|
80
|
+
document = REXML::Document.new(File.read(path))
|
|
81
|
+
unless document.root && %w(testsuite testsuites).include?(document.root.name)
|
|
82
|
+
@validation_errors << "#{path}: No JUnit testsuite/testsuites element found"
|
|
83
|
+
return
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
REXML::XPath.each(document, '//testcase') do |testcase|
|
|
87
|
+
file = normalize(testcase.attributes['file'])
|
|
88
|
+
if file.nil?
|
|
89
|
+
@validation_errors << "#{path}: A failing testcase has no file attribute" if failed?(testcase)
|
|
90
|
+
next
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
@durations[file] += testcase.attributes['time'].to_f
|
|
94
|
+
@failed_files << file if failed?(testcase)
|
|
95
|
+
end
|
|
96
|
+
# An empty suite still counts as a parsed shard.
|
|
97
|
+
@parsed_paths << path
|
|
98
|
+
rescue REXML::ParseException, SystemCallError => e
|
|
99
|
+
# Keep processing other files if a JUnit report is truncated, for example by a job timeout.
|
|
100
|
+
message = "Could not read junit.xml: #{path}: #{e.message}"
|
|
101
|
+
@validation_errors << message
|
|
102
|
+
warn "[rails_tia] #{message}"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def failed?(testcase)
|
|
106
|
+
testcase.elements.any? { |element| FAILURE_ELEMENTS.include?(element.name) }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# rspec_junit_formatter emits paths in the form "./spec/...".
|
|
110
|
+
def normalize(file)
|
|
111
|
+
return nil if file.nil? || file.strip.empty?
|
|
112
|
+
|
|
113
|
+
file.delete_prefix('./')
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|