Skip to content

Commit

Permalink
fix: handle backward compatability
Browse files Browse the repository at this point in the history
Signed-off-by: Khaled Emara <[email protected]>
  • Loading branch information
KhaledEmaraDev committed May 31, 2023
1 parent 1e84f67 commit 00f86fa
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
15 changes: 9 additions & 6 deletions lib/cypress_on_rails/command_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CypressOnRails
# loads and evals the command files
class CommandExecutor
def self.perform(file,command_options = nil)
load_cypress_helper
load_e2e_helper
file_data = File.read(file)
eval file_data, binding, file
rescue => e
Expand All @@ -13,12 +13,15 @@ def self.perform(file,command_options = nil)
raise e
end

def self.load_cypress_helper
cypress_helper_file = "#{configuration.install_folder}/cypress_helper"
if File.exist?("#{cypress_helper_file}.rb")
Kernel.require cypress_helper_file
def self.load_e2e_helper
e2e_helper_file = "#{configuration.install_folder}/e2e_helper.rb"
if File.exist?(e2e_helper_file)
Kernel.require e2e_helper_file
elsif File.exist?("#{configuration.cypress_folder}/cypress_helper.rb")
Kernel.require e2e_helper_file
warn "cypress_helper.rb and cypress_folder are deprecated, please use the install generator to create e2e_helper.rb using install_folder"
else
logger.warn "could not find #{cypress_helper_file}.rb"
logger.warn "could not find #{e2e_helper_file}"
end
end

Expand Down
5 changes: 4 additions & 1 deletion lib/cypress_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module CypressOnRails
class Configuration
attr_accessor :api_prefix
attr_accessor :install_folder
attr_accessor :cypress_folder
attr_accessor :use_middleware
attr_accessor :use_vcr_middleware
attr_accessor :logger
Expand All @@ -17,7 +18,9 @@ def initialize

def reset
self.api_prefix = ''
self.install_folder = 'spec/e2e'
self.install_folder = nil
self.cypress_folder = 'spec/cypress'
warn "cypress_folder is deprecated, please use the install generator to use the new folder structure"
self.use_middleware = true
self.use_vcr_middleware = false
self.logger = Logger.new(STDOUT)
Expand Down
12 changes: 10 additions & 2 deletions lib/cypress_on_rails/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ def call(env)
request = Rack::Request.new(env)
if request.path.start_with?("#{configuration.api_prefix}/__e2e__/command")
configuration.tagged_logged { handle_command(request) }
elsif request.path.start_with?("#{configuration.api_prefix}/__cypress__/command")
configuration.tagged_logged { handle_command(request) }
warn "/__cypress__/command is deprecated. Please use the install generator to use /__e2e__/command instead."
else
@app.call(env)
end
end

private

Command = Struct.new(:name, :options, :cypress_folder) do
Command = Struct.new(:name, :options, :install_folder) do
# @return [Array<Cypress::Middleware::Command>]
def self.from_body(body, configuration)
if body.is_a?(Array)
Expand All @@ -34,7 +37,12 @@ def self.from_body(body, configuration)
command_params = [body]
end
command_params.map do |params|
new(params.fetch('name'), params['options'], configuration.install_folder)
if defined?(configuration.install_folder)
new(params.fetch('name'), params['options'], configuration.install_folder)
else
new(params.fetch('name'), params['options'], configuration.cypress_folder)
warn "cypress_folder is deprecated. Please use the install generator to use install_folder instead."
end
end
end

Expand Down
37 changes: 17 additions & 20 deletions lib/generators/cypress_on_rails/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,37 @@ def install_framework
directories = options.install_folder.split('/')
directories.pop
install_dir = "#{Dir.pwd}/#{directories.join('/')}"

command = nil
packages = []

packages << 'cypress' if options.install_cypress
packages << 'playwright' if options.install_playwright

if options.install_with == 'yarn'
command = "yarn --cwd=#{install_dir} add #{packages.join(' ')} --dev"
elsif options.install_with == 'npm'
command = "cd #{install_dir}; npm install #{packages.join(' ')} --save-dev"
end
if command
say command
fail 'failed to install cypress' unless system(command)
end

if options.install_cypress
if options.install_with == 'yarn'
command = "yarn --cwd=#{install_dir} add cypress --dev"
elsif options.install_with == 'npm'
command = "cd #{install_dir}; npm install cypress --save-dev"
end
if command
say command
fail 'failed to install cypress' unless system(command)
end
template "spec/cypress/support/index.js.erb", "#{options.cypress_folder}/support/index.js"
copy_file "spec/cypress/support/commands.js", "#{options.cypress_folder}/support/commands.js"
copy_file "spec/cypress.config.js", "#{options.cypress_folder}/../cypress.config.js"
end
command = nil
if options.install_playwright
if options.install_with == 'yarn'
command = "yarn --cwd=#{install_dir} add playwright --dev"
elsif options.install_with == 'npm'
command = "cd #{install_dir}; npm install playwright --save-dev"
end
if command
say command
fail 'failed to install playwright' unless system(command)
end
template "spec/playwright/support/index.js.erb", "#{options.playwright_folder}/support/index.js"
copy_file "spec/playwright.config.js", "#{options.playwright_folder}/../playwright.config.js"
end
end

def add_initial_files
template "config/initializers/cypress_on_rails.rb.erb", "config/initializers/cypress_on_rails.rb"
template "spec/e2e/cypress_helper.rb.erb", "#{options.install_folder}/cypress_helper.rb"
template "spec/e2e/e2e_helper.rb.erb", "#{options.install_folder}/e2e_helper.rb"
directory 'spec/e2e/app_commands', "#{options.install_folder}/app_commands"
if options.install_cypress
copy_file "spec/cypress/support/on-rails.js", "#{options.cypress_folder}/support/on-rails.js"
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/cypress_on_rails/update_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class UpdateGenerator < Rails::Generators::Base

def update_generated_files
template "config/initializers/cypress_on_rails.rb.erb", "config/initializers/cypress_on_rails.rb"
template "spec/e2e/cypress_helper.rb.erb", "#{options.install_folder}/cypress_helper.rb"
template "spec/e2e/e2e_helper.rb.erb", "#{options.install_folder}/e2e_helper.rb"
directory 'spec/e2e/app_commands', "#{options.install_folder}/app_commands"

if options.install_cypress
Expand Down

0 comments on commit 00f86fa

Please sign in to comment.