Skip to content

Commit

Permalink
test(git-authors): add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpipy committed Oct 31, 2023
1 parent c56ad45 commit 0759c4a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
6 changes: 2 additions & 4 deletions bin/git-authors
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
LIST=false
NO_EMAIL=false
FILE=""
EDITOR=$(git var GIT_EDITOR)

while [[ $# -gt 0 ]]; do
case $1 in
Expand Down Expand Up @@ -37,10 +36,10 @@ fi
authors() {
if $NO_EMAIL; then
# email will be used to uniq authors.
git shortlog -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \
git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \
| awk -F'<' '{gsub(/ +$/, "", $1); print $1}'
else
git shortlog -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++'
git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++'
fi
}

Expand All @@ -52,5 +51,4 @@ if $LIST; then
authors
else
authors >> "$FILE"
test -n "$EDITOR" && $EDITOR "$FILE"
fi
45 changes: 45 additions & 0 deletions tests/test_authors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os, subprocess

expected_authors_list = "test <[email protected]>\ntestagain <[email protected]>\n"
expected_authors_list_without_email = "test\ntestagain\n"
authors_file = "AUTHORS"

class TestGitAuthors:
def test_init(self, temp_repo):
git = temp_repo.get_repo_git()
tmp_file = temp_repo.get_file(0)
temp_repo.writefile(tmp_file, "A")
git.add(".")
git.commit("-m", "test: add data A")
git.config("--local", "user.name", "testagain")
git.config("--local", "user.email", "[email protected]")
temp_repo.writefile(tmp_file, "B")
git.add(".")
git.commit("-m", "test: add data B")

def test_output_authors_has_email_without_any_parameter(self, temp_repo):
git = temp_repo.get_repo_git()
rs = temp_repo.invoke_extras_command("authors")
with open(authors_file) as f:
content = f.read()
print(content)
print(expected_authors_list)
assert content == expected_authors_list

def test_list_authors_has_email_defaultly(self, temp_repo):
git = temp_repo.get_repo_git()
actual = temp_repo.invoke_extras_command("authors", "--list")
actual = actual.stdout.decode()
assert actual == expected_authors_list
actual = temp_repo.invoke_extras_command("authors", "-l")
actual = actual.stdout.decode()
assert actual == expected_authors_list

def test_list_authors_has_not_email(self, temp_repo):
git = temp_repo.get_repo_git()
actual = temp_repo.invoke_extras_command("authors", "--list", "--no-email")
actual = actual.stdout.decode()
assert actual == expected_authors_list_without_email
actual = temp_repo.invoke_extras_command("authors", "-l", "--no-email")
actual = actual.stdout.decode()
assert actual == expected_authors_list_without_email

0 comments on commit 0759c4a

Please sign in to comment.