Show More
@@ -1,124 +1,128 | |||||
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 | from unittest import TestCase |
|
18 | from unittest import TestCase | |
19 |
|
19 | |||
20 | import nose.tools as nt |
|
20 | import nose.tools as nt | |
21 |
|
21 | |||
22 | from IPython.utils.process import (find_cmd, FindCmdError, arg_split, |
|
22 | from IPython.utils.process import (find_cmd, FindCmdError, arg_split, | |
23 | system, getoutput, getoutputerror) |
|
23 | system, getoutput, getoutputerror) | |
24 | from IPython.testing import decorators as dec |
|
24 | from IPython.testing import decorators as dec | |
25 | from IPython.testing import tools as tt |
|
25 | from IPython.testing import tools as tt | |
26 |
|
26 | |||
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 | # Tests |
|
28 | # Tests | |
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 |
|
30 | |||
31 | def test_find_cmd_python(): |
|
31 | def test_find_cmd_python(): | |
32 | """Make sure we find sys.exectable for python.""" |
|
32 | """Make sure we find sys.exectable for python.""" | |
33 | nt.assert_equals(find_cmd('python'), sys.executable) |
|
33 | nt.assert_equals(find_cmd('python'), sys.executable) | |
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 | nt.assert_true(path.endswith('pythonw.exe')) |
|
55 | nt.assert_true(path.endswith('pythonw.exe')) | |
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 | # [u'h\N{LATIN SMALL LETTER A WITH CARON}llo', [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']], |
|
71 | # [u'h\N{LATIN SMALL LETTER A WITH CARON}llo', [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']], | |
72 | ['something "with quotes"', ['something', '"with quotes"']], |
|
72 | ['something "with quotes"', ['something', '"with quotes"']], | |
73 | ] |
|
73 | ] | |
74 | for argstr, argv in tests: |
|
74 | for argstr, argv in tests: | |
75 | nt.assert_equal(arg_split(argstr), argv) |
|
75 | nt.assert_equal(arg_split(argstr), argv) | |
76 |
|
76 | |||
77 | @dec.skip_if_not_win32 |
|
77 | @dec.skip_if_not_win32 | |
78 | def test_arg_split_win32(): |
|
78 | def test_arg_split_win32(): | |
79 | """Ensure that argument lines are correctly split like in a shell.""" |
|
79 | """Ensure that argument lines are correctly split like in a shell.""" | |
80 | tests = [['hi', ['hi']], |
|
80 | tests = [['hi', ['hi']], | |
81 | [u'hi', [u'hi']], |
|
81 | [u'hi', [u'hi']], | |
82 | ['hello there', ['hello', 'there']], |
|
82 | ['hello there', ['hello', 'there']], | |
83 | # [u'h\N{LATIN SMALL LETTER A WITH CARON}llo', [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']], |
|
83 | # [u'h\N{LATIN SMALL LETTER A WITH CARON}llo', [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']], | |
84 | ['something "with quotes"', ['something', 'with quotes']], |
|
84 | ['something "with quotes"', ['something', 'with quotes']], | |
85 | ] |
|
85 | ] | |
86 | for argstr, argv in tests: |
|
86 | for argstr, argv in tests: | |
87 | nt.assert_equal(arg_split(argstr), argv) |
|
87 | nt.assert_equal(arg_split(argstr), argv) | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | class SubProcessTestCase(TestCase, tt.TempFileMixin): |
|
90 | class SubProcessTestCase(TestCase, tt.TempFileMixin): | |
91 | def setUp(self): |
|
91 | def setUp(self): | |
92 | """Make a valid python temp file.""" |
|
92 | """Make a valid python temp file.""" | |
93 | lines = ["from __future__ import print_function", |
|
93 | lines = ["from __future__ import print_function", | |
94 | "import sys", |
|
94 | "import sys", | |
95 | "print('on stdout', end='', file=sys.stdout)", |
|
95 | "print('on stdout', end='', file=sys.stdout)", | |
96 | "print('on stderr', end='', file=sys.stderr)", |
|
96 | "print('on stderr', end='', file=sys.stderr)", | |
97 | "sys.stdout.flush()", |
|
97 | "sys.stdout.flush()", | |
98 | "sys.stderr.flush()"] |
|
98 | "sys.stderr.flush()"] | |
99 | self.mktmp('\n'.join(lines)) |
|
99 | self.mktmp('\n'.join(lines)) | |
100 |
|
100 | |||
101 | def test_system(self): |
|
101 | def test_system(self): | |
102 | status = system('python "%s"' % self.fname) |
|
102 | status = system('python "%s"' % self.fname) | |
103 | self.assertEquals(status, 0) |
|
103 | self.assertEquals(status, 0) | |
104 |
|
104 | |||
105 | def test_system_quotes(self): |
|
105 | def test_system_quotes(self): | |
106 | status = system('python -c "import sys"') |
|
106 | status = system('python -c "import sys"') | |
107 | self.assertEquals(status, 0) |
|
107 | self.assertEquals(status, 0) | |
108 |
|
108 | |||
109 | def test_getoutput(self): |
|
109 | def test_getoutput(self): | |
110 | out = getoutput('python "%s"' % self.fname) |
|
110 | out = getoutput('python "%s"' % self.fname) | |
111 | self.assertEquals(out, 'on stdout') |
|
111 | self.assertEquals(out, 'on stdout') | |
112 |
|
112 | |||
113 | def test_getoutput_quoted(self): |
|
113 | def test_getoutput_quoted(self): | |
114 | out = getoutput('python -c "print (1)"') |
|
114 | out = getoutput('python -c "print (1)"') | |
115 | self.assertEquals(out.strip(), '1') |
|
115 | self.assertEquals(out.strip(), '1') | |
|
116 | ||||
|
117 | #Invalid quoting on windows | |||
|
118 | @dec.skip_win32 | |||
|
119 | def test_getoutput_quoted2(self): | |||
116 | out = getoutput("python -c 'print (1)'") |
|
120 | out = getoutput("python -c 'print (1)'") | |
117 | self.assertEquals(out.strip(), '1') |
|
121 | self.assertEquals(out.strip(), '1') | |
118 | out = getoutput("python -c 'print (\"1\")'") |
|
122 | out = getoutput("python -c 'print (\"1\")'") | |
119 | self.assertEquals(out.strip(), '1') |
|
123 | self.assertEquals(out.strip(), '1') | |
120 |
|
124 | |||
121 | def test_getoutput(self): |
|
125 | def test_getoutput(self): | |
122 | out, err = getoutputerror('python "%s"' % self.fname) |
|
126 | out, err = getoutputerror('python "%s"' % self.fname) | |
123 | self.assertEquals(out, 'on stdout') |
|
127 | self.assertEquals(out, 'on stdout') | |
124 | self.assertEquals(err, 'on stderr') |
|
128 | self.assertEquals(err, 'on stderr') |
General Comments 0
You need to be logged in to leave comments.
Login now