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