blob: ef87d79240f757fa562ed15064feee75754b758c [file] [log] [blame]
Fred Drake4c81d602001-07-23 04:08:01 +00001'''
2 Tests for commands module
3 Nick Mathewson
4'''
5import unittest
6import os, tempfile, re
7
Barry Warsaw04f357c2002-07-23 19:04:11 +00008from test.test_support import TestSkipped, run_unittest
Fred Drake4c81d602001-07-23 04:08:01 +00009from 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
17if os.name != 'posix':
18 raise TestSkipped('Not posix; skipping test_commands')
19
20
21class 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
Guido van Rossum3b0a3292002-08-09 16:38:32 +000027 # 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 try:
31 dir = tempfile.mkdtemp()
32 name = os.path.join(dir, "foo")
33
34 status, output = getstatusoutput('cat ' + name)
35 self.assertNotEquals(status, 0)
36 finally:
37 os.rmdir(dir)
Fred Drake4c81d602001-07-23 04:08:01 +000038
39 def test_getstatus(self):
Fred Drake4993c512002-04-01 23:56:03 +000040 # This pattern should match 'ls -ld /.' on any posix
Jason Tishler884554d2002-09-30 15:44:41 +000041 # system, however perversely configured. Even on systems
42 # (e.g., Cygwin) where user and group names can have spaces:
43 # drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 /
44 # drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 /
45 # Note that the first case above has a space in the group name
46 # while the second one has a space in both names.
Fred Drake4993c512002-04-01 23:56:03 +000047 pat = r'''d......... # It is a directory.
Jason Tishlere257ec92002-12-05 20:18:39 +000048 \+? # It may have ACLs.
Fred Drake4c81d602001-07-23 04:08:01 +000049 \s+\d+ # It has some number of links.
Jason Tishler884554d2002-09-30 15:44:41 +000050 [^/]* # Skip user, group, size, and date.
Martin v. Löwiscdbc1312002-06-06 09:52:49 +000051 /\. # and end with the name of the file.
Fred Drake4c81d602001-07-23 04:08:01 +000052 '''
53
Fred Drake4993c512002-04-01 23:56:03 +000054 self.assert_(re.match(pat, getstatus("/."), re.VERBOSE))
Fred Drake4c81d602001-07-23 04:08:01 +000055
Fred Drake2e2be372001-09-20 21:33:42 +000056
57def test_main():
58 run_unittest(CommandTests)
59
60
61if __name__ == "__main__":
62 test_main()