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