From 68f45fe079b133797460a4575f550da3c60bcca0 Mon Sep 17 00:00:00 2001 From: Markus Hofbauer Date: Fri, 2 Feb 2024 14:13:12 +0100 Subject: [PATCH] add filter for known collisions --- .../alias_collision/check_alias_collision.py | 56 +++++++++++++++++-- .../tests/test_check_alias_collision.py | 24 ++++++-- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/tools/alias_collision/check_alias_collision.py b/tools/alias_collision/check_alias_collision.py index effb126ec..9da18112d 100644 --- a/tools/alias_collision/check_alias_collision.py +++ b/tools/alias_collision/check_alias_collision.py @@ -12,6 +12,45 @@ ERROR_MESSAGE_TEMPLATE = ( "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: if Path(path_string).is_dir(): @@ -47,6 +86,9 @@ class Collision: existing_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]: 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: - files = list(path.rglob("*.zsh")) - aliases = [find_aliases_in_file(file) for file in files] + aliases = [find_aliases_in_file(file) for file in path.rglob("*.zsh")] return list(itertools.chain(*aliases)) @@ -94,10 +135,13 @@ def main(): args = parse_arguments() aliases = find_all_aliases(args.folder) collisions = check_for_duplicates(aliases) - print_collisions(collisions) - # TODO enable once all collisions are fixed - # if collisions: - # exit(-1) + new_collisions = [ + collision + for collision in collisions + if collision.is_new_collision(KNOWN_COLLISIONS) + ] + print_collisions(new_collisions) + return -1 if collisions else 0 if __name__ == "__main__": diff --git a/tools/alias_collision/tests/test_check_alias_collision.py b/tools/alias_collision/tests/test_check_alias_collision.py index 5d7c28d2d..57ca17ed2 100644 --- a/tools/alias_collision/tests/test_check_alias_collision.py +++ b/tools/alias_collision/tests/test_check_alias_collision.py @@ -87,9 +87,7 @@ def test_find_aliases_in_file__one_conditional_alias_should_find_none( assert [] == result -def test_check_for_duplicates__no_duplicates_should_return_empty_dict( - fs: FakeFilesystem, -) -> None: +def test_check_for_duplicates__no_duplicates_should_return_empty_dict() -> None: result = check_for_duplicates( [ Alias("g", "git", Path("git.zsh")), @@ -100,9 +98,7 @@ def test_check_for_duplicates__no_duplicates_should_return_empty_dict( assert result == [] -def test_check_for_duplicates__duplicates_should_have_one_collision( - fs: FakeFilesystem, -) -> None: +def test_check_for_duplicates__duplicates_should_have_one_collision() -> None: result = check_for_duplicates( [ 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")), ) ] + + +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