2022-04-15 11:34:17 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-02-02 12:48:57 +00:00
|
|
|
from pyfakefs.fake_filesystem import FakeFilesystem
|
|
|
|
import pytest
|
|
|
|
|
2022-04-15 11:34:17 +00:00
|
|
|
from check_alias_collision import (
|
|
|
|
dir_path,
|
|
|
|
find_all_aliases,
|
|
|
|
find_aliases_in_file,
|
|
|
|
check_for_duplicates,
|
|
|
|
Alias,
|
2022-11-22 16:57:15 +00:00
|
|
|
Collision,
|
2022-04-15 11:34:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
THREE_ALIASES = """
|
|
|
|
alias g='git'
|
|
|
|
|
|
|
|
alias ga='git add'
|
|
|
|
alias gaa='git add --all'
|
|
|
|
"""
|
|
|
|
|
|
|
|
CONDITIONAL_ALIAS = """
|
|
|
|
is-at-least 2.8 "$git_version" \
|
|
|
|
&& alias gfa='git fetch --all --prune --jobs=10' \
|
|
|
|
|| alias gfa='git fetch --all --prune'
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2024-02-02 12:48:57 +00:00
|
|
|
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_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(
|
|
|
|
fs: FakeFilesystem,
|
|
|
|
) -> None:
|
|
|
|
with pytest.raises(NotADirectoryError):
|
|
|
|
dir_path("test")
|
|
|
|
|
|
|
|
|
|
|
|
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__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
|
|
|
|
|
|
|
|
|
2024-02-02 13:13:12 +00:00
|
|
|
def test_check_for_duplicates__no_duplicates_should_return_empty_dict() -> None:
|
2024-02-02 12:48:57 +00:00
|
|
|
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 == []
|
|
|
|
|
|
|
|
|
2024-02-02 13:13:12 +00:00
|
|
|
def test_check_for_duplicates__duplicates_should_have_one_collision() -> None:
|
2024-02-02 12:48:57 +00:00
|
|
|
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")),
|
2022-11-22 16:57:15 +00:00
|
|
|
)
|
2024-02-02 12:48:57 +00:00
|
|
|
]
|
2024-02-02 13:13:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|