3
3
4
4
"""Tests that our test infrastructure is really working!"""
5
5
6
+ from __future__ import annotations
7
+
6
8
import datetime
7
9
import os
8
10
import re
9
11
import sys
10
12
import warnings
11
13
14
+ from typing import List , Tuple
15
+
12
16
import pytest
13
17
14
18
import coverage
15
19
from coverage .exceptions import CoverageWarning
16
20
from coverage .files import actual_path
21
+ from coverage .types import TArc
17
22
18
23
from tests .coveragetest import CoverageTest
19
24
from tests .helpers import (
22
27
)
23
28
24
29
25
- def test_xdist_sys_path_nuttiness_is_fixed ():
30
+ def test_xdist_sys_path_nuttiness_is_fixed () -> None :
26
31
# See conftest.py:fix_xdist_sys_path
27
32
assert sys .path [1 ] != ''
28
33
assert os .environ .get ('PYTHONPATH' ) is None
29
34
30
35
31
- def test_assert_count_equal ():
36
+ def test_assert_count_equal () -> None :
32
37
assert_count_equal (set (), set ())
33
38
assert_count_equal ({"a" : 1 , "b" : 2 }, ["b" , "a" ])
34
39
with pytest .raises (AssertionError ):
@@ -40,7 +45,7 @@ def test_assert_count_equal():
40
45
class CoverageTestTest (CoverageTest ):
41
46
"""Test the methods in `CoverageTest`."""
42
47
43
- def test_file_exists (self ):
48
+ def test_file_exists (self ) -> None :
44
49
self .make_file ("whoville.txt" , "We are here!" )
45
50
self .assert_exists ("whoville.txt" )
46
51
self .assert_doesnt_exist ("shadow.txt" )
@@ -51,7 +56,7 @@ def test_file_exists(self):
51
56
with pytest .raises (AssertionError , match = msg ):
52
57
self .assert_exists ("shadow.txt" )
53
58
54
- def test_file_count (self ):
59
+ def test_file_count (self ) -> None :
55
60
self .make_file ("abcde.txt" , "abcde" )
56
61
self .make_file ("axczz.txt" , "axczz" )
57
62
self .make_file ("afile.txt" , "afile" )
@@ -82,8 +87,8 @@ def test_file_count(self):
82
87
with pytest .raises (AssertionError , match = msg ):
83
88
self .assert_file_count ("*.q" , 10 )
84
89
85
- def test_assert_recent_datetime (self ):
86
- def now_delta (seconds ) :
90
+ def test_assert_recent_datetime (self ) -> None :
91
+ def now_delta (seconds : int ) -> datetime . datetime :
87
92
"""Make a datetime `seconds` seconds from now."""
88
93
return datetime .datetime .now () + datetime .timedelta (seconds = seconds )
89
94
@@ -103,7 +108,7 @@ def now_delta(seconds):
103
108
with pytest .raises (AssertionError ):
104
109
self .assert_recent_datetime (now_delta (1 ), seconds = 120 )
105
110
106
- def test_assert_warnings (self ):
111
+ def test_assert_warnings (self ) -> None :
107
112
cov = coverage .Coverage ()
108
113
109
114
# Make a warning, it should catch it properly.
@@ -152,7 +157,7 @@ def test_assert_warnings(self):
152
157
with self .assert_warnings (cov , ["Hello there!" ]):
153
158
raise ZeroDivisionError ("oops" )
154
159
155
- def test_assert_no_warnings (self ):
160
+ def test_assert_no_warnings (self ) -> None :
156
161
cov = coverage .Coverage ()
157
162
158
163
# Happy path: no warnings.
@@ -165,7 +170,7 @@ def test_assert_no_warnings(self):
165
170
with self .assert_warnings (cov , []):
166
171
cov ._warn ("Watch out!" )
167
172
168
- def test_sub_python_is_this_python (self ):
173
+ def test_sub_python_is_this_python (self ) -> None :
169
174
# Try it with a Python command.
170
175
self .set_environ ('COV_FOOBAR' , 'XYZZY' )
171
176
self .make_file ("showme.py" , """\
@@ -174,10 +179,10 @@ def test_sub_python_is_this_python(self):
174
179
print(os.__file__)
175
180
print(os.environ['COV_FOOBAR'])
176
181
""" )
177
- out = self .run_command ("python showme.py" ).splitlines ()
178
- assert actual_path (out [0 ]) == actual_path (sys .executable )
179
- assert out [1 ] == os .__file__
180
- assert out [2 ] == 'XYZZY'
182
+ out_lines = self .run_command ("python showme.py" ).splitlines ()
183
+ assert actual_path (out_lines [0 ]) == actual_path (sys .executable )
184
+ assert out_lines [1 ] == os .__file__
185
+ assert out_lines [2 ] == 'XYZZY'
181
186
182
187
# Try it with a "coverage debug sys" command.
183
188
out = self .run_command ("coverage debug sys" )
@@ -191,7 +196,7 @@ def test_sub_python_is_this_python(self):
191
196
_ , _ , environ = environ .rpartition (":" )
192
197
assert environ .strip () == "COV_FOOBAR = XYZZY"
193
198
194
- def test_run_command_stdout_stderr (self ):
199
+ def test_run_command_stdout_stderr (self ) -> None :
195
200
# run_command should give us both stdout and stderr.
196
201
self .make_file ("outputs.py" , """\
197
202
import sys
@@ -202,7 +207,7 @@ def test_run_command_stdout_stderr(self):
202
207
assert "StdOut\n " in out
203
208
assert "StdErr\n " in out
204
209
205
- def test_stdout (self ):
210
+ def test_stdout (self ) -> None :
206
211
# stdout is captured.
207
212
print ("This is stdout" )
208
213
print ("Line 2" )
@@ -219,14 +224,19 @@ class CheckUniqueFilenamesTest(CoverageTest):
219
224
220
225
class Stub :
221
226
"""A stand-in for the class we're checking."""
222
- def __init__ (self , x ) :
227
+ def __init__ (self , x : int ) -> None :
223
228
self .x = x
224
229
225
- def method (self , filename , a = 17 , b = "hello" ):
230
+ def method (
231
+ self ,
232
+ filename : str ,
233
+ a : int = 17 ,
234
+ b : str = "hello" ,
235
+ ) -> Tuple [int , str , int , str ]:
226
236
"""The method we'll wrap, with args to be sure args work."""
227
237
return (self .x , filename , a , b )
228
238
229
- def test_detect_duplicate (self ):
239
+ def test_detect_duplicate (self ) -> None :
230
240
stub = self .Stub (23 )
231
241
CheckUniqueFilenames .hook (stub , "method" )
232
242
@@ -259,7 +269,7 @@ def oops(x):
259
269
ARCZ_MISSING = "3-2 78 8B"
260
270
ARCZ_UNPREDICTED = "79"
261
271
262
- def test_check_coverage_possible (self ):
272
+ def test_check_coverage_possible (self ) -> None :
263
273
msg = r"(?s)Possible arcs differ: .*- \(6, 3\).*\+ \(6, 7\)"
264
274
with pytest .raises (AssertionError , match = msg ):
265
275
self .check_coverage (
@@ -269,7 +279,7 @@ def test_check_coverage_possible(self):
269
279
arcz_unpredicted = self .ARCZ_UNPREDICTED ,
270
280
)
271
281
272
- def test_check_coverage_missing (self ):
282
+ def test_check_coverage_missing (self ) -> None :
273
283
msg = r"(?s)Missing arcs differ: .*- \(3, 8\).*\+ \(7, 8\)"
274
284
with pytest .raises (AssertionError , match = msg ):
275
285
self .check_coverage (
@@ -279,7 +289,7 @@ def test_check_coverage_missing(self):
279
289
arcz_unpredicted = self .ARCZ_UNPREDICTED ,
280
290
)
281
291
282
- def test_check_coverage_unpredicted (self ):
292
+ def test_check_coverage_unpredicted (self ) -> None :
283
293
msg = r"(?s)Unpredicted arcs differ: .*- \(3, 9\).*\+ \(7, 9\)"
284
294
with pytest .raises (AssertionError , match = msg ):
285
295
self .check_coverage (
@@ -300,7 +310,7 @@ class ReLinesTest(CoverageTest):
300
310
("[13]" , "line1\n line2\n line3\n " , "line1\n line3\n " ),
301
311
("X" , "line1\n line2\n line3\n " , "" ),
302
312
])
303
- def test_re_lines (self , pat , text , result ) :
313
+ def test_re_lines (self , pat : str , text : str , result : str ) -> None :
304
314
assert re_lines_text (pat , text ) == result
305
315
assert re_lines (pat , text ) == result .splitlines ()
306
316
@@ -309,26 +319,26 @@ def test_re_lines(self, pat, text, result):
309
319
("[13]" , "line1\n line2\n line3\n " , "line2\n " ),
310
320
("X" , "line1\n line2\n line3\n " , "line1\n line2\n line3\n " ),
311
321
])
312
- def test_re_lines_inverted (self , pat , text , result ) :
322
+ def test_re_lines_inverted (self , pat : str , text : str , result : str ) -> None :
313
323
assert re_lines_text (pat , text , match = False ) == result
314
324
assert re_lines (pat , text , match = False ) == result .splitlines ()
315
325
316
326
@pytest .mark .parametrize ("pat, text, result" , [
317
327
("2" , "line1\n line2\n line3\n " , "line2" ),
318
328
])
319
- def test_re_line (self , pat , text , result ) :
329
+ def test_re_line (self , pat : str , text : str , result : str ) -> None :
320
330
assert re_line (pat , text ) == result
321
331
322
332
@pytest .mark .parametrize ("pat, text" , [
323
333
("line" , "line1\n line2\n line3\n " ), # too many matches
324
334
("X" , "line1\n line2\n line3\n " ), # no matches
325
335
])
326
- def test_re_line_bad (self , pat , text ) :
336
+ def test_re_line_bad (self , pat : str , text : str ) -> None :
327
337
with pytest .raises (AssertionError ):
328
338
re_line (pat , text )
329
339
330
340
331
- def _same_python_executable (e1 , e2 ) :
341
+ def _same_python_executable (e1 : str , e2 : str ) -> bool :
332
342
"""Determine if `e1` and `e2` refer to the same Python executable.
333
343
334
344
Either path could include symbolic links. The two paths might not refer
@@ -365,7 +375,7 @@ class ArczTest(CoverageTest):
365
375
("-11 12 2-5" , [(- 1 , 1 ), (1 , 2 ), (2 , - 5 )]),
366
376
("-QA CB IT Z-A" , [(- 26 , 10 ), (12 , 11 ), (18 , 29 ), (35 , - 10 )]),
367
377
])
368
- def test_arcz_to_arcs (self , arcz , arcs ) :
378
+ def test_arcz_to_arcs (self , arcz : str , arcs : List [ TArc ]) -> None :
369
379
assert arcz_to_arcs (arcz ) == arcs
370
380
371
381
@pytest .mark .parametrize ("arcs, arcz_repr" , [
@@ -382,58 +392,58 @@ def test_arcz_to_arcs(self, arcz, arcs):
382
392
)
383
393
),
384
394
])
385
- def test_arcs_to_arcz_repr (self , arcs , arcz_repr ) :
395
+ def test_arcs_to_arcz_repr (self , arcs : List [ TArc ] , arcz_repr : str ) -> None :
386
396
assert arcs_to_arcz_repr (arcs ) == arcz_repr
387
397
388
398
389
399
class AssertCoverageWarningsTest (CoverageTest ):
390
400
"""Tests of assert_coverage_warnings"""
391
401
392
- def test_one_warning (self ):
402
+ def test_one_warning (self ) -> None :
393
403
with pytest .warns (Warning ) as warns :
394
404
warnings .warn ("Hello there" , category = CoverageWarning )
395
405
assert_coverage_warnings (warns , "Hello there" )
396
406
397
- def test_many_warnings (self ):
407
+ def test_many_warnings (self ) -> None :
398
408
with pytest .warns (Warning ) as warns :
399
409
warnings .warn ("The first" , category = CoverageWarning )
400
410
warnings .warn ("The second" , category = CoverageWarning )
401
411
warnings .warn ("The third" , category = CoverageWarning )
402
412
assert_coverage_warnings (warns , "The first" , "The second" , "The third" )
403
413
404
- def test_wrong_type (self ):
414
+ def test_wrong_type (self ) -> None :
405
415
with pytest .warns (Warning ) as warns :
406
416
warnings .warn ("Not ours" , category = Warning )
407
417
with pytest .raises (AssertionError ):
408
418
assert_coverage_warnings (warns , "Not ours" )
409
419
410
- def test_wrong_message (self ):
420
+ def test_wrong_message (self ) -> None :
411
421
with pytest .warns (Warning ) as warns :
412
422
warnings .warn ("Goodbye" , category = CoverageWarning )
413
423
with pytest .raises (AssertionError ):
414
424
assert_coverage_warnings (warns , "Hello there" )
415
425
416
- def test_wrong_number_too_many (self ):
426
+ def test_wrong_number_too_many (self ) -> None :
417
427
with pytest .warns (Warning ) as warns :
418
428
warnings .warn ("The first" , category = CoverageWarning )
419
429
warnings .warn ("The second" , category = CoverageWarning )
420
430
with pytest .raises (AssertionError ):
421
431
assert_coverage_warnings (warns , "The first" , "The second" , "The third" )
422
432
423
- def test_wrong_number_too_few (self ):
433
+ def test_wrong_number_too_few (self ) -> None :
424
434
with pytest .warns (Warning ) as warns :
425
435
warnings .warn ("The first" , category = CoverageWarning )
426
436
warnings .warn ("The second" , category = CoverageWarning )
427
437
warnings .warn ("The third" , category = CoverageWarning )
428
438
with pytest .raises (AssertionError ):
429
439
assert_coverage_warnings (warns , "The first" , "The second" )
430
440
431
- def test_regex_matches (self ):
441
+ def test_regex_matches (self ) -> None :
432
442
with pytest .warns (Warning ) as warns :
433
443
warnings .warn ("The first" , category = CoverageWarning )
434
444
assert_coverage_warnings (warns , re .compile ("f?rst" ))
435
445
436
- def test_regex_doesnt_match (self ):
446
+ def test_regex_doesnt_match (self ) -> None :
437
447
with pytest .warns (Warning ) as warns :
438
448
warnings .warn ("The first" , category = CoverageWarning )
439
449
with pytest .raises (AssertionError ):
0 commit comments