2 Tests for commands module
6 import os
, tempfile
, re
8 from test
.test_support
import run_unittest
, reap_children
, import_module
, \
11 # Silence Py3k warning
12 commands
= import_module('commands', deprecated
=True)
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.
35 dir = tempfile
.mkdtemp()
36 name
= os
.path
.join(dir, "foo")
38 status
, output
= commands
.getstatusoutput('cat ' + name
)
39 self
.assertNotEquals(status
, 0)
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.
59 with
check_warnings((".*commands.getstatus.. is deprecated",
61 self
.assertTrue(re
.match(pat
, commands
.getstatus("/."), re
.VERBOSE
))
65 run_unittest(CommandTests
)
69 if __name__
== "__main__":