michael@0: #!/usr/bin/env python michael@0: # Copyright (c) 2002-2003 ActiveState Corp. michael@0: # Author: Trent Mick (TrentM@ActiveState.com) michael@0: michael@0: """Test suite for which.py.""" michael@0: michael@0: import sys michael@0: import os michael@0: import re michael@0: import tempfile michael@0: import unittest michael@0: michael@0: import testsupport michael@0: michael@0: #XXX:TODO michael@0: # - def test_registry_success(self): ...App Paths setting michael@0: # - def test_registry_noexist(self): michael@0: # - test all the other options michael@0: # - test on linux michael@0: # - test the module API michael@0: michael@0: class WhichTestCase(unittest.TestCase): michael@0: def setUp(self): michael@0: """Create a temp directory with a couple test "commands". michael@0: The temp dir can be added to the PATH, etc, for testing purposes. michael@0: """ michael@0: # Find the which.py to call. michael@0: whichPy = os.path.join(os.path.dirname(__file__), michael@0: os.pardir, "which.py") michael@0: self.which = sys.executable + " " + whichPy michael@0: michael@0: # Setup the test environment. michael@0: self.tmpdir = tempfile.mktemp() michael@0: os.makedirs(self.tmpdir) michael@0: if sys.platform.startswith("win"): michael@0: self.testapps = ['whichtestapp1.exe', michael@0: 'whichtestapp2.exe', michael@0: 'whichtestapp3.wta'] michael@0: else: michael@0: self.testapps = ['whichtestapp1', 'whichtestapp2'] michael@0: for app in self.testapps: michael@0: path = os.path.join(self.tmpdir, app) michael@0: open(path, 'wb').write('\n') michael@0: os.chmod(path, 0755) michael@0: michael@0: def tearDown(self): michael@0: testsupport.rmtree(self.tmpdir) michael@0: michael@0: def test_opt_h(self): michael@0: output, error, retval = testsupport.run(self.which+' --h') michael@0: token = 'Usage:' michael@0: self.failUnless(output.find(token) != -1, michael@0: "'%s' was not found in 'which -h' output: '%s' "\ michael@0: % (token, output)) michael@0: self.failUnless(retval == 0, michael@0: "'which -h' did not return 0: retval=%d" % retval) michael@0: michael@0: def test_opt_help(self): michael@0: output, error, retval = testsupport.run(self.which+' --help') michael@0: token = 'Usage:' michael@0: self.failUnless(output.find(token) != -1, michael@0: "'%s' was not found in 'which --help' output: '%s' "\ michael@0: % (token, output)) michael@0: self.failUnless(retval == 0, michael@0: "'which --help' did not return 0: retval=%d" % retval) michael@0: michael@0: def test_opt_version(self): michael@0: output, error, retval = testsupport.run(self.which+' --version') michael@0: versionRe = re.compile("^which \d+\.\d+\.\d+$") michael@0: versionMatch = versionRe.search(output.strip()) michael@0: self.failUnless(versionMatch, michael@0: "Version, '%s', from 'which --version' does not "\ michael@0: "match pattern, '%s'."\ michael@0: % (output.strip(), versionRe.pattern)) michael@0: self.failUnless(retval == 0, michael@0: "'which --version' did not return 0: retval=%d"\ michael@0: % retval) michael@0: michael@0: def test_no_args(self): michael@0: output, error, retval = testsupport.run(self.which) michael@0: self.failUnless(retval == -1, michael@0: "'which' with no args should return -1: retval=%d"\ michael@0: % retval) michael@0: michael@0: def test_one_failure(self): michael@0: output, error, retval = testsupport.run( michael@0: self.which+' whichtestapp1') michael@0: self.failUnless(retval == 1, michael@0: "One failure did not return 1: retval=%d" % retval) michael@0: michael@0: def test_two_failures(self): michael@0: output, error, retval = testsupport.run( michael@0: self.which+' whichtestapp1 whichtestapp2') michael@0: self.failUnless(retval == 2, michael@0: "Two failures did not return 2: retval=%d" % retval) michael@0: michael@0: def _match(self, path1, path2): michael@0: #print "_match: %r =?= %r" % (path1, path2) michael@0: if sys.platform.startswith('win'): michael@0: path1 = os.path.normpath(os.path.normcase(path1)) michael@0: path2 = os.path.normpath(os.path.normcase(path2)) michael@0: path1 = os.path.splitext(path1)[0] michael@0: path2 = os.path.splitext(path2)[0] michael@0: return path1 == path2 michael@0: else: michael@0: return os.path.samefile(path1, path2) michael@0: michael@0: def test_one_success(self): michael@0: os.environ["PATH"] += os.pathsep + self.tmpdir michael@0: output, error, retval = testsupport.run(self.which+' -q whichtestapp1') michael@0: expectedOutput = os.path.join(self.tmpdir, "whichtestapp1") michael@0: self.failUnless(self._match(output.strip(), expectedOutput), michael@0: "Output, %r, and expected output, %r, do not match."\ michael@0: % (output.strip(), expectedOutput)) michael@0: self.failUnless(retval == 0, michael@0: "'which ...' should have returned 0: retval=%d" % retval) michael@0: michael@0: def test_two_successes(self): michael@0: os.environ["PATH"] += os.pathsep + self.tmpdir michael@0: apps = ['whichtestapp1', 'whichtestapp2'] michael@0: output, error, retval = testsupport.run( michael@0: self.which + ' -q ' + ' '.join(apps)) michael@0: lines = output.strip().split("\n") michael@0: for app, line in zip(apps, lines): michael@0: expected = os.path.join(self.tmpdir, app) michael@0: self.failUnless(self._match(line, expected), michael@0: "Output, %r, and expected output, %r, do not match."\ michael@0: % (line, expected)) michael@0: self.failUnless(retval == 0, michael@0: "'which ...' should have returned 0: retval=%d" % retval) michael@0: michael@0: if sys.platform.startswith("win"): michael@0: def test_PATHEXT_failure(self): michael@0: os.environ["PATH"] += os.pathsep + self.tmpdir michael@0: output, error, retval = testsupport.run(self.which+' whichtestapp3') michael@0: self.failUnless(retval == 1, michael@0: "'which ...' should have returned 1: retval=%d" % retval) michael@0: michael@0: def test_PATHEXT_success(self): michael@0: os.environ["PATH"] += os.pathsep + self.tmpdir michael@0: os.environ["PATHEXT"] += os.pathsep + '.wta' michael@0: output, error, retval = testsupport.run(self.which+' whichtestapp3') michael@0: expectedOutput = os.path.join(self.tmpdir, "whichtestapp3") michael@0: self.failUnless(self._match(output.strip(), expectedOutput), michael@0: "Output, %r, and expected output, %r, do not match."\ michael@0: % (output.strip(), expectedOutput)) michael@0: self.failUnless(retval == 0, michael@0: "'which ...' should have returned 0: retval=%d" % retval) michael@0: michael@0: def test_exts(self): michael@0: os.environ["PATH"] += os.pathsep + self.tmpdir michael@0: output, error, retval = testsupport.run(self.which+' -e .wta whichtestapp3') michael@0: expectedOutput = os.path.join(self.tmpdir, "whichtestapp3") michael@0: self.failUnless(self._match(output.strip(), expectedOutput), michael@0: "Output, %r, and expected output, %r, do not match."\ michael@0: % (output.strip(), expectedOutput)) michael@0: self.failUnless(retval == 0, michael@0: "'which ...' should have returned 0: retval=%d" % retval) michael@0: michael@0: michael@0: michael@0: def suite(): michael@0: """Return a unittest.TestSuite to be used by test.py.""" michael@0: return unittest.makeSuite(WhichTestCase) michael@0: michael@0: if __name__ == "__main__": michael@0: unittest.main() michael@0: