This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 758
/
Copy pathjson_formatter.rb
102 lines (89 loc) · 3.16 KB
/
json_formatter.rb
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
RSpec::Support.require_rspec_core "formatters/base_formatter"
require 'json'
module RSpec
module Core
module Formatters
# @private
class JsonFormatter < BaseFormatter
Formatters.register self, :message, :dump_summary, :dump_profile, :stop, :seed, :close
attr_reader :output_hash
def initialize(output)
super
@output_hash = {
:version => RSpec::Core::Version::STRING
}
end
def message(notification)
(@output_hash[:messages] ||= []) << notification.message
end
def dump_summary(summary)
@output_hash[:summary] = {
:duration => summary.duration,
:example_count => summary.example_count,
:failure_count => summary.failure_count,
:pending_count => summary.pending_count,
:errors_outside_of_examples_count => summary.errors_outside_of_examples_count
}
@output_hash[:summary_line] = summary.totals_line
end
def stop(notification)
@output_hash[:examples] = notification.examples.map do |example|
format_example(example).tap do |hash|
e = example.exception
if e
hash[:exception] = {
:class => e.class.name,
:message => e.message,
:backtrace => e.backtrace,
}
end
end
end
end
def seed(notification)
return unless notification.seed_used?
@output_hash[:seed] = notification.seed
end
def close(_notification)
output.write @output_hash.to_json
end
def dump_profile(profile)
@output_hash[:profile] = {}
dump_profile_slowest_examples(profile)
dump_profile_slowest_example_groups(profile)
end
# @api private
def dump_profile_slowest_examples(profile)
@output_hash[:profile] = {}
@output_hash[:profile][:examples] = profile.slowest_examples.map do |example|
format_example(example).tap do |hash|
hash[:run_time] = example.execution_result.run_time
end
end
@output_hash[:profile][:slowest] = profile.slow_duration
@output_hash[:profile][:total] = profile.duration
end
# @api private
def dump_profile_slowest_example_groups(profile)
@output_hash[:profile] ||= {}
@output_hash[:profile][:groups] = profile.slowest_groups.map do |loc, hash|
hash.update(:location => loc)
end
end
private
def format_example(example)
{
:id => example.id,
:description => example.description,
:full_description => example.full_description,
:status => example.execution_result.status.to_s,
:file_path => example.metadata[:file_path],
:line_number => example.metadata[:line_number],
:run_time => example.execution_result.run_time,
:pending_message => example.execution_result.pending_message,
}
end
end
end
end
end