home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 June / maximum-cd-2011-06.iso / DiscContents / LibO_3.3.1_Win_x86_install_multi.exe / libreoffice1.cab / test_popen.py < prev    next >
Encoding:
Python Source  |  2011-02-15  |  1.4 KB  |  48 lines

  1. #! /usr/bin/env python
  2. """Basic tests for os.popen()
  3.  
  4.   Particularly useful for platforms that fake popen.
  5. """
  6.  
  7. import unittest
  8. from test import test_support
  9. import os, sys
  10.  
  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.
  16. python = sys.executable
  17. if ' ' in python:
  18.     python = '"' + python + '"'     # quote embedded space for cmdline
  19.  
  20. class PopenTest(unittest.TestCase):
  21.     def _do_test_commandline(self, cmdline, expected):
  22.         cmd = '%s -c "import sys;print sys.argv" %s' % (python, cmdline)
  23.         data = os.popen(cmd).read()
  24.         got = eval(data)[1:] # strip off argv[0]
  25.         self.assertEqual(got, expected)
  26.  
  27.     def test_popen(self):
  28.         self.assertRaises(TypeError, os.popen)
  29.         self._do_test_commandline(
  30.             "foo bar",
  31.             ["foo", "bar"]
  32.         )
  33.         self._do_test_commandline(
  34.             'foo "spam and eggs" "silly walk"',
  35.             ["foo", "spam and eggs", "silly walk"]
  36.         )
  37.         self._do_test_commandline(
  38.             'foo "a \\"quoted\\" arg" bar',
  39.             ["foo", 'a "quoted" arg', "bar"]
  40.         )
  41.         test_support.reap_children()
  42.  
  43. def test_main():
  44.     test_support.run_unittest(PopenTest)
  45.  
  46. if __name__ == "__main__":
  47.     test_main()
  48.