summaryrefslogtreecommitdiff
path: root/tests/test_testing.py
blob: b6ffe1a07e8db6a054824a1fd870f48f8b7f74c6 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# -*- coding: utf-8 -*-
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""Tests that our test infrastructure is really working!"""

import datetime
import os
import re
import sys

import pytest

import coverage
from coverage import tomlconfig
from coverage.backunittest import TestCase, unittest
from coverage.files import actual_path
from coverage.misc import StopEverything

from tests.coveragetest import CoverageTest
from tests.helpers import (
    arcs_to_arcz_repr, arcz_to_arcs,
    CheckUniqueFilenames, re_lines, re_line, without_module,
)
from tests.mixins import convert_skip_exceptions


def test_xdist_sys_path_nuttiness_is_fixed():
    # See conftest.py:fix_xdist_sys_path
    assert sys.path[1] != ''
    assert os.environ.get('PYTHONPATH') is None


class TestingTest(TestCase):
    """Tests of helper methods on `backunittest.TestCase`."""

    def test_assert_count_equal(self):
        self.assertCountEqual(set(), set())
        with pytest.raises(AssertionError):
            self.assertCountEqual({1,2,3}, set())
        with pytest.raises(AssertionError):
            self.assertCountEqual({1,2,3}, {4,5,6})


class CoverageTestTest(CoverageTest):
    """Test the methods in `CoverageTest`."""

    def test_file_exists(self):
        self.make_file("whoville.txt", "We are here!")
        self.assert_exists("whoville.txt")
        self.assert_doesnt_exist("shadow.txt")
        msg = "File 'whoville.txt' shouldn't exist"
        with pytest.raises(AssertionError, match=msg):
            self.assert_doesnt_exist("whoville.txt")
        msg = "File 'shadow.txt' should exist"
        with pytest.raises(AssertionError, match=msg):
            self.assert_exists("shadow.txt")

    def test_file_count(self):
        self.make_file("abcde.txt", "abcde")
        self.make_file("axczz.txt", "axczz")
        self.make_file("afile.txt", "afile")
        self.assert_file_count("a*.txt", 3)
        self.assert_file_count("*c*.txt", 2)
        self.assert_file_count("afile.*", 1)
        self.assert_file_count("*.q", 0)
        msg = re.escape(
            "There should be 13 files matching 'a*.txt', but there are these: "
            "['abcde.txt', 'afile.txt', 'axczz.txt']"
        )
        with pytest.raises(AssertionError, match=msg):
            self.assert_file_count("a*.txt", 13)
        msg = re.escape(
            "There should be 12 files matching '*c*.txt', but there are these: "
            "['abcde.txt', 'axczz.txt']"
        )
        with pytest.raises(AssertionError, match=msg):
            self.assert_file_count("*c*.txt", 12)
        msg = re.escape(
            "There should be 11 files matching 'afile.*', but there are these: "
            "['afile.txt']"
        )
        with pytest.raises(AssertionError, match=msg):
            self.assert_file_count("afile.*", 11)
        msg = re.escape(
            "There should be 10 files matching '*.q', but there are these: []"
        )
        with pytest.raises(AssertionError, match=msg):
            self.assert_file_count("*.q", 10)

    def test_assert_startwith(self):
        self.assert_starts_with("xyzzy", "xy")
        self.assert_starts_with("xyz\nabc", "xy")
        self.assert_starts_with("xyzzy", ("x", "z"))
        msg = re.escape("'xyz' doesn't start with 'a'")
        with pytest.raises(AssertionError, match=msg):
            self.assert_starts_with("xyz", "a")
        msg = re.escape("'xyz\\nabc' doesn't start with 'a'")
        with pytest.raises(AssertionError, match=msg):
            self.assert_starts_with("xyz\nabc", "a")

    def test_assert_recent_datetime(self):
        def now_delta(seconds):
            """Make a datetime `seconds` seconds from now."""
            return datetime.datetime.now() + datetime.timedelta(seconds=seconds)

        # Default delta is 10 seconds.
        self.assert_recent_datetime(now_delta(0))
        self.assert_recent_datetime(now_delta(-9))
        with pytest.raises(AssertionError):
            self.assert_recent_datetime(now_delta(-11))
        with pytest.raises(AssertionError):
            self.assert_recent_datetime(now_delta(1))

        # Delta is settable.
        self.assert_recent_datetime(now_delta(0), seconds=120)
        self.assert_recent_datetime(now_delta(-100), seconds=120)
        with pytest.raises(AssertionError):
            self.assert_recent_datetime(now_delta(-1000), seconds=120)
        with pytest.raises(AssertionError):
            self.assert_recent_datetime(now_delta(1), seconds=120)

    def test_assert_warnings(self):
        cov = coverage.Coverage()

        # Make a warning, it should catch it properly.
        with self.assert_warnings(cov, ["Hello there!"]):
            cov._warn("Hello there!")

        # The expected warnings are regexes.
        with self.assert_warnings(cov, ["Hello.*!"]):
            cov._warn("Hello there!")

        # There can be a bunch of actual warnings.
        with self.assert_warnings(cov, ["Hello.*!"]):
            cov._warn("You there?")
            cov._warn("Hello there!")

        # There can be a bunch of expected warnings.
        with self.assert_warnings(cov, ["Hello.*!", "You"]):
            cov._warn("You there?")
            cov._warn("Hello there!")

        # But if there are a bunch of expected warnings, they have to all happen.
        warn_regex = r"Didn't find warning 'You' in \['Hello there!'\]"
        with pytest.raises(AssertionError, match=warn_regex):
            with self.assert_warnings(cov, ["Hello.*!", "You"]):
                cov._warn("Hello there!")

        # Make a different warning than expected, it should raise an assertion.
        warn_regex = r"Didn't find warning 'Not me' in \['Hello there!'\]"
        with pytest.raises(AssertionError, match=warn_regex):
            with self.assert_warnings(cov, ["Not me"]):
                cov._warn("Hello there!")

        # Try checking a warning that shouldn't appear: happy case.
        with self.assert_warnings(cov, ["Hi"], not_warnings=["Bye"]):
            cov._warn("Hi")

        # But it should fail if the unexpected warning does appear.
        warn_regex = r"Found warning 'Bye' in \['Hi', 'Bye'\]"
        with pytest.raises(AssertionError, match=warn_regex):
            with self.assert_warnings(cov, ["Hi"], not_warnings=["Bye"]):
                cov._warn("Hi")
                cov._warn("Bye")

        # assert_warnings shouldn't hide a real exception.
        with pytest.raises(ZeroDivisionError, match="oops"):
            with self.assert_warnings(cov, ["Hello there!"]):
                raise ZeroDivisionError("oops")

    def test_assert_no_warnings(self):
        cov = coverage.Coverage()

        # Happy path: no warnings.
        with self.assert_warnings(cov, []):
            pass

        # If you said there would be no warnings, and there were, fail!
        warn_regex = r"Unexpected warnings: \['Watch out!'\]"
        with pytest.raises(AssertionError, match=warn_regex):
            with self.assert_warnings(cov, []):
                cov._warn("Watch out!")

    def test_sub_python_is_this_python(self):
        # Try it with a Python command.
        self.set_environ('COV_FOOBAR', 'XYZZY')
        self.make_file("showme.py", """\
            import os, sys
            print(sys.executable)
            print(os.__file__)
            print(os.environ['COV_FOOBAR'])
            """)
        out = self.run_command("python showme.py").splitlines()
        assert actual_path(out[0]) == actual_path(sys.executable)
        assert out[1] == os.__file__
        assert out[2] == 'XYZZY'

        # Try it with a "coverage debug sys" command.
        out = self.run_command("coverage debug sys")

        executable = re_line(out, "executable:")
        executable = executable.split(":", 1)[1].strip()
        assert _same_python_executable(executable, sys.executable)

        # "environment: COV_FOOBAR = XYZZY" or "COV_FOOBAR = XYZZY"
        environ = re_line(out, "COV_FOOBAR")
        _, _, environ = environ.rpartition(":")
        assert environ.strip() == "COV_FOOBAR = XYZZY"

    def test_run_command_stdout_stderr(self):
        # run_command should give us both stdout and stderr.
        self.make_file("outputs.py", """\
            import sys
            sys.stderr.write("StdErr\\n")
            print("StdOut")
            """)
        out = self.run_command("python outputs.py")
        assert "StdOut\n" in out
        assert "StdErr\n" in out


class CheckUniqueFilenamesTest(CoverageTest):
    """Tests of CheckUniqueFilenames."""

    run_in_temp_dir = False

    class Stub(object):
        """A stand-in for the class we're checking."""
        def __init__(self, x):
            self.x = x

        def method(self, filename, a=17, b="hello"):
            """The method we'll wrap, with args to be sure args work."""
            return (self.x, filename, a, b)

    def test_detect_duplicate(self):
        stub = self.Stub(23)
        CheckUniqueFilenames.hook(stub, "method")

        # Two method calls with different names are fine.
        assert stub.method("file1") == (23, "file1", 17, "hello")
        assert stub.method("file2", 1723, b="what") == (23, "file2", 1723, "what")

        # A duplicate file name trips an assertion.
        with pytest.raises(AssertionError):
            stub.method("file1")


