DEV: Improve rspec gem backtrace exclusion ENV vars (#30056)

Followup:

* https://github.com/discourse/discourse/pull/28160
* https://github.com/discourse/discourse/pull/25921

In the previous PRs we added 2 environent variables
to control backtrace output for errors in rspec,
`RSPEC_EXCLUDE_NOISE_IN_BACKTRACE`, and
`RSPEC_EXCLUDE_GEMS_IN_BACKTRACE`

These largely do the same thing, and we want to enable
that behaviour by default.

This commit consolidates them into one env var,
`DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE`, which is
disabled by default, meaning gem backtraces will not
be shown in rspec backtraces by default.

Also for the request spec use case with `RspecErrorTracker`,
we now show an indicator of how many lines were hidden from
the backtrace e.g. "...(21 framework line(s) excluded)",
and for this and the normal rspec backtrace exclusion we
show a warning if `DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE`
is not enabled.
This commit is contained in:
Martin Brennan 2024-12-04 09:54:11 +10:00 committed by GitHub
parent 9541c9bf18
commit aca6c462a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -264,7 +264,10 @@ RSpec.configure do |config|
# Sometimes the backtrace is quite big for failing specs, this will # Sometimes the backtrace is quite big for failing specs, this will
# remove rspec/gem paths from the backtrace so it's easier to see the # remove rspec/gem paths from the backtrace so it's easier to see the
# actual application code that caused the failure. # actual application code that caused the failure.
if ENV["RSPEC_EXCLUDE_NOISE_IN_BACKTRACE"] #
# This behaviour is enabled by default, to include gems in
# the backtrace set DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE=1
if ENV["DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE"] != "1"
config.backtrace_exclusion_patterns = [ config.backtrace_exclusion_patterns = [
%r{/lib\d*/ruby/}, %r{/lib\d*/ruby/},
%r{bin/}, %r{bin/},
@ -686,23 +689,43 @@ RSpec.configure do |config|
end end
config.after :each do |example| config.after :each do |example|
# This behaviour is enabled by default, to include gems in
# the backtrace set DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE=1
if example.exception && ENV["DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE"] != "1"
lines = (RSpec.current_example.metadata[:extra_failure_lines] ||= +"")
lines << "Warning: DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE has not been enabled, gem backtrace will be excluded from the output\n"
end
if example.exception && RspecErrorTracker.exceptions.present? if example.exception && RspecErrorTracker.exceptions.present?
lines = (RSpec.current_example.metadata[:extra_failure_lines] ||= +"") lines = (RSpec.current_example.metadata[:extra_failure_lines] ||= +"")
lines << "\n"
lines << "~~~~~~~ SERVER EXCEPTIONS ~~~~~~~" lines << "~~~~~~~ SERVER EXCEPTIONS ~~~~~~~"
lines << "\n"
RspecErrorTracker.exceptions.each_with_index do |(path, ex), index| RspecErrorTracker.exceptions.each_with_index do |(path, ex), index|
lines << "\n" lines << "\n"
lines << "Error encountered while processing #{path}" lines << "Error encountered while processing #{path}.\n"
lines << " #{ex.class}: #{ex.message}" lines << " #{ex.class}: #{ex.message}\n"
framework_lines_excluded = 0
ex.backtrace.each_with_index do |line, backtrace_index| ex.backtrace.each_with_index do |line, backtrace_index|
if ENV["RSPEC_EXCLUDE_GEMS_IN_BACKTRACE"] if ENV["DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE"] != "1"
next if line.match?(%r{/gems/}) if line.match?(%r{/gems/})
framework_lines_excluded += 1
next
else
if framework_lines_excluded.positive?
lines << " ...(#{framework_lines_excluded} framework line(s) excluded)\n"
framework_lines_excluded = 0
end
end
end end
lines << " #{line}\n" lines << " #{line}\n"
end end
end end
lines << "\n"
lines << "~~~~~~~ END SERVER EXCEPTIONS ~~~~~~~" lines << "~~~~~~~ END SERVER EXCEPTIONS ~~~~~~~"
lines << "\n" lines << "\n"
end end