Skip to content

Commit

Permalink
refactor(git-authors): add unit test for option --output
Browse files Browse the repository at this point in the history
    * doc(git-authors): update option `--output` usage
  • Loading branch information
vanpipy committed Nov 19, 2023
1 parent cf9fe2d commit ba344d0
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 36 deletions.
83 changes: 49 additions & 34 deletions bin/git-authors
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
#!/usr/bin/env bash

LIST=false
NO_EMAIL=false
FILE=""
list=0
no_email=0
output=""
file=""

while [[ $# -gt 0 ]]; do
case $1 in
-l|--list )
LIST=true
shift
;;
--no-email )
NO_EMAIL=true
shift
;;
* )
break
esac
done

if ! $LIST; then
FILE=$1
if [ -z "$FILE" ]; then
FILE=$(find . -mindepth 1 -maxdepth 1 \( -iname '*authors*' -o -iname '*contributors*' \) | head -n1)
if [ -z "$FILE" ]; then
FILE='AUTHORS'
fi
fi
if [[ $# = 1 ]] && [[ "$1" = "--no-email" ]]; then
echo >&2 "--no-email option only can be used with --list | -l | --output"
exit 1
fi

#
# list authors sorted by number of commits (descending).
#

authors() {
if $NO_EMAIL; then
if [[ $no_email = 1 ]]; then
# email will be used to uniq authors.
git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \
| awk -F'<' '{gsub(/ +$/, "", $1); print $1}'
Expand All @@ -43,12 +23,47 @@ authors() {
fi
}

#
# authors.
#
if [[ -z "$file" ]]; then
file=$(find . -mindepth 1 -maxdepth 1 -iregex '.*\(authors\|contributors\).*' | head -n1)
if [[ -z "$file" ]]; then
file="AUTHORS"
fi
fi

if [[ $# = 0 ]]; then
authors >> "$file"
exit 0
fi

while [[ $# -gt 0 ]]; do
case $1 in
-l|--list )
list=1
;;
--no-email )
no_email=1
;;
--output )
if [[ -n $2 ]] && [[ $2 =~ ^[a-zA-Z] ]]; then
output=$2
shift
else
echo >&2 "option $1 requires a letter prefix value"
exit 1
fi
;;
* )
break
esac
shift
done

if [[ -n "$output" ]]; then
authors >> "$output"
exit 0
fi

if $LIST; then
if [[ $list = 1 ]]; then
authors
else
authors >> "$FILE"
exit 0
fi
29 changes: 28 additions & 1 deletion man/git-authors.1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.SH "NAME"
\fBgit\-authors\fR \- Generate authors report
.SH "SYNOPSIS"
\fBgit\-authors\fR [\-l, \-\-list] [\-\-no\-email]
\fBgit\-authors\fR \fBgit\-authors\fR [\-l, \-\-list] [\-\-no\-email] \fBgit\-authors\fR [\-\-output] <filename> [\-\-no\-email]
.SH "DESCRIPTION"
.TS
allbox;
Expand All @@ -20,6 +20,14 @@ Show authors\.
\-\-no\-email
.P
Don\'t show authors\' email\.
.P
\-\-output
.P
output the authors
.P
<filename>
.P
the filename which is letter prefix to save the authors
.SH "EXAMPLES"
Updating AUTHORS file:
.IP "" 4
Expand Down Expand Up @@ -51,6 +59,25 @@ nickl\-
Leila Muhtasib
.fi
.IP "" 0
.P
Output authors
.IP "" 4
.nf
$ git authors \-\-output authors\-history || cat authors\-history
TJ Holowaychuk <tj@vision\-media\.ca>
hemanth\.hm <hemanth\.hm@gmail\.com>
Jonhnny Weslley <jw@jonhnnyweslley\.net>
nickl\- <github@jigsoft\.co\.za>
Leila Muhtasib <muhtasib@gmail\.com>

$ git authors \-\-output authors\-history \-\-no\-email || cat authors\-history
TJ Holowaychuk
hemanth\.hm
Jonhnny Weslley
nickl\-
Leila Muhtasib
.fi
.IP "" 0
.SH "AUTHOR"
Written by Titus Wormer <\fItituswormer@gmail\.com\fR>
.SH "REPORTING BUGS"
Expand Down
29 changes: 28 additions & 1 deletion man/git-authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/git-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ git-authors(1) -- Generate authors report

## SYNOPSIS

`git-authors`
`git-authors` [-l, --list] [--no-email]
`git-authors` [--output] &lt;filename&gt; [--no-email]

## DESCRIPTION

Expand All @@ -21,6 +23,14 @@ git-authors(1) -- Generate authors report

Don't show authors' email.

--output

output the authors

&lt;filename&gt;

the filename which is letter prefix to save the authors

## EXAMPLES

Updating AUTHORS file:
Expand All @@ -45,6 +55,22 @@ git-authors(1) -- Generate authors report
nickl-
Leila Muhtasib

Output authors

$ git authors --output authors-history || cat authors-history
TJ Holowaychuk <[email protected]>
hemanth.hm <[email protected]>
Jonhnny Weslley <[email protected]>
nickl- <[email protected]>
Leila Muhtasib <[email protected]>

$ git authors --output authors-history --no-email || cat authors-history
TJ Holowaychuk
hemanth.hm
Jonhnny Weslley
nickl-
Leila Muhtasib

## AUTHOR

Written by Titus Wormer &lt;<[email protected]>&gt;
Expand Down
33 changes: 33 additions & 0 deletions tests/test_authors.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,36 @@ def test_list_authors_has_not_email(self, temp_repo):
actual = temp_repo.invoke_extras_command("authors", "-l", "--no-email")
actual = actual.stdout.decode()
assert actual == expected_authors_list_without_email

def test_output_authors_has_email_into_AUTHORS(self, temp_repo):
git = temp_repo.get_repo_git()
temp_repo.invoke_extras_command("authors", "--output")
with open(authors_file) as f:
content = f.read()
assert content == expected_authors_list

def test_output_authors_has_email_into_target_file(self, temp_repo):
git = temp_repo.get_repo_git()
temp_repo.invoke_extras_command("authors", "--output", "AUTHORS_TARGET_FILE_A")
with open("AUTHORS_TARGET_FILE_A") as f:
content = f.read()
assert content == expected_authors_list

def test_output_authors_has_not_email_into_target_file(self, temp_repo):
git = temp_repo.get_repo_git()
rs = temp_repo.invoke_extras_command("authors", "--output", "AUTHORS_TARGET_FILE_B", "--no-email")
with open("AUTHORS_TARGET_FILE_B") as f:
content = f.read()
assert content == expected_authors_list_without_email

def test_fail_to_output_authors_when_an_option_like_follow_output_parameter(self, temp_repo):
git = temp_repo.get_repo_git()
actual = temp_repo.invoke_extras_command("authors", "--output", "--no-email")
actual = actual.stderr.decode()
assert actual == "option --output requires a value\n"

def test_fail_to_output_authors_when_only_no_email_option(self, temp_repo):
git = temp_repo.get_repo_git()
actual = temp_repo.invoke_extras_command("authors", "--no-email")
actual = actual.stderr.decode()
assert actual == "--no-email option only can be used with --list | -l | --output\n"

0 comments on commit ba344d0

Please sign in to comment.