Skip to content

Commit 53394bf

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

File tree

6 files changed

+173
-15
lines changed

6 files changed

+173
-15
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: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,109 @@
1-
.\" generated with Ronn-NG/v0.9.1
2-
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
3-
.TH "GIT\-ALIAS" "1" "August 2021" "" "Git Extras"
1+
.\" generated with Ronn/v0.7.3
2+
.\" http://github.com/rtomayko/ronn/tree/0.7.3
3+
.
4+
.TH "GIT\-ALIAS" "1" "September 2023" "" "Git Extras"
5+
.
46
.SH "NAME"
57
\fBgit\-alias\fR \- Define, search and show aliases
8+
.
69
.SH "SYNOPSIS"
710
\fBgit\-alias\fR
11+
.
812
.br
913
\fBgit\-alias\fR <search\-pattern>
14+
.
1015
.br
1116
\fBgit\-alias\fR <alias\-name> <command>
17+
.
18+
.br
19+
\fBgit\-alias\fR \-\-global <alias\-name> <command>
20+
.
21+
.br
22+
\fBgit\-alias\fR \-\-local <alias\-name> <command>
23+
.
1224
.SH "DESCRIPTION"
1325
List all aliases, show one alias, or set one (global) alias\.
26+
.
1427
.SH "OPTIONS"
28+
<\-\-global>
29+
.
30+
.P
31+
write and read the config from ~/\.gitconfig file only\.
32+
.
33+
.P
34+
<\-\-local>
35+
.
36+
.P
37+
write and read the config from \.git/config file only\.
38+
.
39+
.P
1540
<search\-pattern>
41+
.
1642
.P
1743
The pattern used to search aliases\.
44+
.
1845
.P
1946
<alias\-name>
47+
.
2048
.P
2149
The name of the alias to create\.
50+
.
2251
.P
2352
<command>
53+
.
2454
.P
2555
The command for which you are creating an alias\.
56+
.
2657
.SH "EXAMPLES"
2758
Defining a new alias:
59+
.
2860
.IP "" 4
61+
.
2962
.nf
63+
3064
$ git alias last "cat\-file commit HEAD"
65+
.
3166
.fi
67+
.
3268
.IP "" 0
69+
.
3370
.P
3471
Providing only one argument, \fBgit\-alias\fR searches for aliases matching the given value:
72+
.
3573
.IP "" 4
74+
.
3675
.nf
76+
3777
$ git alias ^la
3878
last = cat\-file commit HEAD
79+
.
3980
.fi
81+
.
4082
.IP "" 0
83+
.
4184
.P
4285
\fBgit\-alias\fR will show all aliases if no argument is given:
86+
.
4387
.IP "" 4
88+
.
4489
.nf
90+
4591
$ git alias
4692
s = status
4793
amend = commit \-\-amend
4894
rank = shortlog \-sn \-\-no\-merges
4995
whatis = show \-s \-\-pretty=\'tformat:%h (%s, %ad)\' \-\-date=short
5096
whois = !sh \-c \'git log \-i \-1 \-\-pretty="format:%an <%ae>
97+
.
5198
.fi
99+
.
52100
.IP "" 0
101+
.
53102
.SH "AUTHOR"
54103
Written by Jonhnny Weslley <\fIjw@jonhnnyweslley\.net\fR>
104+
.
55105
.SH "REPORTING BUGS"
56106
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
107+
.
57108
.SH "SEE ALSO"
58109
<\fIhttps://github\.com/tj/git\-extras\fR>

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)