1 """Basic tests for os.popen()
2
3 Particularly useful for platforms that fake popen.
4 """
5
6 import unittest
7 from test import support
8 import os, sys
9
10 if not hasattr(os, 'popen'):
11 raise unittest.SkipTest("need os.popen()")
12
13 # Test that command-lines get down as we expect.
14 # To do this we execute:
15 # python -c "import sys;print(sys.argv)" {rest_of_commandline}
16 # This results in Python being spawned and printing the sys.argv list.
17 # We can then eval() the result of this, and see what each argv was.
18 python = sys.executable
19 if ' ' in python:
20 python = '"' + python + '"' # quote embedded space for cmdline
21
22 @support.requires_subprocess()
23 class ESC[4;38;5;81mPopenTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
24
25 def _do_test_commandline(self, cmdline, expected):
26 cmd = '%s -c "import sys; print(sys.argv)" %s'
27 cmd = cmd % (python, cmdline)
28 with os.popen(cmd) as p:
29 data = p.read()
30 got = eval(data)[1:] # strip off argv[0]
31 self.assertEqual(got, expected)
32
33 def test_popen(self):
34 self.assertRaises(TypeError, os.popen)
35 self._do_test_commandline(
36 "foo bar",
37 ["foo", "bar"]
38 )
39 self._do_test_commandline(
40 'foo "spam and eggs" "silly walk"',
41 ["foo", "spam and eggs", "silly walk"]
42 )
43 self._do_test_commandline(
44 'foo "a \\"quoted\\" arg" bar',
45 ["foo", 'a "quoted" arg', "bar"]
46 )
47 support.reap_children()
48
49 def test_return_code(self):
50 self.assertEqual(os.popen("exit 0").close(), None)
51 status = os.popen("exit 42").close()
52 if os.name == 'nt':
53 self.assertEqual(status, 42)
54 else:
55 self.assertEqual(os.waitstatus_to_exitcode(status), 42)
56
57 def test_contextmanager(self):
58 with os.popen("echo hello") as f:
59 self.assertEqual(f.read(), "hello\n")
60
61 def test_iterating(self):
62 with os.popen("echo hello") as f:
63 self.assertEqual(list(f), ["hello\n"])
64
65 def test_keywords(self):
66 with os.popen(cmd="exit 0", mode="w", buffering=-1):
67 pass
68
69 if __name__ == "__main__":
70 unittest.main()