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