add filter for known collisions

This commit is contained in:
Markus Hofbauer 2024-02-02 14:13:12 +01:00
parent b4a077fdd8
commit 68f45fe079
2 changed files with 68 additions and 12 deletions

View file

@ -12,6 +12,45 @@ ERROR_MESSAGE_TEMPLATE = (
"Alias `%s` defined in `%s` already exists as alias `%s` in `%s`." "Alias `%s` defined in `%s` already exists as alias `%s` in `%s`."
) )
# TODO: We want that list to be empty
KNOWN_COLLISIONS = [
"bcubc",
"pbl",
"gcd",
"h",
"brs",
"github",
"stackoverflow",
"zcl",
"afs",
"allpkgs",
"mydeb",
"jh",
"n",
"a",
"p",
"sf",
"sp",
"hs",
"db",
"rn",
"rs",
"ru",
"sc",
"sd",
"sd",
"sp",
"c",
"dr",
"rake",
"rubies",
"h",
"ma",
"map",
"mis",
"m",
]
def dir_path(path_string: str) -> Path: def dir_path(path_string: str) -> Path:
if Path(path_string).is_dir(): if Path(path_string).is_dir():
@ -47,6 +86,9 @@ class Collision:
existing_alias: Alias existing_alias: Alias
new_alias: Alias new_alias: Alias
def is_new_collision(self, known_collisions: List[str]) -> bool:
return self.new_alias.alias not in known_collisions
def find_aliases_in_file(file: Path) -> List[Alias]: def find_aliases_in_file(file: Path) -> List[Alias]:
matches = re.findall(r"^alias (.*)='(.*)'", file.read_text(), re.M) matches = re.findall(r"^alias (.*)='(.*)'", file.read_text(), re.M)
@ -54,8 +96,7 @@ def find_aliases_in_file(file: Path) -> List[Alias]:
def find_all_aliases(path: Path) -> list: def find_all_aliases(path: Path) -> list:
files = list(path.rglob("*.zsh")) aliases = [find_aliases_in_file(file) for file in path.rglob("*.zsh")]
aliases = [find_aliases_in_file(file) for file in files]
return list(itertools.chain(*aliases)) return list(itertools.chain(*aliases))
@ -94,10 +135,13 @@ def main():
args = parse_arguments() args = parse_arguments()
aliases = find_all_aliases(args.folder) aliases = find_all_aliases(args.folder)
collisions = check_for_duplicates(aliases) collisions = check_for_duplicates(aliases)
print_collisions(collisions) new_collisions = [
# TODO enable once all collisions are fixed collision
# if collisions: for collision in collisions
# exit(-1) if collision.is_new_collision(KNOWN_COLLISIONS)
]
print_collisions(new_collisions)
return -1 if collisions else 0
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -87,9 +87,7 @@ def test_find_aliases_in_file__one_conditional_alias_should_find_none(
assert [] == result assert [] == result
def test_check_for_duplicates__no_duplicates_should_return_empty_dict( def test_check_for_duplicates__no_duplicates_should_return_empty_dict() -> None:
fs: FakeFilesystem,
) -> None:
result = check_for_duplicates( result = check_for_duplicates(
[ [
Alias("g", "git", Path("git.zsh")), Alias("g", "git", Path("git.zsh")),
@ -100,9 +98,7 @@ def test_check_for_duplicates__no_duplicates_should_return_empty_dict(
assert result == [] assert result == []
def test_check_for_duplicates__duplicates_should_have_one_collision( def test_check_for_duplicates__duplicates_should_have_one_collision() -> None:
fs: FakeFilesystem,
) -> None:
result = check_for_duplicates( result = check_for_duplicates(
[ [
Alias("gc", "git commit", Path("git.zsh")), Alias("gc", "git commit", Path("git.zsh")),
@ -115,3 +111,19 @@ def test_check_for_duplicates__duplicates_should_have_one_collision(
Alias("gc", "git clone", Path("git.zsh")), Alias("gc", "git clone", Path("git.zsh")),
) )
] ]
def test_is_new_collision__new_alias_not_in_known_collisions__should_return_true() -> (
None
):
known_collisions = ["gc", "gd"]
new_alias = Alias("ga", "git add", Path("git.zsh"))
collision = Collision(Alias("gd", "git diff", Path("git.zsh")), new_alias)
assert collision.is_new_collision(known_collisions) is True
def test_is_new_collision__new_alias_in_known_collisions__should_return_false() -> None:
known_collisions = ["gc", "gd", "ga"]
new_alias = Alias("ga", "git add", Path("git.zsh"))
collision = Collision(Alias("gd", "git diff", Path("git.zsh")), new_alias)
assert collision.is_new_collision(known_collisions) is False