Skip to content

Commit 570911a

Browse files
committed
Add option to add rails gems to exclusion filters
Rails in backtraces can get quite spammy, and the configuration option to `filter_gems_from_backtrace` is not well-known since it is not found in the generator's `rails_helper.rb`. This adds `filter_rails_from_backtrace!` as a convenience method for nixing rails gems in the backtrace and adds it to `rails_helper.rb` to raise awareness.
1 parent 819a697 commit 570911a

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

features/.nav

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Changelog.md
77
- RailsVersions.md (Rails versions)
88
- directory_structure.feature
9+
- backtrace_filtering.feature
910
- model_specs:
1011
- transactional_examples.feature
1112
- mocks:

features/backtrace_filtering.feature

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Feature: backtrace filtering
2+
3+
The following configuration setting will filter out lines in backtraces that come from Rails gems in order to reduce the noise in test failure output:
4+
5+
```ruby
6+
RSpec.configure do |config|
7+
config.filter_rails_from_backtrace!
8+
end
9+
```
10+
11+
`rspec` will always show the full backtrace output when run with the `--backtrace` commandline option.
12+
13+
Background: Using `filter_rails_from_backtrace!`
14+
Given a file named "spec/failing_spec.rb" with:
15+
"""ruby
16+
require "rails_helper"
17+
18+
RSpec.configure do |config|
19+
config.filter_rails_from_backtrace!
20+
end
21+
22+
RSpec.describe "Controller", :type => :controller do
23+
controller do
24+
def index
25+
raise "Something went wrong."
26+
end
27+
end
28+
29+
describe "GET index" do
30+
it "raises an error" do
31+
get :index
32+
end
33+
end
34+
end
35+
"""
36+
37+
Scenario: Using the bare `rspec` command
38+
When I run `rspec`
39+
Then the output should contain "1 example, 1 failure"
40+
And the output should not contain "activesupport"
41+
42+
Scenario: Using `rspec --backtrace`
43+
When I run `rspec --backtrace`
44+
Then the output should contain "1 example, 1 failure"
45+
And the output should contain "activesupport"

lib/generators/rspec/install/templates/spec/rails_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@
5858
# The different available types are documented in the features, such as in
5959
# https://relishapp.com/rspec/rspec-rails/docs
6060
config.infer_spec_type_from_file_location!
61+
62+
# Filter lines from Rails gems in backtraces.
63+
config.filter_rails_from_backtrace!
64+
# arbitrary gems may also be filtered via:
65+
# config.filter_gems_from_backtrace("gem name")
6166
end

lib/rspec/rails/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ def infer_spec_type_from_file_location!
8989
end
9090
end
9191
end
92+
93+
# Adds exclusion filters for gems included with Rails
94+
def filter_rails_from_backtrace!
95+
filter_gems_from_backtrace "actionmailer", "actionpack" ,"actionview"
96+
filter_gems_from_backtrace "activemodel", "activerecord",
97+
"activesupport", "activejob"
98+
end
9299
end
93100

94101
config.include RSpec::Rails::ControllerExampleGroup, :type => :controller

spec/generators/rspec/install/install_generator_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def use_transactional_fixtures
3131
match(/config\.use_transactional_fixtures/m)
3232
end
3333

34+
def filter_rails_from_backtrace
35+
match(/config\.filter_rails_from_backtrace!/m)
36+
end
37+
3438
setup_default_destination
3539

3640
let(:rails_helper) { content_for('spec/rails_helper.rb') }
@@ -69,6 +73,12 @@ def use_transactional_fixtures
6973
expect(rails_helper).to use_transactional_fixtures
7074
end
7175

76+
specify "excluding rails gems from the backtrace" do
77+
run_generator
78+
79+
expect(rails_helper).to filter_rails_from_backtrace
80+
end
81+
7282
if RSpec::Rails::FeatureCheck.can_maintain_test_schema?
7383
specify "checking for maintaining the schema" do
7484
run_generator

spec/rspec/rails/configuration_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@
9999
end
100100
end
101101

102+
specify "#filter_rails_from_backtrace! adds exclusion patterns for rails gems" do
103+
config.filter_rails_from_backtrace!
104+
105+
gems = %w(
106+
actionmailer
107+
actionpack
108+
actionview
109+
activemodel
110+
activerecord
111+
activesupport
112+
activejob
113+
)
114+
exclusions = config.backtrace_exclusion_patterns.map(&:to_s)
115+
aggregate_failures do
116+
gems.each { |gem| expect(exclusions).to include(%r(#{gem})) }
117+
end
118+
end
119+
102120
describe "#infer_spec_type_from_file_location!" do
103121
def in_inferring_type_from_location_environment
104122
in_sub_process do

0 commit comments

Comments
 (0)