Skip to content

Commit d4339ee

Browse files
committed
mypy: test_goldtest.py test_json.py test_lcov.py test_mixins.py test_numbits.py test_oddball.py
1 parent 8ff3c6d commit d4339ee

File tree

7 files changed

+93
-70
lines changed

7 files changed

+93
-70
lines changed

tests/test_goldtest.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""Tests of the helpers in goldtest.py"""
55

6+
from __future__ import annotations
7+
68
import os.path
79
import re
810

@@ -33,7 +35,7 @@
3335
(r'G\w+', 'Gxxx'),
3436
]
3537

36-
def path_regex(path):
38+
def path_regex(path: str) -> str:
3739
"""Convert a file path into a regex that will match that path on any OS."""
3840
return re.sub(r"[/\\]", r"[/\\\\]", path.replace(".", "[.]"))
3941

@@ -48,16 +50,16 @@ def path_regex(path):
4850
class CompareTest(CoverageTest):
4951
"""Tests of goldtest.py:compare()"""
5052

51-
def setUp(self):
53+
def setUp(self) -> None:
5254
super().setUp()
5355
self.addCleanup(remove_tree, ACTUAL_DIR)
5456

55-
def test_good(self):
57+
def test_good(self) -> None:
5658
self.make_file("out/gettysburg.txt", GOOD_GETTY)
5759
compare(gold_path("testing/getty"), "out", scrubs=SCRUBS)
5860
self.assert_doesnt_exist(ACTUAL_GETTY_FILE)
5961

60-
def test_bad(self):
62+
def test_bad(self) -> None:
6163
self.make_file("out/gettysburg.txt", BAD_GETTY)
6264

6365
# compare() raises an assertion.
@@ -78,7 +80,7 @@ def test_bad(self):
7880
saved = f.read()
7981
assert saved == BAD_GETTY
8082

81-
def test_good_needs_scrubs(self):
83+
def test_good_needs_scrubs(self) -> None:
8284
# Comparing the "good" result without scrubbing the variable parts will fail.
8385
self.make_file("out/gettysburg.txt", GOOD_GETTY)
8486

@@ -91,7 +93,7 @@ def test_good_needs_scrubs(self):
9193
assert "- 11/19/1863, Gettysburg, Pennsylvania" in stdout
9294
assert "+ 11/19/9999, Gettysburg, Pennsylvania" in stdout
9395

94-
def test_actual_extra(self):
96+
def test_actual_extra(self) -> None:
9597
self.make_file("out/gettysburg.txt", GOOD_GETTY)
9698
self.make_file("out/another.more", "hi")
9799

@@ -107,7 +109,7 @@ def test_actual_extra(self):
107109
# But only the files matching the file_pattern are considered.
108110
compare(gold_path("testing/getty"), "out", file_pattern="*.txt", scrubs=SCRUBS)
109111

110-
def test_xml_good(self):
112+
def test_xml_good(self) -> None:
111113
self.make_file("out/output.xml", """\
112114
<?xml version="1.0" ?>
113115
<the_root c="three" b="222" a="one">
@@ -118,7 +120,7 @@ def test_xml_good(self):
118120
""")
119121
compare(gold_path("testing/xml"), "out", scrubs=SCRUBS)
120122

121-
def test_xml_bad(self):
123+
def test_xml_bad(self) -> None:
122124
self.make_file("out/output.xml", """\
123125
<?xml version="1.0" ?>
124126
<the_root c="nine" b="2" a="one">
@@ -147,25 +149,25 @@ class ContainsTest(CoverageTest):
147149

148150
run_in_temp_dir = False
149151

150-
def test_contains(self):
152+
def test_contains(self) -> None:
151153
contains(GOLD_GETTY_FILE, "Four", "fathers", "dedicated")
152154
msg = rf"Missing content in {GOLD_GETTY_FILE_RX}: 'xyzzy'"
153155
with pytest.raises(AssertionError, match=msg):
154156
contains(GOLD_GETTY_FILE, "Four", "fathers", "xyzzy", "dedicated")
155157

156-
def test_contains_rx(self):
158+
def test_contains_rx(self) -> None:
157159
contains_rx(GOLD_GETTY_FILE, r"Fo.r", r"f[abc]thers", "dedi[cdef]ated")
158160
msg = rf"Missing regex in {GOLD_GETTY_FILE_RX}: r'm\[opq\]thers'"
159161
with pytest.raises(AssertionError, match=msg):
160162
contains_rx(GOLD_GETTY_FILE, r"Fo.r", r"m[opq]thers")
161163

