| 1 |
''' |
| 2 |
Tests for commands module |
| 3 |
Nick Mathewson |
| 4 |
''' |
| 5 |
import unittest |
| 6 |
import os, tempfile, re |
| 7 |
|
| 8 |
from test.test_support import run_unittest, reap_children, import_module, \ |
| 9 |
check_warnings |
| 10 |
|
| 11 |
|
| 12 |
commands = import_module('commands', deprecated=True) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
if os.name != 'posix': |
| 21 |
raise unittest.SkipTest('Not posix; skipping test_commands') |
| 22 |
|
| 23 |
|
| 24 |
class CommandTests(unittest.TestCase): |
| 25 |
|
| 26 |
def test_getoutput(self): |
| 27 |
self.assertEqual(commands.getoutput('echo xyzzy'), 'xyzzy') |
| 28 |
self.assertEqual(commands.getstatusoutput('echo xyzzy'), (0, 'xyzzy')) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
dir = None |
| 34 |
try: |
| 35 |
dir = tempfile.mkdtemp() |
| 36 |
name = os.path.join(dir, "foo") |
| 37 |
|
| 38 |
status, output = commands.getstatusoutput('cat ' + name) |
| 39 |
self.assertNotEqual(status, 0) |
| 40 |
finally: |
| 41 |
if dir is not None: |
| 42 |
os.rmdir(dir) |
| 43 |
|
| 44 |
def test_getstatus(self): |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
pat = r'''d......... # It is a directory. |
| 57 |
[.+@]? # It may have special attributes. |
| 58 |
\s+\d+ # It has some number of links. |
| 59 |
[^/]* # Skip user, group, size, and date. |
| 60 |
/\. # and end with the name of the file. |
| 61 |
''' |
| 62 |
|
| 63 |
with check_warnings((".*commands.getstatus.. is deprecated", |
| 64 |
DeprecationWarning)): |
| 65 |
self.assertTrue(re.match(pat, commands.getstatus("/."), re.VERBOSE)) |
| 66 |
|
| 67 |
|
| 68 |
def test_main(): |
| 69 |
run_unittest(CommandTests) |
| 70 |
reap_children() |
| 71 |
|
| 72 |
|
| 73 |
if __name__ == "__main__": |
| 74 |
test_main() |