blob: 397e4a31ce1243cb814d8c503d6acdd489554fd5 [file] [log] [blame]
Mark Hammonde7fefbf2002-04-03 01:47:00 +00001#! /usr/bin/env python
2"""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
8from test import test_support
9import 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:
13# python -c "import sys;print sys.argv" {rest_of_commandline}
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 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)
Thomas Woutersb2137042007-02-01 18:02:27 +000025 data = os.popen(cmd).read()
26 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 )
43 test_support.reap_children()
Mark Hammonde7fefbf2002-04-03 01:47:00 +000044
Thomas Woutersb2137042007-02-01 18:02:27 +000045def test_main():
46 test_support.run_unittest(PopenTest)
47
48if __name__ == "__main__":
49 test_main()