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