diff options
author | Ondrej Certik <ondrej.certik@gmail.com> | 2013-03-09 12:19:24 +0100 |
---|---|---|
committer | Ondrej Certik <ondrej.certik@gmail.com> | 2013-03-09 12:21:10 +0100 |
commit | 2fbdd9903fc9bf6e1fe797e92c0157abd67850ce (patch) | |
tree | 1060f970ac860cdf6b76c5aeabf1516d94faae62 /numpy/distutils/tests/test_exec_command.py | |
parent | 6c6194a42cd0b5f47cfa7d43d7ae71c6b5e5ae84 (diff) | |
download | numpy-2fbdd9903fc9bf6e1fe797e92c0157abd67850ce.tar.gz |
TST: add robust tests for exec_command()
First, we test both stdout and stderr.
Second, we now test both _exec_command_posix() and _exec_command(), see
numpy/distutils/exec_command.py. This reveals a bug in the implementation which
will be fixed in the next commit. For now the failing test is commented out so
that all tests still pass.
Diffstat (limited to 'numpy/distutils/tests/test_exec_command.py')
-rw-r--r-- | numpy/distutils/tests/test_exec_command.py | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/numpy/distutils/tests/test_exec_command.py b/numpy/distutils/tests/test_exec_command.py index f2c4fbd13..d9983125d 100644 --- a/numpy/distutils/tests/test_exec_command.py +++ b/numpy/distutils/tests/test_exec_command.py @@ -1,3 +1,4 @@ +import os import sys import StringIO @@ -17,13 +18,55 @@ class redirect_stdout(object): self._stdout.flush() sys.stdout = self.old_stdout +class redirect_stderr(object): + """Context manager to redirect stderr for exec_command test.""" + def __init__(self, stderr=None): + self._stderr = stderr or sys.stderr -def test_exec_command(): + def __enter__(self): + self.old_stderr = sys.stderr + sys.stderr = self._stderr + + def __exit__(self, exc_type, exc_value, traceback): + self._stderr.flush() + sys.stderr = self.old_stderr + +class emulate_nonposix(object): + """Context manager to emulate os.name != 'posix' """ + def __init__(self, osname='non-posix'): + self._new_name = osname + + def __enter__(self): + self._old_name = os.name + os.name = self._new_name + + def __exit__(self, exc_type, exc_value, traceback): + os.name = self._old_name + + +def test_exec_command_stdout(): # Regression test for gh-2999 and gh-2915. # There are several packages (nose, scipy.weave.inline, Sage inline # Fortran) that replace stdout, in which case it doesn't have a fileno # method. This is tested here, with a do-nothing command that fails if the # presence of fileno() is assumed in exec_command. + + # Test posix version: with redirect_stdout(StringIO.StringIO()): exec_command.exec_command("cd '.'") + # Test non-posix version: + with emulate_nonposix(): + with redirect_stdout(StringIO.StringIO()): + exec_command.exec_command("cd '.'") + +def test_exec_command_stderr(): + # Test posix version: + with redirect_stderr(StringIO.StringIO()): + exec_command.exec_command("cd '.'") + + # Test non-posix version: + # Note: this test reveals a failure + #with emulate_nonposix(): + # with redirect_stderr(StringIO.StringIO()): + # exec_command.exec_command("cd '.'") |