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
|
# File 'lib/profiler/test_profiler.rb', line 40
def run(&block)
profile = Models::Profile.new
profile.profile_type = "test"
profile.path = @test_file
profile.method = "TEST"
test_collector = Collectors::TestCollector.new(
profile,
test_name: @test_name,
test_file: @test_file,
test_line: @test_line,
framework: @framework
)
collectors = [test_collector] + TEST_COLLECTOR_CLASSES.map { |klass| klass.new(profile) }
collectors.each { |c| c.subscribe if c.respond_to?(:subscribe) }
exception_collector = collectors.find { |c| c.is_a?(Collectors::ExceptionCollector) }
memory_before = current_memory if Profiler.configuration.track_memory
test_status = "passed"
error_message = nil
previous_token = Profiler::CurrentContext.token
Profiler::CurrentContext.token = profile.token
begin
result = block.call
if result.respond_to?(:passed?)
unless result.passed?
test_status = result.skipped? ? "pending" : "failed"
msg = result.failure&.message.to_s
error_message = msg.empty? ? nil : msg
end
test_collector.(
assertions: result.respond_to?(:assertions) ? result.assertions : nil,
skip_reason: (result.skipped? && result.failure) ? result.failure.message : nil
)
end
if result.respond_to?(:execution_result)
er = result.execution_result
rspec_status = er.status&.to_s
if rspec_status && rspec_status != "passed" && test_status == "passed"
test_status = rspec_status
msg = er.exception&.message.to_s
error_message = msg.empty? ? nil : msg
end
end
result
rescue Exception => e test_status = "failed"
error_message = "#{e.class}: #{e.message}"
exception_collector&.capture(e) if e.is_a?(StandardError)
raise
ensure
Profiler::CurrentContext.token = previous_token
if Profiler.configuration.track_memory
profile.memory = current_memory - memory_before
end
test_collector.update_status(test_status, error_message)
profile.finish(test_status == "passed" ? 200 : 500)
collectors.each do |collector|
begin
collector.collect if collector.respond_to?(:collect)
profile.add_collector_metadata(collector)
rescue => e
warn "Profiler TestProfiler: Collector #{collector.class} failed: #{e.message}"
end
end
Profiler.storage.save(profile.token, profile)
end
end
|