162-
def test_contains_any(self):
164+
def test_contains_any(self) -> None:
163165
contains_any(GOLD_GETTY_FILE, "Five", "Four", "Three")
164166
msg = rf"Missing content in {GOLD_GETTY_FILE_RX}: 'One' \[1 of 3\]"
165167
with pytest.raises(AssertionError, match=msg):
166168
contains_any(GOLD_GETTY_FILE, "One", "Two", "Three")
167169

168-
def test_doesnt_contain(self):
170+
def test_doesnt_contain(self) -> None:
169171
doesnt_contain(GOLD_GETTY_FILE, "One", "Two", "Three")
170172
msg = rf"Forbidden content in {GOLD_GETTY_FILE_RX}: 'Four'"
171173
with pytest.raises(AssertionError, match=msg):

tests/test_json.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
33

44
"""Test json-based summary reporting for coverage.py"""
5-
from datetime import datetime
5+
6+
from __future__ import annotations
7+
68
import json
79
import os
810

11+
from datetime import datetime
12+
from typing import Any, Dict
13+
914
import coverage
15+
from coverage import Coverage
16+
1017
from tests.coveragetest import UsingModulesMixin, CoverageTest
1118

1219

1320
class JsonReportTest(UsingModulesMixin, CoverageTest):
1421
"""Tests of the JSON reports from coverage.py."""
15-
def _assert_expected_json_report(self, cov, expected_result):
22+
23+
def _assert_expected_json_report(
24+
self,
25+
cov: Coverage,
26+
expected_result: Dict[str, Any],
27+
) -> None:
1628
"""
1729
Helper for tests that handles the common ceremony so the tests can be clearly show the
1830
consequences of setting various arguments.
@@ -39,7 +51,7 @@ def _assert_expected_json_report(self, cov, expected_result):
3951
del (parsed_result['meta']['timestamp'])
4052
assert parsed_result == expected_result
4153

42-
def test_branch_coverage(self):
54+
def test_branch_coverage(self) -> None:
4355
cov = coverage.Coverage(branch=True)
4456
expected_result = {
4557
'meta': {
@@ -91,7 +103,7 @@ def test_branch_coverage(self):
91103
}
92104
self._assert_expected_json_report(cov, expected_result)
93105

94-
def test_simple_line_coverage(self):
106+
def test_simple_line_coverage(self) -> None:
95107
cov = coverage.Coverage()
96108
expected_result = {
97109
'meta': {
@@ -125,7 +137,7 @@ def test_simple_line_coverage(self):
125137
}
126138
self._assert_expected_json_report(cov, expected_result)
127139

128-
def run_context_test(self, relative_files):
140+
def run_context_test(self, relative_files: bool) -> None:
129141
"""A helper for two tests below."""
130142
self.make_file("config", """\
131143
[run]
@@ -187,8 +199,8 @@ def run_context_test(self, relative_files):
187199
}
188200
self._assert_expected_json_report(cov, expected_result)
189201

190-
def test_context_non_relative(self):
202+
def test_context_non_relative(self) -> None:
191203
self.run_context_test(relative_files=False)
192204

193-
def test_context_relative(self):
205+
def test_context_relative(self) -> None:
194206
self.run_context_test(relative_files=True)

tests/test_lcov.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""Test LCOV-based summary reporting for coverage.py."""
55

6+
from __future__ import annotations
7+
68
import math
79
import textwrap
810

@@ -15,7 +17,7 @@
1517
class LcovTest(CoverageTest):
1618
"""Tests of the LCOV reports from coverage.py."""
1719

18-
def create_initial_files(self):
20+
def create_initial_files(self) -> None:
1921
"""
2022
Helper for tests that handles the common ceremony so the tests can
2123
show the consequences of changes in the setup.
@@ -44,13 +46,12 @@ def test_volume(self):
4446
self.assertAlmostEqual(cuboid_volume(5.5),166.375)
4547
""")
4648

47-
def get_lcov_report_content(self, filename="coverage.lcov"):
49+
def get_lcov_report_content(self, filename: str="coverage.lcov") -> str:
4850
"""Return the content of an LCOV report."""
4951
with open(filename, "r") as file:
50-
file_contents = file.read()
51-
return file_contents
52+
return file.read()
5253

53-
def test_lone_file(self):
54+
def test_lone_file(self) -> None:
5455
"""For a single file with a couple of functions, the lcov should cover
5556
the function definitions themselves, but not the returns."""
5657
self.make_file("main_file.py", """\
@@ -81,7 +82,7 @@ def IsItTrue():
8182
actual_result = self.get_lcov_report_content()
8283
assert expected_result == actual_result
8384

