summaryrefslogtreecommitdiff
path: root/tests/test_plugins.py
blob: 5039082ea49adc2a1ba7ef21837efe4e3c49345c (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""Tests for plugins."""

import os.path
import sys

from nose.plugins.skip import SkipTest

import coverage
from coverage.codeunit import CodeUnit
from coverage.parser import CodeParser
from coverage.plugin import Plugins, overrides

import coverage.plugin

from tests.coveragetest import CoverageTest


class FakeConfig(object):
    """A fake config for use in tests."""

    def __init__(self, plugin, options):
        self.plugin = plugin
        self.options = options
        self.asked_for = []

    def get_plugin_options(self, module):
        """Just return the options for `module` if this is the right module."""
        self.asked_for.append(module)
        if module == self.plugin:
            return self.options
        else:
            return {}


class PluginUnitTest(CoverageTest):
    """Test Plugins.load_plugins directly."""

    def test_importing_and_configuring(self):
        self.make_file("plugin1.py", """\
            from coverage import CoveragePlugin

            class Plugin(CoveragePlugin):
                def __init__(self, options):
                    super(Plugin, self).__init__(options)
                    self.this_is = "me"
            """)

        config = FakeConfig("plugin1", {'a':'hello'})
        plugins = list(Plugins.load_plugins(["plugin1"], config))

        self.assertEqual(len(plugins), 1)
        self.assertEqual(plugins[0].this_is, "me")
        self.assertEqual(plugins[0].options, {'a':'hello'})
        self.assertEqual(config.asked_for, ['plugin1'])

    def test_importing_and_configuring_more_than_one(self):
        self.make_file("plugin1.py", """\
            from coverage import CoveragePlugin

            class Plugin(CoveragePlugin):
                def __init__(self, options):
                    super(Plugin, self).__init__(options)
                    self.this_is = "me"
            """)
        self.make_file("plugin2.py", """\
            from coverage import CoveragePlugin

            class Plugin(CoveragePlugin):
                pass
            """)

        config = FakeConfig("plugin1", {'a':'hello'})
        plugins = list(Plugins.load_plugins(["plugin1", "plugin2"], config))

        self.assertEqual(len(plugins), 2)
        self.assertEqual(plugins[0].this_is, "me")
        self.assertEqual(plugins[0].options, {'a':'hello'})
        self.assertEqual(plugins[1].options, {})
        self.assertEqual(config.asked_for, ['plugin1', 'plugin2'])

    def test_cant_import(self):
        with self.assertRaises(ImportError):
            _ = Plugins.load_plugins(["plugin_not_there"], None)

    def test_ok_to_not_define_plugin(self):
        self.make_file("plugin2.py", """\
            from coverage import CoveragePlugin

            Nothing = 0
            """)
        plugins = list(Plugins.load_plugins(["plugin2"], None))
        self.assertEqual(plugins, [])


class PluginTest(CoverageTest):
    """Test plugins through the Coverage class."""

    def test_plugin_imported(self):
        # Prove that a plugin will be imported.
        self.make_file("my_plugin.py", """\
            with open("evidence.out", "w") as f:
                f.write("we are here!")
            """)

        self.assert_doesnt_exist("evidence.out")
        cov = coverage.Coverage()
        cov.config["run:plugins"] = ["my_plugin"]
        cov.start()
        cov.stop()

        with open("evidence.out") as f:
            self.assertEqual(f.read(), "we are here!")

    def test_missing_plugin_raises_import_error(self):
        # Prove that a missing plugin will raise an ImportError.
        with self.assertRaises(ImportError):
            cov = coverage.Coverage()
            cov.config["run:plugins"] = ["does_not_exist_woijwoicweo"]
            cov.start()
        cov.stop()

    def test_bad_plugin_isnt_hidden(self):
        # Prove that a plugin with an error in it will raise the error.
        self.make_file("plugin_over_zero.py", """\
            1/0
            """)
        with self.assertRaises(ZeroDivisionError):
            cov = coverage.Coverage()
            cov.config["run:plugins"] = ["plugin_over_zero"]
            cov.start()
        cov.stop()

    def test_importing_myself(self):
        if sys.platform == 'win32':
            raise SkipTest("Plugin stuff is jank on windows.. fixing soon...")

        self.make_file("simple.py", """\
            import try_xyz
            a = 1
            b = 2
            """)
        self.make_file("try_xyz.py", """\
            c = 3
            d = 4
            """)

        cov = coverage.Coverage()
        cov.config["run:plugins"] = ["tests.test_plugins"]

        # Import the python file, executing it.
        self.start_import_stop(cov, "simple")

        _, statements, missing, _ = cov.analysis("simple.py")
        self.assertEqual(statements, [1,2,3])
        self.assertEqual(missing, [])
        _, statements, _, _ = cov.analysis("/src/try_ABC.zz")
        self.assertEqual(statements, [105, 106, 107, 205, 206, 207])


class Plugin(coverage.CoveragePlugin):
    def file_tracer(self, filename):
        if "xyz.py" in filename:
            file_tracer = FileTracer(filename)
            return file_tracer

    def file_reporter(self, filename):
        return FileReporter(filename)


class FileTracer(coverage.plugin.FileTracer):
    def __init__(self, filename):
        self._filename = filename
        self._source_filename = os.path.join(
            "/src",
            os.path.basename(filename.replace("xyz.py", "ABC.zz"))
        )

    def source_filename(self):
        return self._source_filename

    def line_number_range(self, frame):
        lineno = frame.f_lineno
        return lineno*100+5, lineno*100+7


class FileReporter(coverage.plugin.FileReporter):
    def get_parser(self, exclude=None):
        return PluginParser()

class PluginParser(CodeParser):
    def parse_source(self):
        return set([105, 106, 107, 205, 206, 207]), set([])


class OverridesTest(CoverageTest):
    """Test plugins.py:overrides."""

    run_in_temp_dir = False

    def test_overrides(self):
        class SomeBase(object):
            """Base class, two base methods."""
            def method1(self):
                pass

            def method2(self):
                pass

        class Derived1(SomeBase):
            """Simple single inheritance."""
            def method1(self):
                pass

        self.assertTrue(overrides(Derived1(), "method1", SomeBase))
        self.assertFalse(overrides(Derived1(), "method2", SomeBase))

        class FurtherDerived1(Derived1):
            """Derive again from Derived1, inherit its method1."""
            pass

        self.assertTrue(overrides(FurtherDerived1(), "method1", SomeBase))
        self.assertFalse(overrides(FurtherDerived1(), "method2", SomeBase))

        class FurtherDerived2(Derived1):
            """Override the overridden method."""
            def method1(self):
                pass

        self.assertTrue(overrides(FurtherDerived2(), "method1", SomeBase))
        self.assertFalse(overrides(FurtherDerived2(), "method2", SomeBase))

        class Mixin(object):
            """A mixin that overrides method1."""
            def method1(self):
                pass

        class Derived2(Mixin, SomeBase):
            """A class that gets the method from the mixin."""
            pass

        self.assertTrue(overrides(Derived2(), "method1", SomeBase))
        self.assertFalse(overrides(Derived2(), "method2", SomeBase))