Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 2 | """Basic tests for os.popen() |
| 3 | |
| 4 | Particularly useful for platforms that fake popen. |
| 5 | """ |
| 6 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 7 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test import support |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 9 | import os, sys |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 10 | |
| 11 | # Test that command-lines get down as we expect. |
| 12 | # To do this we execute: |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 13 | # python -c "import sys;print(sys.argv)" {rest_of_commandline} |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 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. |
Tim Peters | 61cd0db | 2003-03-07 21:10:21 +0000 | [diff] [blame] | 16 | python = sys.executable |
| 17 | if ' ' in python: |
| 18 | python = '"' + python + '"' # quote embedded space for cmdline |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 19 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 20 | class PopenTest(unittest.TestCase): |
Guido van Rossum | aa588c4 | 2007-06-15 03:35:38 +0000 | [diff] [blame] | 21 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 22 | def _do_test_commandline(self, cmdline, expected): |
Guido van Rossum | 96ca691 | 2007-06-15 03:49:03 +0000 | [diff] [blame] | 23 | cmd = '%s -c "import sys; print(sys.argv)" %s' |
Guido van Rossum | aa588c4 | 2007-06-15 03:35:38 +0000 | [diff] [blame] | 24 | cmd = cmd % (python, cmdline) |
Benjamin Peterson | aa7cec0 | 2010-10-31 01:35:53 +0000 | [diff] [blame] | 25 | with os.popen(cmd) as p: |
| 26 | data = p.read() |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 27 | got = eval(data)[1:] # strip off argv[0] |
| 28 | self.assertEqual(got, expected) |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 29 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 30 | def test_popen(self): |
| 31 | self.assertRaises(TypeError, os.popen) |
| 32 | self._do_test_commandline( |
| 33 | "foo bar", |
| 34 | ["foo", "bar"] |
| 35 | ) |
| 36 | self._do_test_commandline( |
| 37 | 'foo "spam and eggs" "silly walk"', |
| 38 | ["foo", "spam and eggs", "silly walk"] |
| 39 | ) |
| 40 | self._do_test_commandline( |
| 41 | 'foo "a \\"quoted\\" arg" bar', |
| 42 | ["foo", 'a "quoted" arg', "bar"] |
| 43 | ) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 44 | support.reap_children() |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 45 | |
Amaury Forgeot d'Arc | 97e5f28 | 2009-07-11 09:35:13 +0000 | [diff] [blame] | 46 | def test_return_code(self): |
| 47 | self.assertEqual(os.popen("exit 0").close(), None) |
| 48 | if os.name == 'nt': |
| 49 | self.assertEqual(os.popen("exit 42").close(), 42) |
| 50 | else: |
| 51 | self.assertEqual(os.popen("exit 42").close(), 42 << 8) |
| 52 | |
Antoine Pitrou | ac62535 | 2009-12-09 00:01:27 +0000 | [diff] [blame] | 53 | def test_contextmanager(self): |
| 54 | with os.popen("echo hello") as f: |
| 55 | self.assertEqual(f.read(), "hello\n") |
| 56 | |
| 57 | def test_iterating(self): |
| 58 | with os.popen("echo hello") as f: |
| 59 | self.assertEqual(list(f), ["hello\n"]) |
| 60 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 61 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 62 | support.run_unittest(PopenTest) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 63 | |
| 64 | if __name__ == "__main__": |
| 65 | test_main() |