test_exec_command.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import os
  2. import pytest
  3. import sys
  4. from tempfile import TemporaryFile
  5. from numpy.distutils import exec_command
  6. from numpy.distutils.exec_command import get_pythonexe
  7. from numpy.testing import tempdir, assert_, assert_warns, IS_WASM
  8. # In python 3 stdout, stderr are text (unicode compliant) devices, so to
  9. # emulate them import StringIO from the io module.
  10. from io import StringIO
  11. class redirect_stdout:
  12. """Context manager to redirect stdout for exec_command test."""
  13. def __init__(self, stdout=None):
  14. self._stdout = stdout or sys.stdout
  15. def __enter__(self):
  16. self.old_stdout = sys.stdout
  17. sys.stdout = self._stdout
  18. def __exit__(self, exc_type, exc_value, traceback):
  19. self._stdout.flush()
  20. sys.stdout = self.old_stdout
  21. # note: closing sys.stdout won't close it.
  22. self._stdout.close()
  23. class redirect_stderr:
  24. """Context manager to redirect stderr for exec_command test."""
  25. def __init__(self, stderr=None):
  26. self._stderr = stderr or sys.stderr
  27. def __enter__(self):
  28. self.old_stderr = sys.stderr
  29. sys.stderr = self._stderr
  30. def __exit__(self, exc_type, exc_value, traceback):
  31. self._stderr.flush()
  32. sys.stderr = self.old_stderr
  33. # note: closing sys.stderr won't close it.
  34. self._stderr.close()
  35. class emulate_nonposix:
  36. """Context manager to emulate os.name != 'posix' """
  37. def __init__(self, osname='non-posix'):
  38. self._new_name = osname
  39. def __enter__(self):
  40. self._old_name = os.name
  41. os.name = self._new_name
  42. def __exit__(self, exc_type, exc_value, traceback):
  43. os.name = self._old_name
  44. def test_exec_command_stdout():
  45. # Regression test for gh-2999 and gh-2915.
  46. # There are several packages (nose, scipy.weave.inline, Sage inline
  47. # Fortran) that replace stdout, in which case it doesn't have a fileno
  48. # method. This is tested here, with a do-nothing command that fails if the
  49. # presence of fileno() is assumed in exec_command.
  50. # The code has a special case for posix systems, so if we are on posix test
  51. # both that the special case works and that the generic code works.
  52. # Test posix version:
  53. with redirect_stdout(StringIO()):
  54. with redirect_stderr(TemporaryFile()):
  55. with assert_warns(DeprecationWarning):
  56. exec_command.exec_command("cd '.'")
  57. if os.name == 'posix':
  58. # Test general (non-posix) version:
  59. with emulate_nonposix():
  60. with redirect_stdout(StringIO()):
  61. with redirect_stderr(TemporaryFile()):
  62. with assert_warns(DeprecationWarning):
  63. exec_command.exec_command("cd '.'")
  64. def test_exec_command_stderr():
  65. # Test posix version:
  66. with redirect_stdout(TemporaryFile(mode='w+')):
  67. with redirect_stderr(StringIO()):
  68. with assert_warns(DeprecationWarning):
  69. exec_command.exec_command("cd '.'")
  70. if os.name == 'posix':
  71. # Test general (non-posix) version:
  72. with emulate_nonposix():
  73. with redirect_stdout(TemporaryFile()):
  74. with redirect_stderr(StringIO()):
  75. with assert_warns(DeprecationWarning):
  76. exec_command.exec_command("cd '.'")
  77. @pytest.mark.skipif(IS_WASM, reason="Cannot start subprocess")
  78. class TestExecCommand:
  79. def setup_method(self):
  80. self.pyexe = get_pythonexe()
  81. def check_nt(self, **kws):
  82. s, o = exec_command.exec_command('cmd /C echo path=%path%')
  83. assert_(s == 0)
  84. assert_(o != '')
  85. s, o = exec_command.exec_command(
  86. '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe)
  87. assert_(s == 0)
  88. assert_(o == 'win32')
  89. def check_posix(self, **kws):
  90. s, o = exec_command.exec_command("echo Hello", **kws)
  91. assert_(s == 0)
  92. assert_(o == 'Hello')
  93. s, o = exec_command.exec_command('echo $AAA', **kws)
  94. assert_(s == 0)
  95. assert_(o == '')
  96. s, o = exec_command.exec_command('echo "$AAA"', AAA='Tere', **kws)
  97. assert_(s == 0)
  98. assert_(o == 'Tere')
  99. s, o = exec_command.exec_command('echo "$AAA"', **kws)
  100. assert_(s == 0)
  101. assert_(o == '')
  102. if 'BBB' not in os.environ:
  103. os.environ['BBB'] = 'Hi'
  104. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  105. assert_(s == 0)
  106. assert_(o == 'Hi')
  107. s, o = exec_command.exec_command('echo "$BBB"', BBB='Hey', **kws)
  108. assert_(s == 0)
  109. assert_(o == 'Hey')
  110. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  111. assert_(s == 0)
  112. assert_(o == 'Hi')
  113. del os.environ['BBB']
  114. s, o = exec_command.exec_command('echo "$BBB"', **kws)
  115. assert_(s == 0)
  116. assert_(o == '')
  117. s, o = exec_command.exec_command('this_is_not_a_command', **kws)
  118. assert_(s != 0)
  119. assert_(o != '')
  120. s, o = exec_command.exec_command('echo path=$PATH', **kws)
  121. assert_(s == 0)
  122. assert_(o != '')
  123. s, o = exec_command.exec_command(
  124. '"%s" -c "import sys,os;sys.stderr.write(os.name)"' %
  125. self.pyexe, **kws)
  126. assert_(s == 0)
  127. assert_(o == 'posix')
  128. def check_basic(self, *kws):
  129. s, o = exec_command.exec_command(
  130. '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws)
  131. assert_(s != 0)
  132. assert_(o != '')
  133. s, o = exec_command.exec_command(
  134. '"%s" -c "import sys;sys.stderr.write(\'0\');'
  135. 'sys.stderr.write(\'1\');sys.stderr.write(\'2\')"' %
  136. self.pyexe, **kws)
  137. assert_(s == 0)
  138. assert_(o == '012')
  139. s, o = exec_command.exec_command(
  140. '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws)
  141. assert_(s == 15)
  142. assert_(o == '')
  143. s, o = exec_command.exec_command(
  144. '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws)
  145. assert_(s == 0)
  146. assert_(o == 'Heipa')
  147. def check_execute_in(self, **kws):
  148. with tempdir() as tmpdir:
  149. fn = "file"
  150. tmpfile = os.path.join(tmpdir, fn)
  151. with open(tmpfile, 'w') as f:
  152. f.write('Hello')
  153. s, o = exec_command.exec_command(
  154. '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' %
  155. (self.pyexe, fn), **kws)
  156. assert_(s != 0)
  157. assert_(o != '')
  158. s, o = exec_command.exec_command(
  159. '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); '
  160. 'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws)
  161. assert_(s == 0)
  162. assert_(o == 'Hello')
  163. def test_basic(self):
  164. with redirect_stdout(StringIO()):
  165. with redirect_stderr(StringIO()):
  166. with assert_warns(DeprecationWarning):
  167. if os.name == "posix":
  168. self.check_posix(use_tee=0)
  169. self.check_posix(use_tee=1)
  170. elif os.name == "nt":
  171. self.check_nt(use_tee=0)
  172. self.check_nt(use_tee=1)
  173. self.check_execute_in(use_tee=0)
  174. self.check_execute_in(use_tee=1)