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

Pass version to command when execing #255

Merged
merged 6 commits into from
Dec 6, 2014
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions lib/chef-dk/command/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ def run(params)
exec(*params)
raise "Exec failed without an exception, your ruby is buggy" # should never get here
end

def needs_version?(params)
# Force version to get passed down to command
false
end

def needs_help?(params)
cmd_idx = params.find_index {|x| !x.start_with? '-'}
switch_idx = params.find_index {|x| ['-h', '--help'].include? x}
if cmd_idx && switch_idx && cmd_idx < switch_idx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just check if the first parameter is -h or --help? Why do this more complicated thing instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have a good answer for you other than that is just the way I was thinking about what I wanted to do at the time. Checking the first parameter would indeed be simpler.
Ill update the PR in a bit

false
else
super
end
end
end
end
end
Expand Down
43 changes: 42 additions & 1 deletion spec/unit/command/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
let(:command_options) { [] }

def run_command
command_instance.run(command_options)
command_instance.run_with_default_options(command_options)
end

it "has a usage banner" do
Expand Down Expand Up @@ -97,6 +97,47 @@ def run_command
expect{ run_command }.to raise_error # XXX: this isn't a test we just need to swallow the exception
end

['-v', '--version', '-h', '--help'].each do |switch|
context "when running a command with #{switch}" do
let(:command_options) { %W[gem list #{switch}] }

it "should call exec to fire off the command with the correct environment" 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)

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
end

['-h', '--help'].each do |switch|
context "when running a exec with #{switch} and things after it" do
let(:command_options) { %W[#{switch} gem] }

it "should call not call exec, but it should print the banner" do
expect(ENV).not_to receive(:[]=)
expect(command_instance).to receive(:banner)
expect(command_instance).not_to receive(:exec)
run_command
end
end
end

['-h', '--help'].each do |switch|
context "when running a exec with #{switch}" do
let(:command_options) { ["#{switch}"] }

it "should call not call exec, but it should print the banner" do
expect(ENV).not_to receive(:[]=)
expect(command_instance).to receive(:banner)
expect(command_instance).not_to receive(:exec)
run_command
end
end
end
end

context "when running command that does not exist" do
Expand Down