blob: 2b687ad31ecbec76efa0fc5496dde1cdb35c7538 [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
7import os
8import sys
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test.test_support import TestSkipped
Mark Hammonde7fefbf2002-04-03 01:47:00 +000010from os import popen
11
12# Test that command-lines get down as we expect.
13# To do this we execute:
14# python -c "import sys;print sys.argv" {rest_of_commandline}
15# This results in Python being spawned and printing the sys.argv list.
16# We can then eval() the result of this, and see what each argv was.
Tim Peters61cd0db2003-03-07 21:10:21 +000017python = sys.executable
18if ' ' in python:
19 python = '"' + python + '"' # quote embedded space for cmdline
Mark Hammonde7fefbf2002-04-03 01:47:00 +000020def _do_test_commandline(cmdline, expected):
Tim Peters61cd0db2003-03-07 21:10:21 +000021 cmd = '%s -c "import sys;print sys.argv" %s' % (python, cmdline)
Mark Hammonde7fefbf2002-04-03 01:47:00 +000022 data = popen(cmd).read()
23 got = eval(data)[1:] # strip off argv[0]
24 if got != expected:
25 print "Error in popen commandline handling."
26 print " executed '%s', expected '%r', but got '%r'" \
27 % (cmdline, expected, got)
28
29def _test_commandline():
30 _do_test_commandline("foo bar", ["foo", "bar"])
31 _do_test_commandline('foo "spam and eggs" "silly walk"', ["foo", "spam and eggs", "silly walk"])
32 _do_test_commandline('foo "a \\"quoted\\" arg" bar', ["foo", 'a "quoted" arg', "bar"])
33 print "popen seemed to process the command-line correctly"
34
35def main():
36 print "Test popen:"
37 _test_commandline()
38
39main()