3
3
4
4
"""Tests about understanding how third-party code is installed."""
5
5
6
+ from __future__ import annotations
7
+
6
8
import os
7
9
import os .path
8
10
import shutil
9
11
12
+ from pathlib import Path
13
+ from typing import Iterator , cast
14
+
10
15
import pytest
11
16
12
17
from coverage import env
16
21
from tests .helpers import re_lines , run_command
17
22
18
23
19
- def run_in_venv (cmd ) :
24
+ def run_in_venv (cmd : str ) -> str :
20
25
r"""Run `cmd` in the virtualenv at `venv`.
21
26
22
27
The first word of the command will be adjusted to run it from the
@@ -37,13 +42,13 @@ def run_in_venv(cmd):
37
42
38
43
39
44
@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 :
41
46
"""Create a virtualenv with a few test packages for VirtualenvTest to use.
42
47
43
48
Returns the directory containing the "venv" virtualenv.
44
49
"""
45
50
46
- venv_world = tmp_path_factory .mktemp ("venv_world" )
51
+ venv_world = cast ( Path , tmp_path_factory .mktemp ("venv_world" ) )
47
52
with change_dir (venv_world ):
48
53
# Create a virtualenv.
49
54
run_command ("python -m venv venv" )
@@ -153,9 +158,9 @@ def testp():
153
158
"coverage" ,
154
159
"python -m coverage" ,
155
160
], name = "coverage_command" )
156
- def coverage_command_fixture (request ) :
161
+ def coverage_command_fixture (request : pytest . FixtureRequest ) -> str :
157
162
"""Parametrized fixture to use multiple forms of "coverage" command."""
158
- return request .param
163
+ return cast ( str , request .param )
159
164
160
165
161
166
class VirtualenvTest (CoverageTest ):
@@ -164,7 +169,7 @@ class VirtualenvTest(CoverageTest):
164
169
expected_stdout = "33\n 110\n 198\n 1.5\n "
165
170
166
171
@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 ] :
168
173
"""For running tests inside venv_world, and cleaning up made files."""
169
174
with change_dir (venv_world ):
170
175
self .make_file ("myproduct.py" , """\
@@ -188,12 +193,12 @@ def in_venv_world_fixture(self, venv_world):
188
193
if fname not in {"venv" , "another_pkg" , "bug888" }:
189
194
os .remove (fname )
190
195
191
- def get_trace_output (self ):
196
+ def get_trace_output (self ) -> str :
192
197
"""Get the debug output of coverage.py"""
193
198
with open ("debug_out.txt" ) as f :
194
199
return f .read ()
195
200
196
- def test_third_party_venv_isnt_measured (self , coverage_command ) :
201
+ def test_third_party_venv_isnt_measured (self , coverage_command : str ) -> None :
197
202
out = run_in_venv (coverage_command + " run --source=. myproduct.py" )
198
203
# In particular, this warning doesn't appear:
199
204
# 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):
218
223
assert "coverage" not in out
219
224
assert "colorsys" not in out
220
225
221
- def test_us_in_venv_isnt_measured (self , coverage_command ) :
226
+ def test_us_in_venv_isnt_measured (self , coverage_command : str ) -> None :
222
227
out = run_in_venv (coverage_command + " run --source=third myproduct.py" )
223
228
assert out == self .expected_stdout
224
229
@@ -245,7 +250,7 @@ def test_us_in_venv_isnt_measured(self, coverage_command):
245
250
assert "coverage" not in out
246
251
assert "colorsys" not in out
247
252
248
- def test_venv_isnt_measured (self , coverage_command ) :
253
+ def test_venv_isnt_measured (self , coverage_command : str ) -> None :
249
254
out = run_in_venv (coverage_command + " run myproduct.py" )
250
255
assert out == self .expected_stdout
251
256
@@ -261,7 +266,7 @@ def test_venv_isnt_measured(self, coverage_command):
261
266
assert "colorsys" not in out
262
267
263
268
@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 :
265
270
# https://github.com/nedbat/coveragepy/issues/1150
266
271
# Django coverage plugin was incorrectly getting warnings:
267
272
# "Already imported: ... django/template/blah.py"
@@ -277,7 +282,7 @@ def test_venv_with_dynamic_plugin(self, coverage_command):
277
282
# Already imported a file that will be measured: ...third/render.py (already-imported)
278
283
assert out == "HTML: hello.html@1723\n "
279
284
280
- def test_installed_namespace_packages (self , coverage_command ) :
285
+ def test_installed_namespace_packages (self , coverage_command : str ) -> None :
281
286
# https://github.com/nedbat/coveragepy/issues/1231
282
287
# When namespace packages were installed, they were considered
283
288
# third-party packages. Test that isn't still happening.
@@ -319,7 +324,7 @@ def test_installed_namespace_packages(self, coverage_command):
319
324
assert "fifth" in out
320
325
assert "sixth" in out
321
326
322
- def test_bug_888 (self , coverage_command ) :
327
+ def test_bug_888 (self , coverage_command : str ) -> None :
323
328
out = run_in_venv (
324
329
coverage_command +
325
330
" run --source=bug888/app,bug888/plugin bug888/app/testcov/main.py"
0 commit comments