6
6
"""
7
7
8
8
import os
9
+
9
10
import pytest
11
+ from packaging .version import Version
12
+
13
+ _pytest_version = Version (pytest .__version__ )
14
+ PYTEST_GE_8_0 = any ([_pytest_version .is_devrelease ,
15
+ _pytest_version .is_prerelease ,
16
+ _pytest_version >= Version ('8.0' )])
10
17
11
18
12
19
def pytest_addoption (parser ):
@@ -17,52 +24,106 @@ def pytest_addoption(parser):
17
24
"string to specify multiple packages." )
18
25
19
26
20
- @pytest .hookimpl (tryfirst = True )
21
- def pytest_ignore_collect (path , config ):
27
+ if PYTEST_GE_8_0 :
22
28
23
- # NOTE: it is important that when we don't want to skip a file we return
24
- # None and not False - if we return False pytest will not call any other
25
- # pytest_ignore_collect function in other plugins, e.g. pytest-doctestplus.
29
+ @pytest .hookimpl (tryfirst = True )
30
+ def pytest_ignore_collect (collection_path , config ):
26
31
27
- # If the --package/-P option wasn't specified, don't do anything
28
- if config . getvalue ( 'package' ) is None :
29
- return None
32
+ # NOTE: it is important that when we don't want to skip a file we return
33
+ # None and not False - if we return False pytest will not call any other
34
+ # pytest_ignore_collect function in other plugins, e.g. pytest-doctestplus.
30
35
31
- # If the path is a directory, never skip - just do the filtering on a file
32
- # by file basis.
33
- if os .path .isdir (path ):
34
- return None
36
+ # If the --package/-P option wasn't specified, don't do anything
37
+ if config .getvalue ('package' ) is None :
38
+ return None
35
39
36
- # Otherwise ignore filename for remainder of checks
37
- path = os .path .dirname (path )
40
+ # If the path is a directory, never skip - just do the filtering on a file
41
+ # by file basis.
42
+ if collection_path .is_dir ():
43
+ return None
38
44
39
- # Split path into components
40
- path = path .split (os .path .sep )
45
+ # Otherwise ignore filename for remainder of checks
46
+ path = str (collection_path .parent )
47
+
48
+ # Split path into components
49
+ path = path .split (os .path .sep )
50
+
51
+ # Now cycle through and find the top level of the package - this is the
52
+ # last one that contains an ``__init__.py`` or ``index.rst`` file. We need
53
+ # to make sure that at least one of these files was found before escaping.
54
+ found_prev = False
55
+ for i in range (len (path ), - 1 , - 1 ):
56
+ subpath = os .path .sep .join (path [:i ])
57
+ found = (os .path .exists (os .path .join (subpath , '__init__.py' )) or
58
+ os .path .exists (os .path .join (subpath , 'index.rst' )))
59
+ if found_prev and not found :
60
+ break
61
+ found_prev = found
41
62
42
- # Now cycle through and find the top level of the package - this is the
43
- # last one that contains an ``__init__.py`` or ``index.rst`` file. We need
44
- # to make sure that at least one of these files was found before escaping.
45
- found_prev = False
46
- for i in range (len (path ), - 1 , - 1 ):
47
- subpath = os .path .sep .join (path [:i ])
48
- found = (os .path .exists (os .path .join (subpath , '__init__.py' )) or
49
- os .path .exists (os .path .join (subpath , 'index.rst' )))
50
- if found_prev and not found :
51
- break
52
- found_prev = found
63
+ subpackage_path = path [i + 1 :]
53
64
54
- subpackage_path = path [i + 1 :]
65
+ # Find selected sub-packages
66
+ selected = config .getvalue ('package' ).strip ().split (',' )
55
67
56
- # Find selected sub-packages
57
- selected = config .getvalue ('package' ).strip ().split (',' )
68
+ # Finally, we check if this is one of the specified ones
69
+ for subpackage_target in selected :
70
+ for i , target in enumerate (subpackage_target .split ('.' )):
71
+ if i >= len (subpackage_path ) or target != subpackage_path [i ]:
72
+ break
58
73
59
- # Finally, we check if this is one of the specified ones
60
- for subpackage_target in selected :
61
- for i , target in enumerate (subpackage_target .split ('.' )):
62
- if i >= len (subpackage_path ) or target != subpackage_path [i ]:
63
- break
74
+ else :
75
+ return None
76
+
77
+ return True
78
+
79
+ else :
80
+
81
+ @pytest .hookimpl (tryfirst = True )
82
+ def pytest_ignore_collect (path , config ):
64
83
65
- else :
84
+ # NOTE: it is important that when we don't want to skip a file we return
85
+ # None and not False - if we return False pytest will not call any other
86
+ # pytest_ignore_collect function in other plugins, e.g. pytest-doctestplus.
87
+
88
+ # If the --package/-P option wasn't specified, don't do anything
89
+ if config .getvalue ('package' ) is None :
90
+ return None
91
+
92
+ # If the path is a directory, never skip - just do the filtering on a file
93
+ # by file basis.
94
+ if os .path .isdir (path ):
66
95
return None
67
96
68
- return True
97
+ # Otherwise ignore filename for remainder of checks
98
+ path = os .path .dirname (path )
99
+
100
+ # Split path into components
101
+ path = path .split (os .path .sep )
102
+
103
+ # Now cycle through and find the top level of the package - this is the
104
+ # last one that contains an ``__init__.py`` or ``index.rst`` file. We need
105
+ # to make sure that at least one of these files was found before escaping.
106
+ found_prev = False
107
+ for i in range (len (path ), - 1 , - 1 ):
108
+ subpath = os .path .sep .join (path [:i ])
109
+ found = (os .path .exists (os .path .join (subpath , '__init__.py' )) or
110
+ os .path .exists (os .path .join (subpath , 'index.rst' )))
111
+ if found_prev and not found :
112
+ break
113
+ found_prev = found
114
+
115
+ subpackage_path = path [i + 1 :]
116
+
117
+ # Find selected sub-packages
118
+ selected = config .getvalue ('package' ).strip ().split (',' )
119
+
120
+ # Finally, we check if this is one of the specified ones
121
+ for subpackage_target in selected :
122
+ for i , target in enumerate (subpackage_target .split ('.' )):
123
+ if i >= len (subpackage_path ) or target != subpackage_path [i ]:
124
+ break
125
+
126
+ else :
127
+ return None
128
+
129
+ return True
0 commit comments