Skip to content

Commit

Permalink
Merge pull request #232 from dylanratcliffe/formatter_factset
Browse files Browse the repository at this point in the history
Formatter factset
  • Loading branch information
dylanratcliffe authored Jun 12, 2019
2 parents cce35ca + 34c8224 commit 69b7926
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/onceover/controlrepo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def initialize(opts = {})

@onceover_yaml = ENV['ONCEOVER_YAML'] || opts[:onceover_yaml] || File.expand_path("#{@root}/spec/onceover.yaml")

if File.exists?(@onceover_yaml) && _data = YAML.load_file(@onceover_yaml)
if File.exist?(@onceover_yaml) && _data = YAML.load_file(@onceover_yaml)
opts.merge!(_data.fetch('opts',{})||{})
end
opts.fetch(:facts_dir,'').sub!(%r{^[^/.].+} ){|path| File.expand_path(path, @root)}
Expand Down Expand Up @@ -475,7 +475,7 @@ def self.init(repo)

# Add .onceover to Gitignore
gitignore_path = File.expand_path('.gitignore', repo.root)
if File.exists? gitignore_path
if File.exist? gitignore_path
gitignore_content = (File.open(gitignore_path, 'r') {|f| f.read }).split("\n")
message = "#{'changed'.green}"
else
Expand Down Expand Up @@ -567,7 +567,7 @@ def self.evaluate_template(template_name, bind)

def self.init_write_file(contents, out_file)
create_dirs_and_log(File.dirname(out_file))
if File.exists?(out_file)
if File.exist?(out_file)
puts "#{'skipped'.yellow} #{Pathname.new(out_file).relative_path_from(Pathname.new(Dir.pwd)).to_s} #{'(exists)'.yellow}"
else
File.open(out_file,'w') {|f| f.write(contents)}
Expand Down
32 changes: 23 additions & 9 deletions lib/onceover/rspec/formatters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def dump_failures notification
# Put some spacing before the results
@output << "\n\n\n"

failures.each do |_name, role|
failures.each do |name, errors|
@output << Onceover::Controlrepo.evaluate_template('error_summary.yaml.erb', binding)
end

Expand All @@ -74,20 +74,34 @@ def extract_failures notification

# Further group by error
grouped.each do |role, failures|
grouped[role] = failures.uniq { |f| f.metadata[:execution_result].exception.to_s }
grouped[role] = failures.group_by { |f| f.metadata[:execution_result].exception.to_s }
end

# Extract the errors and remove all RSpec objects
grouped.each do |role, failures|
grouped[role] = {
name: role,
errors: failures.map { |f| parse_errors(f.metadata[:execution_result].exception.to_s)}.flatten,
}
grouped[role] = failures.map { |_description, fails| extract_failure_data(fails)}.flatten
end

grouped
end

# Extaracts data out of RSpec failres
def extract_failure_data(fails)
# The only difference between these failures should be the factsets that it
# failed on. Extract that list then just use the first failure for the rest
# of the data as it should be the same
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)
# Add the details of the factsets tio each result
results.map do |r|
r[:factsets] = factsets
r
end
end

# Parses information out of a string error
def parse_errors(raw_error)
# Check if the error is a compilation error
match = COMPILATION_ERROR.match(raw_error)
Expand Down Expand Up @@ -247,9 +261,9 @@ def output_results(directory)
files = Dir["#{directory}/*.yaml"]

# Merge data
errors = files.reduce({}) do |errs, file|
roles = files.reduce({}) do |errs, file|
# Read all files and merge them
errs.merge(YAML.load(File.read(file))) # rubocop:disable Security/YAMLLoad
errs.merge(YAML.load(File.read(file))) {|key, oldval, newval| [oldval, newval].flatten }# rubocop:disable Security/YAMLLoad
end

# Delete files from the disk
Expand All @@ -258,7 +272,7 @@ def output_results(directory)
@output << "\n\n\n"

# Output errors
errors.each do |_name, role|
roles.each do |name, errors|
@output << Onceover::Controlrepo.evaluate_template('error_summary.yaml.erb', binding)
end
@output << "\n"
Expand Down
7 changes: 5 additions & 2 deletions templates/error_summary.yaml.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= bold(role[:name]) %>: <%= red('failed') %>
<%= bold(name) %>: <%= red('failed') %>
errors:
<% role[:errors].each do |error| -%>
<% errors.each do |error| -%>
<%= red(error[:text]) %>
<% if error[:file] -%>
file: <%= bold(error[:file]) %>
Expand All @@ -11,4 +11,7 @@
<% if error[:column] -%>
column: <%= bold(error[:column]) %>
<% end -%>
<% if error[:factsets] -%>
factsets: <%= bold(error[:factsets].join(', ')) %>
<% end -%>
<% end -%>

0 comments on commit 69b7926

Please sign in to comment.