blob: accf187b54d7f3e053cbde1240ba60121f4bfb78 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Mark Hammonde7fefbf2002-04-03 01:47:00 +00002"""Basic tests for os.popen()
3
4 Particularly useful for platforms that fake popen.
5"""
6
Thomas Woutersb2137042007-02-01 18:02:27 +00007import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test import support
Thomas Woutersb2137042007-02-01 18:02:27 +00009import os, sys
Mark Hammonde7fefbf2002-04-03 01:47:00 +000010
11# Test that command-lines get down as we expect.
12# To do this we execute:
Neal Norwitz752abd02008-05-13 04:55:24 +000013# python -c "import sys;print(sys.argv)" {rest_of_commandline}
Mark Hammonde7fefbf2002-04-03 01:47:00 +000014# 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 Peters61cd0db2003-03-07 21:10:21 +000016python = sys.executable
17if ' ' in python:
18 python = '"' + python + '"' # quote embedded space for cmdline
Mark Hammonde7fefbf2002-04-03 01:47:00 +000019
Thomas Woutersb2137042007-02-01 18:02:27 +000020class PopenTest(unittest.TestCase):
Guido van Rossumaa588c42007-06-15 03:35:38 +000021
Thomas Woutersb2137042007-02-01 18:02:27 +000022 def _do_test_commandline(self, cmdline, expected):
Guido van Rossum96ca6912007-06-15 03:49:03 +000023 cmd = '%s -c "import sys; print(sys.argv)" %s'
Guido van Rossumaa588c42007-06-15 03:35:38 +000024 cmd = cmd % (python, cmdline)
Benjamin Petersonaa7cec02010-10-31 01:35:53 +000025 with os.popen(cmd) as p:
26 data = p.read()
Thomas Woutersb2137042007-02-01 18:02:27 +000027 got = eval(data)[1:] # strip off argv[0]
28 self.assertEqual(got, expected)
Mark Hammonde7fefbf2002-04-03 01:47:00 +000029
Thomas Woutersb2137042007-02-01 18:02:27 +000030 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 Petersonee8712c2008-05-20 21:35:26 +000044 support.reap_children()
Mark Hammonde7fefbf2002-04-03 01:47:00 +000045
Amaury Forgeot d'Arc97e5f282009-07-11 09:35:13 +000046 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 Pitrouac625352009-12-09 00:01:27 +000053 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 Woutersb2137042007-02-01 18:02:27 +000061def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000062 support.run_unittest(PopenTest)
Thomas Woutersb2137042007-02-01 18:02:27 +000063
64if __name__ == "__main__":
65 test_main()