Browse files

Don't crash on exception when no command's been run

  • Loading branch information...
1 parent ee7b2bd commit 1fa540ec169919930db3a0e6e4a2f5d9421b9aeb @jyurek jyurek committed Mar 1, 2013
View
9 lib/cocaine/command_line.rb
@@ -74,21 +74,22 @@ def command(interpolations = {})
def run(interpolations = {})
output = ''
+ @exit_status = nil
begin
full_command = command(interpolations)
log("#{colored("Command")} :: #{full_command}")
output = execute(full_command)
rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError
ensure
- @exit_status = $?.exitstatus
+ @exit_status = $?.exitstatus if $?.respond_to?(:exitstatus)
end
- if $?.exitstatus == 127
+ if @exit_status == 127
raise Cocaine::CommandNotFoundError
end
- unless @expected_outcodes.include?($?.exitstatus)
+ unless @expected_outcodes.include?(@exit_status)
message = [
- "Command '#{full_command}' returned #{$?.exitstatus}. Expected #{@expected_outcodes.join(", ")}",
+ "Command '#{full_command}' returned #{@exit_status}. Expected #{@expected_outcodes.join(", ")}",
"Here is the command output:\n",
output
].join("\n")
View
9 spec/cocaine/command_line_spec.rb
@@ -69,6 +69,15 @@
command.run(:hello_world => "Hello, world").should match(/Hello, world/)
end
+ it "does not blow up if running the command errored before the actual execution" do
+ assuming_no_processes_have_been_run
+ command = Cocaine::CommandLine.new("echo", ":hello_world")
+ command.stubs(:command).raises("An Error")
+
+ lambda{ command.run }.should raise_error("An Error")
+ command.exit_status.should be_nil
+ end
+
it "quotes command line options differently if we're on windows" do
on_windows!
cmd = Cocaine::CommandLine.new("convert",
View
1 spec/spec_helper.rb
@@ -14,4 +14,5 @@
config.mock_with :mocha
config.include WithExitstatus
config.include StubOS
+ config.include UnsettingExitstatus
end
View
7 spec/support/unsetting_exitstatus.rb
@@ -0,0 +1,7 @@
+module UnsettingExitstatus
+ def assuming_no_processes_have_been_run
+ class << $?
+ undef_method :exitstatus
+ end
+ end
+end
View
2 spec/support/with_exitstatus.rb
@@ -1,6 +1,6 @@
module WithExitstatus
def with_exitstatus_returning(code)
- saved_exitstatus = $?.nil? ? 0 : $?.exitstatus
+ saved_exitstatus = $?.respond_to?(:exitstatus) ? $?.exitstatus : 0
begin
`ruby -e "exit #{code.to_i}"`
yield

0 comments on commit 1fa540e

Please sign in to comment.