Skip to content

Commit

Permalink
Move compiler configuration into configuration class (#374)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ahangarha authored Oct 31, 2023
1 parent 91f7fbe commit 758b737
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/shakapacker/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/shakapacker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions spec/shakapacker/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions spec/shakapacker/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions spec/shakapacker/spec_helper_initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 758b737

Please sign in to comment.