Show More
@@ -103,9 +103,7 def process_handler(cmd, callback, stderr=subprocess.PIPE): | |||||
103 |
|
103 | |||
104 |
|
104 | |||
105 | def getoutput(cmd): |
|
105 | def getoutput(cmd): | |
106 | """Return standard output of executing cmd in a shell. |
|
106 | """Run a command and return its stdout/stderr as a string. | |
107 |
|
||||
108 | Accepts the same arguments as os.system(). |
|
|||
109 |
|
107 | |||
110 | Parameters |
|
108 | Parameters | |
111 | ---------- |
|
109 | ---------- | |
@@ -114,9 +112,12 def getoutput(cmd): | |||||
114 |
|
112 | |||
115 | Returns |
|
113 | Returns | |
116 | ------- |
|
114 | ------- | |
117 |
|
|
115 | output : str | |
|
116 | A string containing the combination of stdout and stderr from the | |||
|
117 | subprocess, in whatever order the subprocess originally wrote to its | |||
|
118 | file descriptors (so the order of the information in this string is the | |||
|
119 | correct order as would be seen if running the command in a terminal). | |||
118 | """ |
|
120 | """ | |
119 |
|
||||
120 | out = process_handler(cmd, lambda p: p.communicate()[0], subprocess.STDOUT) |
|
121 | out = process_handler(cmd, lambda p: p.communicate()[0], subprocess.STDOUT) | |
121 | if out is None: |
|
122 | if out is None: | |
122 | return '' |
|
123 | return '' |
@@ -15,6 +15,7 Tests for platutils.py | |||||
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 | |
@@ -24,13 +25,15 from IPython.utils.process import (find_cmd, FindCmdError, arg_split, | |||||
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 | |
@@ -102,30 +105,34 class SubProcessTestCase(TestCase, tt.TempFileMixin): | |||||
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 | self.assertEqual(out, 'on stdout') |
|
117 | # we can't rely on the order the line buffered streams are flushed | |
|
118 | try: | |||
|
119 | self.assertEqual(out, 'on stderron stdout') | |||
|
120 | except AssertionError: | |||
|
121 | self.assertEqual(out, 'on stdouton stderr') | |||
115 |
|
122 | |||
116 | def test_getoutput_quoted(self): |
|
123 | def test_getoutput_quoted(self): | |
117 |
out = getoutput(' |
|
124 | out = getoutput('%s -c "print (1)"' % python) | |
118 | self.assertEqual(out.strip(), '1') |
|
125 | self.assertEqual(out.strip(), '1') | |
119 |
|
126 | |||
120 | #Invalid quoting on windows |
|
127 | #Invalid quoting on windows | |
121 | @dec.skip_win32 |
|
128 | @dec.skip_win32 | |
122 | def test_getoutput_quoted2(self): |
|
129 | def test_getoutput_quoted2(self): | |
123 |
out = getoutput(" |
|
130 | out = getoutput("%s -c 'print (1)'" % python) | |
124 | self.assertEqual(out.strip(), '1') |
|
131 | self.assertEqual(out.strip(), '1') | |
125 |
out = getoutput(" |
|
132 | out = getoutput("%s -c 'print (\"1\")'" % python) | |
126 | self.assertEqual(out.strip(), '1') |
|
133 | self.assertEqual(out.strip(), '1') | |
127 |
|
134 | |||
128 | def test_getoutput(self): |
|
135 | def test_getoutput_error(self): | |
129 |
out, err = getoutputerror(' |
|
136 | out, err = getoutputerror('%s "%s"' % (python, self.fname)) | |
130 | self.assertEqual(out, 'on stdout') |
|
137 | self.assertEqual(out, 'on stdout') | |
131 | 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