diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /python/which/test | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'python/which/test')
-rw-r--r-- | python/which/test/test_which.py | 168 | ||||
-rw-r--r-- | python/which/test/testsupport.py | 83 |
2 files changed, 251 insertions, 0 deletions
diff --git a/python/which/test/test_which.py b/python/which/test/test_which.py new file mode 100644 index 000000000..b56976989 --- /dev/null +++ b/python/which/test/test_which.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python +# Copyright (c) 2002-2003 ActiveState Corp. +# Author: Trent Mick (TrentM@ActiveState.com) + +"""Test suite for which.py.""" + +import sys +import os +import re +import tempfile +import unittest + +import testsupport + +#XXX:TODO +# - def test_registry_success(self): ...App Paths setting +# - def test_registry_noexist(self): +# - test all the other options +# - test on linux +# - test the module API + +class WhichTestCase(unittest.TestCase): + def setUp(self): + """Create a temp directory with a couple test "commands". + The temp dir can be added to the PATH, etc, for testing purposes. + """ + # Find the which.py to call. + whichPy = os.path.join(os.path.dirname(__file__), + os.pardir, "which.py") + self.which = sys.executable + " " + whichPy + + # Setup the test environment. + self.tmpdir = tempfile.mktemp() + os.makedirs(self.tmpdir) + if sys.platform.startswith("win"): + self.testapps = ['whichtestapp1.exe', + 'whichtestapp2.exe', + 'whichtestapp3.wta'] + else: + self.testapps = ['whichtestapp1', 'whichtestapp2'] + for app in self.testapps: + path = os.path.join(self.tmpdir, app) + open(path, 'wb').write('\n') + os.chmod(path, 0755) + + def tearDown(self): + testsupport.rmtree(self.tmpdir) + + def test_opt_h(self): + output, error, retval = testsupport.run(self.which+' --h') + token = 'Usage:' + self.failUnless(output.find(token) != -1, + "'%s' was not found in 'which -h' output: '%s' "\ + % (token, output)) + self.failUnless(retval == 0, + "'which -h' did not return 0: retval=%d" % retval) + + def test_opt_help(self): + output, error, retval = testsupport.run(self.which+' --help') + token = 'Usage:' + self.failUnless(output.find(token) != -1, + "'%s' was not found in 'which --help' output: '%s' "\ + % (token, output)) + self.failUnless(retval == 0, + "'which --help' did not return 0: retval=%d" % retval) + + def test_opt_version(self): + output, error, retval = testsupport.run(self.which+' --version') + versionRe = re.compile("^which \d+\.\d+\.\d+$") + versionMatch = versionRe.search(output.strip()) + self.failUnless(versionMatch, + "Version, '%s', from 'which --version' does not "\ + "match pattern, '%s'."\ + % (output.strip(), versionRe.pattern)) + self.failUnless(retval == 0, + "'which --version' did not return 0: retval=%d"\ + % retval) + + def test_no_args(self): + output, error, retval = testsupport.run(self.which) + self.failUnless(retval == -1, + "'which' with no args should return -1: retval=%d"\ + % retval) + + def test_one_failure(self): + output, error, retval = testsupport.run( + self.which+' whichtestapp1') + self.failUnless(retval == 1, + "One failure did not return 1: retval=%d" % retval) + + def test_two_failures(self): + output, error, retval = testsupport.run( + self.which+' whichtestapp1 whichtestapp2') + self.failUnless(retval == 2, + "Two failures did not return 2: retval=%d" % retval) + + def _match(self, path1, path2): + #print "_match: %r =?= %r" % (path1, path2) + if sys.platform.startswith('win'): + path1 = os.path.normpath(os.path.normcase(path1)) + path2 = os.path.normpath(os.path.normcase(path2)) + path1 = os.path.splitext(path1)[0] + path2 = os.path.splitext(path2)[0] + return path1 == path2 + else: + return os.path.samefile(path1, path2) + + def test_one_success(self): + os.environ["PATH"] += os.pathsep + self.tmpdir + output, error, retval = testsupport.run(self.which+' -q whichtestapp1') + expectedOutput = os.path.join(self.tmpdir, "whichtestapp1") + self.failUnless(self._match(output.strip(), expectedOutput), + "Output, %r, and expected output, %r, do not match."\ + % (output.strip(), expectedOutput)) + self.failUnless(retval == 0, + "'which ...' should have returned 0: retval=%d" % retval) + + def test_two_successes(self): + os.environ["PATH"] += os.pathsep + self.tmpdir + apps = ['whichtestapp1', 'whichtestapp2'] + output, error, retval = testsupport.run( + self.which + ' -q ' + ' '.join(apps)) + lines = output.strip().split("\n") + for app, line in zip(apps, lines): + expected = os.path.join(self.tmpdir, app) + self.failUnless(self._match(line, expected), + "Output, %r, and expected output, %r, do not match."\ + % (line, expected)) + self.failUnless(retval == 0, + "'which ...' should have returned 0: retval=%d" % retval) + + if sys.platform.startswith("win"): + def test_PATHEXT_failure(self): + os.environ["PATH"] += os.pathsep + self.tmpdir + output, error, retval = testsupport.run(self.which+' whichtestapp3') + self.failUnless(retval == 1, + "'which ...' should have returned 1: retval=%d" % retval) + + def test_PATHEXT_success(self): + os.environ["PATH"] += os.pathsep + self.tmpdir + os.environ["PATHEXT"] += os.pathsep + '.wta' + output, error, retval = testsupport.run(self.which+' whichtestapp3') + expectedOutput = os.path.join(self.tmpdir, "whichtestapp3") + self.failUnless(self._match(output.strip(), expectedOutput), + "Output, %r, and expected output, %r, do not match."\ + % (output.strip(), expectedOutput)) + self.failUnless(retval == 0, + "'which ...' should have returned 0: retval=%d" % retval) + + def test_exts(self): + os.environ["PATH"] += os.pathsep + self.tmpdir + output, error, retval = testsupport.run(self.which+' -e .wta whichtestapp3') + expectedOutput = os.path.join(self.tmpdir, "whichtestapp3") + self.failUnless(self._match(output.strip(), expectedOutput), + "Output, %r, and expected output, %r, do not match."\ + % (output.strip(), expectedOutput)) + self.failUnless(retval == 0, + "'which ...' should have returned 0: retval=%d" % retval) + + + +def suite(): + """Return a unittest.TestSuite to be used by test.py.""" + return unittest.makeSuite(WhichTestCase) + +if __name__ == "__main__": + unittest.main() + diff --git a/python/which/test/testsupport.py b/python/which/test/testsupport.py new file mode 100644 index 000000000..8ff519fad --- /dev/null +++ b/python/which/test/testsupport.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# Copyright (c) 2002-2003 ActiveState Corp. +# Author: Trent Mick (TrentM@ActiveState.com) + +import os +import sys +import types + + +#---- Support routines + +def _escapeArg(arg): + """Escape the given command line argument for the shell.""" + #XXX There is a *lot* more that we should escape here. + return arg.replace('"', r'\"') + + +def _joinArgv(argv): + r"""Join an arglist to a string appropriate for running. + >>> import os + >>> _joinArgv(['foo', 'bar "baz']) + 'foo "bar \\"baz"' + """ + cmdstr = "" + for arg in argv: + if ' ' in arg: + cmdstr += '"%s"' % _escapeArg(arg) + else: + cmdstr += _escapeArg(arg) + cmdstr += ' ' + if cmdstr.endswith(' '): cmdstr = cmdstr[:-1] # strip trailing space + return cmdstr + + +def run(argv): + """Prepare and run the given arg vector, 'argv', and return the + results. Returns (<stdout lines>, <stderr lines>, <return value>). + Note: 'argv' may also just be the command string. + """ + if type(argv) in (types.ListType, types.TupleType): + cmd = _joinArgv(argv) + else: + cmd = argv + if sys.platform.startswith('win'): + i, o, e = os.popen3(cmd) + output = o.read() + error = e.read() + i.close() + e.close() + try: + retval = o.close() + except IOError: + # IOError is raised iff the spawned app returns -1. Go + # figure. + retval = -1 + if retval is None: + retval = 0 + else: + import popen2 + p = popen2.Popen3(cmd, 1) + i, o, e = p.tochild, p.fromchild, p.childerr + output = o.read() + error = e.read() + i.close() + o.close() + e.close() + retval = (p.wait() & 0xFF00) >> 8 + if retval > 2**7: # 8-bit signed 1's-complement conversion + retval -= 2**8 + return output, error, retval + + +def _rmtreeOnError(rmFunction, filePath, excInfo): + if excInfo[0] == OSError: + # presuming because file is read-only + os.chmod(filePath, 0777) + rmFunction(filePath) + +def rmtree(dirname): + import shutil + shutil.rmtree(dirname, 0, _rmtreeOnError) + + |