From e7fd202cbd535ebbb667a9c85ae137acdaaf461f Mon Sep 17 00:00:00 2001 From: Daniel D'Avella Date: Thu, 11 Apr 2024 09:14:04 -0400 Subject: [PATCH] Add a warning when pattern doesn't match any codemods --- src/codemodder/registry.py | 11 +++++++---- tests/codemods/test_include_exclude.py | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/codemodder/registry.py b/src/codemodder/registry.py index 9691eefb3..2f5fc6693 100644 --- a/src/codemodder/registry.py +++ b/src/codemodder/registry.py @@ -83,15 +83,18 @@ def match_codemods( base_codemods[codemod.id] = codemod # Remove duplicates and preserve order - return list(dict.fromkeys(base_codemods.values())) + return list(base_codemods.values()) matched_codemods = [] for name in codemod_include: if "*" in name: pat = re.compile(name.replace("*", ".*")) - matched_codemods.extend( - [codemod for codemod in self.codemods if pat.match(codemod.id)] - ) + pattern_matches = [code for code in self.codemods if pat.match(code.id)] + matched_codemods.extend(pattern_matches) + if not pattern_matches: + logger.warning( + "Given codemod pattern '%s' does not match any codemods.", name + ) continue try: diff --git a/tests/codemods/test_include_exclude.py b/tests/codemods/test_include_exclude.py index eb436a7a0..5f4699aee 100644 --- a/tests/codemods/test_include_exclude.py +++ b/tests/codemods/test_include_exclude.py @@ -105,6 +105,13 @@ def test_include_sast_with_prefix(self): c for c in self.registry.codemods if c.origin == "sonar" ] + def test_warn_pattern_no_match(self, caplog): + assert self.registry.match_codemods(["*doesntexist*"], None) == [] + assert ( + "Given codemod pattern '*doesntexist*' does not match any codemods" + in caplog.text + ) + def test_exclude_with_pattern(self): assert self.registry.match_codemods(None, ["*django*"], sast_only=False) == [ c