Unittest - Replace None by objects representing the state of stdout/err

This commit is contained in:
Renato Alves 2014-10-20 13:29:30 +01:00
parent 811d0e029d
commit b4f33ec0c6
2 changed files with 21 additions and 3 deletions

View file

@ -32,4 +32,17 @@ class CommandError(Exception):
class HookError(Exception):
pass
class TimeoutWaitingForStream(object):
def __init__(self, name):
self.stream = name
def __repr__(self):
return "*** Timeout reached while waiting for %s ***".format(self.name)
class StreamsAreMerged(object):
def __repr__(self):
return "*** Streams are merged, STDERR is not available ***"
# vim: ai sts=4 et sw=4

View file

@ -13,7 +13,7 @@ try:
import simplejson as json
except ImportError:
import json
from .exceptions import CommandError
from .exceptions import CommandError, TimeoutWaitingForStream, StreamsAreMerged
USED_PORTS = set()
ON_POSIX = 'posix' in sys.builtin_module_names
@ -106,11 +106,11 @@ def _get_output(proc, input, timeout=None):
try:
out = outq.get(timeout=timeout)
except Empty:
out = None
out = TimeoutWaitingForStream("stdout")
try:
err = errq.get(timeout=timeout)
except Empty:
err = None
err = TimeoutWaitingForStream("stderr")
return out, err, exit
@ -133,6 +133,11 @@ def run_cmd_wait(cmd, input=None, stdout=PIPE, stderr=PIPE,
close_fds=ON_POSIX, env=env)
out, err, exit = _get_output(p, input, timeout)
# If streams were merged err should always be None so represent it with a
# more informative object
if merge_streams:
err = StreamsAreMerged()
if exit != 0:
raise CommandError(cmd, exit, out, err)