blob: a539c243fe5b8b29908d4c85f87d4c91d7275467 [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
Neal Norwitzc473d5e2007-03-20 05:21:21 +00007
Ezio Melotti9bab5242010-08-03 08:32:53 +00008from test.test_support import (run_unittest, reap_children, import_module,
9 check_warnings)
Fred Drake4c81d602001-07-23 04:08:01 +000010
Ezio Melotti3897a442010-08-02 01:33:30 +000011from test.test_support import TestSkipped, run_unittest, reap_children, import_module
Ezio Melotti3897a442010-08-02 01:33:30 +000012# Silence Py3k warning
13import_module('commands', deprecated=True)
Fred Drake4c81d602001-07-23 04:08:01 +000014from commands import *
15
16# The module says:
17# "NB This only works (and is only relevant) for UNIX."
18#
19# Actually, getoutput should work on any platform with an os.popen, but
20# I'll take the comment as given, and skip this suite.
21
22if os.name != 'posix':
23 raise TestSkipped('Not posix; skipping test_commands')
24
25
26class CommandTests(unittest.TestCase):
27
28 def test_getoutput(self):
29 self.assertEquals(getoutput('echo xyzzy'), 'xyzzy')
30 self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
31
Guido van Rossum3b0a3292002-08-09 16:38:32 +000032 # we use mkdtemp in the next line to create an empty directory
33 # under our exclusive control; from that, we can invent a pathname
34 # that we _know_ won't exist. This is guaranteed to fail.
Thomas Wouters590fe022003-03-25 18:50:19 +000035 dir = None
Guido van Rossum3b0a3292002-08-09 16:38:32 +000036 try:
37 dir = tempfile.mkdtemp()
38 name = os.path.join(dir, "foo")
39
40 status, output = getstatusoutput('cat ' + name)
41 self.assertNotEquals(status, 0)
42 finally:
Thomas Wouters590fe022003-03-25 18:50:19 +000043 if dir is not None:
44 os.rmdir(dir)
Fred Drake4c81d602001-07-23 04:08:01 +000045
46 def test_getstatus(self):
Fred Drake4993c512002-04-01 23:56:03 +000047 # This pattern should match 'ls -ld /.' on any posix
Jason Tishler884554d2002-09-30 15:44:41 +000048 # system, however perversely configured. Even on systems
49 # (e.g., Cygwin) where user and group names can have spaces:
50 # drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 /
51 # drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 /
52 # Note that the first case above has a space in the group name
53 # while the second one has a space in both names.
Fred Drake4993c512002-04-01 23:56:03 +000054 pat = r'''d......... # It is a directory.
Jason Tishlere257ec92002-12-05 20:18:39 +000055 \+? # It may have ACLs.
Fred Drake4c81d602001-07-23 04:08:01 +000056 \s+\d+ # It has some number of links.
Jason Tishler884554d2002-09-30 15:44:41 +000057 [^/]* # Skip user, group, size, and date.
Martin v. Löwiscdbc1312002-06-06 09:52:49 +000058 /\. # and end with the name of the file.
Fred Drake4c81d602001-07-23 04:08:01 +000059 '''
Ezio Melotti9bab5242010-08-03 08:32:53 +000060 with check_warnings(quiet=True):
61 self.assertTrue(re.match(pat, getstatus("/."), re.VERBOSE))
Fred Drake4c81d602001-07-23 04:08:01 +000062
Fred Drake2e2be372001-09-20 21:33:42 +000063
64def test_main():
65 run_unittest(CommandTests)
Neal Norwitzb15ac312006-06-29 04:10:08 +000066 reap_children()
Fred Drake2e2be372001-09-20 21:33:42 +000067
68
69if __name__ == "__main__":
70 test_main()