Skip to content

Commit 62065e1

Browse files
committed
test(git-alias): add its unit test
1 parent d2dde69 commit 62065e1

File tree

6 files changed

+132
-13
lines changed

6 files changed

+132
-13
lines changed

bin/git-alias

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
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+
--global
12+
For writing options: write to global ~/.gitconfig file
13+
For reading options: read only from global ~/.gitconfig
14+
15+
--local
16+
For writing options: write to the repository .git/config file
17+
For reading options: read only from the repository .git/config
818
HERE
919
}
1020

21+
if [[ "$1" == "--local" || "$1" == "--global" ]]; then
22+
options=$1
23+
shift
24+
fi
25+
1126
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" ;;
27+
0) git config $options --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort ;;
28+
1) git config $options --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort | grep -e "$1" ;;
29+
2) git config $options alias."$1" "$2" ;;
1530
*) >&2 echo "error: too many arguments." && usage && exit 1 ;;
1631
esac

man/git-alias.1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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"
@@ -9,9 +9,21 @@
99
\fBgit\-alias\fR <search\-pattern>
1010
.br
1111
\fBgit\-alias\fR <alias\-name> <command>
12+
.br
13+
\fBgit\-alias\fR \-\-global <alias\-name> <command>
14+
.br
15+
\fBgit\-alias\fR \-\-local <alias\-name> <command>
1216
.SH "DESCRIPTION"
1317
List all aliases, show one alias, or set one (global) alias\.
1418
.SH "OPTIONS"
19+
<\-\-global>
20+
.P
21+
write and read the config from ~/\.gitconfig file only\.
22+
.P
23+
<\-\-local>
24+
.P
25+
write and read the config from \.git/config file only\.
26+
.P
1527
<search\-pattern>
1628
.P
1729
The pattern used to search aliases\.

man/git-alias.html

Lines changed: 12 additions & 2 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ git-alias(1) -- Define, search and show aliases
66
`git-alias`
77
`git-alias` &lt;search-pattern&gt;
88
`git-alias` &lt;alias-name&gt; &lt;command&gt;
9+
`git-alias` --global &lt;alias-name&gt; &lt;command&gt;
10+
`git-alias` --local &lt;alias-name&gt; &lt;command&gt;
911

1012
## DESCRIPTION
1113

1214
List all aliases, show one alias, or set one (global) alias.
1315

1416
## OPTIONS
1517

18+
&lt;--global&gt;
19+
20+
write and read the config from ~/.gitconfig file only.
21+
22+
&lt;--local&gt;
23+
24+
write and read the config from .git/config file only.
25+
1626
&lt;search-pattern&gt;
1727

1828
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)