|
1 ''' |
|
2 Tests for commands module |
|
3 Nick Mathewson |
|
4 ''' |
|
5 import unittest |
|
6 import os, tempfile, re |
|
7 |
|
8 from test.test_support import TestSkipped, run_unittest, reap_children |
|
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 mkdtemp in the next line to create an empty directory |
|
28 # under our exclusive control; from that, we can invent a pathname |
|
29 # that we _know_ won't exist. This is guaranteed to fail. |
|
30 dir = None |
|
31 try: |
|
32 dir = tempfile.mkdtemp() |
|
33 name = os.path.join(dir, "foo") |
|
34 |
|
35 status, output = getstatusoutput('cat ' + name) |
|
36 self.assertNotEquals(status, 0) |
|
37 finally: |
|
38 if dir is not None: |
|
39 os.rmdir(dir) |
|
40 |
|
41 def test_getstatus(self): |
|
42 # This pattern should match 'ls -ld /.' on any posix |
|
43 # system, however perversely configured. Even on systems |
|
44 # (e.g., Cygwin) where user and group names can have spaces: |
|
45 # drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 / |
|
46 # drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 / |
|
47 # Note that the first case above has a space in the group name |
|
48 # while the second one has a space in both names. |
|
49 pat = r'''d......... # It is a directory. |
|
50 \+? # It may have ACLs. |
|
51 \s+\d+ # It has some number of links. |
|
52 [^/]* # Skip user, group, size, and date. |
|
53 /\. # and end with the name of the file. |
|
54 ''' |
|
55 |
|
56 self.assert_(re.match(pat, getstatus("/."), re.VERBOSE)) |
|
57 |
|
58 |
|
59 def test_main(): |
|
60 run_unittest(CommandTests) |
|
61 reap_children() |
|
62 |
|
63 |
|
64 if __name__ == "__main__": |
|
65 test_main() |