Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def load_config_file(file)
if file.exist?
# Newer Psych doesn't load aliases by default
load_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load
YAML.send(load_method, ERB.new(File.read(file)).result).symbolize_keys
template = File.read(file)
rendered = ERB.new(template, trim_mode: "-").result
YAML.send(load_method, rendered).symbolize_keys
else
raise "Configuration file not found in #{file}"
end
Expand Down
82 changes: 82 additions & 0 deletions test/cli/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,78 @@ class CliMainTest < CliTestCase
end
end

test "config with blank line trimming" do
template = <<~YAML
service: app
image: dhh/app
servers:
- "1.1.1.1"
<% if true -%>
- "1.1.1.2"
<% end -%>
registry:
username: user
password: pw
builder:
arch: amd64
YAML

expected_rendered = ERB.new(template, trim_mode: "-").result

Dir.mktmpdir do |dir|
config_path = File.join(dir, "deploy.yml")
File.write(config_path, template)

load_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load
original_load = YAML.method(load_method)

YAML.expects(load_method).with(expected_rendered).returns(original_load.call(expected_rendered))

run_command_with_config_path("config", config_path: config_path)
end
end

test "config with destination blank line trimming" do
base_template = <<~YAML
service: app
image: dhh/app
servers:
- "1.1.1.1"
registry:
username: user
password: pw
builder:
arch: amd64
YAML

destination_template = <<~YAML
servers:
- "2.2.2.2"
<% if true -%>
- "2.2.2.3"
<% end -%>
YAML

expected_destination = ERB.new(destination_template, trim_mode: "-").result

Dir.mktmpdir do |dir|
base_path = File.join(dir, "deploy.yml")
File.write(base_path, base_template)

destination_path = File.join(dir, "deploy.world.yml")
File.write(destination_path, destination_template)

load_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load
original_load = YAML.method(load_method)
load_sequence = sequence("config_files")

YAML.expects(load_method).with(base_template).in_sequence(load_sequence).returns(original_load.call(base_template))
YAML.expects(load_method).with(expected_destination).in_sequence(load_sequence).returns(original_load.call(expected_destination))

run_command_with_config_path("config", config_path: base_path, destination: "world")
end
end

test "init" do
in_dummy_git_repo do
run_command("init").tap do |output|
Expand Down Expand Up @@ -647,6 +719,16 @@ def run_command(*command, config_file: "deploy_simple")
end
end

def run_command_with_config_path(*command, config_path:, destination: nil)
argv = [ *command ]
argv += [ "-d", destination ] if destination
argv += [ "-c", config_path ]

with_argv([ *argv ]) do
stdouted { Kamal::Cli::Main.start }
end
end

def in_dummy_git_repo
Dir.mktmpdir do |tmpdir|
Dir.chdir(tmpdir) do
Expand Down