From 758b73704a977b388c2bf8f943cd08cb14974eb5 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 31 Oct 2023 07:35:36 +0330 Subject: [PATCH] Move compiler configuration into configuration class (#374) * Add test for config.asset_host * Add test for config.relative_url_root * Isolate env variables for the tests * Refactor and ease the usage of temporary env variables in test --- lib/shakapacker/compiler.rb | 8 ++-- lib/shakapacker/configuration.rb | 8 ++++ spec/shakapacker/compiler_spec.rb | 14 +++--- spec/shakapacker/configuration_spec.rb | 48 +++++++++++++++++++++ spec/shakapacker/spec_helper_initializer.rb | 18 ++++++++ 5 files changed, 88 insertions(+), 8 deletions(-) diff --git a/lib/shakapacker/compiler.rb b/lib/shakapacker/compiler.rb index 4e95a38f4..4c0dcb094 100644 --- a/lib/shakapacker/compiler.rb +++ b/lib/shakapacker/compiler.rb @@ -106,9 +106,11 @@ def webpack_env Shakapacker.set_shakapacker_env_variables_for_backward_compatibility - env.merge("SHAKAPACKER_ASSET_HOST" => ENV.fetch("SHAKAPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host), - "SHAKAPACKER_RELATIVE_URL_ROOT" => ENV.fetch("SHAKAPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root), - "SHAKAPACKER_CONFIG" => instance.config_path.to_s) + env.merge( + "SHAKAPACKER_ASSET_HOST" => instance.config.asset_host, + "SHAKAPACKER_RELATIVE_URL_ROOT" => instance.config.relative_url_root, + "SHAKAPACKER_CONFIG" => instance.config_path.to_s + ) end def bin_shakapacker_path diff --git a/lib/shakapacker/configuration.rb b/lib/shakapacker/configuration.rb index 7ff6722a4..033ef3bdb 100644 --- a/lib/shakapacker/configuration.rb +++ b/lib/shakapacker/configuration.rb @@ -116,6 +116,14 @@ def fetch(key) data.fetch(key, defaults[:shakapacker_precompile]) end + def asset_host + ENV.fetch("SHAKAPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host) + end + + def relative_url_root + ENV.fetch("SHAKAPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root) + end + private def data @data ||= load diff --git a/spec/shakapacker/compiler_spec.rb b/spec/shakapacker/compiler_spec.rb index e81888e3b..96ba78f73 100644 --- a/spec/shakapacker/compiler_spec.rb +++ b/spec/shakapacker/compiler_spec.rb @@ -50,10 +50,14 @@ expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to be nil expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to be nil - ENV["SHAKAPACKER_ASSET_HOST"] = "foo.bar" - ENV["SHAKAPACKER_RELATIVE_URL_ROOT"] = "/baz" - - expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar" - expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz" + custom_env_variables = { + "SHAKAPACKER_ASSET_HOST" => "foo.bar", + "SHAKAPACKER_RELATIVE_URL_ROOT" => "/baz" + } + + with_env_variable(custom_env_variables) do + expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar" + expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz" + end end end diff --git a/spec/shakapacker/configuration_spec.rb b/spec/shakapacker/configuration_spec.rb index 0c70c4944..9f6f6c6b8 100644 --- a/spec/shakapacker/configuration_spec.rb +++ b/spec/shakapacker/configuration_spec.rb @@ -330,4 +330,52 @@ expect(actual).to eq(expected) end end + + describe "#asset_host" do + let(:config) do + Shakapacker::Configuration.new( + root_path: ROOT_PATH, + config_path: Pathname.new(File.expand_path("./test_app/config/shakapacker.yml", __dir__)), + env: "production" + ) + end + + it "returns the value of SHAKAPACKER_ASSET_HOST if set" do + with_env_variable("SHAKAPACKER_ASSET_HOST" => "custom_host.abc") do + expect(config.asset_host).to eq "custom_host.abc" + end + end + + it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_ASSET_HOST is not set" do + allow(ActionController::Base.helpers).to receive(:compute_asset_host).and_return("domain.abc") + + with_env_variable("SHAKAPACKER_ASSET_HOST" => nil) do + expect(config.asset_host).to eq "domain.abc" + end + end + end + + describe "#relative_url_root" do + let(:config) do + Shakapacker::Configuration.new( + root_path: ROOT_PATH, + config_path: Pathname.new(File.expand_path("./test_app/config/shakapacker.yml", __dir__)), + env: "production" + ) + end + + it "returns the value of SHAKAPACKER_RELATIVE_URL_ROOT if set" do + with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => "custom_value") do + expect(config.relative_url_root).to eq "custom_value" + end + end + + it "returns ActionController::Base.helpers.compute_asset_host if SHAKAPACKER_RELATIVE_URL_ROOT is not set" do + allow(ActionController::Base).to receive(:relative_url_root).and_return("abcd") + + with_env_variable("SHAKAPACKER_RELATIVE_URL_ROOT" => nil) do + expect(config.relative_url_root).to eq "abcd" + end + end + end end diff --git a/spec/shakapacker/spec_helper_initializer.rb b/spec/shakapacker/spec_helper_initializer.rb index 65209edc9..400b7b525 100644 --- a/spec/shakapacker/spec_helper_initializer.rb +++ b/spec/shakapacker/spec_helper_initializer.rb @@ -22,3 +22,21 @@ def with_rails_env(env) Rails.env = ActiveSupport::StringInquirer.new(original) reloaded_config end + +# Temportarily set env variables to a custom value +# arg: a hash with key:value for each custom env +# Keys could be string or symbol +def with_env_variable(custom_env_hash) + original_env = {} + custom_env_hash.each do |key, new_value| + upcased_key = key.to_s.upcase + original_env[upcased_key] = new_value + ENV[upcased_key] = new_value + end + + yield +ensure + original_env.each do |key, original_value| + ENV[key] = original_value + end +end