From b4a077fdd85b38d06aeb706e54344622df6692b8 Mon Sep 17 00:00:00 2001 From: Markus Hofbauer Date: Fri, 2 Feb 2024 13:48:57 +0100 Subject: [PATCH] refactor to pytest --- .github/workflows/main.yml | 2 +- .../alias_collision/check_alias_collision.py | 7 +- tools/alias_collision/requirements.txt | 2 + .../tests/test_check_alias_collision.py | 151 ++++++++++-------- 4 files changed, 90 insertions(+), 72 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9170daee7..11b70afe2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -56,4 +56,4 @@ jobs: - name: Run unit tests run: | cd tools/alias_collision/ - python -m unittest discover tests + python -m pytest diff --git a/tools/alias_collision/check_alias_collision.py b/tools/alias_collision/check_alias_collision.py index ac8e2fa20..effb126ec 100644 --- a/tools/alias_collision/check_alias_collision.py +++ b/tools/alias_collision/check_alias_collision.py @@ -3,6 +3,7 @@ from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser from pathlib import Path from dataclasses import dataclass +from typing import List, Dict import itertools import re @@ -47,7 +48,7 @@ class Collision: new_alias: Alias -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) return [Alias(match[0], match[1], file) for match in matches] @@ -58,7 +59,7 @@ def find_all_aliases(path: Path) -> list: return list(itertools.chain(*aliases)) -def check_for_duplicates(aliases: list[Alias]) -> list[Collision]: +def check_for_duplicates(aliases: List[Alias]) -> List[Collision]: elements = {} collisions = [] for alias in aliases: @@ -70,7 +71,7 @@ def check_for_duplicates(aliases: list[Alias]) -> list[Collision]: return collisions -def print_collisions(collisions: dict[Alias, Alias]) -> None: +def print_collisions(collisions: Dict[Alias, Alias]) -> None: if collisions: print(f"Found {len(collisions)} alias collisions:\n") for collision in collisions: diff --git a/tools/alias_collision/requirements.txt b/tools/alias_collision/requirements.txt index 230c9dde1..edf3ab03b 100644 --- a/tools/alias_collision/requirements.txt +++ b/tools/alias_collision/requirements.txt @@ -1 +1,3 @@ pyfakefs +pytest +pytest-sugar diff --git a/tools/alias_collision/tests/test_check_alias_collision.py b/tools/alias_collision/tests/test_check_alias_collision.py index 4eb0ae343..5d7c28d2d 100644 --- a/tools/alias_collision/tests/test_check_alias_collision.py +++ b/tools/alias_collision/tests/test_check_alias_collision.py @@ -1,6 +1,8 @@ from pathlib import Path -from pyfakefs.fake_filesystem_unittest import TestCase +from pyfakefs.fake_filesystem import FakeFilesystem +import pytest + from check_alias_collision import ( dir_path, find_all_aliases, @@ -25,78 +27,91 @@ is-at-least 2.8 "$git_version" \ """ -class CheckAliasCollisionTest(TestCase): - def setUp(self) -> None: - self.setUpPyfakefs() +def test_dir_path__is_dir__input_path(fs: FakeFilesystem) -> None: + fs.create_dir("test") + assert Path("test") == dir_path("test") - def test_dir_path__is_dir__input_path(self) -> None: - self.fs.create_dir("test") - self.assertEqual(Path("test"), dir_path("test")) - def test_dir_path__is_file__raise_not_a_directory_error(self) -> None: - self.fs.create_file("test") - with self.assertRaises(NotADirectoryError): - dir_path("test") +def test_dir_path__is_file__raise_not_a_directory_error(fs: FakeFilesystem) -> None: + fs.create_file("test") + with pytest.raises(NotADirectoryError): + dir_path("test") - def test_dir_path__does_not_exist__raise_not_a_directory_error(self) -> None: - with self.assertRaises(NotADirectoryError): - dir_path("test") - def test_find_all_aliases__empty_folder_should_return_empty_list(self) -> None: - self.fs.create_dir("test") - result = find_all_aliases(Path("test")) - self.assertListEqual([], result) +def test_dir_path__does_not_exist__raise_not_a_directory_error( + fs: FakeFilesystem, +) -> None: + with pytest.raises(NotADirectoryError): + dir_path("test") - def test_find_aliases_in_file__empty_text_should_return_empty_list(self) -> None: - self.fs.create_file("empty.zsh") - result = find_aliases_in_file(Path("empty.zsh")) - self.assertListEqual([], result) - def test_find_aliases_in_file__one_alias_should_find_one(self) -> None: - self.fs.create_file("one.zsh", contents="alias g='git'") - result = find_aliases_in_file(Path("one.zsh")) - self.assertListEqual([Alias("g", "git", Path("one.zsh"))], result) +def test_find_all_aliases__empty_folder_should_return_empty_list( + fs: FakeFilesystem, +) -> None: + fs.create_dir("test") + result = find_all_aliases(Path("test")) + assert [] == result - def test_find_aliases_in_file__three_aliases_should_find_three(self) -> None: - self.fs.create_file("three.zsh", contents=THREE_ALIASES) - result = find_aliases_in_file(Path("three.zsh")) - self.assertListEqual( - [ - Alias("g", "git", Path("three.zsh")), - Alias("ga", "git add", Path("three.zsh")), - Alias("gaa", "git add --all", Path("three.zsh")), - ], - result, - ) - - def test_find_aliases_in_file__one_conditional_alias_should_find_none(self) -> None: - self.fs.create_file("conditional.zsh", contents=CONDITIONAL_ALIAS) - result = find_aliases_in_file(Path("conditional.zsh")) - self.assertListEqual([], result) - - def test_check_for_duplicates__no_duplicates_should_return_empty_dict(self) -> None: - result = check_for_duplicates( - [ - Alias("g", "git", Path("git.zsh")), - Alias("ga", "git add", Path("git.zsh")), - Alias("gaa", "git add --all", Path("git.zsh")), - ] - ) - self.assertListEqual(result, []) - - def test_check_for_duplicates__duplicates_should_have_one_collision(self) -> None: - result = check_for_duplicates( - [ - Alias("gc", "git commit", Path("git.zsh")), - Alias("gc", "git clone", Path("git.zsh")), - ] - ) - self.assertListEqual( - result, - [ - Collision( - Alias("gc", "git commit", Path("git.zsh")), - Alias("gc", "git clone", Path("git.zsh")), - ) - ], + +def test_find_aliases_in_file__empty_text_should_return_empty_list( + fs: FakeFilesystem, +) -> None: + fs.create_file("empty.zsh") + result = find_aliases_in_file(Path("empty.zsh")) + assert [] == result + + +def test_find_aliases_in_file__one_alias_should_find_one(fs: FakeFilesystem) -> None: + fs.create_file("one.zsh", contents="alias g='git'") + result = find_aliases_in_file(Path("one.zsh")) + assert [Alias("g", "git", Path("one.zsh"))] == result + + +def test_find_aliases_in_file__three_aliases_should_find_three( + fs: FakeFilesystem, +) -> None: + fs.create_file("three.zsh", contents=THREE_ALIASES) + result = find_aliases_in_file(Path("three.zsh")) + assert [ + Alias("g", "git", Path("three.zsh")), + Alias("ga", "git add", Path("three.zsh")), + Alias("gaa", "git add --all", Path("three.zsh")), + ] == result + + +def test_find_aliases_in_file__one_conditional_alias_should_find_none( + fs: FakeFilesystem, +) -> None: + fs.create_file("conditional.zsh", contents=CONDITIONAL_ALIAS) + result = find_aliases_in_file(Path("conditional.zsh")) + assert [] == result + + +def test_check_for_duplicates__no_duplicates_should_return_empty_dict( + fs: FakeFilesystem, +) -> None: + result = check_for_duplicates( + [ + Alias("g", "git", Path("git.zsh")), + Alias("ga", "git add", Path("git.zsh")), + Alias("gaa", "git add --all", Path("git.zsh")), + ] + ) + assert result == [] + + +def test_check_for_duplicates__duplicates_should_have_one_collision( + fs: FakeFilesystem, +) -> None: + result = check_for_duplicates( + [ + Alias("gc", "git commit", Path("git.zsh")), + Alias("gc", "git clone", Path("git.zsh")), + ] + ) + assert result == [ + Collision( + Alias("gc", "git commit", Path("git.zsh")), + Alias("gc", "git clone", Path("git.zsh")), ) + ]