Skip to content

Commit acf4c1b

Browse files
committed
Refactor and ease the usage of temporary env variables in test
1 parent f7fa802 commit acf4c1b

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

spec/shakapacker/compiler_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@
5050
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to be nil
5151
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to be nil
5252

53-
ENV["SHAKAPACKER_ASSET_HOST"] = "foo.bar"
54-
ENV["SHAKAPACKER_RELATIVE_URL_ROOT"] = "/baz"
55-
56-
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar"
57-
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz"
53+
custom_env_variables = {
54+
"WEBPACKER_ASSET_HOST" => "foo.bar",
55+
"WEBPACKER_RELATIVE_URL_ROOT" => "/baz"
56+
}
57+
58+
with_env_variable(custom_env_variables) do
59+
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar"
60+
expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz"
61+
end
5862
end
5963
end

spec/shakapacker/configuration_spec.rb

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -341,24 +341,17 @@
341341
end
342342

343343
it "returns the value of SHAKAPACKER_ASSET_HOST if set" do
344-
original_env_value = ENV["SHAKAPACKER_ASSET_HOST"]
345-
ENV["SHAKAPACKER_ASSET_HOST"] = "custom_host.abc"
346-
347-
expect(config.asset_host).to eq "custom_host.abc"
348-
349-
ensure
350-
ENV["SHAKAPACKER_ASSET_HOST"] = original_env_value
344+
with_env_variable("SHAKAPACKER_ASSET_HOST" => "custom_host.abc") do
345+
expect(config.asset_host).to eq "custom_host.abc"
346+
end
351347
end
352348

353349
it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_ASSET_HOST is not set" do
354-
original_env_value = ENV["SHAKAPACKER_ASSET_HOST"]
355-
ENV["SHAKAPACKER_ASSET_HOST"] = nil
356-
357350
allow(ActionController::Base.helpers).to receive(:compute_asset_host).and_return("domain.abc")
358351

359-
expect(config.asset_host).to eq "domain.abc"
360-
ensure
361-
ENV["SHAKAPACKER_ASSET_HOST"] = original_env_value
352+
with_env_variable("SHAKAPACKER_ASSET_HOST" => nil) do
353+
expect(config.asset_host).to eq "domain.abc"
354+
end
362355
end
363356
end
364357

@@ -372,25 +365,17 @@
372365
end
373366

374367
it "returns the value of SHAKAPACKER_RELATIVE_URL_ROOT if set" do
375-
original_env_value = ENV["SHAKAPACKER_RELATIVE_URL_ROOT"]
376-
ENV["SHAKAPACKER_RELATIVE_URL_ROOT"] = "custom_value"
377-
378-
expect(config.relative_url_root).to eq "custom_value"
379-
380-
ensure
381-
ENV["SHAKAPACKER_RELATIVE_URL_ROOT"] = original_env_value
368+
with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => "custom_value") do
369+
expect(config.relative_url_root).to eq "custom_value"
370+
end
382371
end
383372

384373
it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_RELATIVE_URL_ROOT is not set" do
385-
original_env_value = ENV["SHAKAPACKER_RELATIVE_URL_ROOT"]
386-
ENV["SHAKAPACKER_RELATIVE_URL_ROOT"] = nil
387-
388374
allow(ActionController::Base).to receive(:relative_url_root).and_return("abcd")
389375

390-
expect(config.relative_url_root).to eq "abcd"
391-
392-
ensure
393-
ENV["SHAKAPACKER_RELATIVE_URL_ROOT"] = original_env_value
376+
with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => nil) do
377+
expect(config.relative_url_root).to eq "abcd"
378+
end
394379
end
395380
end
396381
end

spec/shakapacker/spec_helper_initializer.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,21 @@ def with_rails_env(env)
2222
Rails.env = ActiveSupport::StringInquirer.new(original)
2323
reloaded_config
2424
end
25+
26+
# Temportarily set env variables to a custom value
27+
# arg: a hash with key:value for each custom env
28+
# Keys could be string or symbol
29+
def with_env_variable(custom_env_hash)
30+
original_env = {}
31+
custom_env_hash.each do |key, new_value|
32+
upcased_key = key.to_s.upcase
33+
original_env[upcased_key] = new_value
34+
ENV[upcased_key] = new_value
35+
end
36+
37+
yield
38+
ensure
39+
original_env.each do |key, original_value|
40+
ENV[key] = original_value
41+
end
42+
end

0 commit comments

Comments
 (0)