mirror of
https://github.com/not-kennethreitz/envoy.git
synced 2026-06-05 15:10:19 +00:00
Merge pull request #71 from kracekumar/master
Handle exception for non existing commands
This commit is contained in:
+17
-8
@@ -13,6 +13,7 @@ import shlex
|
||||
import signal
|
||||
import subprocess
|
||||
import threading
|
||||
import traceback
|
||||
|
||||
|
||||
__version__ = '0.0.2'
|
||||
@@ -30,11 +31,13 @@ def _terminate_process(process):
|
||||
else:
|
||||
os.kill(process.pid, signal.SIGTERM)
|
||||
|
||||
|
||||
def _kill_process(process):
|
||||
if sys.platform == 'win32':
|
||||
_terminate_process(process)
|
||||
else:
|
||||
os.kill(process.pid, signal.SIGKILL)
|
||||
if sys.platform == 'win32':
|
||||
_terminate_process(process)
|
||||
else:
|
||||
os.kill(process.pid, signal.SIGKILL)
|
||||
|
||||
|
||||
def _is_alive(thread):
|
||||
if hasattr(thread, "is_alive"):
|
||||
@@ -42,6 +45,7 @@ def _is_alive(thread):
|
||||
else:
|
||||
return thread.isAlive()
|
||||
|
||||
|
||||
class Command(object):
|
||||
def __init__(self, cmd):
|
||||
self.cmd = cmd
|
||||
@@ -73,13 +77,13 @@ class Command(object):
|
||||
|
||||
if sys.version_info[0] >= 3:
|
||||
self.out, self.err = self.process.communicate(
|
||||
input = bytes(self.data, "UTF-8") if self.data else None
|
||||
input = bytes(self.data, "UTF-8") if self.data else None
|
||||
)
|
||||
else:
|
||||
self.out, self.err = self.process.communicate(self.data)
|
||||
except Exception as exc:
|
||||
self.exc = exc
|
||||
|
||||
|
||||
|
||||
thread = threading.Thread(target=target)
|
||||
thread.start()
|
||||
@@ -206,14 +210,19 @@ def run(command, data=None, timeout=None, kill_timeout=None, env=None, cwd=None)
|
||||
data = history[-1].std_out[0:10*1024]
|
||||
|
||||
cmd = Command(c)
|
||||
out, err = cmd.run(data, timeout, kill_timeout, env, cwd)
|
||||
try:
|
||||
out, err = cmd.run(data, timeout, kill_timeout, env, cwd)
|
||||
status_code = cmd.returncode
|
||||
except OSError as e:
|
||||
out, err = '', u"\n".join([e.strerror, traceback.format_exc()])
|
||||
status_code = 127
|
||||
|
||||
r = Response(process=cmd)
|
||||
|
||||
r.command = c
|
||||
r.std_out = out
|
||||
r.std_err = err
|
||||
r.status_code = cmd.returncode
|
||||
r.status_code = status_code
|
||||
|
||||
history.append(r)
|
||||
|
||||
|
||||
+7
-1
@@ -2,6 +2,7 @@ import unittest
|
||||
import envoy
|
||||
import time
|
||||
|
||||
|
||||
class SimpleTest(unittest.TestCase):
|
||||
|
||||
def test_input(self):
|
||||
@@ -26,6 +27,11 @@ class SimpleTest(unittest.TestCase):
|
||||
self.assertEqual(r.std_out.rstrip(), sentinel)
|
||||
self.assertEqual(r.status_code, 0)
|
||||
|
||||
def test_non_existing_command(self):
|
||||
r = envoy.run("blah")
|
||||
self.assertEqual(r.status_code, 127)
|
||||
|
||||
|
||||
class ConnectedCommandTests(unittest.TestCase):
|
||||
|
||||
def test_status_code_none(self):
|
||||
@@ -49,4 +55,4 @@ class ConnectedCommandTests(unittest.TestCase):
|
||||
self.assertEqual(r.status_code, 0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user