ยปCore Development>Code coverage>Lib/test/test_popen.py

Python code coverage for Lib/test/test_popen.py

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