Skip to content

Commit 55c06fc

Browse files
pirjbenoittgt
andcommitted
Add testing snippets
Snippet is a self-contained example that defines the configuration, Rails project code and specs to run. Snippets: - allow for clean separation between different snippets - work quite fast - reuse the already installed gems that specs and Cukes use - do not litter - do not depend on other parts of the build In theory snippets retain the ability to use generators and arbitrary commands, but it makes the case under test less evident. Co-authored-by: Benoit Tigeot <[email protected]>
1 parent 4e88eb3 commit 55c06fc

7 files changed

+118
-21
lines changed

BUILD_DETAIL.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ $ 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+
$ cd snippets
82+
$ find . -name '*.rb' | xargs -I_ -L1 sh -c "echo Running _ && ruby _"
83+
```
84+
6985
## YARD documentation
7086

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

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 /master/, /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

Rakefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,18 @@ end
224224

225225
task acceptance: ['smoke:app', 'no_active_record:smoke:app', :cucumber]
226226

227-
task default: [:spec, :acceptance]
227+
desc "Run short, self-contained snippets"
228+
task :snippets do
229+
sh "echo Running snippets"
230+
sh <<-SH
231+
(
232+
cd snippets
233+
ruby use_active_record_false.rb
234+
)
235+
SH
236+
end
237+
238+
task default: [:spec, :acceptance, :snippets]
228239

229240
task :verify_private_key_present do
230241
private_key = File.expand_path('~/.gem/rspec-gem-private_key.pem')

script/custom_build_functions.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ function run_cukes {
1818
function run_all_spec_suites {
1919
fold "one-by-one specs" run_specs_one_by_one
2020
}
21+
22+
function run_snippets {
23+
echo "Running snippets"
24+
(
25+
cd snippets
26+
\find . -name '*.rb' | xargs -I_ -L1 sh -c "echo Running _ && ruby _"
27+
)
28+
}

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" run_snippets
24+
2325
if documentation_enforced; then
2426
fold "doc check" check_documentation_coverage
2527
fi

snippets/use_active_record_false.rb

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

0 commit comments

Comments
 (0)