Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Basic tests for os.popen() |
| 3 | |
| 4 | Particularly useful for platforms that fake popen. |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import sys |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 9 | from test.test_support import TestSkipped |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 10 | from 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. |
| 17 | def _do_test_commandline(cmdline, expected): |
Neal Norwitz | d69030d | 2002-07-20 20:35:13 +0000 | [diff] [blame] | 18 | cmd = '%s -c "import sys;print sys.argv" %s' % (sys.executable, cmdline) |
Mark Hammond | e7fefbf | 2002-04-03 01:47:00 +0000 | [diff] [blame] | 19 | data = popen(cmd).read() |
| 20 | got = eval(data)[1:] # strip off argv[0] |
| 21 | if got != expected: |
| 22 | print "Error in popen commandline handling." |
| 23 | print " executed '%s', expected '%r', but got '%r'" \ |
| 24 | % (cmdline, expected, got) |
| 25 | |
| 26 | def _test_commandline(): |
| 27 | _do_test_commandline("foo bar", ["foo", "bar"]) |
| 28 | _do_test_commandline('foo "spam and eggs" "silly walk"', ["foo", "spam and eggs", "silly walk"]) |
| 29 | _do_test_commandline('foo "a \\"quoted\\" arg" bar', ["foo", 'a "quoted" arg', "bar"]) |
| 30 | print "popen seemed to process the command-line correctly" |
| 31 | |
| 32 | def main(): |
| 33 | print "Test popen:" |
| 34 | _test_commandline() |
| 35 | |
| 36 | main() |