Skip to content

Commit 497c1ee

Browse files
committed
Ensure bin/rails test command defaults to test` env
`bin/rails test` was defauling to the `development` environment because `ENV['RAILS_ENV']` and `ENV['RACK_ENV']` are nil and `default_rails_env` defaults to the `development` environment when an environment is not provided. This resulted in Spring ignoring even explicitly setting the environment option when running tests. The environment would get overwritten with `development` even when `test` was set. Tests seemed to run fine, but if there was a required file or included module the test would not be able to find those files because the environment was set incorrectly. Borrowing code from `RailsConsole` I updated `RailsTest` to set the default env to `test` but take `--environment` and `-e` into account like the other commands. I added tests to ensure that when not set the environment will default to `test`.
1 parent f18afb8 commit 497c1ee

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/spring/commands/rails.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ def extract_environment(args)
8484
end
8585

8686
class RailsTest < Rails
87+
def env(args)
88+
environment = "test"
89+
90+
args.each.with_index do |arg, i|
91+
if arg =~ /--environment=(\w+)/
92+
environment = $1
93+
elsif i > 0 && args[i - 1] == "-e"
94+
environment = arg
95+
end
96+
end
97+
98+
environment
99+
end
100+
87101
def command_name
88102
"test"
89103
end

test/unit/commands_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,19 @@ class CommandsTest < ActiveSupport::TestCase
5656
assert_equal "test", command.env(["test:models"])
5757
assert_nil command.env(["test_foo"])
5858
end
59+
60+
test 'RailsTest#command defaults to test rails environment' do
61+
command = Spring::Commands::RailsTest.new
62+
assert_equal 'test', command.env([])
63+
end
64+
65+
test 'RailsTest#command sets rails environment from --environment option' do
66+
command = Spring::Commands::RailsTest.new
67+
assert_equal 'foo', command.env(['--environment=foo'])
68+
end
69+
70+
test 'RailsTest#command sets rails environment from -e option' do
71+
command = Spring::Commands::RailsTest.new
72+
assert_equal 'foo', command.env(['-e', 'foo'])
73+
end
5974
end

0 commit comments

Comments
 (0)