2
2
Custom lint tests.
3
3
"""
4
4
5
- import asyncio
6
5
from pathlib import Path
7
6
8
7
import pytest
@@ -23,20 +22,23 @@ def _ci_patterns() -> set[str]:
23
22
return ci_patterns
24
23
25
24
26
- async def _tests_from_pattern (ci_pattern : str ) -> set [str ]:
25
+ def _tests_from_pattern (
26
+ ci_pattern : str ,
27
+ capsys : pytest .CaptureFixture [str ],
28
+ ) -> set [str ]:
27
29
"""
28
30
From a CI pattern, get all tests ``pytest`` would collect.
29
31
"""
32
+ # Clear the captured output.
33
+ capsys .readouterr ()
30
34
tests : set [str ] = set ()
31
- args = ["pytest" , "-q" , "--collect-only" , ci_pattern ]
32
- process = await asyncio .create_subprocess_exec (
33
- * args ,
34
- stdout = asyncio .subprocess .PIPE ,
35
- )
36
- data , _ = await process .communicate ()
35
+ pytest .main (args = ["-q" , "--collect-only" , ci_pattern ])
36
+ data = capsys .readouterr ().out
37
37
for line in data .splitlines ():
38
- if line and b"collected in" not in line :
39
- tests .add (line .decode ())
38
+ # We filter empty lines and lines which look like
39
+ # "9 tests collected in 0.01s".
40
+ if line and "collected in" not in line :
41
+ tests .add (line )
40
42
return tests
41
43
42
44
@@ -54,22 +56,19 @@ def test_ci_patterns_valid() -> None:
54
56
assert collect_only_result == 0 , message
55
57
56
58
57
- @pytest .mark .asyncio ()
58
- async def test_tests_collected_once () -> None :
59
+ def test_tests_collected_once (
60
+ capsys : pytest .CaptureFixture [str ],
61
+ ) -> None :
59
62
"""
60
63
Each test in the test suite is collected exactly once.
61
64
62
65
This does not necessarily mean that they are run - they may be skipped.
63
66
"""
64
67
ci_patterns = _ci_patterns ()
65
68
tests_to_patterns : dict [str , set [str ]] = {}
66
- tasks = []
67
- for pattern in ci_patterns :
68
- tasks .append (_tests_from_pattern (ci_pattern = pattern ))
69
69
70
- results = await asyncio .gather (* tasks )
71
- for index , pattern in enumerate (ci_patterns ):
72
- tests = results [index ]
70
+ for pattern in ci_patterns :
71
+ tests = _tests_from_pattern (ci_pattern = pattern , capsys = capsys )
73
72
for test in tests :
74
73
if test in tests_to_patterns :
75
74
tests_to_patterns [test ].add (pattern )
@@ -84,6 +83,6 @@ async def test_tests_collected_once() -> None:
84
83
)
85
84
assert len (patterns ) == 1 , message
86
85
87
- all_tests = await _tests_from_pattern (ci_pattern = "." )
86
+ all_tests = _tests_from_pattern (ci_pattern = "." , capsys = capsys )
88
87
assert tests_to_patterns .keys () - all_tests == set ()
89
88
assert all_tests - tests_to_patterns .keys () == set ()
0 commit comments