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