44
55from __future__ import annotations
66
7+ import copy
8+ import os
79import re
10+ from collections .abc import Iterator
11+ from contextlib import contextmanager
812from pathlib import Path
913
1014import pytest
@@ -28,7 +32,8 @@ def test__is_in_ignore_list_re_match() -> None:
2832
2933TEST_DIRECTORY = Path (__file__ ).parent .parent
3034INIT_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 )
3237this_file = {
3338 "basename" : "lint.unittest_expand_modules" ,
3439 "basepath" : EXPAND_MODULES ,
@@ -37,6 +42,14 @@ def test__is_in_ignore_list_re_match() -> None:
3742 "path" : EXPAND_MODULES ,
3843}
3944
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+
4053this_file_from_init = {
4154 "basename" : "lint" ,
4255 "basepath" : INIT_PATH ,
@@ -117,6 +130,27 @@ def _list_expected_package_modules(
117130 )
118131
119132
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+
120154class TestExpandModules (CheckerTestCase ):
121155 """Test the expand_modules function while allowing options to be set."""
122156
@@ -159,6 +193,40 @@ def test_expand_modules(
159193 assert modules == expected
160194 assert not errors
161195
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+
162230 @pytest .mark .parametrize (
163231 "files_or_modules,expected" ,
164232 [
0 commit comments