summaryrefslogtreecommitdiffstats
path: root/python/which/test/test_which.py
blob: b56976989729bfc7d117e6ac5bf6a2e85990b83a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# Copyright (c) 2002-2003 ActiveState Corp.
# Author: Trent Mick (TrentM@ActiveState.com)

"""Test suite for which.py."""

import sys
import os
import re
import tempfile
import unittest

import testsupport

#XXX:TODO
#   - def test_registry_success(self): ...App Paths setting
#   - def test_registry_noexist(self):
#   - test all the other options
#   - test on linux
#   - test the module API

class WhichTestCase(unittest.TestCase):
    def setUp(self):
        """Create a temp directory with a couple test "commands".
        The temp dir can be added to the PATH, etc, for testing purposes.
        """
        # Find the which.py to call.
        whichPy = os.path.join(os.path.dirname(__file__),
                               os.pardir, "which.py")
        self.which = sys.executable + " " + whichPy

        # Setup the test environment.
        self.tmpdir = tempfile.mktemp()
        os.makedirs(self.tmpdir)
        if sys.platform.startswith("win"):
            self.testapps = ['whichtestapp1.exe',
                             'whichtestapp2.exe',
                             'whichtestapp3.wta']
        else:
            self.testapps = ['whichtestapp1', 'whichtestapp2']
        for app in self.testapps:
            path = os.path.join(self.tmpdir, app)
            open(path, 'wb').write('\n')
            os.chmod(path, 0755)

    def tearDown(self):
        testsupport.rmtree(self.tmpdir)

    def test_opt_h(self):
        output, error, retval = testsupport.run(self.which+' --h')
        token = 'Usage:'
        self.failUnless(output.find(token) != -1,
                        "'%s' was not found in 'which -h' output: '%s' "\
                        % (token, output))
        self.failUnless(retval == 0,
                        "'which -h' did not return 0: retval=%d" % retval)

    def test_opt_help(self):
        output, error, retval = testsupport.run(self.which+' --help')
        token = 'Usage:'
        self.failUnless(output.find(token) != -1,
                        "'%s' was not found in 'which --help' output: '%s' "\
                        % (token, output))
        self.failUnless(retval == 0,
                        "'which --help' did not return 0: retval=%d" % retval)

    def test_opt_version(self):
        output, error, retval = testsupport.run(self.which+' --version')
        versionRe = re.compile("^which \d+\.\d+\.\d+$")
        versionMatch = versionRe.search(output.strip())
        self.failUnless(versionMatch,
                        "Version, '%s', from 'which --version' does not "\
                        "match pattern, '%s'."\
                        % (output.strip(), versionRe.pattern))
        self.failUnless(retval == 0,
                        "'which --version' did not return 0: retval=%d"\
                        % retval)

    def test_no_args(self):
        output, error, retval = testsupport.run(self.which)
        self.failUnless(retval == -1,
                        "'which' with no args should return -1: retval=%d"\
                        % retval)

    def test_one_failure(self):
        output, error, retval = testsupport.run(
            self.which+' whichtestapp1')
        self.failUnless(retval == 1,
            "One failure did not return 1: retval=%d" % retval)

    def test_two_failures(self):
        output, error, retval = testsupport.run(
            self.which+' whichtestapp1 whichtestapp2')
        self.failUnless(retval == 2,
            "Two failures did not return 2: retval=%d" % retval)

    def _match(self, path1, path2):
        #print "_match: %r =?= %r" % (path1, path2)
        if sys.platform.startswith('win'):
            path1 = os.path.normpath(os.path.normcase(path1))
            path2 = os.path.normpath(os.path.normcase(path2))
            path1 = os.path.splitext(path1)[0]
            path2 = os.path.splitext(path2)[0]
            return path1 == path2
        else:
            return os.path.samefile(path1, path2)

    def test_one_success(self):
        os.environ["PATH"] += os.pathsep + self.tmpdir 
        output, error, retval = testsupport.run(self.which+' -q whichtestapp1')
        expectedOutput = os.path.join(self.tmpdir, "whichtestapp1")
        self.failUnless(self._match(output.strip(), expectedOutput),
            "Output, %r, and expected output, %r, do not match."\
            % (output.strip(), expectedOutput))
        self.failUnless(retval == 0,
            "'which ...' should have returned 0: retval=%d" % retval)

    def test_two_successes(self):
        os.environ["PATH"] += os.pathsep + self.tmpdir 
        apps = ['whichtestapp1', 'whichtestapp2']
        output, error, retval = testsupport.run(
            self.which + ' -q ' + ' '.join(apps))
        lines = output.strip().split("\n")
        for app, line in zip(apps, lines):
            expected = os.path.join(self.tmpdir, app)
            self.failUnless(self._match(line, expected),
                "Output, %r, and expected output, %r, do not match."\
                % (line, expected))
        self.failUnless(retval == 0,
            "'which ...' should have returned 0: retval=%d" % retval)

    if sys.platform.startswith("win"):
        def test_PATHEXT_failure(self):
            os.environ["PATH"] += os.pathsep + self.tmpdir 
            output, error, retval = testsupport.run(self.which+' whichtestapp3')
            self.failUnless(retval == 1,
                "'which ...' should have returned 1: retval=%d" % retval)

        def test_PATHEXT_success(self):
            os.environ["PATH"] += os.pathsep + self.tmpdir 
            os.environ["PATHEXT"] += os.pathsep + '.wta'
            output, error, retval = testsupport.run(self.which+' whichtestapp3')
            expectedOutput = os.path.join(self.tmpdir, "whichtestapp3")
            self.failUnless(self._match(output.strip(), expectedOutput),
                "Output, %r, and expected output, %r, do not match."\
                % (output.strip(), expectedOutput))
            self.failUnless(retval == 0,
                "'which ...' should have returned 0: retval=%d" % retval)

        def test_exts(self):
            os.environ["PATH"] += os.pathsep + self.tmpdir 
            output, error, retval = testsupport.run(self.which+' -e .wta whichtestapp3')
            expectedOutput = os.path.join(self.tmpdir, "whichtestapp3")
            self.failUnless(self._match(output.strip(), expectedOutput),
                "Output, %r, and expected output, %r, do not match."\
                % (output.strip(), expectedOutput))
            self.failUnless(retval == 0,
                "'which ...' should have returned 0: retval=%d" % retval)



def suite():
    """Return a unittest.TestSuite to be used by test.py."""
    return unittest.makeSuite(WhichTestCase)

if __name__ == "__main__":
    unittest.main()