##// END OF EJS Templates
Is it not in main thread?
Itamar Turner-Trauring -
Show More
@@ -1,173 +1,173 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 18 import os
19 19 import time
20 20 from _thread import interrupt_main # Py 3
21 21 import threading
22 22
23 23 import nose.tools as nt
24 24
25 25 from IPython.utils.process import (find_cmd, FindCmdError, arg_split,
26 26 system, getoutput, getoutputerror,
27 27 get_output_error_code)
28 28 from IPython.testing import decorators as dec
29 29 from IPython.testing import tools as tt
30 30
31 31 python = os.path.basename(sys.executable)
32 32
33 33 #-----------------------------------------------------------------------------
34 34 # Tests
35 35 #-----------------------------------------------------------------------------
36 36
37 37
38 38 @dec.skip_win32
39 39 def test_find_cmd_ls():
40 40 """Make sure we can find the full path to ls."""
41 41 path = find_cmd('ls')
42 42 nt.assert_true(path.endswith('ls'))
43 43
44 44
45 45 def has_pywin32():
46 46 try:
47 47 import win32api
48 48 except ImportError:
49 49 return False
50 50 return True
51 51
52 52
53 53 @dec.onlyif(has_pywin32, "This test requires win32api to run")
54 54 def test_find_cmd_pythonw():
55 55 """Try to find pythonw on Windows."""
56 56 path = find_cmd('pythonw')
57 57 assert path.lower().endswith('pythonw.exe'), path
58 58
59 59
60 60 @dec.onlyif(lambda : sys.platform != 'win32' or has_pywin32(),
61 61 "This test runs on posix or in win32 with win32api installed")
62 62 def test_find_cmd_fail():
63 63 """Make sure that FindCmdError is raised if we can't find the cmd."""
64 64 nt.assert_raises(FindCmdError,find_cmd,'asdfasdf')
65 65
66 66
67 67 @dec.skip_win32
68 68 def test_arg_split():
69 69 """Ensure that argument lines are correctly split like in a shell."""
70 70 tests = [['hi', ['hi']],
71 71 [u'hi', [u'hi']],
72 72 ['hello there', ['hello', 'there']],
73 73 # \u01ce == \N{LATIN SMALL LETTER A WITH CARON}
74 74 # Do not use \N because the tests crash with syntax error in
75 75 # some cases, for example windows python2.6.
76 76 [u'h\u01cello', [u'h\u01cello']],
77 77 ['something "with quotes"', ['something', '"with quotes"']],
78 78 ]
79 79 for argstr, argv in tests:
80 80 nt.assert_equal(arg_split(argstr), argv)
81 81
82 82 @dec.skip_if_not_win32
83 83 def test_arg_split_win32():
84 84 """Ensure that argument lines are correctly split like in a shell."""
85 85 tests = [['hi', ['hi']],
86 86 [u'hi', [u'hi']],
87 87 ['hello there', ['hello', 'there']],
88 88 [u'h\u01cello', [u'h\u01cello']],
89 89 ['something "with quotes"', ['something', 'with quotes']],
90 90 ]
91 91 for argstr, argv in tests:
92 92 nt.assert_equal(arg_split(argstr), argv)
93 93
94 94
95 95 class SubProcessTestCase(tt.TempFileMixin):
96 96 def setUp(self):
97 97 """Make a valid python temp file."""
98 98 lines = [ "import sys",
99 99 "print('on stdout', end='', file=sys.stdout)",
100 100 "print('on stderr', end='', file=sys.stderr)",
101 101 "sys.stdout.flush()",
102 102 "sys.stderr.flush()"]
103 103 self.mktmp('\n'.join(lines))
104 104
105 105 def test_system(self):
106 106 status = system('%s "%s"' % (python, self.fname))
107 107 self.assertEqual(status, 0)
108 108
109 109 def test_system_quotes(self):
110 110 status = system('%s -c "import sys"' % python)
111 111 self.assertEqual(status, 0)
112 112
113 113 def test_system_interrupt(self):
114 114 """
115 115 When interrupted in the way ipykernel interrupts IPython, the
116 116 subprocess is interrupted.
117 117 """
118 raise RuntimeError("Is this even being run on Windows?")
119 118 if threading.main_thread() != threading.current_thread():
119 raise RuntimeEror("Not in main thread")
120 120 raise nt.SkipTest("Can't run this test if not in main thread.")
121 121
122 122 def interrupt():
123 123 # Wait for subprocess to start:
124 124 time.sleep(0.5)
125 125 interrupt_main()
126 126
127 127 threading.Thread(target=interrupt).start()
128 128 try:
129 129 status = system('%s -c "import time; time.sleep(5)"' % python)
130 130 except KeyboardInterrupt:
131 131 # Success!
132 132 return
133 133 self.assertNotEqual(
134 134 status, 0, "The process wasn't interrupted. Status: %s" % (status,)
135 135 )
136 136
137 137 def test_getoutput(self):
138 138 out = getoutput('%s "%s"' % (python, self.fname))
139 139 # we can't rely on the order the line buffered streams are flushed
140 140 try:
141 141 self.assertEqual(out, 'on stderron stdout')
142 142 except AssertionError:
143 143 self.assertEqual(out, 'on stdouton stderr')
144 144
145 145 def test_getoutput_quoted(self):
146 146 out = getoutput('%s -c "print (1)"' % python)
147 147 self.assertEqual(out.strip(), '1')
148 148
149 149 #Invalid quoting on windows
150 150 @dec.skip_win32
151 151 def test_getoutput_quoted2(self):
152 152 out = getoutput("%s -c 'print (1)'" % python)
153 153 self.assertEqual(out.strip(), '1')
154 154 out = getoutput("%s -c 'print (\"1\")'" % python)
155 155 self.assertEqual(out.strip(), '1')
156 156
157 157 def test_getoutput_error(self):
158 158 out, err = getoutputerror('%s "%s"' % (python, self.fname))
159 159 self.assertEqual(out, 'on stdout')
160 160 self.assertEqual(err, 'on stderr')
161 161
162 162 def test_get_output_error_code(self):
163 163 quiet_exit = '%s -c "import sys; sys.exit(1)"' % python
164 164 out, err, code = get_output_error_code(quiet_exit)
165 165 self.assertEqual(out, '')
166 166 self.assertEqual(err, '')
167 167 self.assertEqual(code, 1)
168 168 out, err, code = get_output_error_code('%s "%s"' % (python, self.fname))
169 169 self.assertEqual(out, 'on stdout')
170 170 self.assertEqual(err, 'on stderr')
171 171 self.assertEqual(code, 0)
172 172
173 173
General Comments 0
You need to be logged in to leave comments. Login now