Class: OnceoverFormatter
- Inherits:
-
Object
show all
- Defined in:
- lib/onceover/rspec/formatters.rb
Constant Summary
collapse
- COMPILATION_ERROR =
%r{error during compilation: (?<error>.*)}
- ERROR_WITH_LOCATION =
rubocop:disable Lint/MixedRegexpCaptureTypes
%r{(?<error>.*?)\s(at )?(\((file: (?<file>.*?), )?line: (?<line>\d+)(, column: (?<column>\d+))?\))(; )?}
- ERROR_WITHOUT_LOCATION =
rubocop:enable Lint/MixedRegexpCaptureTypes
%r{(?<error>.*?)\son node}
Instance Method Summary
collapse
Constructor Details
Returns a new instance of OnceoverFormatter.
20
21
22
23
|
# File 'lib/onceover/rspec/formatters.rb', line 20
def initialize output
@output = output
@previous_role = nil
end
|
Instance Method Details
#black(text) ⇒ Object
187
188
189
|
# File 'lib/onceover/rspec/formatters.rb', line 187
def black(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :black)
end
|
#blue(text) ⇒ Object
203
204
205
|
# File 'lib/onceover/rspec/formatters.rb', line 203
def blue(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :blue)
end
|
#bold(text) ⇒ Object
219
220
221
|
# File 'lib/onceover/rspec/formatters.rb', line 219
def bold(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :bold)
end
|
#calculate_relative_source(file) ⇒ Object
This method calculates where the original source file is relative to the user’s current location. This is more compliacted than it sounds because if we are running from the root of the controlrepo and we have an error in:
/Users/dylan/git/puppet_controlrepo/.onceover/etc/puppetlabs/code/environments/production/site/role/manifests/lb.pp
We need that to end up pointing at the original source file not the cached one i.e.
site/role/manifests/lb.pp
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/onceover/rspec/formatters.rb', line 170
def calculate_relative_source(file)
return nil if file.nil?
file = Pathname.new(file)
tempdir = Pathname.new(RSpec.configuration.onceover_tempdir)
root = Pathname.new(RSpec.configuration.onceover_root)
environmentpath = Pathname.new(RSpec.configuration.onceover_environmentpath)
file.relative_path_from(tempdir + environmentpath + "production").to_s
end
|
#class_name(text) ⇒ Object
Below are defined the styles for the output
183
184
185
|
# File 'lib/onceover/rspec/formatters.rb', line 183
def class_name(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :bold)
end
|
#cyan(text) ⇒ Object
211
212
213
|
# File 'lib/onceover/rspec/formatters.rb', line 211
def cyan(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :cyan)
end
|
#dump_failures(notification) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/onceover/rspec/formatters.rb', line 62
def dump_failures notification
require 'onceover/controlrepo'
failures = (notification)
@output << "\n\n\n"
failures.each do |name, errors|
@output << Onceover::Controlrepo.evaluate_template('error_summary.yaml.erb', binding)
end
@output << "\n"
end
|
#example_failed(notification) ⇒ Object
52
53
54
55
|
# File 'lib/onceover/rspec/formatters.rb', line 52
def example_failed notification
@output << "\b\b"
@output << "#{red('F')} "
end
|
#example_group_started(notification) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/onceover/rspec/formatters.rb', line 25
def example_group_started notification
if notification.group.parent_groups == [notification.group]
role = notification.group.description
if role != @previous_role
@output << "\n"
@output << class_name("#{notification.group.description}:")
padding = (longest_group - role.length) + 1
padding.times { @output << ' ' }
@previous_role = role
end
else
@output << '? '
end
end
|
#example_passed(notification) ⇒ Object
47
48
49
50
|
# File 'lib/onceover/rspec/formatters.rb', line 47
def example_passed notification
@output << "\b\b"
@output << "#{green('P')} "
end
|
#example_pending(notification) ⇒ Object
57
58
59
60
|
# File 'lib/onceover/rspec/formatters.rb', line 57
def example_pending notification
@output << "\b\b"
@output << "#{yellow('?')} "
end
|
Extaracts data out of RSpec failres
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/onceover/rspec/formatters.rb', line 100
def (fails)
metadata = fails[0].metadata
raw_error = metadata[:execution_result].exception.to_s
factsets = fails.map { |f| f.metadata[:example_group][:description].gsub('using fact set ','') }
results = parse_errors(raw_error)
results.map do |r|
r[:factsets] = factsets
r
end
end
|
rubocop:disable Style/CombinableLoops
This method takes a notification and formats it into a hash that can be printed easily
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/onceover/rspec/formatters.rb', line 81
def notification
grouped = notification.failed_examples.group_by { |e| e.metadata[:example_group][:parent_example_group][:description]}
grouped.each do |role, failures|
grouped[role] = failures.group_by { |f| f.metadata[:execution_result].exception.to_s }
end
grouped.each do |role, failures|
grouped[role] = failures.map { |_description, fails| (fails)}.flatten
end
grouped
end
|
#green(text) ⇒ Object
195
196
197
|
# File 'lib/onceover/rspec/formatters.rb', line 195
def green(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :green)
end
|
#longest_group ⇒ Object
223
224
225
|
# File 'lib/onceover/rspec/formatters.rb', line 223
def longest_group
RSpec.configuration.world.example_groups.max { |a,b| a.description.length <=> b.description.length}.description.length
end
|
#magenta(text) ⇒ Object
207
208
209
|
# File 'lib/onceover/rspec/formatters.rb', line 207
def magenta(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :magenta)
end
|
#parse_errors(raw_error) ⇒ Object
Parses information out of a string error
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
|
# File 'lib/onceover/rspec/formatters.rb', line 116
def parse_errors(raw_error)
match = COMPILATION_ERROR.match(raw_error)
if match
compilation_error = match['error']
if ERROR_WITH_LOCATION.match(compilation_error)
scanned_errors = match['error'].scan(ERROR_WITH_LOCATION)
scanned_errors.delete_if { |e| e.first.empty? }
scanned_errors.map do |error_matches|
{
text: error_matches[0],
file: calculate_relative_source(error_matches[1]),
line: error_matches[2],
column: error_matches[3],
}
end
elsif ERROR_WITHOUT_LOCATION.match(compilation_error)
scanned_errors = match['error'].scan(ERROR_WITHOUT_LOCATION)
scanned_errors.delete_if { |e| e.first.empty? }
scanned_errors.map do |error_matches|
{
text: error_matches[0],
}
end
else
[{
text: raw_error,
}]
end
else
[{
text: raw_error,
}]
end
end
|
#red(text) ⇒ Object
191
192
193
|
# File 'lib/onceover/rspec/formatters.rb', line 191
def red(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :red)
end
|
#white(text) ⇒ Object
215
216
217
|
# File 'lib/onceover/rspec/formatters.rb', line 215
def white(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :white)
end
|
#yellow(text) ⇒ Object
199
200
201
|
# File 'lib/onceover/rspec/formatters.rb', line 199
def yellow(text)
RSpec::Core::Formatters::ConsoleCodes.wrap(text, :yellow)
end
|