Top Level Namespace

Defined Under Namespace

Modules: PlainColorizer, RSpec Classes: String, TrunkAnalyticsListener

Constant Summary collapse

ANSI_ESCAPE_PATTERN =
%r{(?:\e[@-Z\\-_]|\e\[[0-?]*[ -/]*[@-~])}

Instance Method Summary collapse

Instance Method Details

#escape(str) ⇒ Object



63
64
65
# File 'lib/trunk_spec_helper.rb', line 63

def escape(str)
  str.dump[1..-2]
end

#exception_backtrace_lines(exception, example) ⇒ Object



264
265
266
267
268
269
# File 'lib/trunk_spec_helper.rb', line 264

def exception_backtrace_lines(exception, example)
  presenter = RSpec::Core::Formatters::ExceptionPresenter.new(exception, example)
  Array(presenter.formatted_backtrace)
rescue StandardError
  Array(exception.backtrace)
end

#format_exception_backtrace(exception, example) ⇒ Object

trunk-ignore(rubocop/Metrics/MethodLength,rubocop/Metrics/AbcSize)



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/trunk_spec_helper.rb', line 239

def format_exception_backtrace(exception, example)
  return '' unless exception

  lines = exception_backtrace_lines(exception, example)

  cause = exception.cause
  depth = 0
  while cause && depth < 10
    lines << ''
    lines << "Caused by: #{cause.class}: #{cause.message}"
    lines.concat(exception_backtrace_lines(cause, example))
    cause = cause.cause
    depth += 1
  end

  result = lines.join("\n")
  # The exception presenter may choke on MultipleExceptionError, such as errors in before
  # and after hooks, so we fall back to the legacy formatter
  return legacy_format_exception_backtrace(exception) if result.strip.empty?

  strip_ansi_codes(result)
rescue StandardError
  legacy_format_exception_backtrace(exception)
end

#format_exception_message(exception, example) ⇒ Object

Defer to RSpec’s own ExceptionPresenter so the failure_message field matches what users see in their RSpec console output (Failure/Error: <source line>, the exception class and message, and any “Caused by:” chain).



229
230
231
232
233
234
235
236
# File 'lib/trunk_spec_helper.rb', line 229

def format_exception_message(exception, example)
  return '' unless exception

  presenter = RSpec::Core::Formatters::ExceptionPresenter.new(exception, example)
  strip_ansi_codes(presenter.fully_formatted(nil, PlainColorizer))
rescue StandardError
  legacy_format_exception_message(exception)
end

#knapsack_detector_command?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/trunk_spec_helper.rb', line 78

def knapsack_detector_command?
  command_line = "#{$PROGRAM_NAME} #{ARGV.join(' ')}".strip
  command_line.include?('knapsack_pro:rspec_test_example_detector') ||
    command_line.include?('knapsack_pro:queue:rspec:initialize')
end

#knapsack_detector_mode?Boolean

Knapsack example detector instantiates all test cases in order to determine how to shard them These instantiations should not generate test bundles, so we disable the gem when running under knapsack_pro:rspec_test_example_detector

Returns:

  • (Boolean)


74
75
76
# File 'lib/trunk_spec_helper.rb', line 74

def knapsack_detector_mode?
  knapsack_detector_command?
end

#legacy_format_exception_backtrace(exception) ⇒ Object

trunk-ignore(rubocop/Metrics/MethodLength,rubocop/Metrics/AbcSize)



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/trunk_spec_helper.rb', line 282

def legacy_format_exception_backtrace(exception)
  case exception
  when RSpec::Core::MultipleExceptionError
    strip_ansi_codes(exception.all_exceptions.map do |e|
      if e.backtrace && !e.backtrace.empty?
        "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
      else
        "#{e.class}: #{e.message}"
      end
    end.join("\n\n"))
  else
    strip_ansi_codes(exception.backtrace&.join("\n") || '')
  end
end

#legacy_format_exception_message(exception) ⇒ Object



271
272
273
274
275
276
277
278
279
# File 'lib/trunk_spec_helper.rb', line 271

def legacy_format_exception_message(exception)
  case exception
  when RSpec::Core::MultipleExceptionError
    messages = exception.all_exceptions.map { |e| "#{e.class}: #{e.message}" }
    strip_ansi_codes("#{exception.class}: #{messages.join(' | ')}")
  else
    strip_ansi_codes(exception.to_s)
  end
end

#quarantine_query_failure_exit?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/trunk_spec_helper.rb', line 89

def quarantine_query_failure_exit?
  ENV['TRUNK_QUARANTINE_QUERY_FAILURE_EXIT'] == 'true'
end

#strip_ansi_codes(text) ⇒ Object



67
68
69
# File 'lib/trunk_spec_helper.rb', line 67

def strip_ansi_codes(text)
  text.to_s.gsub(ANSI_ESCAPE_PATTERN, '')
end

#trunk_disabledObject



84
85
86
87
# File 'lib/trunk_spec_helper.rb', line 84

def trunk_disabled
  knapsack_detector_mode? || ENV['DISABLE_RSPEC_TRUNK_FLAKY_TESTS'] == 'true' ||
    ENV['TRUNK_ORG_URL_SLUG'].nil? || ENV['TRUNK_API_TOKEN'].nil?
end