Skip to content

Commit bddb987

Browse files
authored
test(git-alias): add its unit test (#1077)
1 parent df1997c commit bddb987

File tree

6 files changed

+159
-26
lines changed

6 files changed

+159
-26
lines changed

bin/git-alias

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
#!/usr/bin/env bash
22

3+
options=""
4+
35
usage() {
46
cat <<HERE
5-
usage: git alias # list all aliases
6-
or: git alias <search-pattern> # show aliases matching pattern
7-
or: git alias <alias-name> <command> # alias a command
7+
usage: git alias [options] # list all aliases
8+
or: git alias [options] <search-pattern> # show aliases matching pattern
9+
or: git alias [options] <alias-name> <command> # alias a command
10+
options:
11+
12+
--global Show or create alias in the system config
13+
--local Show or create alias in the repository config
814
HERE
915
}
1016

17+
if [[ "$1" == "--local" || "$1" == "--global" ]]; then
18+
options=$1
19+
shift
20+
fi
21+
1122
case $# in
12-
0) git config --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort ;;
13-
1) git alias | grep -e "$1" ;;
14-
2) git config --global alias."$1" "$2" ;;
23+
0)
24+
if [[ -z "$options" ]]; then
25+
git config --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort
26+
else
27+
git config "$options" --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort
28+
fi
29+
;;
30+
1)
31+
if [[ -z "$options" ]]; then
32+
git config --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort | grep -e "$1"
33+
else
34+
git config "$options" --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort | grep -e "$1"
35+
fi
36+
;;
37+
2)
38+
if [[ -z "$options" ]]; then
39+
git config alias."$1" "$2"
40+
else
41+
git config "$options" alias."$1" "$2"
42+
fi
43+
;;
1544
*) >&2 echo "error: too many arguments." && usage && exit 1 ;;
1645
esac

man/git-alias.1

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
.\" generated with Ronn-NG/v0.9.1
22
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
3-
.TH "GIT\-ALIAS" "1" "August 2021" "" "Git Extras"
3+
.TH "GIT\-ALIAS" "1" "September 2023" "" "Git Extras"
44
.SH "NAME"
55
\fBgit\-alias\fR \- Define, search and show aliases
66
.SH "SYNOPSIS"
7-
\fBgit\-alias\fR
8-
.br
9-
\fBgit\-alias\fR <search\-pattern>
10-
.br
11-
\fBgit\-alias\fR <alias\-name> <command>
7+
\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>
128
.SH "DESCRIPTION"
13-
List all aliases, show one alias, or set one (global) alias\.
9+
List all aliases, show one alias, or set one (global or local) alias\.
1410
.SH "OPTIONS"
11+
\-\-global
12+
.P
13+
Show or create alias in the system config
14+
.P
15+
\-\-local
16+
.P
17+
Show or create alias in the repository config
18+
.P
1519
<search\-pattern>
1620
.P
1721
The pattern used to search aliases\.

man/git-alias.html

Lines changed: 19 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/git-alias.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,30 @@ git-alias(1) -- Define, search and show aliases
33

44
## SYNOPSIS
55

6-
`git-alias`
7-
`git-alias` &lt;search-pattern&gt;
8-
`git-alias` &lt;alias-name&gt; &lt;command&gt;
6+
`git-alias`
7+
`git-alias` &lt;search-pattern&gt;
8+
`git-alias` &lt;alias-name&gt; &lt;command&gt;
9+
`git-alias` [--global]
10+
`git-alias` [--local]
11+
`git-alias` [--global] &lt;search-pattern&gt;
12+
`git-alias` [--local] &lt;search-pattern&gt;
13+
`git-alias` [--global] &lt;alias-name&gt; &lt;command&gt;
14+
`git-alias` [--local] &lt;alias-name&gt; &lt;command&gt;
915

1016
## DESCRIPTION
1117

12-
List all aliases, show one alias, or set one (global) alias.
18+
List all aliases, show one alias, or set one (global or local) alias.
1319

1420
## OPTIONS
1521

22+
--global
23+
24+
Show or create alias in the system config
25+
26+
--local
27+
28+
Show or create alias in the repository config
29+
1630
&lt;search-pattern&gt;
1731

1832
The pattern used to search aliases.

tests/helper.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import tempfile
55
import git
66

