Skip to content

Commit 85177d4

Browse files
committed
Move exclude part to own function, add unit test
1 parent 7e993e5 commit 85177d4

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

pylsp_mypy/plugin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ def didSettingsChange(workspace: str, settings: Dict[str, Any]) -> None:
142142
settingsCache[workspace] = settings.copy()
143143

144144

145+
def match_exclude_patterns(document_path: str, exclude_patterns: list) -> bool:
146+
for pattern in exclude_patterns:
147+
# This makes sure that \\ characters are encoded correctly so for
148+
# example windows paths are matched without any bad escape errors
149+
pattern = pattern.encode("unicode-escape").decode()
150+
151+
try:
152+
if re.search(pattern, document_path):
153+
log.debug(f"{document_path} matches " f"exclude pattern '{pattern}'")
154+
return True
155+
except re.error as e:
156+
log.error(f"pattern {pattern} is not a valid regular expression: {e}")
157+
158+
return False
159+
160+
145161
@hookimpl
146162
def pylsp_lint(
147163
config: Config, workspace: Workspace, document: Document, is_saved: bool

test/test_plugin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ def foo():
330330
assert diag["code"] == "unreachable"
331331

332332

333+
@pytest.mark.parametrize(
334+
"document_path,pattern,pattern_matched",
335+
(
336+
("/workspace/my-file.py", "/someting-else", False),
337+
("/workspace/my-file.py", "^/workspace$", False),
338+
("/workspace/my-file.py", "/workspace", True),
339+
("/workspace/my-file.py", "^/workspace(.*)$", True),
340+
# This is a broken regex (missing ')'), but should not choke
341+
("/workspace/my-file.py", "/((workspace)", False),
342+
# A windows path is tricky with all those \\ but should work
343+
("d:\\a\\my-file.py", "\\a", True),
344+
),
345+
)
346+
def test_match_exclude_patterns(document_path, pattern, pattern_matched):
347+
assert (
348+
plugin.match_exclude_patterns(document_path=document_path, exclude_patterns=[pattern])
349+
is pattern_matched
350+
)
351+
352+
333353
def test_config_exclude(tmpdir, workspace):
334354
"""When exclude is set in config then mypy should not run for that file."""
335355
doc = Document(DOC_URI, workspace, DOC_TYPE_ERR)

0 commit comments

Comments
 (0)