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