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