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