Skip to content

Commit 6090721

Browse files
authored
Merge pull request #159 from Home-Chef-Tech/add-cypress-dir-environment-variable
Add configurability for external Cypress project through additional environment variable
2 parents bb1bf9b + ae2a607 commit 6090721

File tree

8 files changed

+62
-12
lines changed

8 files changed

+62
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ preferred environment variables project-wide using a tool like
155155
[dotenv](https://github.com/bkeepers/dotenv).
156156

157157

158-
* **CYPRESS_RAILS_DIR** (default: `Dir.pwd`) the directory of your project
158+
* **CYPRESS_RAILS_DIR** (default: `Dir.pwd`) the directory of your Rails project
159+
* **CYPRESS_RAILS_CYPRESS_DIR** (default: _same value as `rails_dir`_) the directory of your Cypress project
159160
* **CYPRESS_RAILS_HOST** (default: `"127.0.0.1"`) the hostname to bind to
160161
* **CYPRESS_RAILS_PORT** (default: _a random available port_) the port to run
161162
the Rails test server on

exe/cypress-rails

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
ENV["RAILS_ENV"] ||= "test"
44
require "pathname"
55
require "cypress-rails"
6-
require Pathname.new(CypressRails::Config.new.dir).join("config/environment")
6+
require Pathname.new(CypressRails::Config.new.rails_dir).join("config/environment")
77

88
command = ARGV[0]
99
case command

lib/cypress-rails/config.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
module CypressRails
44
class Config
5-
attr_accessor :dir, :host, :port, :base_path, :transactional_server, :cypress_cli_opts
5+
attr_accessor :rails_dir, :cypress_dir, :host, :port, :base_path, :transactional_server, :cypress_cli_opts
66

77
def initialize(
8-
dir: Env.fetch("CYPRESS_RAILS_DIR", default: Dir.pwd),
8+
rails_dir: Env.fetch("CYPRESS_RAILS_DIR", default: Dir.pwd),
9+
cypress_dir: Env.fetch("CYPRESS_RAILS_CYPRESS_DIR", default: rails_dir),
910
host: Env.fetch("CYPRESS_RAILS_HOST", default: "127.0.0.1"),
1011
port: Env.fetch("CYPRESS_RAILS_PORT"),
1112
base_path: Env.fetch("CYPRESS_RAILS_BASE_PATH", default: "/"),
1213
transactional_server: Env.fetch("CYPRESS_RAILS_TRANSACTIONAL_SERVER", type: :boolean, default: true),
1314
cypress_cli_opts: Env.fetch("CYPRESS_RAILS_CYPRESS_OPTS", default: "")
1415
)
15-
@dir = dir
16+
@rails_dir = rails_dir
17+
@cypress_dir = cypress_dir
1618
@host = host
1719
@port = port
1820
@base_path = base_path
@@ -25,7 +27,8 @@ def to_s
2527
2628
cypress-rails configuration:
2729
============================
28-
CYPRESS_RAILS_DIR.....................#{dir.inspect}
30+
CYPRESS_RAILS_DIR.....................#{rails_dir.inspect}
31+
CYPRESS_RAILS_CYPRESS_DIR.............#{cypress_dir.inspect}
2932
CYPRESS_RAILS_HOST....................#{host.inspect}
3033
CYPRESS_RAILS_PORT....................#{port.inspect}
3134
CYPRESS_RAILS_BASE_PATH...............#{base_path.inspect}

lib/cypress-rails/finds_bin.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module CypressRails
44
class FindsBin
55
LOCAL_PATH = "node_modules/.bin/cypress"
66

7-
def call(dir = Dir.pwd)
8-
local_path = Pathname.new(dir).join(LOCAL_PATH)
7+
def call(cypress_dir = Dir.pwd)
8+
local_path = Pathname.new(cypress_dir).join(LOCAL_PATH)
99
if File.exist?(local_path)
1010
local_path
1111
else

lib/cypress-rails/init.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Init
1919
})
2020
JS
2121

22-
def call(dir = Dir.pwd)
23-
config_path = File.join(dir, "cypress.config.js")
22+
def call(cypress_dir = Config.new.cypress_dir)
23+
config_path = File.join(cypress_dir, "cypress.config.js")
2424
if !File.exist?(config_path)
2525
File.write(config_path, DEFAULT_CONFIG)
2626
puts "Cypress config initialized in `#{config_path}'"

lib/cypress-rails/launches_cypress.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def call(command, config)
2424
port: config.port,
2525
transactional_server: config.transactional_server
2626
)
27-
bin = @finds_bin.call(config.dir)
27+
bin = @finds_bin.call(config.cypress_dir)
2828

2929
set_exit_hooks!(config)
3030

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

3535
puts "\nLaunching Cypress…\n$ #{command}\n"

script/test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ echo "---> Running tests"
1515
bundle exec rake
1616
./script/test_example_app
1717

18+
bundle exec rake test
19+
1820
echo "---> Job's done!"
1921

test/config_test.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require_relative "test_helper"
2+
3+
class ConfigTest < Minitest::Test
4+
def test_that_rails_dir_and_cypress_dir_use_default_directory
5+
config = CypressRails::Config.new
6+
expected_directory_path = Dir.pwd
7+
8+
assert_equal(expected_directory_path, config.rails_dir)
9+
assert_equal(expected_directory_path, config.cypress_dir)
10+
end
11+
12+
def test_that_rails_dir_and_cypress_dir_can_be_independently_set
13+
mock_env(
14+
"CYPRESS_RAILS_DIR" => "path/to/cypress-rails",
15+
"CYPRESS_RAILS_CYPRESS_DIR" => "path/to/another/cypress/directory"
16+
) do
17+
config = CypressRails::Config.new
18+
19+
assert_equal("path/to/cypress-rails", config.rails_dir)
20+
assert_equal("path/to/another/cypress/directory", config.cypress_dir)
21+
end
22+
end
23+
24+
def test_that_cypress_dir_uses_same_directory_as_rails_dir_when_not_set
25+
mock_env("CYPRESS_RAILS_DIR" => "path/to/cypress-rails") do
26+
config = CypressRails::Config.new
27+
28+
assert_nil(ENV["CYPRESS_RAILS_CYPRESS_DIR"])
29+
assert_equal("path/to/cypress-rails", config.cypress_dir)
30+
end
31+
end
32+
33+
private
34+
35+
def mock_env(partial_env_hash)
36+
old = ENV.to_hash
37+
ENV.update partial_env_hash
38+
begin
39+
yield
40+
ensure
41+
ENV.replace old
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)