Skip to content

Commit 324e98e

Browse files
authored
Merge pull request #2467 from rspec/let-name-collision
Prevent collisions on let(:name) and let(:method_name)
2 parents a785198 + 073daad commit 324e98e

File tree

9 files changed

+179
-27
lines changed

9 files changed

+179
-27
lines changed

BUILD_DETAIL.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ $ bundle exec cucumber
6666
$ bin/cucumber
6767
```
6868

69+
## Snippets
70+
71+
RSpec Rails uses snippets, self-contained examples that are used to cover
72+
cases and regressions that don't need a full-blown example application to
73+
reproduce.
74+
75+
Snippets reuse the already installed gems, and don't attempt to install gem
76+
versions that are not on the system already to prevent version mismatches.
77+
78+
Run with:
79+
80+
```
81+
$ script/run_snippets.sh
82+
```
83+
6984
## YARD documentation
7085

7186
RSpec uses [YARD](https://yardoc.org/) for API documentation on the [rspec.info site](https://rspec.info/).

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Enhancements:
1111
(Andrew W. Lee, #2372)
1212
* Improve request spec "controller" scafold when no action is specified.
1313
(Thomas Hareau, #2399)
14+
* Introduce testing snippets concept (Phil Pirozhkov, Benoit Tigeot, #2423)
15+
* Prevent collisions with `let(:name)` for Rails 6.1 and `let(:method_name)` on older
16+
Rails. (Benoit Tigeot, #2461)
1417

1518
Bug Fixes:
1619

Gemfile

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
source "https://rubygems.org"
2-
version_file = File.expand_path('.rails-version', __dir__)
3-
RAILS_VERSION = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp) || ""
42

53
gemspec
64

@@ -22,28 +20,11 @@ end
2220

2321
gem 'capybara'
2422

25-
MAJOR =
26-
case RAILS_VERSION
27-
when /5-2-stable/
28-
5
29-
when /stable/, nil, false, ''
30-
6
31-
else
32-
/(\d+)[\.|-]\d+/.match(RAILS_VERSION).captures.first.to_i
33-
end
34-
35-
if MAJOR >= 6
36-
# sqlite3 is an optional, unspecified, dependency and Rails 6.0 only supports `~> 1.4`
37-
gem 'sqlite3', '~> 1.4', platforms: [:ruby]
38-
else
39-
# Similarly, Rails 5.0 only supports '~> 1.3.6'. Rails 5.1-5.2 support '~> 1.3', '>= 1.3.6'
40-
gem 'sqlite3', '~> 1.3.6', platforms: [:ruby]
41-
end
42-
4323
# Until 1.13.2 is released due to Rubygems usage
4424
gem 'ffi', '~> 1.12.0'
4525

4626
custom_gemfile = File.expand_path('Gemfile-custom', __dir__)
4727
eval_gemfile custom_gemfile if File.exist?(custom_gemfile)
4828

29+
eval_gemfile 'Gemfile-sqlite-dependencies'
4930
eval_gemfile 'Gemfile-rails-dependencies'

Gemfile-sqlite-dependencies

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version_file = File.expand_path('.rails-version', __dir__)
2+
RAILS_VERSION = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp) || ""
3+
4+
MAJOR =
5+
case RAILS_VERSION
6+
when /5-2-stable/
7+
5
8+
when /master/, /stable/, nil, false, ''
9+
6
10+
else
11+
/(\d+)[\.|-]\d+/.match(RAILS_VERSION).captures.first.to_i
12+
end
13+
14+
if MAJOR >= 6
15+
# sqlite3 is an optional, unspecified, dependency and Rails 6.0 only supports `~> 1.4`
16+
gem 'sqlite3', '~> 1.4', platforms: [:ruby]
17+
else
18+
# Similarly, Rails 5.0 only supports '~> 1.3.6'. Rails 5.1-5.2 support '~> 1.3', '>= 1.3.6'
19+
gem 'sqlite3', '~> 1.3.6', platforms: [:ruby]
20+
end

lib/rspec/rails/fixture_support.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ module FixtureSupport
99
include RSpec::Rails::MinitestAssertionAdapter
1010
include ActiveRecord::TestFixtures
1111

12+
# @private prevent ActiveSupport::TestFixtures to start a DB transaction.
13+
# Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after
14+
# and let(:method_name) before Rails 6.1.
15+
def run_in_transaction?
16+
use_transactional_tests && !self.class.uses_transaction?(self)
17+
end
18+
1219
included do
1320
if RSpec.configuration.use_active_record?
1421
include Fixtures
@@ -50,13 +57,6 @@ def proxy_method_warning_if_called_in_before_context_scope(method_name)
5057
end
5158
end
5259
end
53-
54-
if ::Rails.version.to_f >= 6.1
55-
# @private return the example name for TestFixtures
56-
def name
57-
@example
58-
end
59-
end
6060
end
6161
end
6262
end

script/run_build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ fi
2020

2121
fold "cukes" run_cukes
2222

23+
fold "snippets" script/run_snippets.sh
24+
2325
if documentation_enforced; then
2426
fold "doc check" check_documentation_coverage
2527
fi

script/run_snippets.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
(
5+
cd snippets
6+
# This is required to load `bundle/inline`
7+
unset RUBYOPT
8+
for snippet in *.rb;
9+
do
10+
echo Running $snippet
11+
ruby $snippet
12+
done
13+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
if __FILE__ =~ /^snippets/
2+
fail "Snippets are supposed to be run from their own directory to avoid side " \
3+
"effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \
4+
"loaded by the root `.rspec`."
5+
end
6+
7+
# We opt-out from using RubyGems, but `bundler/inline` requires it
8+
require 'rubygems'
9+
10+
require "bundler/inline"
11+
12+
# We pass `false` to `gemfile` to skip the installation of gems,
13+
# because it may install versions that would conflict with versions
14+
# from the main `Gemfile.lock`.
15+
gemfile(false) do
16+
source "https://rubygems.org"
17+
18+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
19+
20+
# Those Gemfiles carefully pick the right versions depending on
21+
# settings in the ENV, `.rails-version` and `maintenance-branch`.
22+
Dir.chdir('..') do
23+
eval_gemfile 'Gemfile-sqlite-dependencies'
24+
# This Gemfile expects `maintenance-branch` file to be present
25+
# in the current directory.
26+
eval_gemfile 'Gemfile-rspec-dependencies'
27+
# This Gemfile expects `.rails-version` file
28+
eval_gemfile 'Gemfile-rails-dependencies'
29+
end
30+
31+
gem "rspec-rails", path: "../"
32+
end
33+
34+
# Run specs at exit
35+
require "rspec/autorun"
36+
37+
require "rails"
38+
require "active_record/railtie"
39+
require "rspec/rails"
40+
41+
# This connection will do for database-independent bug reports
42+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
43+
44+
RSpec.configure do |config|
45+
config.use_transactional_fixtures = true
46+
end
47+
48+
RSpec.describe 'Foo' do
49+
subject { true }
50+
51+
# Rails 6.1 and after
52+
let(:name) { raise "Should never raise" }
53+
# Before Rails 6.1
54+
let(:method_name) { raise "Should never raise" }
55+
56+
it { is_expected.to be_truthy }
57+
end

snippets/use_active_record_false.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
if __FILE__ =~ /^snippets/
2+
fail "Snippets are supposed to be run from their own directory to avoid side " \
3+
"effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \
4+
"loaded by the root `.rspec`."
5+
end
6+
7+
# We opt-out from using RubyGems, but `bundler/inline` requires it
8+
require 'rubygems'
9+
10+
require "bundler/inline"
11+
12+
# We pass `false` to `gemfile` to skip the installation of gems,
13+
# because it may install versions that would conflict with versions
14+
# from the main `Gemfile.lock`.
15+
gemfile(false) do
16+
source "https://rubygems.org"
17+
18+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
19+
20+
# Those Gemfiles carefully pick the right versions depending on
21+
# settings in the ENV, `.rails-version` and `maintenance-branch`.
22+
Dir.chdir('..') do
23+
eval_gemfile 'Gemfile-sqlite-dependencies'
24+
# This Gemfile expects `maintenance-branch` file to be present
25+
# in the current directory.
26+
eval_gemfile 'Gemfile-rspec-dependencies'
27+
# This Gemfile expects `.rails-version` file
28+
eval_gemfile 'Gemfile-rails-dependencies'
29+
end
30+
31+
gem "rspec-rails", path: "../"
32+
end
33+
34+
# Run specs at exit
35+
require "rspec/autorun"
36+
37+
# This snippet describes the case when ActiveRecord is loaded, but
38+
# `use_active_record` is set to `false` in RSpec configuration.
39+
40+
# Initialization
41+
require "active_record/railtie"
42+
require "rspec/rails"
43+
44+
# This connection will do for database-independent bug reports
45+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
46+
47+
# RSpec configuration
48+
RSpec.configure do |config|
49+
config.use_active_record = false
50+
end
51+
52+
# Rails project code
53+
class Foo
54+
end
55+
56+
# Rails project specs
57+
RSpec.describe Foo do
58+
it 'does not not break' do
59+
Foo
60+
end
61+
end

0 commit comments

Comments
 (0)