4
4
5
5
from __future__ import annotations
6
6
7
+ import copy
8
+ import os
7
9
import re
10
+ from collections .abc import Iterator
11
+ from contextlib import contextmanager
8
12
from pathlib import Path
9
13
10
14
import pytest
@@ -28,7 +32,8 @@ def test__is_in_ignore_list_re_match() -> None:
28
32
29
33
TEST_DIRECTORY = Path (__file__ ).parent .parent
30
34
INIT_PATH = str (TEST_DIRECTORY / "lint/__init__.py" )
31
- EXPAND_MODULES = str (TEST_DIRECTORY / "lint/unittest_expand_modules.py" )
35
+ EXPAND_MODULES_BASE = "unittest_expand_modules.py"
36
+ EXPAND_MODULES = str (TEST_DIRECTORY / "lint" / EXPAND_MODULES_BASE )
32
37
this_file = {
33
38
"basename" : "lint.unittest_expand_modules" ,
34
39
"basepath" : EXPAND_MODULES ,
@@ -37,6 +42,14 @@ def test__is_in_ignore_list_re_match() -> None:
37
42
"path" : EXPAND_MODULES ,
38
43
}
39
44
45
+ this_file_relative_to_parent = {
46
+ "basename" : "lint.unittest_expand_modules" ,
47
+ "basepath" : EXPAND_MODULES_BASE ,
48
+ "isarg" : True ,
49
+ "name" : "lint.unittest_expand_modules" ,
50
+ "path" : EXPAND_MODULES_BASE ,
51
+ }
52
+
40
53
this_file_from_init = {
41
54
"basename" : "lint" ,
42
55
"basepath" : INIT_PATH ,
@@ -117,6 +130,27 @@ def _list_expected_package_modules(
117
130
)
118
131
119
132
133
+ def _list_expected_package_modules_relative () -> tuple [dict [str , object ], ...]:
134
+ """Generates reusable list of modules for our package with relative path input."""
135
+ abs_result = copy .deepcopy (_list_expected_package_modules ())
136
+ for item in abs_result :
137
+ assert isinstance (item ["basepath" ], str )
138
+ assert isinstance (item ["path" ], str )
139
+ item ["basepath" ] = os .path .relpath (item ["basepath" ], str (Path (__file__ ).parent ))
140
+ item ["path" ] = os .path .relpath (item ["path" ], str (Path (__file__ ).parent ))
141
+ return abs_result
142
+
143
+
144
+ @contextmanager
145
+ def pushd (path : Path ) -> Iterator [None ]:
146
+ prev = os .getcwd ()
147
+ os .chdir (path )
148
+ try :
149
+ yield
150
+ finally :
151
+ os .chdir (prev )
152
+
153
+
120
154
class TestExpandModules (CheckerTestCase ):
121
155
"""Test the expand_modules function while allowing options to be set."""
122
156
@@ -159,6 +193,40 @@ def test_expand_modules(
159
193
assert modules == expected
160
194
assert not errors
161
195
196
+ @pytest .mark .parametrize (
197
+ "files_or_modules,expected" ,
198
+ [
199
+ (
200
+ [Path (__file__ ).name ],
201
+ {this_file_relative_to_parent ["path" ]: this_file_relative_to_parent },
202
+ ),
203
+ (
204
+ ["./" ],
205
+ {
206
+ module ["path" ]: module # pylint: disable=unsubscriptable-object
207
+ for module in _list_expected_package_modules_relative ()
208
+ },
209
+ ),
210
+ ],
211
+ )
212
+ @set_config (ignore_paths = "" )
213
+ def test_expand_modules_relative_path (
214
+ self , files_or_modules : list [str ], expected : dict [str , ModuleDescriptionDict ]
215
+ ) -> None :
216
+ """Test expand_modules with the default value of ignore-paths and relative path as input."""
217
+ ignore_list : list [str ] = []
218
+ ignore_list_re : list [re .Pattern [str ]] = []
219
+ with pushd (Path (__file__ ).parent ):
220
+ modules , errors = expand_modules (
221
+ files_or_modules ,
222
+ [],
223
+ ignore_list ,
224
+ ignore_list_re ,
225
+ self .linter .config .ignore_paths ,
226
+ )
227
+ assert modules == expected
228
+ assert not errors
229
+
162
230
@pytest .mark .parametrize (
163
231
"files_or_modules,expected" ,
164
232
[
0 commit comments