/Lib/test/test_popen.py

http://unladen-swallow.googlecode.com/ · Python · 47 lines · 31 code · 6 blank · 10 comment · 2 complexity · 2a435d759eb5d2968bb6c6c6bc8cdc99 MD5 · raw file

  1. #! /usr/bin/env python
  2. """Basic tests for os.popen()
  3. Particularly useful for platforms that fake popen.
  4. """
  5. import unittest
  6. from test import test_support
  7. import os, sys
  8. # Test that command-lines get down as we expect.
  9. # To do this we execute:
  10. # python -c "import sys;print sys.argv" {rest_of_commandline}
  11. # This results in Python being spawned and printing the sys.argv list.
  12. # We can then eval() the result of this, and see what each argv was.
  13. python = sys.executable
  14. if ' ' in python:
  15. python = '"' + python + '"' # quote embedded space for cmdline
  16. class PopenTest(unittest.TestCase):
  17. def _do_test_commandline(self, cmdline, expected):
  18. cmd = '%s -c "import sys;print sys.argv" %s' % (python, cmdline)
  19. data = os.popen(cmd).read()
  20. got = eval(data)[1:] # strip off argv[0]
  21. self.assertEqual(got, expected)
  22. def test_popen(self):
  23. self.assertRaises(TypeError, os.popen)
  24. self._do_test_commandline(
  25. "foo bar",
  26. ["foo", "bar"]
  27. )
  28. self._do_test_commandline(
  29. 'foo "spam and eggs" "silly walk"',
  30. ["foo", "spam and eggs", "silly walk"]
  31. )
  32. self._do_test_commandline(
  33. 'foo "a \\"quoted\\" arg" bar',
  34. ["foo", 'a "quoted" arg', "bar"]
  35. )
  36. test_support.reap_children()
  37. def test_main():
  38. test_support.run_unittest(PopenTest)
  39. if __name__ == "__main__":
  40. test_main()