Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing array commands to format_command #374

Merged
merged 2 commits into from
Oct 18, 2024
Merged
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
14 changes: 12 additions & 2 deletions lib/kafo/puppet_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ def self.aio_puppet?
false
end

# Format a command for use in Open3 that properly filters the environment
# for use with Puppet
#
# @param command [String, Array]
# The command to execute. A string implies it'll run with a shell while
# an array runs without. That removes the need to escape input and is
# recommended.
# @return [Array]
# A command for use in Open3
def self.format_command(command)
cmd = command.is_a?(Array) ? command : [command]
if aio_puppet?
[clean_env_vars, command, :unsetenv_others => true]
[clean_env_vars] + cmd + [:unsetenv_others => true]
else
[::ENV, command, :unsetenv_others => false]
[::ENV] + cmd + [:unsetenv_others => false]
end
end

Expand Down
48 changes: 43 additions & 5 deletions test/kafo/puppet_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module Kafo
describe "with 'puppet' in PATH" do
specify do
::ENV.stub(:[], '/usr/bin:/usr/local/bin') do
File.stub(:executable?, Proc.new { |path| path == '/usr/local/bin/puppet' }) do
File.stub(:executable?, proc { |path| path == '/usr/local/bin/puppet' }) do
_(pc).must_equal '/usr/local/bin/puppet'
end
end
Expand All @@ -67,7 +67,7 @@ module Kafo
describe "with AIO 'puppet' only" do
specify do
::ENV.stub(:[], '/usr/bin:/usr/local/bin') do
File.stub(:executable?, Proc.new { |path| path == '/opt/puppetlabs/bin/puppet' }) do
File.stub(:executable?, proc { |path| path == '/opt/puppetlabs/bin/puppet' }) do
_(pc).must_equal '/opt/puppetlabs/bin/puppet'
end
end
Expand All @@ -90,7 +90,7 @@ module Kafo
let(:puppet_command) { '/usr/bin/puppet' }

specify 'as a real file' do
File.stub(:realpath, ->(path) { path }) do
File.stub(:realpath, proc { |path| path }) do
refute subject
end
end
Expand All @@ -102,7 +102,7 @@ module Kafo
end

specify 'as a broken symlink' do
File.stub(:realpath, ->(path) { raise Errno::ENOENT, 'No such file or directory' }) do
File.stub(:realpath, proc { |_path| raise Errno::ENOENT, 'No such file or directory' }) do
refute subject
end
end
Expand All @@ -112,11 +112,49 @@ module Kafo
let(:puppet_command) { 'puppet' }

specify 'non-existant' do
File.stub(:realpath, ->(path) { raise Errno::ENOENT, 'No such file or directory' }) do
File.stub(:realpath, proc { |_path| raise Errno::ENOENT, 'No such file or directory' }) do
refute subject
end
end
end
end

describe '.format_command' do
describe 'with Puppet AIO' do
specify 'with a string' do
PuppetCommand.stub(:aio_puppet?, true) do
PuppetCommand.stub(:clean_env_vars, {'FOO' => 'bar'}) do
expected = [{'FOO' => 'bar'}, 'echo hello world', { :unsetenv_others => true }]
assert_equal(expected, PuppetCommand.format_command('echo hello world'))
end
end
end

specify 'with an array' do
PuppetCommand.stub(:aio_puppet?, true) do
PuppetCommand.stub(:clean_env_vars, {'FOO' => 'bar'}) do
expected = [{'FOO' => 'bar'}, 'echo', 'hello', 'world', { :unsetenv_others => true }]
evgeni marked this conversation as resolved.
Show resolved Hide resolved
assert_equal(expected, PuppetCommand.format_command(['echo', 'hello', 'world']))
end
end
end
end

describe 'with regular Puppet' do
specify 'with a string' do
PuppetCommand.stub(:aio_puppet?, false) do
expected = [::ENV, 'echo hello world', { :unsetenv_others => false }]
assert_equal(expected, PuppetCommand.format_command('echo hello world'))
end
end

specify 'with an array' do
PuppetCommand.stub(:aio_puppet?, false) do
expected = [::ENV, 'echo', 'hello', 'world', { :unsetenv_others => false }]
assert_equal(expected, PuppetCommand.format_command(['echo', 'hello', 'world']))
end
end
end
end
end
end
Loading