Skip to content

Commit

Permalink
Merge pull request #159 from Home-Chef-Tech/add-cypress-dir-environme…
Browse files Browse the repository at this point in the history
…nt-variable

Add configurability for external Cypress project through additional environment variable
  • Loading branch information
searls authored Feb 13, 2024
2 parents bb1bf9b + ae2a607 commit 6090721
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ preferred environment variables project-wide using a tool like
[dotenv](https://github.com/bkeepers/dotenv).


* **CYPRESS_RAILS_DIR** (default: `Dir.pwd`) the directory of your project
* **CYPRESS_RAILS_DIR** (default: `Dir.pwd`) the directory of your Rails project
* **CYPRESS_RAILS_CYPRESS_DIR** (default: _same value as `rails_dir`_) the directory of your Cypress project
* **CYPRESS_RAILS_HOST** (default: `"127.0.0.1"`) the hostname to bind to
* **CYPRESS_RAILS_PORT** (default: _a random available port_) the port to run
the Rails test server on
Expand Down
2 changes: 1 addition & 1 deletion exe/cypress-rails
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ENV["RAILS_ENV"] ||= "test"
require "pathname"
require "cypress-rails"
require Pathname.new(CypressRails::Config.new.dir).join("config/environment")
require Pathname.new(CypressRails::Config.new.rails_dir).join("config/environment")

command = ARGV[0]
case command
Expand Down
11 changes: 7 additions & 4 deletions lib/cypress-rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

module CypressRails
class Config
attr_accessor :dir, :host, :port, :base_path, :transactional_server, :cypress_cli_opts
attr_accessor :rails_dir, :cypress_dir, :host, :port, :base_path, :transactional_server, :cypress_cli_opts

def initialize(
dir: Env.fetch("CYPRESS_RAILS_DIR", default: Dir.pwd),
rails_dir: Env.fetch("CYPRESS_RAILS_DIR", default: Dir.pwd),
cypress_dir: Env.fetch("CYPRESS_RAILS_CYPRESS_DIR", default: rails_dir),
host: Env.fetch("CYPRESS_RAILS_HOST", default: "127.0.0.1"),
port: Env.fetch("CYPRESS_RAILS_PORT"),
base_path: Env.fetch("CYPRESS_RAILS_BASE_PATH", default: "/"),
transactional_server: Env.fetch("CYPRESS_RAILS_TRANSACTIONAL_SERVER", type: :boolean, default: true),
cypress_cli_opts: Env.fetch("CYPRESS_RAILS_CYPRESS_OPTS", default: "")
)
@dir = dir
@rails_dir = rails_dir
@cypress_dir = cypress_dir
@host = host
@port = port
@base_path = base_path
Expand All @@ -25,7 +27,8 @@ def to_s
cypress-rails configuration:
============================
CYPRESS_RAILS_DIR.....................#{dir.inspect}
CYPRESS_RAILS_DIR.....................#{rails_dir.inspect}
CYPRESS_RAILS_CYPRESS_DIR.............#{cypress_dir.inspect}
CYPRESS_RAILS_HOST....................#{host.inspect}
CYPRESS_RAILS_PORT....................#{port.inspect}
CYPRESS_RAILS_BASE_PATH...............#{base_path.inspect}
Expand Down
4 changes: 2 additions & 2 deletions lib/cypress-rails/finds_bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module CypressRails
class FindsBin
LOCAL_PATH = "node_modules/.bin/cypress"

def call(dir = Dir.pwd)
local_path = Pathname.new(dir).join(LOCAL_PATH)
def call(cypress_dir = Dir.pwd)
local_path = Pathname.new(cypress_dir).join(LOCAL_PATH)
if File.exist?(local_path)
local_path
else
Expand Down
4 changes: 2 additions & 2 deletions lib/cypress-rails/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Init
})
JS

def call(dir = Dir.pwd)
config_path = File.join(dir, "cypress.config.js")
def call(cypress_dir = Config.new.cypress_dir)
config_path = File.join(cypress_dir, "cypress.config.js")
if !File.exist?(config_path)
File.write(config_path, DEFAULT_CONFIG)
puts "Cypress config initialized in `#{config_path}'"
Expand Down
4 changes: 2 additions & 2 deletions lib/cypress-rails/launches_cypress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def call(command, config)
port: config.port,
transactional_server: config.transactional_server
)
bin = @finds_bin.call(config.dir)
bin = @finds_bin.call(config.cypress_dir)

set_exit_hooks!(config)

command = <<~EXEC
CYPRESS_BASE_URL="http://#{server.host}:#{server.port}#{config.base_path}" "#{bin}" #{command} --project "#{config.dir}" #{config.cypress_cli_opts}
CYPRESS_BASE_URL="http://#{server.host}:#{server.port}#{config.base_path}" "#{bin}" #{command} --project "#{config.cypress_dir}" #{config.cypress_cli_opts}
EXEC

puts "\nLaunching Cypress…\n$ #{command}\n"
Expand Down
2 changes: 2 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ echo "---> Running tests"
bundle exec rake
./script/test_example_app

bundle exec rake test

echo "---> Job's done!"

44 changes: 44 additions & 0 deletions test/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative "test_helper"

class ConfigTest < Minitest::Test
def test_that_rails_dir_and_cypress_dir_use_default_directory
config = CypressRails::Config.new
expected_directory_path = Dir.pwd

assert_equal(expected_directory_path, config.rails_dir)
assert_equal(expected_directory_path, config.cypress_dir)
end

def test_that_rails_dir_and_cypress_dir_can_be_independently_set
mock_env(
"CYPRESS_RAILS_DIR" => "path/to/cypress-rails",
"CYPRESS_RAILS_CYPRESS_DIR" => "path/to/another/cypress/directory"
) do
config = CypressRails::Config.new

assert_equal("path/to/cypress-rails", config.rails_dir)
assert_equal("path/to/another/cypress/directory", config.cypress_dir)
end
end

def test_that_cypress_dir_uses_same_directory_as_rails_dir_when_not_set
mock_env("CYPRESS_RAILS_DIR" => "path/to/cypress-rails") do
config = CypressRails::Config.new

assert_nil(ENV["CYPRESS_RAILS_CYPRESS_DIR"])
assert_equal("path/to/cypress-rails", config.cypress_dir)
end
end

private

def mock_env(partial_env_hash)
old = ENV.to_hash
ENV.update partial_env_hash
begin
yield
ensure
ENV.replace old
end
end
end

0 comments on commit 6090721

Please sign in to comment.