python/which/test/test_which.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 #!/usr/bin/env python
     2 # Copyright (c) 2002-2003 ActiveState Corp.
     3 # Author: Trent Mick (TrentM@ActiveState.com)
     5 """Test suite for which.py."""
     7 import sys
     8 import os
     9 import re
    10 import tempfile
    11 import unittest
    13 import testsupport
    15 #XXX:TODO
    16 #   - def test_registry_success(self): ...App Paths setting
    17 #   - def test_registry_noexist(self):
    18 #   - test all the other options
    19 #   - test on linux
    20 #   - test the module API
    22 class WhichTestCase(unittest.TestCase):
    23     def setUp(self):
    24         """Create a temp directory with a couple test "commands".
    25         The temp dir can be added to the PATH, etc, for testing purposes.
    26         """
    27         # Find the which.py to call.
    28         whichPy = os.path.join(os.path.dirname(__file__),
    29                                os.pardir, "which.py")
    30         self.which = sys.executable + " " + whichPy
    32         # Setup the test environment.
    33         self.tmpdir = tempfile.mktemp()
    34         os.makedirs(self.tmpdir)
    35         if sys.platform.startswith("win"):
    36             self.testapps = ['whichtestapp1.exe',
    37                              'whichtestapp2.exe',
    38                              'whichtestapp3.wta']
    39         else:
    40             self.testapps = ['whichtestapp1', 'whichtestapp2']
    41         for app in self.testapps:
    42             path = os.path.join(self.tmpdir, app)
    43             open(path, 'wb').write('\n')
    44             os.chmod(path, 0755)
    46     def tearDown(self):
    47         testsupport.rmtree(self.tmpdir)
    49     def test_opt_h(self):
    50         output, error, retval = testsupport.run(self.which+' --h')
    51         token = 'Usage:'
    52         self.failUnless(output.find(token) != -1,
    53                         "'%s' was not found in 'which -h' output: '%s' "\
    54                         % (token, output))
    55         self.failUnless(retval == 0,
    56                         "'which -h' did not return 0: retval=%d" % retval)
    58     def test_opt_help(self):
    59         output, error, retval = testsupport.run(self.which+' --help')
    60         token = 'Usage:'
    61         self.failUnless(output.find(token) != -1,
    62                         "'%s' was not found in 'which --help' output: '%s' "\
    63                         % (token, output))
    64         self.failUnless(retval == 0,
    65                         "'which --help' did not return 0: retval=%d" % retval)
    67     def test_opt_version(self):
    68         output, error, retval = testsupport.run(self.which+' --version')
    69         versionRe = re.compile("^which \d+\.\d+\.\d+$")
    70         versionMatch = versionRe.search(output.strip())
    71         self.failUnless(versionMatch,
    72                         "Version, '%s', from 'which --version' does not "\
    73                         "match pattern, '%s'."\
    74                         % (output.strip(), versionRe.pattern))
    75         self.failUnless(retval == 0,
    76                         "'which --version' did not return 0: retval=%d"\
    77                         % retval)
    79     def test_no_args(self):
    80         output, error, retval = testsupport.run(self.which)
    81         self.failUnless(retval == -1,
    82                         "'which' with no args should return -1: retval=%d"\
    83                         % retval)
    85     def test_one_failure(self):
    86         output, error, retval = testsupport.run(
    87             self.which+' whichtestapp1')
    88         self.failUnless(retval == 1,
    89             "One failure did not return 1: retval=%d" % retval)
    91     def test_two_failures(self):
    92         output, error, retval = testsupport.run(
    93             self.which+' whichtestapp1 whichtestapp2')
    94         self.failUnless(retval == 2,
    95             "Two failures did not return 2: retval=%d" % retval)
    97     def _match(self, path1, path2):
    98         #print "_match: %r =?= %r" % (path1, path2)
    99         if sys.platform.startswith('win'):
   100             path1 = os.path.normpath(os.path.normcase(path1))
   101             path2 = os.path.normpath(os.path.normcase(path2))
   102             path1 = os.path.splitext(path1)[0]
   103             path2 = os.path.splitext(path2)[0]
   104             return path1 == path2
   105         else:
   106             return os.path.samefile(path1, path2)
   108     def test_one_success(self):
   109         os.environ["PATH"] += os.pathsep + self.tmpdir 
   110         output, error, retval = testsupport.run(self.which+' -q whichtestapp1')
   111         expectedOutput = os.path.join(self.tmpdir, "whichtestapp1")
   112         self.failUnless(self._match(output.strip(), expectedOutput),
   113             "Output, %r, and expected output, %r, do not match."\
   114             % (output.strip(), expectedOutput))
   115         self.failUnless(retval == 0,
   116             "'which ...' should have returned 0: retval=%d" % retval)
   118     def test_two_successes(self):
   119         os.environ["PATH"] += os.pathsep + self.tmpdir 
   120         apps = ['whichtestapp1', 'whichtestapp2']
   121         output, error, retval = testsupport.run(
   122             self.which + ' -q ' + ' '.join(apps))
   123         lines = output.strip().split("\n")
   124         for app, line in zip(apps, lines):
   125             expected = os.path.join(self.tmpdir, app)
   126             self.failUnless(self._match(line, expected),
   127                 "Output, %r, and expected output, %r, do not match."\
   128                 % (line, expected))
   129         self.failUnless(retval == 0,
   130             "'which ...' should have returned 0: retval=%d" % retval)
   132     if sys.platform.startswith("win"):
   133         def test_PATHEXT_failure(self):
   134             os.environ["PATH"] += os.pathsep + self.tmpdir 
   135             output, error, retval = testsupport.run(self.which+' whichtestapp3')
   136             self.failUnless(retval == 1,
   137                 "'which ...' should have returned 1: retval=%d" % retval)
   139         def test_PATHEXT_success(self):
   140             os.environ["PATH"] += os.pathsep + self.tmpdir 
   141             os.environ["PATHEXT"] += os.pathsep + '.wta'
   142             output, error, retval = testsupport.run(self.which+' whichtestapp3')
   143             expectedOutput = os.path.join(self.tmpdir, "whichtestapp3")
   144             self.failUnless(self._match(output.strip(), expectedOutput),
   145                 "Output, %r, and expected output, %r, do not match."\
   146                 % (output.strip(), expectedOutput))
   147             self.failUnless(retval == 0,
   148                 "'which ...' should have returned 0: retval=%d" % retval)
   150         def test_exts(self):
   151             os.environ["PATH"] += os.pathsep + self.tmpdir 
   152             output, error, retval = testsupport.run(self.which+' -e .wta whichtestapp3')
   153             expectedOutput = os.path.join(self.tmpdir, "whichtestapp3")
   154             self.failUnless(self._match(output.strip(), expectedOutput),
   155                 "Output, %r, and expected output, %r, do not match."\
   156                 % (output.strip(), expectedOutput))
   157             self.failUnless(retval == 0,
   158                 "'which ...' should have returned 0: retval=%d" % retval)
   162 def suite():
   163     """Return a unittest.TestSuite to be used by test.py."""
   164     return unittest.makeSuite(WhichTestCase)
   166 if __name__ == "__main__":
   167     unittest.main()

mercurial