Skip to content

Commit

Permalink
Add option to add rails gems to exclusion filters
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
soulcutter committed Sep 18, 2015
1 parent 819a697 commit 570911a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions features/.nav
Expand Up @@ -6,6 +6,7 @@
- Changelog.md
- RailsVersions.md (Rails versions)
- directory_structure.feature
- backtrace_filtering.feature
- model_specs:
- transactional_examples.feature
- mocks:
Expand Down
45 changes: 45 additions & 0 deletions features/backtrace_filtering.feature
@@ -0,0 +1,45 @@
Feature: backtrace filtering

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:

```ruby
RSpec.configure do |config|
config.filter_rails_from_backtrace!
end
```

`rspec` will always show the full backtrace output when run with the `--backtrace` commandline option.

Background: Using `filter_rails_from_backtrace!`
Given a file named "spec/failing_spec.rb" with:
"""ruby
require "rails_helper"
RSpec.configure do |config|
config.filter_rails_from_backtrace!
end
RSpec.describe "Controller", :type => :controller do
controller do
def index
raise "Something went wrong."
end
end
describe "GET index" do
it "raises an error" do
get :index
end
end
end
"""

Scenario: Using the bare `rspec` command
When I run `rspec`
Then the output should contain "1 example, 1 failure"
And the output should not contain "activesupport"

Scenario: Using `rspec --backtrace`
When I run `rspec --backtrace`
Then the output should contain "1 example, 1 failure"
And the output should contain "activesupport"
5 changes: 5 additions & 0 deletions lib/generators/rspec/install/templates/spec/rails_helper.rb
Expand Up @@ -58,4 +58,9 @@
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
7 changes: 7 additions & 0 deletions lib/rspec/rails/configuration.rb
Expand Up @@ -89,6 +89,13 @@ def infer_spec_type_from_file_location!
end
end
end

# Adds exclusion filters for gems included with Rails
def filter_rails_from_backtrace!
filter_gems_from_backtrace "actionmailer", "actionpack" ,"actionview"
filter_gems_from_backtrace "activemodel", "activerecord",
"activesupport", "activejob"
end
end

config.include RSpec::Rails::ControllerExampleGroup, :type => :controller
Expand Down
10 changes: 10 additions & 0 deletions spec/generators/rspec/install/install_generator_spec.rb
Expand Up @@ -31,6 +31,10 @@ def use_transactional_fixtures
match(/config\.use_transactional_fixtures/m)
end

def filter_rails_from_backtrace
match(/config\.filter_rails_from_backtrace!/m)
end

setup_default_destination

let(:rails_helper) { content_for('spec/rails_helper.rb') }
Expand Down Expand Up @@ -69,6 +73,12 @@ def use_transactional_fixtures
expect(rails_helper).to use_transactional_fixtures
end

specify "excluding rails gems from the backtrace" do
run_generator

expect(rails_helper).to filter_rails_from_backtrace
end

if RSpec::Rails::FeatureCheck.can_maintain_test_schema?
specify "checking for maintaining the schema" do
run_generator
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/rails/configuration_spec.rb
Expand Up @@ -99,6 +99,24 @@
end
end

specify "#filter_rails_from_backtrace! adds exclusion patterns for rails gems" do
config.filter_rails_from_backtrace!

gems = %w(
actionmailer
actionpack
actionview
activemodel
activerecord
activesupport
activejob
)
exclusions = config.backtrace_exclusion_patterns.map(&:to_s)
aggregate_failures do
gems.each { |gem| expect(exclusions).to include(%r(#{gem})) }
end
end

describe "#infer_spec_type_from_file_location!" do
def in_inferring_type_from_location_environment
in_sub_process do
Expand Down

0 comments on commit 570911a

Please sign in to comment.