From 173459592db5c9783f5012b6f4d69b7289d2b973 Mon Sep 17 00:00:00 2001 From: vanpipy Date: Wed, 20 Sep 2023 21:59:04 +0800 Subject: [PATCH] test(git-alias): add its unit test --- bin/git-alias | 27 ++++++++++++---- man/git-alias.1 | 14 ++++++++- man/git-alias.html | 14 +++++++-- man/git-alias.md | 10 ++++++ tests/helper.py | 10 +++--- tests/test_git_alias.py | 70 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 tests/test_git_alias.py diff --git a/bin/git-alias b/bin/git-alias index fbc9d2ef5..9aea0bb86 100755 --- a/bin/git-alias +++ b/bin/git-alias @@ -1,16 +1,31 @@ #!/usr/bin/env bash +options="" + usage() { cat < # show aliases matching pattern - or: git alias # alias a command +usage: git alias [options] # list all aliases + or: git alias [options] # show aliases matching pattern + or: git alias [options] # alias a command +options: + --global + For writing options: write to global ~/.gitconfig file + For reading options: read only from global ~/.gitconfig + + --local + For writing options: write to the repository .git/config file + For reading options: read only from the repository .git/config HERE } +if [[ "$1" == "--local" || "$1" == "--global" ]]; then + options="$1" + shift +fi + case $# in - 0) git config --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort ;; - 1) git alias | grep -e "$1" ;; - 2) git config --global alias."$1" "$2" ;; + 0) git config $options --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort ;; + 1) git config $options --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort | grep -e "$1" ;; + 2) git config $options alias."$1" "$2" ;; *) >&2 echo "error: too many arguments." && usage && exit 1 ;; esac diff --git a/man/git-alias.1 b/man/git-alias.1 index d6a5f8442..561569373 100644 --- a/man/git-alias.1 +++ b/man/git-alias.1 @@ -1,6 +1,6 @@ .\" generated with Ronn-NG/v0.9.1 .\" http://github.com/apjanke/ronn-ng/tree/0.9.1 -.TH "GIT\-ALIAS" "1" "August 2021" "" "Git Extras" +.TH "GIT\-ALIAS" "1" "September 2023" "" "Git Extras" .SH "NAME" \fBgit\-alias\fR \- Define, search and show aliases .SH "SYNOPSIS" @@ -9,9 +9,21 @@ \fBgit\-alias\fR .br \fBgit\-alias\fR +.br +\fBgit\-alias\fR \-\-global +.br +\fBgit\-alias\fR \-\-local .SH "DESCRIPTION" List all aliases, show one alias, or set one (global) alias\. .SH "OPTIONS" +<\-\-global> +.P +write and read the config from ~/\.gitconfig file only\. +.P +<\-\-local> +.P +write and read the config from \.git/config file only\. +.P .P The pattern used to search aliases\. diff --git a/man/git-alias.html b/man/git-alias.html index b3eca1f29..1affdd13d 100644 --- a/man/git-alias.html +++ b/man/git-alias.html @@ -79,7 +79,9 @@

SYNOPSIS

git-alias
git-alias <search-pattern>
-git-alias <alias-name> <command>

+git-alias <alias-name> <command>
+git-alias --global <alias-name> <command>
+git-alias --local <alias-name> <command>

DESCRIPTION

@@ -87,6 +89,14 @@

DESCRIPTION

OPTIONS

+

<--global>

+ +

write and read the config from ~/.gitconfig file only.

+ +

<--local>

+ +

write and read the config from .git/config file only.

+

<search-pattern>

The pattern used to search aliases.

@@ -136,7 +146,7 @@

SEE ALSO

  1. -
  2. August 2021
  3. +
  4. September 2023
  5. git-alias(1)
