2001-07-23 01:08:01 -03:00
|
|
|
'''
|
|
|
|
Tests for commands module
|
|
|
|
Nick Mathewson
|
|
|
|
'''
|
|
|
|
import unittest
|
|
|
|
import os, tempfile, re
|
2007-03-20 02:21:21 -03:00
|
|
|
|
2010-03-31 19:01:03 -03:00
|
|
|
from test.test_support import run_unittest, reap_children, import_module, \
|
|
|
|
check_warnings
|
2010-01-30 03:22:54 -04:00
|
|
|
|
|
|
|
# Silence Py3k warning
|
2010-02-07 13:03:15 -04:00
|
|
|
commands = import_module('commands', deprecated=True)
|
2001-07-23 01:08:01 -03:00
|
|
|
|
|
|
|
# The module says:
|
|
|
|
# "NB This only works (and is only relevant) for UNIX."
|
|
|
|
#
|
|
|
|
# Actually, getoutput should work on any platform with an os.popen, but
|
|
|
|
# I'll take the comment as given, and skip this suite.
|
|
|
|
|
|
|
|
if os.name != 'posix':
|
2009-03-26 18:10:30 -03:00
|
|
|
raise unittest.SkipTest('Not posix; skipping test_commands')
|
2001-07-23 01:08:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
class CommandTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_getoutput(self):
|
2010-02-07 13:03:15 -04:00
|
|
|
self.assertEquals(commands.getoutput('echo xyzzy'), 'xyzzy')
|
|
|
|
self.assertEquals(commands.getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
|
2001-07-23 01:08:01 -03:00
|
|
|
|
2002-08-09 13:38:32 -03:00
|
|
|
# we use mkdtemp in the next line to create an empty directory
|
|
|
|
# under our exclusive control; from that, we can invent a pathname
|
|
|
|
# that we _know_ won't exist. This is guaranteed to fail.
|
2003-03-25 14:50:19 -04:00
|
|
|
dir = None
|
2002-08-09 13:38:32 -03:00
|
|
|
try:
|
|
|
|
dir = tempfile.mkdtemp()
|
|
|
|
name = os.path.join(dir, "foo")
|
|
|
|
|
2010-02-07 13:03:15 -04:00
|
|
|
status, output = commands.getstatusoutput('cat ' + name)
|
2002-08-09 13:38:32 -03:00
|
|
|
self.assertNotEquals(status, 0)
|
|
|
|
finally:
|
2003-03-25 14:50:19 -04:00
|
|
|
if dir is not None:
|
|
|
|
os.rmdir(dir)
|
2001-07-23 01:08:01 -03:00
|
|
|
|
|
|
|
def test_getstatus(self):
|
2002-04-01 19:56:03 -04:00
|
|
|
# This pattern should match 'ls -ld /.' on any posix
|
2002-09-30 12:44:41 -03:00
|
|
|
# system, however perversely configured. Even on systems
|
|
|
|
# (e.g., Cygwin) where user and group names can have spaces:
|
|
|
|
# drwxr-xr-x 15 Administ Domain U 4096 Aug 12 12:50 /
|
|
|
|
# drwxr-xr-x 15 Joe User My Group 4096 Aug 12 12:50 /
|
|
|
|
# Note that the first case above has a space in the group name
|
|
|
|
# while the second one has a space in both names.
|
2002-04-01 19:56:03 -04:00
|
|
|
pat = r'''d......... # It is a directory.
|
2002-12-05 16:18:39 -04:00
|
|
|
\+? # It may have ACLs.
|
2001-07-23 01:08:01 -03:00
|
|
|
\s+\d+ # It has some number of links.
|
2002-09-30 12:44:41 -03:00
|
|
|
[^/]* # Skip user, group, size, and date.
|
2002-06-06 06:52:49 -03:00
|
|
|
/\. # and end with the name of the file.
|
2001-07-23 01:08:01 -03:00
|
|
|
'''
|
|
|
|
|
2010-03-31 19:01:03 -03:00
|
|
|
with check_warnings((".*commands.getstatus.. is deprecated",
|
|
|
|
DeprecationWarning)):
|
|
|
|
self.assertTrue(re.match(pat, commands.getstatus("/."), re.VERBOSE))
|
2001-07-23 01:08:01 -03:00
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(CommandTests)
|
2006-06-29 01:10:08 -03:00
|
|
|
reap_children()
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|