Skip to content

Commit 040fecc

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 040fecc

7 files changed

+113
-20
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Enhancements:
1616
(Andrew W. Lee, #2372)
1717
* Improve request spec "controller" scafold when no action is specified.
1818
(Thomas Hareau, #2399)
19+
* Introduce testing snippets concept (Phil Pirozhkov, Benoit Tigeot, #2423)
1920

2021
Bug Fixes:
2122

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

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+
)

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)