2 """Basic tests for os.popen()
4 Particularly useful for platforms that fake popen.
8 from test
import test_support
11 # Test that command-lines get down as we expect.
12 # To do this we execute:
13 # python -c "import sys;print sys.argv" {rest_of_commandline}
14 # This results in Python being spawned and printing the sys.argv list.
15 # We can then eval() the result of this, and see what each argv was.
16 python
= sys
.executable
18 class PopenTest(unittest
.TestCase
):
19 def _do_test_commandline(self
, cmdline
, expected
):
20 cmd
= '%s -c "import sys;print sys.argv" %s' % (python
, cmdline
)
21 data
= os
.popen(cmd
).read() + '\n'
22 got
= eval(data
)[1:] # strip off argv[0]
23 self
.assertEqual(got
, expected
)
26 self
.assertRaises(TypeError, os
.popen
)
27 self
._do
_test
_commandline
(
31 self
._do
_test
_commandline
(
32 'foo "spam and eggs" "silly walk"',
33 ["foo", "spam and eggs", "silly walk"]
35 self
._do
_test
_commandline
(
36 'foo "a \\"quoted\\" arg" bar',
37 ["foo", 'a "quoted" arg', "bar"]
39 test_support
.reap_children()
41 def test_return_code(self
):
42 self
.assertEqual(os
.popen("exit 0").close(), None)
44 self
.assertEqual(os
.popen("exit 42").close(), 42)
46 self
.assertEqual(os
.popen("exit 42").close(), 42 << 8)
49 test_support
.run_unittest(PopenTest
)
51 if __name__
== "__main__":