mirror of
https://github.com/tj/git-extras.git
synced 2026-09-10 15:36:21 -04:00
test(git-alias): add its unit test (#1077)
This commit is contained in:
parent
df1997c0dc
commit
bddb987383
|
|
@ -1,16 +1,45 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
options=""
|
||||
|
||||
usage() {
|
||||
cat <<HERE
|
||||
usage: git alias # list all aliases
|
||||
or: git alias <search-pattern> # show aliases matching pattern
|
||||
or: git alias <alias-name> <command> # alias a command
|
||||
usage: git alias [options] # list all aliases
|
||||
or: git alias [options] <search-pattern> # show aliases matching pattern
|
||||
or: git alias [options] <alias-name> <command> # alias a command
|
||||
options:
|
||||
|
||||
--global Show or create alias in the system config
|
||||
--local Show or create alias in the repository 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)
|
||||
if [[ -z "$options" ]]; then
|
||||
git config --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort
|
||||
else
|
||||
git config "$options" --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort
|
||||
fi
|
||||
;;
|
||||
1)
|
||||
if [[ -z "$options" ]]; then
|
||||
git config --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort | grep -e "$1"
|
||||
else
|
||||
git config "$options" --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort | grep -e "$1"
|
||||
fi
|
||||
;;
|
||||
2)
|
||||
if [[ -z "$options" ]]; then
|
||||
git config alias."$1" "$2"
|
||||
else
|
||||
git config "$options" alias."$1" "$2"
|
||||
fi
|
||||
;;
|
||||
*) >&2 echo "error: too many arguments." && usage && exit 1 ;;
|
||||
esac
|
||||
|
|
|
|||
|
|
@ -1,17 +1,21 @@
|
|||
.\" 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"
|
||||
\fBgit\-alias\fR
|
||||
.br
|
||||
\fBgit\-alias\fR <search\-pattern>
|
||||
.br
|
||||
\fBgit\-alias\fR <alias\-name> <command>
|
||||
\fBgit\-alias\fR \fBgit\-alias\fR <search\-pattern> \fBgit\-alias\fR <alias\-name> <command> \fBgit\-alias\fR [\-\-global] \fBgit\-alias\fR [\-\-local] \fBgit\-alias\fR [\-\-global] <search\-pattern> \fBgit\-alias\fR [\-\-local] <search\-pattern> \fBgit\-alias\fR [\-\-global] <alias\-name> <command> \fBgit\-alias\fR [\-\-local] <alias\-name> <command>
|
||||
.SH "DESCRIPTION"
|
||||
List all aliases, show one alias, or set one (global) alias\.
|
||||
List all aliases, show one alias, or set one (global or local) alias\.
|
||||
.SH "OPTIONS"
|
||||
\-\-global
|
||||
.P
|
||||
Show or create alias in the system config
|
||||
.P
|
||||
\-\-local
|
||||
.P
|
||||
Show or create alias in the repository config
|
||||
.P
|
||||
<search\-pattern>
|
||||
.P
|
||||
The pattern used to search aliases\.
|
||||
|
|
|
|||
|
|
@ -77,16 +77,30 @@
|
|||
</p>
|
||||
<h2 id="SYNOPSIS">SYNOPSIS</h2>
|
||||
|
||||
<p><code>git-alias</code><br>
|
||||
<code>git-alias</code> <search-pattern><br>
|
||||
<code>git-alias</code> <alias-name> <command></p>
|
||||
<p><code>git-alias</code>
|
||||
<code>git-alias</code> <search-pattern>
|
||||
<code>git-alias</code> <alias-name> <command>
|
||||
<code>git-alias</code> [--global]
|
||||
<code>git-alias</code> [--local]
|
||||
<code>git-alias</code> [--global] <search-pattern>
|
||||
<code>git-alias</code> [--local] <search-pattern>
|
||||
<code>git-alias</code> [--global] <alias-name> <command>
|
||||
<code>git-alias</code> [--local] <alias-name> <command></p>
|
||||
|
||||
<h2 id="DESCRIPTION">DESCRIPTION</h2>
|
||||
|
||||
<p>List all aliases, show one alias, or set one (global) alias.</p>
|
||||
<p>List all aliases, show one alias, or set one (global or local) alias.</p>
|
||||
|
||||
<h2 id="OPTIONS">OPTIONS</h2>
|
||||
|
||||
<p>--global</p>
|
||||
|
||||
<p>Show or create alias in the system config</p>
|
||||
|
||||
<p>--local</p>
|
||||
|
||||
<p>Show or create alias in the repository config</p>
|
||||
|
||||
<p><search-pattern></p>
|
||||
|
||||
<p>The pattern used to search aliases.</p>
|
||||
|
|
@ -136,7 +150,7 @@ whois = !sh -c 'git log -i -1 --pretty="format:%an <%ae>
|
|||
|
||||
<ol class='man-decor man-foot man foot'>
|
||||
<li class='tl'></li>
|
||||
<li class='tc'>August 2021</li>
|
||||
<li class='tc'>September 2023</li>
|
||||
<li class='tr'>git-alias(1)</li>
|
||||
</ol>
|
||||
|
||||
|
|
|
|||
|
|
@ -3,16 +3,30 @@ git-alias(1) -- Define, search and show aliases
|
|||
|
||||
## SYNOPSIS
|
||||
|
||||
`git-alias`
|
||||
`git-alias` <search-pattern>
|
||||
`git-alias` <alias-name> <command>
|
||||
`git-alias`
|
||||
`git-alias` <search-pattern>
|
||||
`git-alias` <alias-name> <command>
|
||||
`git-alias` [--global]
|
||||
`git-alias` [--local]
|
||||
`git-alias` [--global] <search-pattern>
|
||||
`git-alias` [--local] <search-pattern>
|
||||
`git-alias` [--global] <alias-name> <command>
|
||||
`git-alias` [--local] <alias-name> <command>
|
||||
|
||||
## DESCRIPTION
|
||||
|
||||
List all aliases, show one alias, or set one (global) alias.
|
||||
List all aliases, show one alias, or set one (global or local) alias.
|
||||
|
||||
## OPTIONS
|
||||
|
||||
--global
|
||||
|
||||
Show or create alias in the system config
|
||||
|
||||
--local
|
||||
|
||||
Show or create alias in the repository config
|
||||
|
||||
<search-pattern>
|
||||
|
||||
The pattern used to search aliases.
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ import shutil
|
|||
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 @@ class TempRepository:
|
|||
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)
|
||||
|
|
|
|||
70
tests/test_git_alias.py
Normal file
70
tests/test_git_alias.py
Normal file
|
|
@ -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")
|
||||
Loading…
Reference in a new issue