move sections
[python/dscho.git] / Lib / test / test_commands.py
blob2d10dea3a30591a78e3948224f30aa4713321944
1 '''
2 Tests for commands module
3 Nick Mathewson
4 '''
5 import unittest
6 import os, tempfile, re
8 from test.test_support import run_unittest, reap_children, import_module, \
9 check_warnings
11 # Silence Py3k warning
12 commands = import_module('commands', deprecated=True)
14 # The module says:
15 # "NB This only works (and is only relevant) for UNIX."
17 # Actually, getoutput should work on any platform with an os.popen, but
18 # I'll take the comment as given, and skip this suite.
20 if os.name != 'posix':
21 raise unittest.SkipTest('Not posix; skipping test_commands')
24 class CommandTests(unittest.TestCase):
26 def test_getoutput(self):
27 self.assertEquals(commands.getoutput('echo xyzzy'), 'xyzzy')
28 self.assertEquals(commands.getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
30 # we use mkdtemp in the next line to create an empty directory
31 # under our exclusive control; from that, we can invent a pathname
32 # that we _know_ won't exist. This is guaranteed to fail.
33 dir = None
34 try:
35 dir = tempfile.mkdtemp()
36 name = os.path.join(dir, "foo")
38 status, output = commands.getstatusoutput('cat ' + name)
39 self.assertNotEquals(status, 0)
40 finally:
41 if dir is not None:
42 os.rmdir(dir)
44 def test_getstatus(self):
45 # This pattern should match 'ls -ld /.' on any posix
46 # system, however perversely configured. Even on systems
47 # (e.g., Cygwin) where user and group names can have spaces:
48 # drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 /
49 # drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 /
50 # Note that the first case above has a space in the group name
51 # while the second one has a space in both names.
52 pat = r'''d......... # It is a directory.
53 \+? # It may have ACLs.
54 \s+\d+ # It has some number of links.
55 [^/]* # Skip user, group, size, and date.
56 /\. # and end with the name of the file.
57 '''
59 with check_warnings((".*commands.getstatus.. is deprecated",
60 DeprecationWarning)):
61 self.assertTrue(re.match(pat, commands.getstatus("/."), re.VERBOSE))
64 def test_main():
65 run_unittest(CommandTests)
66 reap_children()
69 if __name__ == "__main__":
70 test_main()