home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_commands.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  1.6 KB  |  53 lines

  1. '''
  2.    Tests for commands module
  3.    Nick Mathewson
  4. '''
  5. import unittest
  6. import os, tempfile, re
  7.  
  8. from test_support import TestSkipped, run_unittest
  9. from commands import *
  10.  
  11. # The module says:
  12. #   "NB This only works (and is only relevant) for UNIX."
  13. #
  14. # Actually, getoutput should work on any platform with an os.popen, but
  15. # I'll take the comment as given, and skip this suite.
  16.  
  17. if os.name != 'posix':
  18.     raise TestSkipped('Not posix; skipping test_commands')
  19.  
  20.  
  21. class CommandTests(unittest.TestCase):
  22.  
  23.     def test_getoutput(self):
  24.         self.assertEquals(getoutput('echo xyzzy'), 'xyzzy')
  25.         self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
  26.  
  27.         # we use mktemp in the next line to get a filename which we
  28.         # _know_ won't exist.  This is guaranteed to fail.
  29.         status, output = getstatusoutput('cat ' + tempfile.mktemp())
  30.         self.assertNotEquals(status, 0)
  31.  
  32.     def test_getstatus(self):
  33.         # This pattern should match 'ls -ld /.' on any posix
  34.         # system, however perversely configured.
  35.         pat = r'''d.........   # It is a directory.
  36.                   \s+\d+       # It has some number of links.
  37.                   \s+\w+\s+\w+ # It has a user and group, which may
  38.                                #     be named anything.
  39.                   \s+\d+       # It has a size.
  40.                   [^/]*        # Skip the date.
  41.                   /.           # and end with the name of the file.
  42.                '''
  43.  
  44.         self.assert_(re.match(pat, getstatus("/."), re.VERBOSE))
  45.  
  46.  
  47. def test_main():
  48.     run_unittest(CommandTests)
  49.  
  50.  
  51. if __name__ == "__main__":
  52.     test_main()
  53.