Skip to content

Commit

Permalink
Add a warning when pattern doesn't match any codemods
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Apr 11, 2024
1 parent 1bab146 commit e7fd202
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/codemodder/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions tests/codemods/test_include_exclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e7fd202

Please sign in to comment.