blob: b4e48ced7daefd48146f65a79615ecc268f15447 [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
7
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test.support import TestSkipped, run_unittest, reap_children
Fred Drake4c81d602001-07-23 04:08:01 +00009from 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
17if os.name != 'posix':
18 raise TestSkipped('Not posix; skipping test_commands')
19
20
21class 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 Rossum3b0a3292002-08-09 16:38:32 +000027 # 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.
Thomas Wouters590fe022003-03-25 18:50:19 +000030 dir = None
Guido van Rossum3b0a3292002-08-09 16:38:32 +000031 try:
32 dir = tempfile.mkdtemp()
33 name = os.path.join(dir, "foo")
34
35 status, output = getstatusoutput('cat ' + name)
36 self.assertNotEquals(status, 0)
37 finally:
Thomas Wouters590fe022003-03-25 18:50:19 +000038 if dir is not None:
39 os.rmdir(dir)
Fred Drake4c81d602001-07-23 04:08:01 +000040
Fred Drake2e2be372001-09-20 21:33:42 +000041
42def test_main():
43 run_unittest(CommandTests)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044 reap_children()
Fred Drake2e2be372001-09-20 21:33:42 +000045
Fred Drake2e2be372001-09-20 21:33:42 +000046if __name__ == "__main__":
47 test_main()