blob: cac2f6177f3257373126942b7fe33e6e6b625d40 [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
pxinwre1e3c2d2020-12-16 05:20:07 +080010if not hasattr(os, 'popen'):
11 raise unittest.SkipTest("need os.popen()")
12
Mark Hammonde7fefbf2002-04-03 01:47:00 +000013# Test that command-lines get down as we expect.
14# To do this we execute:
Neal Norwitz752abd02008-05-13 04:55:24 +000015# python -c "import sys;print(sys.argv)" {rest_of_commandline}
Mark Hammonde7fefbf2002-04-03 01:47:00 +000016# 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.
Tim Peters61cd0db2003-03-07 21:10:21 +000018python = sys.executable
19if ' ' in python:
20 python = '"' + python + '"' # quote embedded space for cmdline
Mark Hammonde7fefbf2002-04-03 01:47:00 +000021
Thomas Woutersb2137042007-02-01 18:02:27 +000022class PopenTest(unittest.TestCase):
Guido van Rossumaa588c42007-06-15 03:35:38 +000023
Thomas Woutersb2137042007-02-01 18:02:27 +000024 def _do_test_commandline(self, cmdline, expected):
Guido van Rossum96ca6912007-06-15 03:49:03 +000025 cmd = '%s -c "import sys; print(sys.argv)" %s'
Guido van Rossumaa588c42007-06-15 03:35:38 +000026 cmd = cmd % (python, cmdline)
Benjamin Petersonaa7cec02010-10-31 01:35:53 +000027 with os.popen(cmd) as p:
28 data = p.read()
Thomas Woutersb2137042007-02-01 18:02:27 +000029 got = eval(data)[1:] # strip off argv[0]
30 self.assertEqual(got, expected)
Mark Hammonde7fefbf2002-04-03 01:47:00 +000031
Thomas Woutersb2137042007-02-01 18:02:27 +000032 def test_popen(self):
33 self.assertRaises(TypeError, os.popen)
34 self._do_test_commandline(
35 "foo bar",
36 ["foo", "bar"]
37 )
38 self._do_test_commandline(
39 'foo "spam and eggs" "silly walk"',
40 ["foo", "spam and eggs", "silly walk"]
41 )
42 self._do_test_commandline(
43 'foo "a \\"quoted\\" arg" bar',
44 ["foo", 'a "quoted" arg', "bar"]
45 )
Benjamin Petersonee8712c2008-05-20 21:35:26 +000046 support.reap_children()
Mark Hammonde7fefbf2002-04-03 01:47:00 +000047
Amaury Forgeot d'Arc97e5f282009-07-11 09:35:13 +000048 def test_return_code(self):
49 self.assertEqual(os.popen("exit 0").close(), None)
Victor Stinner65a796e2020-04-01 18:49:29 +020050 status = os.popen("exit 42").close()
Amaury Forgeot d'Arc97e5f282009-07-11 09:35:13 +000051 if os.name == 'nt':
Victor Stinner65a796e2020-04-01 18:49:29 +020052 self.assertEqual(status, 42)
Amaury Forgeot d'Arc97e5f282009-07-11 09:35:13 +000053 else:
Victor Stinner65a796e2020-04-01 18:49:29 +020054 self.assertEqual(os.waitstatus_to_exitcode(status), 42)
Amaury Forgeot d'Arc97e5f282009-07-11 09:35:13 +000055
Antoine Pitrouac625352009-12-09 00:01:27 +000056 def test_contextmanager(self):
57 with os.popen("echo hello") as f:
58 self.assertEqual(f.read(), "hello\n")
59
60 def test_iterating(self):
61 with os.popen("echo hello") as f:
62 self.assertEqual(list(f), ["hello\n"])
63
Martin Panterbf19d162015-09-09 01:01:13 +000064 def test_keywords(self):
65 with os.popen(cmd="exit 0", mode="w", buffering=-1):
66 pass
67
Thomas Woutersb2137042007-02-01 18:02:27 +000068if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050069 unittest.main()