84-
def test_simple_line_coverage_two_files(self):
85+
def test_simple_line_coverage_two_files(self) -> None:
8586
"""Test that line coverage is created when coverage is run,
8687
and matches the output of the file below."""
8788
self.create_initial_files()
@@ -119,7 +120,7 @@ def test_simple_line_coverage_two_files(self):
119120
actual_result = self.get_lcov_report_content(filename="data.lcov")
120121
assert expected_result == actual_result
121122

122-
def test_branch_coverage_one_file(self):
123+
def test_branch_coverage_one_file(self) -> None:
123124
"""Test that the reporter produces valid branch coverage."""
124125
self.make_file("main_file.py", """\
125126
#!/usr/bin/env python3
@@ -154,7 +155,7 @@ def is_it_x(x):
154155
actual_result = self.get_lcov_report_content()
155156
assert expected_result == actual_result
156157

157-
def test_branch_coverage_two_files(self):
158+
def test_branch_coverage_two_files(self) -> None:
158159
"""Test that valid branch coverage is generated
159160
in the case of two files."""
160161
self.make_file("main_file.py", """\
@@ -215,7 +216,7 @@ def test_is_it_x(self):
215216
actual_result = self.get_lcov_report_content()
216217
assert actual_result == expected_result
217218

218-
def test_half_covered_branch(self):
219+
def test_half_covered_branch(self) -> None:
219220
"""Test that for a given branch that is only half covered,
220221
the block numbers remain the same, and produces valid lcov.
221222
"""
@@ -251,7 +252,7 @@ def test_half_covered_branch(self):
251252
actual_result = self.get_lcov_report_content()
252253
assert actual_result == expected_result
253254

254-
def test_empty_init_files(self):
255+
def test_empty_init_files(self) -> None:
255256
"""Test that in the case of an empty __init__.py file, the lcov
256257
reporter will note that the file is there, and will note the empty
257258
line. It will also note the lack of branches, and the checksum for

tests/test_mixins.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""Tests of code in tests/mixins.py"""
55

6+
from __future__ import annotations
7+
68
import pytest
79

810
from coverage.misc import import_local_file
@@ -13,12 +15,12 @@
1315
class TempDirMixinTest(TempDirMixin):
1416
"""Test the methods in TempDirMixin."""
1517

16-
def file_text(self, fname):
18+
def file_text(self, fname: str) -> str:
1719
"""Return the text read from a file."""
1820
with open(fname, "rb") as f:
1921
return f.read().decode('ascii')
2022

21-
def test_make_file(self):
23+
def test_make_file(self) -> None:
2224
# A simple file.
2325
self.make_file("fooey.boo", "Hello there")
2426
assert self.file_text("fooey.boo") == "Hello there"
@@ -38,21 +40,21 @@ def test_make_file(self):
3840
""")
3941
assert self.file_text("dedented.txt") == "Hello\nBye\n"
4042

41-
def test_make_file_newline(self):
43+
def test_make_file_newline(self) -> None:
4244
self.make_file("unix.txt", "Hello\n")
4345
assert self.file_text("unix.txt") == "Hello\n"
4446
self.make_file("dos.txt", "Hello\n", newline="\r\n")
4547
assert self.file_text("dos.txt") == "Hello\r\n"
4648
self.make_file("mac.txt", "Hello\n", newline="\r")
4749
assert self.file_text("mac.txt") == "Hello\r"
4850

49-
def test_make_file_non_ascii(self):
51+
def test_make_file_non_ascii(self) -> None:
5052
self.make_file("unicode.txt", "tablo: «ταБℓσ»")
5153
with open("unicode.txt", "rb") as f:
5254
text = f.read()
5355
assert text == b"tablo: \xc2\xab\xcf\x84\xce\xb1\xd0\x91\xe2\x84\x93\xcf\x83\xc2\xbb"
5456

55-
def test_make_bytes_file(self):
57+
def test_make_bytes_file(self) -> None:
5658
self.make_file("binary.dat", bytes=b"\x99\x33\x66hello\0")
5759
with open("binary.dat", "rb") as f:
5860
data = f.read()
@@ -63,12 +65,12 @@ class RestoreModulessMixinTest(TempDirMixin, RestoreModulesMixin):
6365
"""Tests of SysPathModulesMixin."""
6466

6567
@pytest.mark.parametrize("val", [17, 42])
66-
def test_module_independence(self, val):
68+
def test_module_independence(self, val: int) -> None:
6769
self.make_file("xyzzy.py", f"A = {val}")
6870
import xyzzy # pylint: disable=import-error
6971
assert xyzzy.A == val
7072

71-
def test_cleanup_and_reimport(self):
73+
def test_cleanup_and_reimport(self) -> None:
7274
self.make_file("xyzzy.py", "A = 17")
7375
xyzzy = import_local_file("xyzzy")
7476
assert xyzzy.A == 17

0 commit comments

Comments
 (0)