Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Tests for commands module |
| 3 | Nick Mathewson |
| 4 | ''' |
| 5 | import unittest |
| 6 | import os, tempfile, re |
Neal Norwitz | c473d5e | 2007-03-20 05:21:21 +0000 | [diff] [blame] | 7 | |
Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 8 | from test.test_support import run_unittest, reap_children, import_module, \ |
| 9 | check_warnings |
Ezio Melotti | a2d4653 | 2010-01-30 07:22:54 +0000 | [diff] [blame] | 10 | |
| 11 | # Silence Py3k warning |
Georg Brandl | a4f46e1 | 2010-02-07 17:03:15 +0000 | [diff] [blame] | 12 | commands = import_module('commands', deprecated=True) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 13 | |
| 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 | |
| 20 | if os.name != 'posix': |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 21 | raise unittest.SkipTest('Not posix; skipping test_commands') |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class CommandTests(unittest.TestCase): |
| 25 | |
| 26 | def test_getoutput(self): |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 27 | self.assertEqual(commands.getoutput('echo xyzzy'), 'xyzzy') |
| 28 | self.assertEqual(commands.getstatusoutput('echo xyzzy'), (0, 'xyzzy')) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 30 | # 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 Wouters | 590fe02 | 2003-03-25 18:50:19 +0000 | [diff] [blame] | 33 | dir = None |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 34 | try: |
| 35 | dir = tempfile.mkdtemp() |
| 36 | name = os.path.join(dir, "foo") |
| 37 | |
Georg Brandl | a4f46e1 | 2010-02-07 17:03:15 +0000 | [diff] [blame] | 38 | status, output = commands.getstatusoutput('cat ' + name) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 39 | self.assertNotEqual(status, 0) |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 40 | finally: |
Thomas Wouters | 590fe02 | 2003-03-25 18:50:19 +0000 | [diff] [blame] | 41 | if dir is not None: |
| 42 | os.rmdir(dir) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 43 | |
| 44 | def test_getstatus(self): |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 45 | # This pattern should match 'ls -ld /.' on any posix |
Jason Tishler | 884554d | 2002-09-30 15:44:41 +0000 | [diff] [blame] | 46 | # 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 Deily | 5f51182 | 2011-04-05 17:16:09 -0700 | [diff] [blame] | 52 | # Special attributes supported: |
| 53 | # + = has ACLs |
| 54 | # @ = has Mac OS X extended attributes |
| 55 | # . = has a SELinux security context |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 56 | pat = r'''d......... # It is a directory. |
Ned Deily | 5f51182 | 2011-04-05 17:16:09 -0700 | [diff] [blame] | 57 | [.+@]? # It may have special attributes. |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 58 | \s+\d+ # It has some number of links. |
Jason Tishler | 884554d | 2002-09-30 15:44:41 +0000 | [diff] [blame] | 59 | [^/]* # Skip user, group, size, and date. |
Martin v. Löwis | cdbc131 | 2002-06-06 09:52:49 +0000 | [diff] [blame] | 60 | /\. # and end with the name of the file. |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 61 | ''' |
| 62 | |
Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 63 | with check_warnings((".*commands.getstatus.. is deprecated", |
| 64 | DeprecationWarning)): |
| 65 | self.assertTrue(re.match(pat, commands.getstatus("/."), re.VERBOSE)) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 66 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 67 | |
| 68 | def test_main(): |
| 69 | run_unittest(CommandTests) |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 70 | reap_children() |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | test_main() |