Show More
@@ -1,111 +1,111 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-2009 The IPython Development Team |
|
7 | # Copyright (C) 2008-2009 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 | def test_arg_split(): |
|
65 | def test_arg_split(): | |
66 | """Ensure that argument lines are correctly split like in a shell.""" |
|
66 | """Ensure that argument lines are correctly split like in a shell.""" | |
67 | tests = [['hi', ['hi']], |
|
67 | tests = [['hi', ['hi']], | |
68 | [u'hi', [u'hi']], |
|
68 | [u'hi', [u'hi']], | |
69 | ['hello there', ['hello', 'there']], |
|
69 | ['hello there', ['hello', 'there']], | |
70 | [u'h\N{LATIN SMALL LETTER A WITH CARON}llo', [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']], |
|
70 | [u'h\N{LATIN SMALL LETTER A WITH CARON}llo', [u'h\N{LATIN SMALL LETTER A WITH CARON}llo']], | |
71 | ['something "with quotes"', ['something', '"with quotes"']], |
|
71 | ['something "with quotes"', ['something', '"with quotes"']], | |
72 | ] |
|
72 | ] | |
73 | for argstr, argv in tests: |
|
73 | for argstr, argv in tests: | |
74 | nt.assert_equal(arg_split(argstr), argv) |
|
74 | nt.assert_equal(arg_split(argstr), argv) | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | class SubProcessTestCase(TestCase, tt.TempFileMixin): |
|
77 | class SubProcessTestCase(TestCase, tt.TempFileMixin): | |
78 | def setUp(self): |
|
78 | def setUp(self): | |
79 | """Make a valid python temp file.""" |
|
79 | """Make a valid python temp file.""" | |
80 | lines = ["from __future__ import print_function", |
|
80 | lines = ["from __future__ import print_function", | |
81 | "import sys", |
|
81 | "import sys", | |
82 | "print('on stdout', end='', file=sys.stdout)", |
|
82 | "print('on stdout', end='', file=sys.stdout)", | |
83 | "print('on stderr', end='', file=sys.stderr)", |
|
83 | "print('on stderr', end='', file=sys.stderr)", | |
84 | "sys.stdout.flush()", |
|
84 | "sys.stdout.flush()", | |
85 | "sys.stderr.flush()"] |
|
85 | "sys.stderr.flush()"] | |
86 | self.mktmp('\n'.join(lines)) |
|
86 | self.mktmp('\n'.join(lines)) | |
87 |
|
87 | |||
88 | def test_system(self): |
|
88 | def test_system(self): | |
89 | status = system('python "%s"' % self.fname) |
|
89 | status = system('python "%s"' % self.fname) | |
90 | self.assertEquals(status, 0) |
|
90 | self.assertEquals(status, 0) | |
91 |
|
91 | |||
92 | def test_system_quotes(self): |
|
92 | def test_system_quotes(self): | |
93 | status = system('python -c "import sys"') |
|
93 | status = system('python -c "import sys"') | |
94 | self.assertEquals(status, 0) |
|
94 | self.assertEquals(status, 0) | |
95 |
|
95 | |||
96 | def test_getoutput(self): |
|
96 | def test_getoutput(self): | |
97 | out = getoutput('python "%s"' % self.fname) |
|
97 | out = getoutput('python "%s"' % self.fname) | |
98 | self.assertEquals(out, 'on stdout') |
|
98 | self.assertEquals(out, 'on stdout') | |
99 |
|
99 | |||
100 | def test_getoutput_quoted(self): |
|
100 | def test_getoutput_quoted(self): | |
101 | out = getoutput('python -c "print 1"') |
|
101 | out = getoutput('python -c "print (1)"') | |
102 | self.assertEquals(out.strip(), '1') |
|
102 | self.assertEquals(out.strip(), '1') | |
103 | out = getoutput("python -c 'print 1'") |
|
103 | out = getoutput("python -c 'print (1)'") | |
104 | self.assertEquals(out.strip(), '1') |
|
104 | self.assertEquals(out.strip(), '1') | |
105 | out = getoutput("python -c 'print \"1\"'") |
|
105 | out = getoutput("python -c 'print (\"1\")'") | |
106 | self.assertEquals(out.strip(), '1') |
|
106 | self.assertEquals(out.strip(), '1') | |
107 |
|
107 | |||
108 | def test_getoutput(self): |
|
108 | def test_getoutput(self): | |
109 | out, err = getoutputerror('python "%s"' % self.fname) |
|
109 | out, err = getoutputerror('python "%s"' % self.fname) | |
110 | self.assertEquals(out, 'on stdout') |
|
110 | self.assertEquals(out, 'on stdout') | |
111 | self.assertEquals(err, 'on stderr') |
|
111 | self.assertEquals(err, 'on stderr') |
General Comments 0
You need to be logged in to leave comments.
Login now