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