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