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.

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

mercurial