Skip to content

Commit 8fef6f0

Browse files
committed
mypy: test_venv.py
1 parent 08564c0 commit 8fef6f0

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

Diff for: tests/helpers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import textwrap
1616
import warnings
1717

18+
from pathlib import Path
1819
from typing import (
1920
Any, Callable, Iterable, Iterator, List, Optional, Set, Tuple, Type,
2021
TypeVar, Union, cast,
@@ -267,7 +268,7 @@ def arcs_to_arcz_repr(arcs: Optional[Iterable[TArc]]) -> str:
267268

268269

269270
@contextlib.contextmanager
270-
def change_dir(new_dir: str) -> Iterator[None]:
271+
def change_dir(new_dir: Union[str, Path]) -> Iterator[None]:
271272
"""Change directory, and then change back.
272273
273274
Use as a context manager, it will return to the original

Diff for: tests/test_venv.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
"""Tests about understanding how third-party code is installed."""
55

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

12+
from pathlib import Path
13+
from typing import Iterator, cast
14+
1015
import pytest
1116

1217
from coverage import env
@@ -16,7 +21,7 @@
1621
from tests.helpers import re_lines, run_command
1722

1823

19-
def run_in_venv(cmd):
24+
def run_in_venv(cmd: str) -> str:
2025
r"""Run `cmd` in the virtualenv at `venv`.
2126
2227
The first word of the command will be adjusted to run it from the
@@ -37,13 +42,13 @@ def run_in_venv(cmd):
3742

3843

3944
@pytest.fixture(scope="session", name="venv_world")
40-
def venv_world_fixture(tmp_path_factory):
45+
def venv_world_fixture(tmp_path_factory: pytest.TempPathFactory) -> Path:
4146
"""Create a virtualenv with a few test packages for VirtualenvTest to use.
4247
4348
Returns the directory containing the "venv" virtualenv.
4449
"""
4550

46-
venv_world = tmp_path_factory.mktemp("venv_world")
51+
venv_world = cast(Path, tmp_path_factory.mktemp("venv_world"))
4752
with change_dir(venv_world):
4853
# Create a virtualenv.
4954
run_command("python -m venv venv")
@@ -153,9 +158,9 @@ def testp():
153158
"coverage",
154159
"python -m coverage",
155160
], name="coverage_command")
156-
def coverage_command_fixture(request):
161+
def coverage_command_fixture(request: pytest.FixtureRequest) -> str:
157162
"""Parametrized fixture to use multiple forms of "coverage" command."""
158-
return request.param
163+
return cast(str, request.param)
159164

160165

161166
class VirtualenvTest(CoverageTest):
@@ -164,7 +169,7 @@ class VirtualenvTest(CoverageTest):
164169
expected_stdout = "33\n110\n198\n1.5\n"
165170

166171
@pytest.fixture(autouse=True)
167-
def in_venv_world_fixture(self, venv_world):
172+
def in_venv_world_fixture(self, venv_world: Path) -> Iterator[None]:
168173
"""For running tests inside venv_world, and cleaning up made files."""
169174
with change_dir(venv_world):
170175
self.make_file("myproduct.py", """\
@@ -188,12 +193,12 @@ def in_venv_world_fixture(self, venv_world):
188193
if fname not in {"venv", "another_pkg", "bug888"}:
189194
os.remove(fname)
190195

191-
def get_trace_output(self):
196+
def get_trace_output(self) -> str:
192197
"""Get the debug output of coverage.py"""
193198
with open("debug_out.txt") as f:
194199
return f.read()
195200

196-
def test_third_party_venv_isnt_measured(self, coverage_command):
201+
def test_third_party_venv_isnt_measured(self, coverage_command: str) -> None:
197202
out = run_in_venv(coverage_command + " run --source=. myproduct.py")
198203
# In particular, this warning doesn't appear:
199204
# Already imported a file that will be measured: .../coverage/__main__.py
@@ -218,7 +223,7 @@ def test_third_party_venv_isnt_measured(self, coverage_command):
218223
assert "coverage" not in out
219224
assert "colorsys" not in out
220225

221-
def test_us_in_venv_isnt_measured(self, coverage_command):
226+
def test_us_in_venv_isnt_measured(self, coverage_command: str) -> None:
222227
out = run_in_venv(coverage_command + " run --source=third myproduct.py")
223228
assert out == self.expected_stdout
224229

@@ -245,7 +250,7 @@ def test_us_in_venv_isnt_measured(self, coverage_command):
245250
assert "coverage" not in out
246251
assert "colorsys" not in out
247252

248-
def test_venv_isnt_measured(self, coverage_command):
253+
def test_venv_isnt_measured(self, coverage_command: str) -> None:
249254
out = run_in_venv(coverage_command + " run myproduct.py")
250255
assert out == self.expected_stdout
251256

@@ -261,7 +266,7 @@ def test_venv_isnt_measured(self, coverage_command):
261266
assert "colorsys" not in out
262267

263268
@pytest.mark.skipif(not env.C_TRACER, reason="Plugins are only supported with the C tracer.")
264-
def test_venv_with_dynamic_plugin(self, coverage_command):
269+
def test_venv_with_dynamic_plugin(self, coverage_command: str) -> None:
265270
# https://github.com/nedbat/coveragepy/issues/1150
266271
# Django coverage plugin was incorrectly getting warnings:
267272
# "Already imported: ... django/template/blah.py"
@@ -277,7 +282,7 @@ def test_venv_with_dynamic_plugin(self, coverage_command):
277282
# Already imported a file that will be measured: ...third/render.py (already-imported)
278283
assert out == "HTML: hello.html@1723\n"
279284

280-
def test_installed_namespace_packages(self, coverage_command):
285+
def test_installed_namespace_packages(self, coverage_command: str) -> None:
281286
# https://github.com/nedbat/coveragepy/issues/1231
282287
# When namespace packages were installed, they were considered
283288
# third-party packages. Test that isn't still happening.
@@ -319,7 +324,7 @@ def test_installed_namespace_packages(self, coverage_command):
319324
assert "fifth" in out
320325
assert "sixth" in out
321326

322-
def test_bug_888(self, coverage_command):
327+
def test_bug_888(self, coverage_command: str) -> None:
323328
out = run_in_venv(
324329
coverage_command +
325330
" run --source=bug888/app,bug888/plugin bug888/app/testcov/main.py"

Diff for: tox.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ setenv =
109109
T4=tests/test_filereporter.py tests/test_files.py tests/test_goldtest.py tests/test_html.py tests/test_json.py tests/test_lcov.py
110110
T5=tests/test_misc.py tests/test_mixins.py tests/test_numbits.py tests/test_oddball.py tests/test_parser.py tests/test_phystokens.py
111111
T6=tests/test_process.py tests/test_python.py tests/test_report.py tests/test_results.py tests/test_setup.py
112-
T7=tests/test_summary.py tests/test_templite.py tests/test_testing.py tests/test_version.py tests/test_xml.py
113-
# not done yet: test_plugins.py test_venv.py
112+
T7=tests/test_summary.py tests/test_templite.py tests/test_testing.py tests/test_venv.py tests/test_version.py tests/test_xml.py
113+
# not done yet: test_plugins.py
114114
TYPEABLE_T={env:T1} {env:T2} {env:T3} {env:T4} {env:T5} {env:T6} {env:T7}
115115
TYPEABLE={env:TYPEABLE_C} {env:TYPEABLE_T}
116116

0 commit comments

Comments
 (0)