test_process.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # encoding: utf-8
  2. """
  3. Tests for platutils.py
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2008-2011 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. import sys
  15. import os
  16. from unittest import TestCase
  17. import nose.tools as nt
  18. from IPython.utils.process import (find_cmd, FindCmdError, arg_split,
  19. system, getoutput, getoutputerror,
  20. get_output_error_code)
  21. from IPython.testing import decorators as dec
  22. from IPython.testing import tools as tt
  23. python = os.path.basename(sys.executable)
  24. #-----------------------------------------------------------------------------
  25. # Tests
  26. #-----------------------------------------------------------------------------
  27. @dec.skip_win32
  28. def test_find_cmd_ls():
  29. """Make sure we can find the full path to ls."""
  30. path = find_cmd('ls')
  31. nt.assert_true(path.endswith('ls'))
  32. def has_pywin32():
  33. try:
  34. import win32api
  35. except ImportError:
  36. return False
  37. return True
  38. @dec.onlyif(has_pywin32, "This test requires win32api to run")
  39. def test_find_cmd_pythonw():
  40. """Try to find pythonw on Windows."""
  41. path = find_cmd('pythonw')
  42. assert path.lower().endswith('pythonw.exe'), path
  43. @dec.onlyif(lambda : sys.platform != 'win32' or has_pywin32(),
  44. "This test runs on posix or in win32 with win32api installed")
  45. def test_find_cmd_fail():
  46. """Make sure that FindCmdError is raised if we can't find the cmd."""
  47. nt.assert_raises(FindCmdError,find_cmd,'asdfasdf')
  48. @dec.skip_win32
  49. def test_arg_split():
  50. """Ensure that argument lines are correctly split like in a shell."""
  51. tests = [['hi', ['hi']],
  52. [u'hi', [u'hi']],
  53. ['hello there', ['hello', 'there']],
  54. # \u01ce == \N{LATIN SMALL LETTER A WITH CARON}
  55. # Do not use \N because the tests crash with syntax error in
  56. # some cases, for example windows python2.6.
  57. [u'h\u01cello', [u'h\u01cello']],
  58. ['something "with quotes"', ['something', '"with quotes"']],
  59. ]
  60. for argstr, argv in tests:
  61. nt.assert_equal(arg_split(argstr), argv)
  62. @dec.skip_if_not_win32
  63. def test_arg_split_win32():
  64. """Ensure that argument lines are correctly split like in a shell."""
  65. tests = [['hi', ['hi']],
  66. [u'hi', [u'hi']],
  67. ['hello there', ['hello', 'there']],
  68. [u'h\u01cello', [u'h\u01cello']],
  69. ['something "with quotes"', ['something', 'with quotes']],
  70. ]
  71. for argstr, argv in tests:
  72. nt.assert_equal(arg_split(argstr), argv)
  73. class SubProcessTestCase(TestCase, tt.TempFileMixin):
  74. def setUp(self):
  75. """Make a valid python temp file."""
  76. lines = ["from __future__ import print_function",
  77. "import sys",
  78. "print('on stdout', end='', file=sys.stdout)",
  79. "print('on stderr', end='', file=sys.stderr)",
  80. "sys.stdout.flush()",
  81. "sys.stderr.flush()"]
  82. self.mktmp('\n'.join(lines))
  83. def test_system(self):
  84. status = system('%s "%s"' % (python, self.fname))
  85. self.assertEqual(status, 0)
  86. def test_system_quotes(self):
  87. status = system('%s -c "import sys"' % python)
  88. self.assertEqual(status, 0)
  89. def test_getoutput(self):
  90. out = getoutput('%s "%s"' % (python, self.fname))
  91. # we can't rely on the order the line buffered streams are flushed
  92. try:
  93. self.assertEqual(out, 'on stderron stdout')
  94. except AssertionError:
  95. self.assertEqual(out, 'on stdouton stderr')
  96. def test_getoutput_quoted(self):
  97. out = getoutput('%s -c "print (1)"' % python)
  98. self.assertEqual(out.strip(), '1')
  99. #Invalid quoting on windows
  100. @dec.skip_win32
  101. def test_getoutput_quoted2(self):
  102. out = getoutput("%s -c 'print (1)'" % python)
  103. self.assertEqual(out.strip(), '1')
  104. out = getoutput("%s -c 'print (\"1\")'" % python)
  105. self.assertEqual(out.strip(), '1')
  106. def test_getoutput_error(self):
  107. out, err = getoutputerror('%s "%s"' % (python, self.fname))
  108. self.assertEqual(out, 'on stdout')
  109. self.assertEqual(err, 'on stderr')
  110. def test_get_output_error_code(self):
  111. quiet_exit = '%s -c "import sys; sys.exit(1)"' % python
  112. out, err, code = get_output_error_code(quiet_exit)
  113. self.assertEqual(out, '')
  114. self.assertEqual(err, '')
  115. self.assertEqual(code, 1)
  116. out, err, code = get_output_error_code('%s "%s"' % (python, self.fname))
  117. self.assertEqual(out, 'on stdout')
  118. self.assertEqual(err, 'on stderr')
  119. self.assertEqual(code, 0)