Permalink
Browse files

Kill process on error. Throw CommandFailed if command returns nonzero.

  • Loading branch information...
1 parent bce2b3e commit ec92710d8ab90c63dfc0886f193cecf49748e555 @topfunky committed Feb 19, 2010
Showing with 15 additions and 3 deletions.
  1. +5 −3 lib/osxscreenshot.rb
  2. +10 −0 test/test_osxscreenshot.rb
View
8 lib/osxscreenshot.rb
@@ -3,7 +3,9 @@
module OSX
class Screenshot
VERSION = '0.0.3'
-
+
+ class CommandFailed < StandardError; end
+
##
# Takes a screenshot of a website, optionally resizes it, and writes
# it to a temporary directory. Returns the file path to the
@@ -109,9 +111,9 @@ def run_command(cmd, &block)
end
# Error
- system "kill -9 #{pid}"
- raise "Command failed: #{cmd}"
+ raise CommandFailed, "Command failed: #{cmd}"
rescue Timeout::Error => e
+ Process.kill "KILL", pid
return nil
end
View
10 test/test_osxscreenshot.rb
@@ -1,3 +1,4 @@
+# $: << File.dirname(__FILE__) + "/../lib"
require "test/unit"
require "osxscreenshot"
@@ -30,6 +31,15 @@ def test_handles_timeout
assert_nil @tmpfile
end
+ def test_handles_error_when_exiting
+ @tmpfile = nil
+ assert_raise OSX::Screenshot::CommandFailed do
+ OSX::Screenshot.capture("http://example.com", {
+ :webkit2png => "exit 1;"
+ })
+ end
+ end
+
def teardown
if @tmpfile
FileUtils.rm @tmpfile

0 comments on commit ec92710

Please sign in to comment.