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