Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Merge branch 'set-PATH-before-exec'
Browse files Browse the repository at this point in the history
Fixes #103
  • Loading branch information
danielsdeleo committed Jul 15, 2014
2 parents 3841539 + d7919a6 commit 8479ef7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/chef-dk/command/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class Exec < ChefDK::Command::Base
banner "Usage: chef exec SYSTEM_COMMAND"

def run(params)
exec omnibus_env, *params
# Set ENV directly on the "parent" process (us) before running #exec to
# ensure the custom PATH is honored when finding the command to exec
omnibus_env.each {|var, value| ENV[var] = value }
exec(*params)
raise "Exec failed without an exception, your ruby is buggy" # should never get here
end
end
Expand Down
46 changes: 28 additions & 18 deletions spec/unit/command/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,29 @@ def run_command

let(:omnibus_bin_dir) { "/foo/bin" }

let(:expected_env) do
{
'PATH' => "#{omnibus_bin_dir}:#{user_bin_dir}:#{omnibus_embedded_bin_dir}:#{ENV['PATH']}",
'GEM_ROOT' => Gem.default_dir.inspect,
'GEM_HOME' => ENV['GEM_HOME'],
'GEM_PATH' => Gem.path.join(':'),
}
end
let(:expected_PATH) { "#{omnibus_bin_dir}:#{user_bin_dir}:#{omnibus_embedded_bin_dir}:#{ENV['PATH']}" }

let(:expected_GEM_ROOT) { Gem.default_dir.inspect }

let(:expected_GEM_HOME) { ENV['GEM_HOME'] }

let(:expected_GEM_PATH) { Gem.path.join(':') }

before do
allow(command_instance).to receive(:omnibus_embedded_bin_dir).and_return(omnibus_embedded_bin_dir)
allow(command_instance).to receive(:omnibus_bin_dir).and_return(omnibus_bin_dir)
end

it "should call exec to fire off the command with the correct environment" do
expect(command_instance).to receive(:exec).with(expected_env, *command_options)
expect(ENV).to receive(:[]=).with("PATH", expected_PATH)
expect(ENV).to receive(:[]=).with("GEM_ROOT", expected_GEM_ROOT)
expect(ENV).to receive(:[]=).with("GEM_HOME", expected_GEM_HOME)
expect(ENV).to receive(:[]=).with("GEM_PATH", expected_GEM_PATH)

expect(command_instance).to receive(:exec).with(*command_options)
expect{ run_command }.to raise_error # XXX: this isn't a test we just need to swallow the exception
end

end

context "when running command that does not exist" do
Expand All @@ -98,23 +103,28 @@ def run_command

let(:omnibus_embedded_bin_dir) { "/foo/embedded/bin" }

let(:expected_env) do
{
'PATH' => "#{omnibus_bin_dir}:#{user_bin_dir}:#{omnibus_embedded_bin_dir}:#{ENV['PATH']}",
'GEM_ROOT' => Gem.default_dir.inspect,
'GEM_HOME' => Gem.paths.home,
'GEM_PATH' => Gem.path.join(':'),
}
end
let(:expected_PATH) { "#{omnibus_bin_dir}:#{user_bin_dir}:#{omnibus_embedded_bin_dir}:#{ENV['PATH']}" }

let(:expected_GEM_ROOT) { Gem.default_dir.inspect }

let(:expected_GEM_HOME) { ENV['GEM_HOME'] }

let(:expected_GEM_PATH) { Gem.path.join(':') }


before do
allow(command_instance).to receive(:omnibus_embedded_bin_dir).and_return(omnibus_embedded_bin_dir)
allow(command_instance).to receive(:omnibus_bin_dir).and_return(omnibus_bin_dir)
end

it "should raise Errno::ENOENT" do
expect(ENV).to receive(:[]=).with("PATH", expected_PATH)
expect(ENV).to receive(:[]=).with("GEM_ROOT", expected_GEM_ROOT)
expect(ENV).to receive(:[]=).with("GEM_HOME", expected_GEM_HOME)
expect(ENV).to receive(:[]=).with("GEM_PATH", expected_GEM_PATH)

# XXX: this doesn't really test much, but really calling exec will never return to rspec
expect(command_instance).to receive(:exec).with(expected_env, *command_options).and_raise(Errno::ENOENT)
expect(command_instance).to receive(:exec).with(*command_options).and_raise(Errno::ENOENT)
expect{ run_command }.to raise_error(Errno::ENOENT)
end
end
Expand Down

0 comments on commit 8479ef7

Please sign in to comment.