Skip to content

A platform for community discussion. Free, open, simple.

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

Martin Brennanaca6c462a6af
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index dd30745bbe2..7e78157d6bd 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -264,7 +264,10 @@ RSpec.configure do |config|
# 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
# 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 = [
%r{/lib\d*/ruby/},
%r{bin/},
@@ -686,23 +689,43 @@ RSpec.configure do |config|
end
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?
lines = (RSpec.current_example.metadata[:extra_failure_lines] ||= +"")
+ lines << "\n"
lines << "~~~~~~~ SERVER EXCEPTIONS ~~~~~~~"
+ lines << "\n"
RspecErrorTracker.exceptions.each_with_index do |(path, ex), index|
lines << "\n"
- lines << "Error encountered while processing #{path}"
- lines << " #{ex.class}: #{ex.message}"
+ lines << "Error encountered while processing #{path}.\n"
+ lines << " #{ex.class}: #{ex.message}\n"
+ framework_lines_excluded = 0
+
ex.backtrace.each_with_index do |line, backtrace_index|
- if ENV["RSPEC_EXCLUDE_GEMS_IN_BACKTRACE"]
- next if line.match?(%r{/gems/})
+ if ENV["DISCOURSE_INCLUDE_GEMS_IN_RSPEC_BACKTRACE"] != "1"
+ 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
lines << " #{line}\n"
end
end
+ lines << "\n"
lines << "~~~~~~~ END SERVER EXCEPTIONS ~~~~~~~"
lines << "\n"
end