From 169f8622cf6688e573265c432c0aa78310b30f11 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Mon, 1 Dec 2014 22:42:28 -0800 Subject: [PATCH 1/6] Pass version to command when execing --- lib/chef-dk/command/exec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/chef-dk/command/exec.rb b/lib/chef-dk/command/exec.rb index 62eca62aa..68a145e92 100644 --- a/lib/chef-dk/command/exec.rb +++ b/lib/chef-dk/command/exec.rb @@ -30,6 +30,11 @@ 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 end end end From 1ea0ca988b5770d19c5b694642f8c79fa2404d3a Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Tue, 2 Dec 2014 14:12:47 -0800 Subject: [PATCH 2/6] Pass -h/--help in chef exec --- lib/chef-dk/command/exec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/chef-dk/command/exec.rb b/lib/chef-dk/command/exec.rb index 68a145e92..62eb58e49 100644 --- a/lib/chef-dk/command/exec.rb +++ b/lib/chef-dk/command/exec.rb @@ -35,6 +35,16 @@ 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 + false + else + super + end + end end end end From 6fc9f234af5eb2eea71548a82880f958f3519a6c Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Tue, 2 Dec 2014 16:53:06 -0800 Subject: [PATCH 3/6] Added spec for version and help in exec --- spec/unit/command/exec_spec.rb | 43 +++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/spec/unit/command/exec_spec.rb b/spec/unit/command/exec_spec.rb index aed3ec1a1..742998022 100644 --- a/spec/unit/command/exec_spec.rb +++ b/spec/unit/command/exec_spec.rb @@ -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 @@ -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 From 83fd254b4d3455936a3d918212bb738a609f37cf Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Wed, 3 Dec 2014 10:45:13 -0800 Subject: [PATCH 4/6] Simpler logic for -h in exec --- lib/chef-dk/command/exec.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/chef-dk/command/exec.rb b/lib/chef-dk/command/exec.rb index 62eb58e49..8201d0185 100644 --- a/lib/chef-dk/command/exec.rb +++ b/lib/chef-dk/command/exec.rb @@ -37,13 +37,7 @@ def needs_version?(params) 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 - false - else - super - end + ['-h', '--help'].include? params[0] end end end From d7d3f805da0466d8178730f7c7937fc6fbfd3bb8 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Wed, 3 Dec 2014 21:59:53 -0800 Subject: [PATCH 5/6] Minor refactor (grouping loops) --- spec/unit/command/exec_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/unit/command/exec_spec.rb b/spec/unit/command/exec_spec.rb index 742998022..b08f781d2 100644 --- a/spec/unit/command/exec_spec.rb +++ b/spec/unit/command/exec_spec.rb @@ -124,9 +124,7 @@ def run_command run_command end end - end - ['-h', '--help'].each do |switch| context "when running a exec with #{switch}" do let(:command_options) { ["#{switch}"] } From 66fc747e362061112306c069c682f23b4e557608 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Wed, 3 Dec 2014 22:02:01 -0800 Subject: [PATCH 6/6] Stub out msg in exec_spec --- spec/unit/command/exec_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/unit/command/exec_spec.rb b/spec/unit/command/exec_spec.rb index b08f781d2..f41758fbd 100644 --- a/spec/unit/command/exec_spec.rb +++ b/spec/unit/command/exec_spec.rb @@ -118,6 +118,7 @@ def run_command let(:command_options) { %W[#{switch} gem] } it "should call not call exec, but it should print the banner" do + allow(command_instance).to receive(:msg) expect(ENV).not_to receive(:[]=) expect(command_instance).to receive(:banner) expect(command_instance).not_to receive(:exec) @@ -129,6 +130,7 @@ def run_command let(:command_options) { ["#{switch}"] } it "should call not call exec, but it should print the banner" do + allow(command_instance).to receive(:msg) expect(ENV).not_to receive(:[]=) expect(command_instance).to receive(:banner) expect(command_instance).not_to receive(:exec)