Skip to content

Commit 3afdba5

Browse files
authored
Bugfix/fix factorybot ci issue (#583)
* Add new constraints for several gemfiles * Update readme * Ensure generated app has lower factory bot * Split helpers into 2 files * Update helper doc * Add changelog
1 parent c955b55 commit 3afdba5

File tree

10 files changed

+118
-91
lines changed

10 files changed

+118
-91
lines changed

Appraisals

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,43 @@ appraise 'rails_5_2' do
44
gem 'activerecord'
55
gem 'capybara', '< 3.33'
66
gem 'cucumber', '< 6'
7+
gem 'factory_bot', '< 6.4'
78
gem 'psych', '< 4'
89
gem 'rails-html-sanitizer', '< 1.4.3'
9-
gem 'railties', '~> 5.2.4'
10+
gem 'railties', '~> 5.2.8'
1011
gem 'sqlite3', '~> 1.3.13'
1112
end
1213

1314
appraise 'rails_6_0' do
1415
gem 'activerecord'
1516
gem 'capybara', '< 3.35'
1617
gem 'cucumber', '< 6'
17-
gem 'matrix' # Until capybara 3.36+
18+
gem 'factory_bot', '< 6.4'
19+
gem 'matrix'
1820
gem 'psych', '< 4'
1921
gem 'rails-html-sanitizer', '< 1.4.3'
20-
gem 'railties', '~> 6.0.3'
21-
gem 'sqlite3', '~> 1.4'
22+
gem 'railties', '~> 6.0.6'
23+
gem 'sqlite3', '< 1.6'
2224
end
2325

2426
appraise 'rails_6_1' do
2527
gem 'activerecord'
2628
gem 'capybara', '< 3.38'
29+
gem 'factory_bot', '< 6.4'
2730
gem 'psych', '< 4'
28-
gem 'railties', '~> 6.1.3'
31+
gem 'railties', '~> 6.1.7'
2932
gem 'sqlite3', '~> 1.4'
3033
end
3134

3235
appraise 'rails_7_0' do
3336
gem 'activerecord'
3437
gem 'cucumber', '< 10'
35-
gem 'railties', '~> 7.0.0'
36-
gem 'sqlite3', '~> 1.4'
38+
gem 'railties', '~> 7.0.8'
39+
gem 'sqlite3', '~> 1.7'
3740
end
3841

3942
appraise 'rails_7_1' do
4043
gem 'activerecord'
41-
gem 'railties', '~> 7.1.0'
42-
gem 'sqlite3', '~> 1.4'
44+
gem 'railties', '~> 7.1.3'
45+
gem 'sqlite3', '~> 1.7'
4346
end

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
This file is intended to be modified using the [`changelog`](github.com/cucumber/changelog) command-line tool.
99

1010
## [Unreleased]
11+
### Changed
12+
- Internal testing code has been refactored to handle older ruby/rails installs
1113

1214
## [3.0.0] - 2023-11-01
1315
### Changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ With all dependencies installed, all specs and features should pass:
9595

9696
In order to test against multiple versions of key dependencies, the [Appraisal](https://github.com/thoughtbot/appraisal)
9797
gem is used to generate multiple gemfiles, stored in the `gemfiles/` directory.
98-
Normally these will only run on Travis; however, if you want to run the full test suite against
99-
all gemfiles, run the following commands:
98+
Normally these will only run on GitHub via GitHub Actions; however if you want to run the full test
99+
suite against all gemfiles, run the following commands:
100100

101101
[bundle exec] appraisal install
102102
[bundle exec] appraisal rake test
@@ -115,4 +115,4 @@ To support the multiple-gemfile testing, when adding a new dependency the follow
115115

116116
For example, rspec is a primary development dependency, so it lives in the gemspec.
117117

118-
[the helper method]: https://github.com/cucumber/cucumber-rails/blob/main/features/support/cucumber_rails_helper.rb#L19
118+
[the helper method]: ./features/support/cucumber_rails_gem_helper.rb

features/support/cucumber_rails_helper.rb renamed to features/support/cucumber_rails_gem_helper.rb

Lines changed: 38 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@
44
require 'cucumber'
55
require 'capybara'
66

7-
module CucumberRailsHelper
8-
def rails_new(options = {})
9-
# This expectation allows us to wait until the command line monitor has output a README file (i.e. the command has completed)
10-
expect(run_rails_new_command(options)).to have_output(/README/)
11-
12-
cd 'test_app'
13-
configure_rails_gems
14-
configure_rails_requires
15-
configure_rails_layout
16-
clear_bundle_env_vars
17-
end
18-
7+
module CucumberRailsGemHelper
198
def install_cucumber_rails(*options)
9+
configure_rails_gems
2010
add_cucumber_rails(options)
2111
add_rails_conditional_gems
2212
add_remaining_gems(options)
@@ -26,68 +16,11 @@ def install_cucumber_rails(*options)
2616

2717
private
2818

29-
def run_rails_new_command(options)
30-
flags = %w[--skip-action-cable --skip-action-mailer --skip-active-job --skip-bootsnap --skip-bundle --skip-javascript
31-
--skip-jbuilder --skip-listen --skip-spring --skip-sprockets --skip-test-unit --skip-turbolinks --skip-active-storage]
32-
flags += %w[--skip-action-mailbox --skip-action-text] if rails_equal_or_higher_than?('6.0')
33-
run_command "bundle exec rails new test_app #{flags.join(' ')} #{options[:args]}"
34-
end
35-
3619
def configure_rails_gems
3720
%w[bootsnap byebug jbuilder listen rails sass-rails turbolinks webpacker].each { |gem| remove_gem(gem) }
3821
%w[railties activerecord actionpack].each { |rails_gem| add_gem(rails_gem, Rails.version) }
3922
end
4023

41-
def configure_rails_requires
42-
content = File.read(expand_path('config/application.rb'))
43-
%w[active_job/railtie active_storage/engine action_mailer/railtie action_mailbox/engine
44-
action_text/engine action_cable/engine rails/test_unit/railtie sprockets/railtie].each do |require|
45-
content = content.gsub(/^.*require ["']#{require}["']\s*$/, '')
46-
end
47-
overwrite_file('config/application.rb', content)
48-
end
49-
50-
def configure_rails_layout
51-
file = 'app/views/layouts/application.html.erb'
52-
content = File.read(expand_path(file)).gsub(/^\s*<%= stylesheet_link_tag .*%>\s*$/, '')
53-
overwrite_file(file, content)
54-
end
55-
56-
def clear_bundle_env_vars
57-
unset_bundler_env_vars
58-
delete_environment_variable 'BUNDLE_GEMFILE'
59-
end
60-
61-
def rails_equal_or_higher_than?(version)
62-
Rails.gem_version >= Gem::Version.new(version)
63-
end
64-
65-
def remove_gem(name)
66-
content = File.read(expand_path('Gemfile')).gsub(/^\s*gem ["']#{name}["'].*$/, '')
67-
overwrite_file('Gemfile', content)
68-
end
69-
70-
def add_gem(name, *args)
71-
line = convert_gem_opts_to_string(name, *args)
72-
gem_regexp = /gem ["']#{name}["'].*$/
73-
gemfile_content = File.read(expand_path('Gemfile'))
74-
75-
if gemfile_content.match?(gem_regexp)
76-
updated_gemfile_content = gemfile_content.gsub(gem_regexp, line)
77-
overwrite_file('Gemfile', updated_gemfile_content)
78-
else
79-
append_to_file('Gemfile', line)
80-
end
81-
end
82-
83-
def convert_gem_opts_to_string(name, *args)
84-
options = args.last.is_a?(Hash) ? args.pop : {}
85-
parts = ["'#{name}'"]
86-
parts << args.map(&:inspect) if args.any?
87-
parts << options.inspect[1..-2] if options.any?
88-
"gem #{parts.flatten.join(', ')}\n"
89-
end
90-
9124
def add_cucumber_rails(options)
9225
if options.include?(:not_in_test_group)
9326
add_gem 'cucumber-rails', path: File.expand_path('.').to_s
@@ -114,7 +47,11 @@ def add_remaining_gems(options)
11447
add_gem 'capybara', Capybara::VERSION, group: :test
11548
add_gem 'database_cleaner', '>= 2.0.0', group: :test unless options.include?(:no_database_cleaner)
11649
add_gem 'database_cleaner-active_record', '>= 2.0.0', group: :test if options.include?(:database_cleaner_active_record)
117-
add_gem 'factory_bot', '>= 5.0', group: :test unless options.include?(:no_factory_bot)
50+
if rails_equal_or_higher_than?('6.0')
51+
add_gem 'factory_bot', '>= 6.4', group: :test unless options.include?(:no_factory_bot)
52+
else
53+
add_gem 'factory_bot', '< 6.4', group: :test unless options.include?(:no_factory_bot)
54+
end
11855
add_gem 'rspec-expectations', '~> 3.12', group: :test
11956
end
12057

@@ -123,6 +60,36 @@ def bundle_install
12360
run_command_and_stop "bundle config set --local path '#{ENV.fetch('GITHUB_WORKSPACE')}/vendor/bundle'" if ENV.key?('GITHUB_WORKSPACE')
12461
run_command_and_stop 'bundle install --jobs 4'
12562
end
63+
64+
def convert_gem_opts_to_string(name, *args)
65+
options = args.last.is_a?(Hash) ? args.pop : {}
66+
parts = ["'#{name}'"]
67+
parts << args.map(&:inspect) if args.any?
68+
parts << options.inspect[1..-2] if options.any?
69+
"gem #{parts.flatten.join(', ')}\n"
70+
end
71+
72+
def remove_gem(name)
73+
content = File.read(expand_path('Gemfile')).gsub(/^\s*gem ["']#{name}["'].*$/, '')
74+
overwrite_file('Gemfile', content)
75+
end
76+
77+
def add_gem(name, *args)
78+
line = convert_gem_opts_to_string(name, *args)
79+
gem_regexp = /gem ["']#{name}["'].*$/
80+
gemfile_content = File.read(expand_path('Gemfile'))
81+
82+
if gemfile_content.match?(gem_regexp)
83+
updated_gemfile_content = gemfile_content.gsub(gem_regexp, line)
84+
overwrite_file('Gemfile', updated_gemfile_content)
85+
else
86+
append_to_file('Gemfile', line)
87+
end
88+
end
89+
90+
def rails_equal_or_higher_than?(version)
91+
Rails.gem_version >= Gem::Version.new(version)
92+
end
12693
end
12794

128-
World(CucumberRailsHelper)
95+
World(CucumberRailsGemHelper)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails'
4+
require 'cucumber'
5+
require 'capybara'
6+
7+
module CucumberRailsSetupHelper
8+
def rails_new(options = {})
9+
# This expectation allows us to wait until the command line monitor has output a README file (i.e. the command has completed)
10+
expect(run_rails_new_command(options)).to have_output(/README/)
11+
12+
cd 'test_app'
13+
configure_rails_requires
14+
configure_rails_layout
15+
clear_bundle_env_vars
16+
end
17+
18+
private
19+
20+
def run_rails_new_command(options)
21+
flags = %w[--skip-action-cable --skip-action-mailer --skip-active-job --skip-bootsnap --skip-bundle --skip-javascript
22+
--skip-jbuilder --skip-listen --skip-spring --skip-sprockets --skip-test-unit --skip-turbolinks --skip-active-storage]
23+
flags += %w[--skip-action-mailbox --skip-action-text] if rails_equal_or_higher_than?('6.0')
24+
run_command "bundle exec rails new test_app #{flags.join(' ')} #{options[:args]}"
25+
end
26+
27+
def configure_rails_requires
28+
content = File.read(expand_path('config/application.rb'))
29+
%w[active_job/railtie active_storage/engine action_mailer/railtie action_mailbox/engine
30+
action_text/engine action_cable/engine rails/test_unit/railtie sprockets/railtie].each do |require|
31+
content = content.gsub(/^.*require ["']#{require}["']\s*$/, '')
32+
end
33+
overwrite_file('config/application.rb', content)
34+
end
35+
36+
def configure_rails_layout
37+
file = 'app/views/layouts/application.html.erb'
38+
content = File.read(expand_path(file)).gsub(/^\s*<%= stylesheet_link_tag .*%>\s*$/, '')
39+
overwrite_file(file, content)
40+
end
41+
42+
def clear_bundle_env_vars
43+
unset_bundler_env_vars
44+
delete_environment_variable 'BUNDLE_GEMFILE'
45+
end
46+
47+
def rails_equal_or_higher_than?(version)
48+
Rails.gem_version >= Gem::Version.new(version)
49+
end
50+
end
51+
52+
World(CucumberRailsSetupHelper)

gemfiles/rails_5_2.gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ source "https://rubygems.org"
55
gem "activerecord"
66
gem "capybara", "< 3.33"
77
gem "cucumber", "< 6"
8+
gem "factory_bot", "< 6.4"
89
gem "psych", "< 4"
910
gem "rails-html-sanitizer", "< 1.4.3"
10-
gem "railties", "~> 5.2.4"
11+
gem "railties", "~> 5.2.8"
1112
gem "sqlite3", "~> 1.3.13"
1213

1314
gemspec path: "../"

gemfiles/rails_6_0.gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ source "https://rubygems.org"
55
gem "activerecord"
66
gem "capybara", "< 3.35"
77
gem "cucumber", "< 6"
8+
gem "factory_bot", "< 6.4"
89
gem "matrix"
910
gem "psych", "< 4"
1011
gem "rails-html-sanitizer", "< 1.4.3"
11-
gem "railties", "~> 6.0.3"
12-
gem "sqlite3", "~> 1.4"
12+
gem "railties", "~> 6.0.6"
13+
gem "sqlite3", "< 1.6"
1314

1415
gemspec path: "../"

gemfiles/rails_6_1.gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ source "https://rubygems.org"
44

55
gem "activerecord"
66
gem "capybara", "< 3.38"
7+
gem "factory_bot", "< 6.4"
78
gem "psych", "< 4"
8-
gem "railties", "~> 6.1.3"
9+
gem "railties", "~> 6.1.7"
910
gem "sqlite3", "~> 1.4"
1011

1112
gemspec path: "../"

gemfiles/rails_7_0.gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ source "https://rubygems.org"
44

55
gem "activerecord"
66
gem "cucumber", "< 10"
7-
gem "railties", "~> 7.0.0"
8-
gem "sqlite3", "~> 1.4"
7+
gem "railties", "~> 7.0.8"
8+
gem "sqlite3", "~> 1.7"
99

1010
gemspec path: "../"

gemfiles/rails_7_1.gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
source "https://rubygems.org"
44

55
gem "activerecord"
6-
gem "railties", "~> 7.1.0"
7-
gem "sqlite3", "~> 1.4"
6+
gem "railties", "~> 7.1.3"
7+
gem "sqlite3", "~> 1.7"
88

99
gemspec path: "../"

0 commit comments

Comments
 (0)