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 | import warnings |
| 8 | |
| 9 | warnings.filterwarnings('ignore', r".*commands.getstatus.. is deprecated", |
| 10 | DeprecationWarning) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 11 | |
Ezio Melotti | a2d4653 | 2010-01-30 07:22:54 +0000 | [diff] [blame^] | 12 | from test.test_support import run_unittest, reap_children, import_module |
| 13 | |
| 14 | # Silence Py3k warning |
| 15 | import_module('commands', deprecated=True) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 16 | from commands import * |
| 17 | |
| 18 | # The module says: |
| 19 | # "NB This only works (and is only relevant) for UNIX." |
| 20 | # |
| 21 | # Actually, getoutput should work on any platform with an os.popen, but |
| 22 | # I'll take the comment as given, and skip this suite. |
| 23 | |
| 24 | if os.name != 'posix': |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 25 | raise unittest.SkipTest('Not posix; skipping test_commands') |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class CommandTests(unittest.TestCase): |
| 29 | |
| 30 | def test_getoutput(self): |
| 31 | self.assertEquals(getoutput('echo xyzzy'), 'xyzzy') |
| 32 | self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy')) |
| 33 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 34 | # we use mkdtemp in the next line to create an empty directory |
| 35 | # under our exclusive control; from that, we can invent a pathname |
| 36 | # that we _know_ won't exist. This is guaranteed to fail. |
Thomas Wouters | 590fe02 | 2003-03-25 18:50:19 +0000 | [diff] [blame] | 37 | dir = None |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 38 | try: |
| 39 | dir = tempfile.mkdtemp() |
| 40 | name = os.path.join(dir, "foo") |
| 41 | |
| 42 | status, output = getstatusoutput('cat ' + name) |
| 43 | self.assertNotEquals(status, 0) |
| 44 | finally: |
Thomas Wouters | 590fe02 | 2003-03-25 18:50:19 +0000 | [diff] [blame] | 45 | if dir is not None: |
| 46 | os.rmdir(dir) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 47 | |
| 48 | def test_getstatus(self): |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 49 | # This pattern should match 'ls -ld /.' on any posix |
Jason Tishler | 884554d | 2002-09-30 15:44:41 +0000 | [diff] [blame] | 50 | # system, however perversely configured. Even on systems |
| 51 | # (e.g., Cygwin) where user and group names can have spaces: |
| 52 | # drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 / |
| 53 | # drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 / |
| 54 | # Note that the first case above has a space in the group name |
| 55 | # while the second one has a space in both names. |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 56 | pat = r'''d......... # It is a directory. |
Jason Tishler | e257ec9 | 2002-12-05 20:18:39 +0000 | [diff] [blame] | 57 | \+? # It may have ACLs. |
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 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 63 | self.assertTrue(re.match(pat, getstatus("/."), re.VERBOSE)) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 64 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 65 | |
| 66 | def test_main(): |
| 67 | run_unittest(CommandTests) |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 68 | reap_children() |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | test_main() |