11require 'socket'
22require 'timeout'
3+ require 'fileutils'
34require 'cypress_on_rails/configuration'
45
56module CypressOnRails
@@ -25,7 +26,7 @@ def open
2526
2627 def run
2728 start_server do
28- result = run_command ( run_command_str , "Running #{ framework } tests" )
29+ result = run_command ( run_command_args , "Running #{ framework } tests" )
2930 exit ( result ? 0 : 1 )
3031 end
3132 end
@@ -40,7 +41,7 @@ def init
4041 def detect_install_folder
4142 # Check common locations for cypress/playwright installation
4243 possible_folders = [ 'e2e' , 'spec/e2e' , 'spec/cypress' , 'spec/playwright' , 'cypress' , 'playwright' ]
43- folder = possible_folders . find { |f | File . exist? ( f ) }
44+ folder = possible_folders . find { |f | File . exist? ( File . expand_path ( f ) ) }
4445 folder || 'e2e'
4546 end
4647
@@ -94,17 +95,17 @@ def start_server(&block)
9495 end
9596
9697 def spawn_server
97- rails_command = if File . exist? ( 'bin/rails' )
98- 'bin/rails'
98+ rails_args = if File . exist? ( 'bin/rails' )
99+ [ 'bin/rails' ]
99100 else
100- 'bundle exec rails'
101+ [ 'bundle' , ' exec' , ' rails']
101102 end
102103
103- server_command = " #{ rails_command } server -p #{ port } -b #{ host } "
104+ server_args = rails_args + [ ' server' , '-p' , port . to_s , '-b' , host ]
104105
105- puts "Starting Rails server: #{ server_command } "
106+ puts "Starting Rails server: #{ server_args . join ( ' ' ) } "
106107
107- spawn ( server_command , out : $stdout, err : $stderr)
108+ spawn ( * server_args , out : $stdout, err : $stderr)
108109 end
109110
110111 def wait_for_server ( timeout = 30 )
@@ -140,47 +141,47 @@ def open_command
140141 case framework
141142 when :cypress
142143 if command_exists? ( 'yarn' )
143- " yarn cypress open --project #{ install_folder } --config baseUrl=#{ base_url } "
144+ [ ' yarn' , ' cypress' , ' open' , ' --project' , install_folder , ' --config' , " baseUrl=#{ base_url } "]
144145 elsif command_exists? ( 'npx' )
145- " npx cypress open --project #{ install_folder } --config baseUrl=#{ base_url } "
146+ [ ' npx' , ' cypress' , ' open' , ' --project' , install_folder , ' --config' , " baseUrl=#{ base_url } "]
146147 else
147- " cypress open --project #{ install_folder } --config baseUrl=#{ base_url } "
148+ [ ' cypress' , ' open' , ' --project' , install_folder , ' --config' , " baseUrl=#{ base_url } "]
148149 end
149150 when :playwright
150151 if command_exists? ( 'yarn' )
151- " yarn playwright test --ui"
152+ [ ' yarn' , ' playwright' , ' test' , ' --ui' ]
152153 elsif command_exists? ( 'npx' )
153- " npx playwright test --ui"
154+ [ ' npx' , ' playwright' , ' test' , ' --ui' ]
154155 else
155- " playwright test --ui"
156+ [ ' playwright' , ' test' , ' --ui' ]
156157 end
157158 end
158159 end
159160
160- def run_command_str
161+ def run_command_args
161162 case framework
162163 when :cypress
163164 if command_exists? ( 'yarn' )
164- " yarn cypress run --project #{ install_folder } --config baseUrl=#{ base_url } "
165+ [ ' yarn' , ' cypress' , ' run' , ' --project' , install_folder , ' --config' , " baseUrl=#{ base_url } "]
165166 elsif command_exists? ( 'npx' )
166- " npx cypress run --project #{ install_folder } --config baseUrl=#{ base_url } "
167+ [ ' npx' , ' cypress' , ' run' , ' --project' , install_folder , ' --config' , " baseUrl=#{ base_url } "]
167168 else
168- " cypress run --project #{ install_folder } --config baseUrl=#{ base_url } "
169+ [ ' cypress' , ' run' , ' --project' , install_folder , ' --config' , " baseUrl=#{ base_url } "]
169170 end
170171 when :playwright
171172 if command_exists? ( 'yarn' )
172- " yarn playwright test"
173+ [ ' yarn' , ' playwright' , ' test' ]
173174 elsif command_exists? ( 'npx' )
174- " npx playwright test"
175+ [ ' npx' , ' playwright' , ' test' ]
175176 else
176- " playwright test"
177+ [ ' playwright' , ' test' ]
177178 end
178179 end
179180 end
180181
181- def run_command ( command , description )
182- puts "#{ description } : #{ command } "
183- system ( command )
182+ def run_command ( command_args , description )
183+ puts "#{ description } : #{ command_args . join ( ' ' ) } "
184+ system ( * command_args )
184185 end
185186
186187 def command_exists? ( command )
0 commit comments