Skip to content

Commit fb1139f

Browse files
committed
test(git-authors): add unit test
* remove the opening default editor after the AUTHORS generated * avoid the empty authors list https://stackoverflow.com/questions/19741957/emacs-why-shell-command-git-log-works-but-git-shortlog-doesnt/43042420#43042420
1 parent 5ca2f45 commit fb1139f

File tree

2 files changed

+129
-38
lines changed

2 files changed

+129
-38
lines changed

bin/git-authors

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,69 @@
11
#!/usr/bin/env bash
22

3-
LIST=false
4-
NO_EMAIL=false
5-
FILE=""
6-
EDITOR=$(git var GIT_EDITOR)
3+
list=0
4+
no_email=0
5+
output=""
6+
file=""
7+
8+
if [[ $# = 1 ]] && [[ "$1" = "--no-email" ]]; then
9+
echo >&2 "--no-email option only can be used with --list | -l | --output"
10+
exit 1
11+
fi
12+
13+
#
14+
# list authors sorted by number of commits (descending).
15+
#
16+
authors() {
17+
if [[ $no_email = 1 ]]; then
18+
# email will be used to uniq authors.
19+
git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \
20+
| awk -F'<' '{gsub(/ +$/, "", $1); print $1}'
21+
else
22+
git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++'
23+
fi
24+
}
25+
26+
if [[ -z "$file" ]]; then
27+
file=$(find . -mindepth 1 -maxdepth 1 -iregex '.*\(authors\|contributors\).*' | head -n1)
28+
if [[ -z "$file" ]]; then
29+
file="AUTHORS"
30+
fi
31+
fi
32+
33+
if [[ $# = 0 ]]; then
34+
authors >> "$file"
35+
exit 0
36+
fi
737

838
while [[ $# -gt 0 ]]; do
939
case $1 in
1040
-l|--list )
11-
LIST=true
12-
shift
41+
list=1
1342
;;
1443
--no-email )
15-
NO_EMAIL=true
16-
shift
44+
no_email=1
45+
;;
46+
--output )
47+
if [[ -n $2 ]] && [[ $2 =~ ^[a-zA-Z] ]]; then
48+
output=$2
49+
shift
50+
else
51+
echo >&2 "option $1 requires a value"
52+
exit 1
53+
fi
1754
;;
1855
* )
1956
break
2057
esac
58+
shift
2159
done
2260

23-
if ! $LIST; then
24-
FILE=$1
25-
if [ -z "$FILE" ]; then
26-
FILE=$(find . -mindepth 1 -maxdepth 1 \( -iname '*authors*' -o -iname '*contributors*' \) | head -n1)
27-
if [ -z "$FILE" ]; then
28-
FILE='AUTHORS'
29-
fi
30-
fi
61+
if [[ -n "$output" ]]; then
62+
authors >> "$output"
63+
exit 0
3164
fi
3265

33-
#
34-
# list authors sorted by number of commits (descending).
35-
#
36-
37-
authors() {
38-
if $NO_EMAIL; then
39-
# email will be used to uniq authors.
40-
git shortlog -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \
41-
| awk -F'<' '{gsub(/ +$/, "", $1); print $1}'
42-
else
43-
git shortlog -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++'
44-
fi
45-
}
46-
47-
#
48-
# authors.
49-
#
50-
51-
if $LIST; then
66+
if [[ $list = 1 ]]; then
5267
authors
53-
else
54-
authors >> "$FILE"
55-
test -n "$EDITOR" && $EDITOR "$FILE"
68+
exit 0
5669
fi

tests/test_authors.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os, subprocess
2+
3+
expected_authors_list = "test <[email protected]>\ntestagain <[email protected]>\n"
4+
expected_authors_list_without_email = "test\ntestagain\n"
5+
authors_file = "AUTHORS"
6+
7+
class TestGitAuthors:
8+
def test_init(self, temp_repo):
9+
git = temp_repo.get_repo_git()
10+
tmp_file = temp_repo.get_file(0)
11+
temp_repo.writefile(tmp_file, "A")
12+
git.add(".")
13+
git.commit("-m", "test: add data A")
14+
git.config("--local", "user.name", "testagain")
15+
git.config("--local", "user.email", "[email protected]")
16+
temp_repo.writefile(tmp_file, "B")
17+
git.add(".")
18+
git.commit("-m", "test: add data B")
19+
20+
def test_output_authors_has_email_without_any_parameter(self, temp_repo):
21+
git = temp_repo.get_repo_git()
22+
rs = temp_repo.invoke_extras_command("authors")
23+
with open(authors_file) as f:
24+
content = f.read()
25+
print(content)
26+
print(expected_authors_list)
27+
assert content == expected_authors_list
28+
29+
def test_list_authors_has_email_defaultly(self, temp_repo):
30+
git = temp_repo.get_repo_git()
31+
actual = temp_repo.invoke_extras_command("authors", "--list")
32+
actual = actual.stdout.decode()
33+
assert actual == expected_authors_list
34+
actual = temp_repo.invoke_extras_command("authors", "-l")
35+
actual = actual.stdout.decode()
36+
assert actual == expected_authors_list
37+
38+
def test_list_authors_has_not_email(self, temp_repo):
39+
git = temp_repo.get_repo_git()
40+
actual = temp_repo.invoke_extras_command("authors", "--list", "--no-email")
41+
actual = actual.stdout.decode()
42+
assert actual == expected_authors_list_without_email
43+
actual = temp_repo.invoke_extras_command("authors", "-l", "--no-email")
44+
actual = actual.stdout.decode()
45+
assert actual == expected_authors_list_without_email
46+
47+
def test_output_authors_has_email_into_AUTHORS(self, temp_repo):
48+
git = temp_repo.get_repo_git()
49+
temp_repo.invoke_extras_command("authors", "--output")
50+
with open(authors_file) as f:
51+
content = f.read()
52+
assert content == expected_authors_list
53+
54+
def test_output_authors_has_email_into_target_file(self, temp_repo):
55+
git = temp_repo.get_repo_git()
56+
temp_repo.invoke_extras_command("authors", "--output", "AUTHORS_TARGET_FILE_A")
57+
with open("AUTHORS_TARGET_FILE_A") as f:
58+
content = f.read()
59+
assert content == expected_authors_list
60+
61+
def test_output_authors_has_not_email_into_target_file(self, temp_repo):
62+
git = temp_repo.get_repo_git()
63+
rs = temp_repo.invoke_extras_command("authors", "--output", "AUTHORS_TARGET_FILE_B", "--no-email")
64+
with open("AUTHORS_TARGET_FILE_B") as f:
65+
content = f.read()
66+
assert content == expected_authors_list_without_email
67+
68+
def test_fail_to_output_authors_when_an_option_like_follow_output_parameter(self, temp_repo):
69+
git = temp_repo.get_repo_git()
70+
actual = temp_repo.invoke_extras_command("authors", "--output", "--no-email")
71+
actual = actual.stderr.decode()
72+
assert actual == "option --output requires a value\n"
73+
74+
def test_fail_to_output_authors_when_only_no_email_option(self, temp_repo):
75+
git = temp_repo.get_repo_git()
76+
actual = temp_repo.invoke_extras_command("authors", "--no-email")
77+
actual = actual.stderr.decode()
78+
assert actual == "--no-email option only can be used with --list | -l | --output\n"

0 commit comments

Comments
 (0)