blob: 066b18286045d34b6abd78a55d8b047aef1bdddd [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
Florent Xicluna6257a7b2010-03-31 22:01:03 +00008from test.test_support import run_unittest, reap_children, import_module, \
9 check_warnings
Ezio Melottia2d46532010-01-30 07:22:54 +000010
11# Silence Py3k warning
Georg Brandla4f46e12010-02-07 17:03:15 +000012commands = import_module('commands', deprecated=True)
Fred Drake4c81d602001-07-23 04:08:01 +000013
14# The module says:
15# "NB This only works (and is only relevant) for UNIX."
16#
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.
19
20if os.name != 'posix':
Benjamin Petersonbec087f2009-03-26 21:10:30 +000021 raise unittest.SkipTest('Not posix; skipping test_commands')
Fred Drake4c81d602001-07-23 04:08:01 +000022
23
24class CommandTests(unittest.TestCase):
25
26 def test_getoutput(self):
Ezio Melotti2623a372010-11-21 13:34:58 +000027 self.assertEqual(commands.getoutput('echo xyzzy'), 'xyzzy')
28 self.assertEqual(commands.getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
Fred Drake4c81d602001-07-23 04:08:01 +000029
Guido van Rossum3b0a3292002-08-09 16:38:32 +000030 # 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.
Thomas Wouters590fe022003-03-25 18:50:19 +000033 dir = None
Guido van Rossum3b0a3292002-08-09 16:38:32 +000034 try:
35 dir = tempfile.mkdtemp()
36 name = os.path.join(dir, "foo")
37
Georg Brandla4f46e12010-02-07 17:03:15 +000038 status, output = commands.getstatusoutput('cat ' + name)
Ezio Melotti2623a372010-11-21 13:34:58 +000039 self.assertNotEqual(status, 0)
Guido van Rossum3b0a3292002-08-09 16:38:32 +000040 finally:
Thomas Wouters590fe022003-03-25 18:50:19 +000041 if dir is not None:
42 os.rmdir(dir)
Fred Drake4c81d602001-07-23 04:08:01 +000043
44 def test_getstatus(self):
Fred Drake4993c512002-04-01 23:56:03 +000045 # This pattern should match 'ls -ld /.' on any posix
Jason Tishler884554d2002-09-30 15:44:41 +000046 # 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.
Ned Deily5f511822011-04-05 17:16:09 -070052 # Special attributes supported:
53 # + = has ACLs
54 # @ = has Mac OS X extended attributes
55 # . = has a SELinux security context
Fred Drake4993c512002-04-01 23:56:03 +000056 pat = r'''d......... # It is a directory.
Ned Deily5f511822011-04-05 17:16:09 -070057 [.+@]? # It may have special attributes.
Fred Drake4c81d602001-07-23 04:08:01 +000058 \s+\d+ # It has some number of links.
Jason Tishler884554d2002-09-30 15:44:41 +000059 [^/]* # Skip user, group, size, and date.
Martin v. Löwiscdbc1312002-06-06 09:52:49 +000060 /\. # and end with the name of the file.
Fred Drake4c81d602001-07-23 04:08:01 +000061 '''
62
Florent Xicluna6257a7b2010-03-31 22:01:03 +000063 with check_warnings((".*commands.getstatus.. is deprecated",
64 DeprecationWarning)):
65 self.assertTrue(re.match(pat, commands.getstatus("/."), re.VERBOSE))
Fred Drake4c81d602001-07-23 04:08:01 +000066
Fred Drake2e2be372001-09-20 21:33:42 +000067
68def test_main():
69 run_unittest(CommandTests)
Neal Norwitzb15ac312006-06-29 04:10:08 +000070 reap_children()
Fred Drake2e2be372001-09-20 21:33:42 +000071
72
73if __name__ == "__main__":
74 test_main()