@pytest.mark.parametrize("text, pat, result", [
    ("line1\nline2\nline3\n", "line", "line1\nline2\nline3\n"),
    ("line1\nline2\nline3\n", "[13]", "line1\nline3\n"),
    ("line1\nline2\nline3\n", "X", ""),
])
def test_re_lines(text, pat, result):
    assert re_lines(text, pat) == result

@pytest.mark.parametrize("text, pat, result", [
    ("line1\nline2\nline3\n", "line", ""),
    ("line1\nline2\nline3\n", "[13]", "line2\n"),
    ("line1\nline2\nline3\n", "X", "line1\nline2\nline3\n"),
])
def test_re_lines_inverted(text, pat, result):
    assert re_lines(text, pat, match=False) == result

@pytest.mark.parametrize("text, pat, result", [
    ("line1\nline2\nline3\n", "2", "line2"),
])
def test_re_line(text, pat, result):
    assert re_line(text, pat) == result

@pytest.mark.parametrize("text, pat", [
    ("line1\nline2\nline3\n", "line"),      # too many matches
    ("line1\nline2\nline3\n", "X"),         # no matches
])
def test_re_line_bad(text, pat):
    with pytest.raises(AssertionError):
        re_line(text, pat)


def test_convert_skip_exceptions():
    @convert_skip_exceptions
    def some_method(ret=None, exc=None):
        """Be like a test case."""
        if exc:
            raise exc("yikes!")
        return ret

    # Normal flow is normal.
    assert some_method(ret=[17, 23]) == [17, 23]

    # Exceptions are raised normally.
    with pytest.raises(ValueError):
        some_method(exc=ValueError)

    # But a StopEverything becomes a SkipTest.
    with pytest.raises(unittest.SkipTest):
        some_method(exc=StopEverything)


