4
4
require 'cucumber'
5
5
require 'capybara'
6
6
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
19
8
def install_cucumber_rails ( *options )
9
+ configure_rails_gems
20
10
add_cucumber_rails ( options )
21
11
add_rails_conditional_gems
22
12
add_remaining_gems ( options )
@@ -26,68 +16,11 @@ def install_cucumber_rails(*options)
26
16
27
17
private
28
18
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
-
36
19
def configure_rails_gems
37
20
%w[ bootsnap byebug jbuilder listen rails sass-rails turbolinks webpacker ] . each { |gem | remove_gem ( gem ) }
38
21
%w[ railties activerecord actionpack ] . each { |rails_gem | add_gem ( rails_gem , Rails . version ) }
39
22
end
40
23
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
-
91
24
def add_cucumber_rails ( options )
92
25
if options . include? ( :not_in_test_group )
93
26
add_gem 'cucumber-rails' , path : File . expand_path ( '.' ) . to_s
@@ -114,7 +47,11 @@ def add_remaining_gems(options)
114
47
add_gem 'capybara' , Capybara ::VERSION , group : :test
115
48
add_gem 'database_cleaner' , '>= 2.0.0' , group : :test unless options . include? ( :no_database_cleaner )
116
49
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
118
55
add_gem 'rspec-expectations' , '~> 3.12' , group : :test
119
56
end
120
57
@@ -123,6 +60,36 @@ def bundle_install
123
60
run_command_and_stop "bundle config set --local path '#{ ENV . fetch ( 'GITHUB_WORKSPACE' ) } /vendor/bundle'" if ENV . key? ( 'GITHUB_WORKSPACE' )
124
61
run_command_and_stop 'bundle install --jobs 4'
125
62
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
126
93
end
127
94
128
- World ( CucumberRailsHelper )
95
+ World ( CucumberRailsGemHelper )
0 commit comments