Skip to content

Commit

Permalink
Allow passing extra environment variables to PuppetCommand.format_com…
Browse files Browse the repository at this point in the history
…mand
  • Loading branch information
evgeni committed Oct 18, 2024
1 parent c463e3f commit 9bdbf74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/kafo/puppet_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def self.aio_puppet?
# recommended.
# @return [Array]
# A command for use in Open3
def self.format_command(command)
def self.format_command(command, extra_env = {})
cmd = command.is_a?(Array) ? command : [command]
if aio_puppet?
[clean_env_vars] + cmd + [:unsetenv_others => true]
[clean_env_vars.merge(extra_env)] + cmd + [:unsetenv_others => true]
else
[::ENV] + cmd + [:unsetenv_others => false]
[::ENV.to_h.merge(extra_env)] + cmd + [:unsetenv_others => false]
end
end

Expand Down
18 changes: 18 additions & 0 deletions test/kafo/puppet_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ module Kafo
end
end
end

specify 'with extra env' do
PuppetCommand.stub(:aio_puppet?, true) do
PuppetCommand.stub(:clean_env_vars, {'FOO' => 'bar'}) do
expected = [{'FOO' => 'bar', 'EXTRA' => 'cool'}, 'echo hello world', { :unsetenv_others => true }]
assert_equal(expected, PuppetCommand.format_command(['echo hello world'], { 'EXTRA' => 'cool' }))
end
end
end
end

describe 'with regular Puppet' do
Expand All @@ -154,6 +163,15 @@ module Kafo
assert_equal(expected, PuppetCommand.format_command(['echo', 'hello', 'world']))
end
end

specify 'with extra env' do
PuppetCommand.stub(:aio_puppet?, false) do
expected_env = ::ENV.to_h.merge({ 'EXTRA' => 'cool' })
expected = [expected_env, 'echo hello world', { :unsetenv_others => false }]
assert_equal(expected, PuppetCommand.format_command('echo hello world', { 'EXTRA' => 'cool' }))
end
end

end
end
end
Expand Down

0 comments on commit 9bdbf74

Please sign in to comment.