def _same_python_executable(e1, e2):
    """Determine if `e1` and `e2` refer to the same Python executable.

    Either path could include symbolic links.  The two paths might not refer
    to the exact same file, but if they are in the same directory and their
    numeric suffixes aren't different, they are the same executable.

    """
    e1 = os.path.abspath(os.path.realpath(e1))
    e2 = os.path.abspath(os.path.realpath(e2))

    if os.path.dirname(e1) != os.path.dirname(e2):
        return False                                    # pragma: only failure

    e1 = os.path.basename(e1)
    e2 = os.path.basename(e2)

    if e1 == "python" or e2 == "python" or e1 == e2:
        # Python and Python2.3: OK
        # Python2.3 and Python: OK
        # Python and Python: OK
        # Python2.3 and Python2.3: OK
        return True

    return False                                        # pragma: only failure


def test_without_module():
    toml1 = tomlconfig.toml
    with without_module(tomlconfig, 'toml'):
        toml2 = tomlconfig.toml
    toml3 = tomlconfig.toml

    assert toml1 is toml3 is not None
    assert toml2 is None


@pytest.mark.parametrize("arcz, arcs", [
    (".1 12 2.", [(-1, 1), (1, 2), (2, -1)]),
    ("-11 12 2-5", [(-1, 1), (1, 2), (2, -5)]),
    ("-QA CB IT Z-A", [(-26, 10), (12, 11), (18, 29), (35, -10)]),
])
def test_arcz_to_arcs(arcz, arcs):
    assert arcz_to_arcs(arcz) == arcs


@pytest.mark.parametrize("arcs, arcz_repr", [
    ([(-1, 1), (1, 2), (2, -1)], "(-1, 1) # .1\n(1, 2) # 12\n(2, -1) # 2.\n"),
    ([(-1, 1), (1, 2), (2, -5)], "(-1, 1) # .1\n(1, 2) # 12\n(2, -5) # 2-5\n"),
    ([(-26, 10), (12, 11), (18, 29), (35, -10), (1, 33), (100, 7)],
        (
        "(-26, 10) # -QA\n"
        "(12, 11) # CB\n"
        "(18, 29) # IT\n"
        "(35, -10) # Z-A\n"
        "(1, 33) # 1X\n"
        "(100, 7) # ?7\n"
        )
    ),
])
def test_arcs_to_arcz_repr(arcs, arcz_repr):
    assert arcs_to_arcz_repr(arcs) == arcz_repr