class OnceoverFormatter

Constants

COMPILATION_ERROR
ERROR_WITHOUT_LOCATION

rubocop:enable Lint/MixedRegexpCaptureTypes

ERROR_WITH_LOCATION

rubocop:disable Lint/MixedRegexpCaptureTypes

Public Class Methods

new(output) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 20
def initialize output
  @output        = output
  @previous_role = nil
end

Public Instance Methods

black(text) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 187
def black(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :black)
end
blue(text) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 203
def blue(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :blue)
end
bold(text) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 219
def bold(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :bold)
end
calculate_relative_source(file) click to toggle source

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

# 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)

  # Calculate the full relative path
  file.relative_path_from(tempdir + environmentpath + "production").to_s
end
class_name(text) click to toggle source

Below are defined the styles for the output

# File lib/onceover/rspec/formatters.rb, line 183
def class_name(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :bold)
end
cyan(text) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 211
def cyan(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :cyan)
end
dump_failures(notification) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 62
def dump_failures notification
  require 'onceover/controlrepo'

  failures = extract_failures(notification)

  # Put some spacing before the results
  @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) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 52
def example_failed notification
  @output << "\b\b"
  @output << "#{red('F')} "
end
example_group_started(notification) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 25
def example_group_started notification
  if notification.group.parent_groups == [notification.group]
    # If this is the highest level group (The role)
    role = notification.group.description
    if role != @previous_role
      @output << "\n"
      @output << class_name("#{notification.group.description}:")

      # Calculate the padding required
      padding = (longest_group - role.length) + 1
      # Create padding
      padding.times { @output << ' ' }

      # Save the role name
      @previous_role = role
    end
  else
    # If not then this will be a test for that role
    @output << '? '
  end
end
example_passed(notification) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 47
def example_passed notification
  @output << "\b\b"
  @output << "#{green('P')} "
end
example_pending(notification) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 57
def example_pending notification
  @output << "\b\b"
  @output << "#{yellow('?')} "
end
extract_failure_data(fails) click to toggle source

Extaracts data out of RSpec failres

# File lib/onceover/rspec/formatters.rb, line 100
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
extract_failures(notification) click to toggle source

rubocop:disable Style/CombinableLoops

This method takes a notification and formats it into a hash that can be printed easily

# File lib/onceover/rspec/formatters.rb, line 81
def extract_failures notification
  # Group by role
  grouped = notification.failed_examples.group_by { |e| e.metadata[:example_group][:parent_example_group][:description]}

  # Further group by error
  grouped.each do |role, failures|
    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] = failures.map { |_description, fails| extract_failure_data(fails)}.flatten
  end

  grouped
end
green(text) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 195
def green(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :green)
end
longest_group() click to toggle source
# 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) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 207
def magenta(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :magenta)
end
parse_errors(raw_error) click to toggle source

Parses information out of a string error

# File lib/onceover/rspec/formatters.rb, line 116
def parse_errors(raw_error)
  # Check if the error is a compilation error
  match = COMPILATION_ERROR.match(raw_error)
  if match
    compilation_error = match['error']
    # Check if we car parse it
    if ERROR_WITH_LOCATION.match(compilation_error)
      scanned_errors = match['error'].scan(ERROR_WITH_LOCATION)

      # Delete any matches where there was no error text
      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)

      # Delete any matches where there was no error text
      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) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 191
def red(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :red)
end
white(text) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 215
def white(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :white)
end
yellow(text) click to toggle source
# File lib/onceover/rspec/formatters.rb, line 199
def yellow(text)
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, :yellow)
end