summaryrefslogtreecommitdiffstats
path: root/Lib/packaging/tests/test_command_test.py
blob: 7aa1f799107440657d866acece7d4c4978e4e585 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
import re
import sys
import shutil
import unittest as ut1
import packaging.database

from os.path import join
from operator import getitem, setitem, delitem
from packaging.command.build import build
from packaging.tests import unittest
from packaging.tests.support import (TempdirManager, EnvironRestorer,
                                     LoggingCatcher)
from packaging.command.test import test
from packaging.command import set_command
from packaging.dist import Distribution


EXPECTED_OUTPUT_RE = r'''FAIL: test_blah \(myowntestmodule.SomeTest\)
----------------------------------------------------------------------
Traceback \(most recent call last\):
  File ".+/myowntestmodule.py", line \d+, in test_blah
    self.fail\("horribly"\)
AssertionError: horribly
'''

here = os.path.dirname(os.path.abspath(__file__))


class MockBuildCmd(build):
    build_lib = "mock build lib"
    command_name = 'build'
    plat_name = 'whatever'

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        self._record.append("build has run")


class TestTest(TempdirManager,
               EnvironRestorer,
               LoggingCatcher,
               unittest.TestCase):

    restore_environ = ['PYTHONPATH']

    def setUp(self):
        super(TestTest, self).setUp()
        self.addCleanup(packaging.database.clear_cache)
        new_pythonpath = os.path.dirname(os.path.dirname(here))
        pythonpath = os.environ.get('PYTHONPATH')
        if pythonpath is not None:
            new_pythonpath = os.pathsep.join((new_pythonpath, pythonpath))
        os.environ['PYTHONPATH'] = new_pythonpath

    def assert_re_match(self, pattern, string):
        def quote(s):
            lines = ['## ' + line for line in s.split('\n')]
            sep = ["#" * 60]
            return [''] + sep + lines + sep
        msg = quote(pattern) + ["didn't match"] + quote(string)
        msg = "\n".join(msg)
        if not re.search(pattern, string):
            self.fail(msg)

    def prepare_dist(self, dist_name):
        pkg_dir = join(os.path.dirname(__file__), "dists", dist_name)
        temp_pkg_dir = join(self.mkdtemp(), dist_name)
        shutil.copytree(pkg_dir, temp_pkg_dir)
        return temp_pkg_dir

    def safely_replace(self, obj, attr,
                       new_val=None, delete=False, dictionary=False):
        """Replace a object's attribute returning to its original state at the
        end of the test run. Creates the attribute if not present before
        (deleting afterwards). When delete=True, makes sure the value is del'd
        for the test run. If dictionary is set to True, operates of its items
        rather than attributes."""
        if dictionary:
            _setattr, _getattr, _delattr = setitem, getitem, delitem

            def _hasattr(_dict, value):
                return value in _dict
        else:
            _setattr, _getattr, _delattr, _hasattr = (setattr, getattr,
                                                      delattr, hasattr)

        orig_has_attr = _hasattr(obj, attr)
        if orig_has_attr:
            orig_val = _getattr(obj, attr)

        if delete is False:
            _setattr(obj, attr, new_val)
        elif orig_has_attr:
            _delattr(obj, attr)

        def do_cleanup():
            if orig_has_attr:
                _setattr(obj, attr, orig_val)
            elif _hasattr(obj, attr):
                _delattr(obj, attr)

        self.addCleanup(do_cleanup)

    def test_runs_unittest(self):
        module_name, a_module = self.prepare_a_module()
        record = []
        a_module.recorder = lambda *args: record.append("suite")

        class MockTextTestRunner:
            def __init__(*_, **__):
                pass

            def run(_self, suite):
                record.append("run")

        self.safely_replace(ut1, "TextTestRunner", MockTextTestRunner)

        dist = Distribution()
        cmd = test(dist)
        cmd.suite = "%s.recorder" % module_name
        cmd.run()
        self.assertEqual(record, ["suite", "run"])

    def test_builds_before_running_tests(self):
        self.addCleanup(set_command, 'packaging.command.build.build')
        set_command('packaging.tests.test_command_test.MockBuildCmd')

        dist = Distribution()
        dist.get_command_obj('build')._record = record = []
        cmd = test(dist)
        cmd.runner = self.prepare_named_function(lambda: None)
        cmd.ensure_finalized()
        cmd.run()
        self.assertEqual(['build has run'], record)

    @unittest.skip('needs to be written')
    def test_works_with_2to3(self):
        pass

    def test_checks_requires(self):
        dist = Distribution()
        cmd = test(dist)
        phony_project = 'ohno_ohno-impossible_1234-name_stop-that!'
        cmd.tests_require = [phony_project]
        cmd.ensure_finalized()
        logs = self.get_logs()
        self.assertIn(phony_project, logs[-1])

    def prepare_a_module(self):
        tmp_dir = self.mkdtemp()
        sys.path.append(tmp_dir)
        self.addCleanup(sys.path.remove, tmp_dir)

        self.write_file((tmp_dir, 'packaging_tests_a.py'), '')
        import packaging_tests_a as a_module
        return "packaging_tests_a", a_module

    def prepare_named_function(self, func):
        module_name, a_module = self.prepare_a_module()
        a_module.recorder = func
        return "%s.recorder" % module_name

    def test_custom_runner(self):
        dist = Distribution()
        cmd = test(dist)
        record = []
        cmd.runner = self.prepare_named_function(
            lambda: record.append("runner called"))
        cmd.ensure_finalized()
        cmd.run()
        self.assertEqual(["runner called"], record)

    def prepare_mock_ut2(self):
        class MockUTClass:
            def __init__(*_, **__):
                pass

            def discover(self):
                pass

            def run(self, _):
                pass

        class MockUTModule:
            TestLoader = MockUTClass
            TextTestRunner = MockUTClass

        mock_ut2 = MockUTModule()
        self.safely_replace(sys.modules, "unittest2",
                            mock_ut2, dictionary=True)
        return mock_ut2

    def test_gets_unittest_discovery(self):
        mock_ut2 = self.prepare_mock_ut2()
        dist = Distribution()
        cmd = test(dist)
        self.safely_replace(ut1.TestLoader, "discover", lambda: None)
        self.assertEqual(cmd.get_ut_with_discovery(), ut1)

        del ut1.TestLoader.discover
        self.assertEqual(cmd.get_ut_with_discovery(), mock_ut2)

    def test_calls_discover(self):
        self.safely_replace(ut1.TestLoader, "discover", delete=True)
        mock_ut2 = self.prepare_mock_ut2()
        record = []
        mock_ut2.TestLoader.discover = lambda self, path: record.append(path)
        dist = Distribution()
        cmd = test(dist)
        cmd.run()
        self.assertEqual([os.curdir], record)


def test_suite():
    return unittest.makeSuite(TestTest)

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