Skip to content

Commit 7d14551

Browse files
committed
Assign envs before preload
1 parent c579428 commit 7d14551

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

lib/spring/application.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,27 @@ def run
133133
end
134134
end
135135

136+
def set_env_vars(env)
137+
# Delete all env vars which are unchanged from before spring started
138+
original_env.each { |k, v| ENV.delete k if ENV[k] == v }
139+
140+
# Load in the current env vars, except those which *were* changed when spring started
141+
env.each { |k, v| ENV[k] ||= v }
142+
end
143+
136144
def serve(client)
137145
log "got client"
138146
manager.puts
139147

140148
stdout, stderr, stdin = streams = 3.times.map { client.recv_io }
141149
[STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) }
142150

143-
preload unless preloaded?
151+
client_args, client_env = JSON.load(client.read(client.gets.to_i)).values_at("args", "env")
144152

145-
args, env = JSON.load(client.read(client.gets.to_i)).values_at("args", "env")
146-
command = Spring.command(args.shift)
153+
set_env_vars(client_env)
154+
155+
preload unless preloaded?
156+
command = Spring.command(client_args.shift)
147157

148158
connect_database
149159
setup command
@@ -157,15 +167,9 @@ def serve(client)
157167
IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") }
158168
trap("TERM", "DEFAULT")
159169

160-
ARGV.replace(args)
170+
ARGV.replace(client_args)
161171
$0 = command.process_title
162172

163-
# Delete all env vars which are unchanged from before spring started
164-
original_env.each { |k, v| ENV.delete k if ENV[k] == v }
165-
166-
# Load in the current env vars, except those which *were* changed when spring started
167-
env.each { |k, v| ENV[k] ||= v }
168-
169173
# requiring is faster, so if config.cache_classes was true in
170174
# the environment's config file, then we can respect that from
171175
# here on as we no longer need constant reloading.

test/acceptance/app_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ def binstub_prelude
291291
assert_success "bin/rake -p 'Rails.env'", stdout: "test"
292292
end
293293

294+
test "config via environment variable" do
295+
app.env['FORCE_SSL'] = '1'
296+
app.env['RAILS_ENV'] = 'test'
297+
assert_success "bin/rails runner 'p Rails.application.config.force_ssl'", stdout: "true"
298+
299+
app.env['FORCE_SSL'] = '0'
300+
app.env['RAILS_ENV'] = 'development'
301+
assert_success "bin/rails runner 'p Rails.application.config.force_ssl'", stdout: "false"
302+
end
303+
294304
test "setting env vars with rake" do
295305
File.write(app.path("lib/tasks/env.rake"), <<-'CODE')
296306
task :print_rails_env => :environment do

test/acceptance/helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ def application_config
121121
path "config/application.rb"
122122
end
123123

124+
def some_initializer
125+
path "config/initializers/foo.rb"
126+
end
127+
124128
def spring_config
125129
path "config/spring.rb"
126130
end
@@ -295,6 +299,7 @@ def generate
295299
FileUtils.rm_rf(application.path("test/performance"))
296300

297301
File.write(application.gemfile, "#{application.gemfile.read}gem 'spring', '#{Spring::VERSION}'\n")
302+
File.write(application.some_initializer, "Rails.application.config.force_ssl = ('1' == ENV['FORCE_SSL'])\n")
298303

299304
if version.needs_testunit?
300305
File.write(application.gemfile, "#{application.gemfile.read}gem 'spring-commands-testunit'\n")

0 commit comments

Comments
 (0)