PyXR

c:\python24\lib \ test \ test_commands.py



0001 '''
0002    Tests for commands module
0003    Nick Mathewson
0004 '''
0005 import unittest
0006 import os, tempfile, re
0007 
0008 from test.test_support import TestSkipped, run_unittest
0009 from commands import *
0010 
0011 # The module says:
0012 #   "NB This only works (and is only relevant) for UNIX."
0013 #
0014 # Actually, getoutput should work on any platform with an os.popen, but
0015 # I'll take the comment as given, and skip this suite.
0016 
0017 if os.name != 'posix':
0018     raise TestSkipped('Not posix; skipping test_commands')
0019 
0020 
0021 class CommandTests(unittest.TestCase):
0022 
0023     def test_getoutput(self):
0024         self.assertEquals(getoutput('echo xyzzy'), 'xyzzy')
0025         self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
0026 
0027         # we use mkdtemp in the next line to create an empty directory
0028         # under our exclusive control; from that, we can invent a pathname
0029         # that we _know_ won't exist.  This is guaranteed to fail.
0030         dir = None
0031         try:
0032             dir = tempfile.mkdtemp()
0033             name = os.path.join(dir, "foo")
0034 
0035             status, output = getstatusoutput('cat ' + name)
0036             self.assertNotEquals(status, 0)
0037         finally:
0038             if dir is not None:
0039                 os.rmdir(dir)
0040 
0041     def test_getstatus(self):
0042         # This pattern should match 'ls -ld /.' on any posix
0043         # system, however perversely configured.  Even on systems
0044         # (e.g., Cygwin) where user and group names can have spaces:
0045         #     drwxr-xr-x   15 Administ Domain U     4096 Aug 12 12:50 /
0046         #     drwxr-xr-x   15 Joe User My Group     4096 Aug 12 12:50 /
0047         # Note that the first case above has a space in the group name
0048         # while the second one has a space in both names.
0049         pat = r'''d.........   # It is a directory.
0050                   \+?          # It may have ACLs.
0051                   \s+\d+       # It has some number of links.
0052                   [^/]*        # Skip user, group, size, and date.
0053                   /\.          # and end with the name of the file.
0054                '''
0055 
0056         self.assert_(re.match(pat, getstatus("/."), re.VERBOSE))
0057 
0058 
0059 def test_main():
0060     run_unittest(CommandTests)
0061 
0062 
0063 if __name__ == "__main__":
0064     test_main()
0065 

Generated by PyXR 0.9.4
SourceForge.net Logo