class RSpec::Buildkite::Analytics::Uploader::Trace

Attributes

example[RW]
history[R]
id[R]

Public Class Methods

new(example, history) click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 28
def initialize(example, history)
  @id = SecureRandom.uuid
  @example = example
  @history = history
end

Public Instance Methods

as_json() click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 51
def as_json
  {
    id: @id,
    scope: example.example_group.metadata[:full_description],
    name: example.description,
    identifier: example.id,
    location: example.location,
    file_name: generate_file_name(example),
    result: result_state,
    failure: failure_message,
    history: history,
  }
end
failure_message() click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 34
def failure_message
  case example.exception
  when RSpec::Expectations::ExpectationNotMetError
    example.exception.message
  when Exception
    "#{example.exception.class}: #{example.exception.message}"
  end
end
result_state() click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 43
def result_state
  case example.execution_result.status
  when :passed; "passed"
  when :failed; "failed"
  when :pending; "skipped"
  end
end

Private Instance Methods

generate_file_name(example) click to toggle source
# File lib/rspec/buildkite/analytics/uploader.rb, line 67
def generate_file_name(example)
  file_path_regex = /^(.*?\.rb)/
  identifier_file_name = example.id[file_path_regex]
  location_file_name = example.location[file_path_regex]

  if identifier_file_name != location_file_name
    # If the identifier and location files are not the same, we assume
    # that the test was run as part of a shared example. If this isn't the
    # case, then there's something we haven't accounted for
    if example.metadata[:shared_group_inclusion_backtrace].any?
      # Taking the last frame in this backtrace will give us the original
      # entry point for the shared example
      example.metadata[:shared_group_inclusion_backtrace].last.inclusion_location[file_path_regex]
    else
      "Unknown"
    end
  else
    identifier_file_name
  end
end