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 |
| 7 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 8 | from test.test_support import TestSkipped, run_unittest |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 9 | from commands import * |
| 10 | |
| 11 | # The module says: |
| 12 | # "NB This only works (and is only relevant) for UNIX." |
| 13 | # |
| 14 | # Actually, getoutput should work on any platform with an os.popen, but |
| 15 | # I'll take the comment as given, and skip this suite. |
| 16 | |
| 17 | if os.name != 'posix': |
| 18 | raise TestSkipped('Not posix; skipping test_commands') |
| 19 | |
| 20 | |
| 21 | class CommandTests(unittest.TestCase): |
| 22 | |
| 23 | def test_getoutput(self): |
| 24 | self.assertEquals(getoutput('echo xyzzy'), 'xyzzy') |
| 25 | self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy')) |
| 26 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 27 | # we use mkdtemp in the next line to create an empty directory |
| 28 | # under our exclusive control; from that, we can invent a pathname |
| 29 | # that we _know_ won't exist. This is guaranteed to fail. |
| 30 | try: |
| 31 | dir = tempfile.mkdtemp() |
| 32 | name = os.path.join(dir, "foo") |
| 33 | |
| 34 | status, output = getstatusoutput('cat ' + name) |
| 35 | self.assertNotEquals(status, 0) |
| 36 | finally: |
| 37 | os.rmdir(dir) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 38 | |
| 39 | def test_getstatus(self): |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 40 | # This pattern should match 'ls -ld /.' on any posix |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 41 | # system, however perversely configured. |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 42 | pat = r'''d......... # It is a directory. |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 43 | \s+\d+ # It has some number of links. |
| 44 | \s+\w+\s+\w+ # It has a user and group, which may |
| 45 | # be named anything. |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 46 | \s+\d+ # It has a size. |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 47 | [^/]* # Skip the date. |
Martin v. Löwis | cdbc131 | 2002-06-06 09:52:49 +0000 | [diff] [blame] | 48 | /\. # and end with the name of the file. |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 49 | ''' |
| 50 | |
Fred Drake | 4993c51 | 2002-04-01 23:56:03 +0000 | [diff] [blame] | 51 | self.assert_(re.match(pat, getstatus("/."), re.VERBOSE)) |
Fred Drake | 4c81d60 | 2001-07-23 04:08:01 +0000 | [diff] [blame] | 52 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 53 | |
| 54 | def test_main(): |
| 55 | run_unittest(CommandTests) |
| 56 | |
| 57 | |
| 58 | if __name__ == "__main__": |
| 59 | test_main() |