7-
def invoke_git_extras_command(name):
7+
def invoke_git_extras_command(name, *params):
88
current_dir = os.path.dirname(os.path.abspath(__file__))
99
git_extras_bin = os.path.join(current_dir, "..", "bin")
10-
return subprocess.run(os.path.join(git_extras_bin, name), capture_output=True)
10+
script = [os.path.join(git_extras_bin, name), *params]
11+
print(f"Run the script \"{script}\"")
12+
return subprocess.run(script, capture_output=True)
1113

1214
class TempRepository:
1315
def __init__(self, repo_work_dir = None):
@@ -60,7 +62,7 @@ def teardown(self):
6062
shutil.rmtree(self._cwd, ignore_errors=True)
6163
print(f"The temp directory {self._cwd} has been removed")
6264

63-
def invoke_extras_command(self, name):
65+
def invoke_extras_command(self, name, *params):
6466
command = "git-" + name
6567
print(f"Invoke the git-extras command - {command}")
66-
return invoke_git_extras_command(command)
68+
return invoke_git_extras_command(command, *params)

tests/test_git_alias.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class TestGitAlias:
2+
def test_init(self, temp_repo):
3+
git = temp_repo.get_repo_git()
4+
git.config("--global", "alias.globalalias", "status")
5+
git.config("--global", "alias.x", "status")
6+
git.config("--local", "alias.localalias", "status")
7+
git.config("--local", "alias.y", "status")
8+
9+
def test_list_all(self, temp_repo):
10+
actual = temp_repo.invoke_extras_command("alias")
11+
actual = actual.stdout.decode()
12+
assert "globalalias = status" in actual
13+
assert "x = status" in actual
14+
assert "localalias = status" in actual
15+
assert "y = status" in actual
16+
17+
def test_list_all_globally(self, temp_repo):
18+
actual = temp_repo.invoke_extras_command("alias", "--global")
19+
actual = actual.stdout.decode()
20+
assert "globalalias = status" in actual
21+
22+
def test_list_all_locally(self, temp_repo):
23+
actual = temp_repo.invoke_extras_command("alias", "--local")
24+
actual = actual.stdout.decode()
25+
assert "localalias = status" in actual
26+
27+
def test_search_globally(self, temp_repo):
28+
actual = temp_repo.invoke_extras_command("alias", "--global", "global")
29+
actual = actual.stdout.decode()
30+
assert "globalalias = status" in actual
31+
actual = temp_repo.invoke_extras_command("alias", "--global", "local")
32+
actual = actual.stdout.decode()
33+
assert "" == actual
34+
35+
def test_search_locally(self, temp_repo):
36+
actual = temp_repo.invoke_extras_command("alias", "--local", "local")
37+
actual = actual.stdout.decode()
38+
assert "localalias = status" in actual
39+
actual = temp_repo.invoke_extras_command("alias", "--local", "global")
40+
actual = actual.stdout.decode()
41+
assert "" == actual
42+
43+
def test_get_alias_globally_and_defaultly(self, temp_repo):
44+
actual = temp_repo.invoke_extras_command("alias", "globalalias")
45+
actual = actual.stdout.decode()
46+
assert "globalalias = status" in actual
47+
48+
def test_set_alias_globally_and_defaultly(self, temp_repo):
49+
temp_repo.invoke_extras_command("alias", "globalalias", "diff")
50+
actual = temp_repo.invoke_extras_command("alias")
51+
actual = actual.stdout.decode()
52+
assert "globalalias = diff" in actual
53+
54+
def test_get_alias_locally(self, temp_repo):
55+
actual = temp_repo.invoke_extras_command("alias", "--local", "localalias")
56+
actual = actual.stdout.decode()
57+
assert "localalias = status" in actual
58+
59+
def test_set_alias_locally(self, temp_repo):
60+
temp_repo.invoke_extras_command("alias", "--local", "localalias", "diff")
61+
actual = temp_repo.invoke_extras_command("alias")
62+
actual = actual.stdout.decode()
63+
assert "localalias = diff" in actual
64+
65+
def test_teardown(self, temp_repo):
66+
git = temp_repo.get_repo_git()
67+
git.config("--global", "--unset", "alias.globalalias")
68+
git.config("--global", "--unset", "alias.x")
69+
git.config("--local", "--unset", "alias.localalias")
70+
git.config("--local", "--unset", "alias.y")

0 commit comments

Comments
 (0)