blob: 5ea08bc8b4d97192cf5fa20fd0412839eeaaf581 [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
27 # we use mktemp in the next line to get a filename which we
28 # _know_ won't exist. This is guaranteed to fail.
29 status, output = getstatusoutput('cat ' + tempfile.mktemp())
30 self.assertNotEquals(status, 0)
31
32 def test_getstatus(self):
Fred Drake4993c512002-04-01 23:56:03 +000033 # This pattern should match 'ls -ld /.' on any posix
Fred Drake4c81d602001-07-23 04:08:01 +000034 # system, however perversely configured.
Fred Drake4993c512002-04-01 23:56:03 +000035 pat = r'''d......... # It is a directory.
Fred Drake4c81d602001-07-23 04:08:01 +000036 \s+\d+ # It has some number of links.
37 \s+\w+\s+\w+ # It has a user and group, which may
38 # be named anything.
Fred Drake4993c512002-04-01 23:56:03 +000039 \s+\d+ # It has a size.
Fred Drake4c81d602001-07-23 04:08:01 +000040 [^/]* # Skip the date.
Martin v. Löwiscdbc1312002-06-06 09:52:49 +000041 /\. # and end with the name of the file.
Fred Drake4c81d602001-07-23 04:08:01 +000042 '''
43
Fred Drake4993c512002-04-01 23:56:03 +000044 self.assert_(re.match(pat, getstatus("/."), re.VERBOSE))
Fred Drake4c81d602001-07-23 04:08:01 +000045
Fred Drake2e2be372001-09-20 21:33:42 +000046
47def test_main():
48 run_unittest(CommandTests)
49
50
51if __name__ == "__main__":
52 test_main()