python/which/test/test_which.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/python/which/test/test_which.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,168 @@
     1.4 +#!/usr/bin/env python
     1.5 +# Copyright (c) 2002-2003 ActiveState Corp.
     1.6 +# Author: Trent Mick (TrentM@ActiveState.com)
     1.7 +
     1.8 +"""Test suite for which.py."""
     1.9 +
    1.10 +import sys
    1.11 +import os
    1.12 +import re
    1.13 +import tempfile
    1.14 +import unittest
    1.15 +
    1.16 +import testsupport
    1.17 +
    1.18 +#XXX:TODO
    1.19 +#   - def test_registry_success(self): ...App Paths setting
    1.20 +#   - def test_registry_noexist(self):
    1.21 +#   - test all the other options
    1.22 +#   - test on linux
    1.23 +#   - test the module API
    1.24 +
    1.25 +class WhichTestCase(unittest.TestCase):
    1.26 +    def setUp(self):
    1.27 +        """Create a temp directory with a couple test "commands".
    1.28 +        The temp dir can be added to the PATH, etc, for testing purposes.
    1.29 +        """
    1.30 +        # Find the which.py to call.
    1.31 +        whichPy = os.path.join(os.path.dirname(__file__),
    1.32 +                               os.pardir, "which.py")
    1.33 +        self.which = sys.executable + " " + whichPy
    1.34 +
    1.35 +        # Setup the test environment.
    1.36 +        self.tmpdir = tempfile.mktemp()
    1.37 +        os.makedirs(self.tmpdir)
    1.38 +        if sys.platform.startswith("win"):
    1.39 +            self.testapps = ['whichtestapp1.exe',
    1.40 +                             'whichtestapp2.exe',
    1.41 +                             'whichtestapp3.wta']
    1.42 +        else:
    1.43 +            self.testapps = ['whichtestapp1', 'whichtestapp2']
    1.44 +        for app in self.testapps:
    1.45 +            path = os.path.join(self.tmpdir, app)
    1.46 +            open(path, 'wb').write('\n')
    1.47 +            os.chmod(path, 0755)
    1.48 +
    1.49 +    def tearDown(self):
    1.50 +        testsupport.rmtree(self.tmpdir)
    1.51 +
    1.52 +    def test_opt_h(self):
    1.53 +        output, error, retval = testsupport.run(self.which+' --h')
    1.54 +        token = 'Usage:'
    1.55 +        self.failUnless(output.find(token) != -1,
    1.56 +                        "'%s' was not found in 'which -h' output: '%s' "\
    1.57 +                        % (token, output))
    1.58 +        self.failUnless(retval == 0,
    1.59 +                        "'which -h' did not return 0: retval=%d" % retval)
    1.60 +
    1.61 +    def test_opt_help(self):
    1.62 +        output, error, retval = testsupport.run(self.which+' --help')
    1.63 +        token = 'Usage:'
    1.64 +        self.failUnless(output.find(token) != -1,
    1.65 +                        "'%s' was not found in 'which --help' output: '%s' "\
    1.66 +                        % (token, output))
    1.67 +        self.failUnless(retval == 0,
    1.68 +                        "'which --help' did not return 0: retval=%d" % retval)
    1.69 +
    1.70 +    def test_opt_version(self):
    1.71 +        output, error, retval = testsupport.run(self.which+' --version')
    1.72 +        versionRe = re.compile("^which \d+\.\d+\.\d+$")
    1.73 +        versionMatch = versionRe.search(output.strip())
    1.74 +        self.failUnless(versionMatch,
    1.75 +                        "Version, '%s', from 'which --version' does not "\
    1.76 +                        "match pattern, '%s'."\
    1.77 +                        % (output.strip(), versionRe.pattern))
    1.78 +        self.failUnless(retval == 0,
    1.79 +                        "'which --version' did not return 0: retval=%d"\
    1.80 +                        % retval)
    1.81 +
    1.82 +    def test_no_args(self):
    1.83 +        output, error, retval = testsupport.run(self.which)
    1.84 +        self.failUnless(retval == -1,
    1.85 +                        "'which' with no args should return -1: retval=%d"\
    1.86 +                        % retval)
    1.87 +
    1.88 +    def test_one_failure(self):
    1.89 +        output, error, retval = testsupport.run(
    1.90 +            self.which+' whichtestapp1')
    1.91 +        self.failUnless(retval == 1,
    1.92 +            "One failure did not return 1: retval=%d" % retval)
    1.93 +
    1.94 +    def test_two_failures(self):
    1.95 +        output, error, retval = testsupport.run(
    1.96 +            self.which+' whichtestapp1 whichtestapp2')
    1.97 +        self.failUnless(retval == 2,
    1.98 +            "Two failures did not return 2: retval=%d" % retval)
    1.99 +
   1.100 +    def _match(self, path1, path2):
   1.101 +        #print "_match: %r =?= %r" % (path1, path2)
   1.102 +        if sys.platform.startswith('win'):
   1.103 +            path1 = os.path.normpath(os.path.normcase(path1))
   1.104 +            path2 = os.path.normpath(os.path.normcase(path2))
   1.105 +            path1 = os.path.splitext(path1)[0]
   1.106 +            path2 = os.path.splitext(path2)[0]
   1.107 +            return path1 == path2
   1.108 +        else:
   1.109 +            return os.path.samefile(path1, path2)
   1.110 +
   1.111 +    def test_one_success(self):
   1.112 +        os.environ["PATH"] += os.pathsep + self.tmpdir 
   1.113 +        output, error, retval = testsupport.run(self.which+' -q whichtestapp1')
   1.114 +        expectedOutput = os.path.join(self.tmpdir, "whichtestapp1")
   1.115 +        self.failUnless(self._match(output.strip(), expectedOutput),
   1.116 +            "Output, %r, and expected output, %r, do not match."\
   1.117 +            % (output.strip(), expectedOutput))
   1.118 +        self.failUnless(retval == 0,
   1.119 +            "'which ...' should have returned 0: retval=%d" % retval)
   1.120 +
   1.121 +    def test_two_successes(self):
   1.122 +        os.environ["PATH"] += os.pathsep + self.tmpdir 
   1.123 +        apps = ['whichtestapp1', 'whichtestapp2']
   1.124 +        output, error, retval = testsupport.run(
   1.125 +            self.which + ' -q ' + ' '.join(apps))
   1.126 +        lines = output.strip().split("\n")
   1.127 +        for app, line in zip(apps, lines):
   1.128 +            expected = os.path.join(self.tmpdir, app)
   1.129 +            self.failUnless(self._match(line, expected),
   1.130 +                "Output, %r, and expected output, %r, do not match."\
   1.131 +                % (line, expected))
   1.132 +        self.failUnless(retval == 0,
   1.133 +            "'which ...' should have returned 0: retval=%d" % retval)
   1.134 +
   1.135 +    if sys.platform.startswith("win"):
   1.136 +        def test_PATHEXT_failure(self):
   1.137 +            os.environ["PATH"] += os.pathsep + self.tmpdir 
   1.138 +            output, error, retval = testsupport.run(self.which+' whichtestapp3')
   1.139 +            self.failUnless(retval == 1,
   1.140 +                "'which ...' should have returned 1: retval=%d" % retval)
   1.141 +
   1.142 +        def test_PATHEXT_success(self):
   1.143 +            os.environ["PATH"] += os.pathsep + self.tmpdir 
   1.144 +            os.environ["PATHEXT"] += os.pathsep + '.wta'
   1.145 +            output, error, retval = testsupport.run(self.which+' whichtestapp3')
   1.146 +            expectedOutput = os.path.join(self.tmpdir, "whichtestapp3")
   1.147 +            self.failUnless(self._match(output.strip(), expectedOutput),
   1.148 +                "Output, %r, and expected output, %r, do not match."\
   1.149 +                % (output.strip(), expectedOutput))
   1.150 +            self.failUnless(retval == 0,
   1.151 +                "'which ...' should have returned 0: retval=%d" % retval)
   1.152 +
   1.153 +        def test_exts(self):
   1.154 +            os.environ["PATH"] += os.pathsep + self.tmpdir 
   1.155 +            output, error, retval = testsupport.run(self.which+' -e .wta whichtestapp3')
   1.156 +            expectedOutput = os.path.join(self.tmpdir, "whichtestapp3")
   1.157 +            self.failUnless(self._match(output.strip(), expectedOutput),
   1.158 +                "Output, %r, and expected output, %r, do not match."\
   1.159 +                % (output.strip(), expectedOutput))
   1.160 +            self.failUnless(retval == 0,
   1.161 +                "'which ...' should have returned 0: retval=%d" % retval)
   1.162 +
   1.163 +
   1.164 +
   1.165 +def suite():
   1.166 +    """Return a unittest.TestSuite to be used by test.py."""
   1.167 +    return unittest.makeSuite(WhichTestCase)
   1.168 +
   1.169 +if __name__ == "__main__":
   1.170 +    unittest.main()
   1.171 +

mercurial