Skip to content

Commit 0759c4a

Browse files
committed
test(git-authors): add unit test
1 parent c56ad45 commit 0759c4a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

bin/git-authors

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
LIST=false
44
NO_EMAIL=false
55
FILE=""
6-
EDITOR=$(git var GIT_EDITOR)
76

87
while [[ $# -gt 0 ]]; do
98
case $1 in
@@ -37,10 +36,10 @@ fi
3736
authors() {
3837
if $NO_EMAIL; then
3938
# email will be used to uniq authors.
40-
git shortlog -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \
39+
git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \
4140
| awk -F'<' '{gsub(/ +$/, "", $1); print $1}'
4241
else
43-
git shortlog -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++'
42+
git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++'
4443
fi
4544
}
4645

@@ -52,5 +51,4 @@ if $LIST; then
5251
authors
5352
else
5453
authors >> "$FILE"
55-
test -n "$EDITOR" && $EDITOR "$FILE"
5654
fi

tests/test_authors.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

0 commit comments

Comments
 (0)