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