summaryrefslogtreecommitdiff
path: root/numpy/distutils/tests/test_exec_command.py
diff options
context:
space:
mode:
authorJulian Taylor <jtaylor.debian@googlemail.com>2017-03-24 21:55:11 +0100
committerJulian Taylor <jtaylor.debian@googlemail.com>2017-03-24 22:40:21 +0100
commitc29a68e5a5978a1e32fefb8776974f94ab343845 (patch)
treeefd566bdb29fa3a8afe347e2b043f620810ab102 /numpy/distutils/tests/test_exec_command.py
parentcd2c4d9857c8cc5936d0f1424ec4e9c7b5c2a6eb (diff)
downloadnumpy-c29a68e5a5978a1e32fefb8776974f94ab343845.tar.gz
TST: move exec_command tests out of the file itself
Diffstat (limited to 'numpy/distutils/tests/test_exec_command.py')
-rw-r--r--numpy/distutils/tests/test_exec_command.py127
1 files changed, 127 insertions, 0 deletions
diff --git a/numpy/distutils/tests/test_exec_command.py b/numpy/distutils/tests/test_exec_command.py
index 0931f749b..eccc47124 100644
--- a/numpy/distutils/tests/test_exec_command.py
+++ b/numpy/distutils/tests/test_exec_command.py
@@ -5,6 +5,8 @@ import sys
from tempfile import TemporaryFile
from numpy.distutils import exec_command
+from numpy.distutils.exec_command import get_pythonexe
+from numpy.testing import TestCase, run_module_suite, tempdir
# In python 3 stdout, stderr are text (unicode compliant) devices, so to
# emulate them import StringIO from the io module.
@@ -90,3 +92,128 @@ def test_exec_command_stderr():
with redirect_stdout(TemporaryFile()):
with redirect_stderr(StringIO()):
exec_command.exec_command("cd '.'")
+
+
+class TestExecCommand(TestCase):
+ def setUp(self):
+ self.pyexe = get_pythonexe()
+
+ def check_nt(self, **kws):
+ s, o = exec_command.exec_command('echo path=%path%')
+ self.assertEqual(s, 0)
+ self.assertNotEqual(o, '')
+
+ s, o = exec_command.exec_command(
+ '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'win32')
+
+ def check_posix(self, **kws):
+ s, o = exec_command.exec_command("echo Hello", **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'Hello')
+
+ s, o = exec_command.exec_command('echo $AAA', **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, '')
+
+ s, o = exec_command.exec_command('echo "$AAA"', AAA='Tere', **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'Tere')
+
+ s, o = exec_command.exec_command('echo "$AAA"', **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, '')
+
+ if 'BBB' not in os.environ:
+ os.environ['BBB'] = 'Hi'
+ s, o = exec_command.exec_command('echo "$BBB"', **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'Hi')
+
+ s, o = exec_command.exec_command('echo "$BBB"', BBB='Hey', **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'Hey')
+
+ s, o = exec_command.exec_command('echo "$BBB"', **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'Hi')
+
+ del os.environ['BBB']
+
+ s, o = exec_command.exec_command('echo "$BBB"', **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, '')
+
+
+ s, o = exec_command.exec_command('this_is_not_a_command', **kws)
+ self.assertNotEqual(s, 0)
+ self.assertNotEqual(o, '')
+
+ s, o = exec_command.exec_command('echo path=$PATH', **kws)
+ self.assertEqual(s, 0)
+ self.assertNotEqual(o, '')
+
+ s, o = exec_command.exec_command(
+ '"%s" -c "import sys,os;sys.stderr.write(os.name)"' %
+ self.pyexe, **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'posix')
+
+ def check_basic(self, *kws):
+ s, o = exec_command.exec_command(
+ '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws)
+ self.assertNotEqual(s, 0)
+ self.assertNotEqual(o, '')
+
+ s, o = exec_command.exec_command(
+ '"%s" -c "import sys;sys.stderr.write(\'0\');'
+ 'sys.stderr.write(\'1\');sys.stderr.write(\'2\')"' %
+ self.pyexe, **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, '012')
+
+ s, o = exec_command.exec_command(
+ '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws)
+ self.assertEqual(s, 15)
+ self.assertEqual(o, '')
+
+ s, o = exec_command.exec_command(
+ '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'Heipa')
+
+ def check_execute_in(self, **kws):
+ with tempdir() as tmpdir:
+ fn = "file"
+ tmpfile = os.path.join(tmpdir, fn)
+ f = open(tmpfile, 'w')
+ f.write('Hello')
+ f.close()
+
+ s, o = exec_command.exec_command(
+ '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' %
+ (self.pyexe, fn), **kws)
+ self.assertNotEqual(s, 0)
+ self.assertNotEqual(o, '')
+ s, o = exec_command.exec_command(
+ '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); '
+ 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws)
+ self.assertEqual(s, 0)
+ self.assertEqual(o, 'Hello')
+
+ def test_basic(self):
+ with redirect_stdout(StringIO()):
+ with redirect_stderr(StringIO()):
+ if os.name == "posix":
+ self.check_posix(use_tee=0)
+ self.check_posix(use_tee=1)
+ elif os.name == "nt":
+ self.check_nt(use_tee=0)
+ self.check_nt(use_tee=1)
+ self.check_execute_in(use_tee=0)
+ self.check_execute_in(use_tee=1)
+
+
+if __name__ == "__main__":
+ run_module_suite()