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