-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathminitest.rb
More file actions
163 lines (131 loc) · 4.66 KB
/
minitest.rb
File metadata and controls
163 lines (131 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# frozen_string_literal: true
require 'appmap'
require 'appmap/util'
require 'fileutils'
require 'active_support'
require 'active_support/core_ext'
module AppMap
# Integration of AppMap with Minitest. When enabled with APPMAP=true, the AppMap tracer will
# be activated around each test.
module Minitest
APPMAP_OUTPUT_DIR = 'tmp/appmap/minitest'
LOG = (ENV['APPMAP_DEBUG'] == 'true' || ENV['DEBUG'] == 'true')
def self.metadata
AppMap.detect_metadata
end
Recording = Struct.new(:test, :test_name) do
def initialize(test, test_name)
super
warn "Starting recording of test #{test.class}.#{test.name}@#{source_location}" if AppMap::Minitest::LOG
@trace = AppMap.tracing.trace
end
def source_location
location = test.method(test_name).source_location
[ Util.normalize_path(location.first), location.last ].join(':')
end
def finish(failures, exception)
failed = failures.any? || exception
warn "Finishing recording of #{failed ? 'failed ' : ''} test #{test.class}.#{test.name}" if AppMap::Minitest::LOG
warn "Exception: #{exception}" if exception && AppMap::Minitest::LOG
if failed
failure_exception = failures.first || exception
warn "Failure exception: #{failure_exception}" if AppMap::Minitest::LOG
test_failure = Util.extract_test_failure(failure_exception)
end
events = []
AppMap.tracing.delete @trace
events << @trace.next_event.to_h while @trace.event?
AppMap::Minitest.add_event_methods @trace.event_methods
class_map = AppMap.class_map(@trace.event_methods)
feature_group = test.class.name.underscore.split('_')[0...-1].join('_').capitalize
feature_name = test.name.split('_')[1..-1].join(' ')
scenario_name = [feature_group, feature_name].join(' ')
AppMap::Minitest.save name: scenario_name,
class_map: class_map,
source_location: source_location,
test_status: failed ? 'failed' : 'succeeded',
test_failure: test_failure,
exception: exception,
events: events
end
end
@recordings_by_test = {}
@event_methods = Set.new
@recording_count = 0
class << self
def init
FileUtils.mkdir_p APPMAP_OUTPUT_DIR
end
def first_recording?
@recording_count == 0
end
def begin_test(test, name)
AppMap.info 'Configuring AppMap recorder for Minitest' if first_recording?
@recording_count += 1
@recordings_by_test[test.object_id] = Recording.new(test, name)
end
def end_test(test, exception:)
recording = @recordings_by_test.delete(test.object_id)
return warn "No recording found for #{test}" unless recording
recording.finish test.failures || [], exception
end
def config
@config or raise 'AppMap is not configured'
end
def add_event_methods(event_methods)
@event_methods += event_methods
end
def save(name:, class_map:, source_location:, test_status:, test_failure:, exception:, events:)
metadata = AppMap::Minitest.metadata.tap do |m|
m[:name] = name
m[:source_location] = source_location
m[:app] = AppMap.configuration.name
m[:frameworks] ||= []
m[:frameworks] << {
name: 'minitest',
version: Gem.loaded_specs['minitest']&.version&.to_s,
}
m[:recorder] = {
name: 'minitest',
type: 'tests',
}
m[:test_status] = test_status
m[:test_failure] = test_failure if test_failure
if exception
m[:exception] = Util.format_exception(exception)
end
end
appmap = {
version: AppMap::APPMAP_FORMAT_VERSION,
metadata: metadata,
classMap: class_map,
events: events,
}.compact
fname = AppMap::Util.scenario_filename(name)
AppMap::Util.write_appmap(APPMAP_OUTPUT_DIR, fname, appmap, source_location: source_location)
end
def enabled?
AppMap.recording_enabled?(:minitest)
end
def run
init
end
end
end
end
if AppMap::Minitest.enabled?
require 'appmap'
require 'minitest/test'
class ::Minitest::Test
alias run_without_hook run
def run
AppMap::Minitest.begin_test self, name
begin
run_without_hook
ensure
AppMap::Minitest.end_test self, exception: $!
end
end
end
AppMap::Minitest.run
end