blob: da01a878fa0a5ae7348d10a6c72d8962ba81853f [file] [log] [blame]
Mark Hammonde7fefbf2002-04-03 01:47:00 +00001"""Basic tests for os.popen()
2
3 Particularly useful for platforms that fake popen.
4"""
5
Thomas Woutersb2137042007-02-01 18:02:27 +00006import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Thomas Woutersb2137042007-02-01 18:02:27 +00008import os, sys
Mark Hammonde7fefbf2002-04-03 01:47:00 +00009
10# Test that command-lines get down as we expect.
11# To do this we execute:
Neal Norwitz752abd02008-05-13 04:55:24 +000012# python -c "import sys;print(sys.argv)" {rest_of_commandline}
Mark Hammonde7fefbf2002-04-03 01:47:00 +000013# This results in Python being spawned and printing the sys.argv list.
14# We can then eval() the result of this, and see what each argv was.
Tim Peters61cd0db2003-03-07 21:10:21 +000015python = sys.executable
16if ' ' in python:
17 python = '"' + python + '"' # quote embedded space for cmdline
Mark Hammonde7fefbf2002-04-03 01:47:00 +000018
Thomas Woutersb2137042007-02-01 18:02:27 +000019class PopenTest(unittest.TestCase):
Guido van Rossumaa588c42007-06-15 03:35:38 +000020
Thomas Woutersb2137042007-02-01 18:02:27 +000021 def _do_test_commandline(self, cmdline, expected):
Guido van Rossum96ca6912007-06-15 03:49:03 +000022 cmd = '%s -c "import sys; print(sys.argv)" %s'
Guido van Rossumaa588c42007-06-15 03:35:38 +000023 cmd = cmd % (python, cmdline)
Benjamin Petersonaa7cec02010-10-31 01:35:53 +000024 with os.popen(cmd) as p:
25 data = p.read()
Thomas Woutersb2137042007-02-01 18:02:27 +000026 got = eval(data)[1:] # strip off argv[0]
27 self.assertEqual(got, expected)
Mark Hammonde7fefbf2002-04-03 01:47:00 +000028
Thomas Woutersb2137042007-02-01 18:02:27 +000029 def test_popen(self):
30 self.assertRaises(TypeError, os.popen)
31 self._do_test_commandline(
32 "foo bar",
33 ["foo", "bar"]
34 )
35 self._do_test_commandline(
36 'foo "spam and eggs" "silly walk"',
37 ["foo", "spam and eggs", "silly walk"]
38 )
39 self._do_test_commandline(
40 'foo "a \\"quoted\\" arg" bar',
41 ["foo", 'a "quoted" arg', "bar"]
42 )
Benjamin Petersonee8712c2008-05-20 21:35:26 +000043 support.reap_children()
Mark Hammonde7fefbf2002-04-03 01:47:00 +000044
Amaury Forgeot d'Arc97e5f282009-07-11 09:35:13 +000045 def test_return_code(self):
46 self.assertEqual(os.popen("exit 0").close(), None)
47 if os.name == 'nt':
48 self.assertEqual(os.popen("exit 42").close(), 42)
49 else:
50 self.assertEqual(os.popen("exit 42").close(), 42 << 8)
51
Antoine Pitrouac625352009-12-09 00:01:27 +000052 def test_contextmanager(self):
53 with os.popen("echo hello") as f:
54 self.assertEqual(f.read(), "hello\n")
55
56 def test_iterating(self):
57 with os.popen("echo hello") as f:
58 self.assertEqual(list(f), ["hello\n"])
59
Martin Panterbf19d162015-09-09 01:01:13 +000060 def test_keywords(self):
61 with os.popen(cmd="exit 0", mode="w", buffering=-1):
62 pass
63
Thomas Woutersb2137042007-02-01 18:02:27 +000064if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050065 unittest.main()