diff --git a/man/git-alias.md b/man/git-alias.md index 15944fbb4..fbe44ec9a 100644 --- a/man/git-alias.md +++ b/man/git-alias.md @@ -6,6 +6,8 @@ git-alias(1) -- Define, search and show aliases `git-alias` `git-alias` <search-pattern> `git-alias` <alias-name> <command> +`git-alias` --global <alias-name> <command> +`git-alias` --local <alias-name> <command> ## DESCRIPTION @@ -13,6 +15,14 @@ git-alias(1) -- Define, search and show aliases ## OPTIONS + <--global> + + write and read the config from ~/.gitconfig file only. + + <--local> + + write and read the config from .git/config file only. + <search-pattern> The pattern used to search aliases. diff --git a/tests/helper.py b/tests/helper.py index 3c2698bb7..0852c67c3 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -4,10 +4,12 @@ import tempfile import git -def invoke_git_extras_command(name): +def invoke_git_extras_command(name, *params): current_dir = os.path.dirname(os.path.abspath(__file__)) git_extras_bin = os.path.join(current_dir, "..", "bin") - return subprocess.run(os.path.join(git_extras_bin, name), capture_output=True) + script = [os.path.join(git_extras_bin, name), *params] + print(f"Run the script \"{script}\"") + return subprocess.run(script, capture_output=True) class TempRepository: def __init__(self, repo_work_dir = None): @@ -60,7 +62,7 @@ def teardown(self): shutil.rmtree(self._cwd, ignore_errors=True) print(f"The temp directory {self._cwd} has been removed") - def invoke_extras_command(self, name): + def invoke_extras_command(self, name, *params): command = "git-" + name print(f"Invoke the git-extras command - {command}") - return invoke_git_extras_command(command) + return invoke_git_extras_command(command, *params) diff --git a/tests/test_git_alias.py b/tests/test_git_alias.py new file mode 100644 index 000000000..33357a755 --- /dev/null +++ b/tests/test_git_alias.py @@ -0,0 +1,70 @@ +class TestGitAlias: + def test_init(self, temp_repo): + git = temp_repo.get_repo_git() + git.config("--global", "alias.globalalias", "status") + git.config("--global", "alias.x", "status") + git.config("--local", "alias.localalias", "status") + git.config("--local", "alias.y", "status") + + def test_list_all(self, temp_repo): + actual = temp_repo.invoke_extras_command("alias") + actual = actual.stdout.decode() + assert "globalalias = status" in actual + assert "x = status" in actual + assert "localalias = status" in actual + assert "y = status" in actual + + def test_list_all_globally(self, temp_repo): + actual = temp_repo.invoke_extras_command("alias", "--global") + actual = actual.stdout.decode() + assert "globalalias = status" in actual + + def test_list_all_locally(self, temp_repo): + actual = temp_repo.invoke_extras_command("alias", "--local") + actual = actual.stdout.decode() + assert "localalias = status" in actual + + def test_search_globally(self, temp_repo): + actual = temp_repo.invoke_extras_command("alias", "--global", "global") + actual = actual.stdout.decode() + assert "globalalias = status" in actual + actual = temp_repo.invoke_extras_command("alias", "--global", "local") + actual = actual.stdout.decode() + assert "" == actual + + def test_search_locally(self, temp_repo): + actual = temp_repo.invoke_extras_command("alias", "--local", "local") + actual = actual.stdout.decode() + assert "localalias = status" in actual + actual = temp_repo.invoke_extras_command("alias", "--local", "global") + actual = actual.stdout.decode() + assert "" == actual + + def test_get_alias_globally_and_defaultly(self, temp_repo): + actual = temp_repo.invoke_extras_command("alias", "globalalias") + actual = actual.stdout.decode() + assert "globalalias = status" in actual + + def test_set_alias_globally_and_defaultly(self, temp_repo): + temp_repo.invoke_extras_command("alias", "globalalias", "diff") + actual = temp_repo.invoke_extras_command("alias") + actual = actual.stdout.decode() + assert "globalalias = diff" in actual + + def test_get_alias_locally(self, temp_repo): + actual = temp_repo.invoke_extras_command("alias", "--local", "localalias") + actual = actual.stdout.decode() + assert "localalias = status" in actual + + def test_set_alias_locally(self, temp_repo): + temp_repo.invoke_extras_command("alias", "--local", "localalias", "diff") + actual = temp_repo.invoke_extras_command("alias") + actual = actual.stdout.decode() + assert "localalias = diff" in actual + + def test_teardown(self, temp_repo): + git = temp_repo.get_repo_git() + git.config("--global", "--unset", "alias.globalalias") + git.config("--global", "--unset", "alias.x") + git.config("--local", "--unset", "alias.localalias") + git.config("--local", "--unset", "alias.y")