python/which/test/test_which.py

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:3a988dc2462c
1 #!/usr/bin/env python
2 # Copyright (c) 2002-2003 ActiveState Corp.
3 # Author: Trent Mick (TrentM@ActiveState.com)
4
5 """Test suite for which.py."""
6
7 import sys
8 import os
9 import re
10 import tempfile
11 import unittest
12
13 import testsupport
14
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
21
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
31
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)
45
46 def tearDown(self):
47 testsupport.rmtree(self.tmpdir)
48
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)
57
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)
66
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)
78
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)
84
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)
90
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)
96
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)
107
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)
117
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)
131
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)
138
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)
149
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)
159
160
161
162 def suite():
163 """Return a unittest.TestSuite to be used by test.py."""
164 return unittest.makeSuite(WhichTestCase)
165
166 if __name__ == "__main__":
167 unittest.main()
168

mercurial