Skip to content

Commit

Permalink
test(git-authors): add unit test (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpipy authored Nov 18, 2023
1 parent c13ab52 commit cf9fe2d
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 101 deletions.
1 change: 1 addition & 0 deletions .pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
minversion = 7.4
addopts = -ra -q
testpaths = tests
faulthandler_timeout = 5
2 changes: 0 additions & 2 deletions Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,6 @@ $ git squash HEAD~3 "Work on a feature"
Populates the file matching `authors|contributors -i` with the authors of commits, according to the number of commits per author.
Opens the file in `$EDITOR` when set.
See the ["MAPPING AUTHORS" section](https://git-scm.com/docs/git-shortlog#_mapping_authors) of **git-shortlog**(1) to coalesce together commits by the same person.
Updating AUTHORS file:
Expand Down
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
58 changes: 15 additions & 43 deletions man/git-authors.1
Original file line number Diff line number Diff line change
@@ -1,87 +1,59 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "GIT\-AUTHORS" "1" "October 2017" "" "Git Extras"
.
.\" generated with Ronn-NG/v0.9.1
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
.TH "GIT\-AUTHORS" "1" "November 2023" "" "Git Extras"
.SH "NAME"
\fBgit\-authors\fR \- Generate authors report
.
.SH "SYNOPSIS"
\fBgit\-authors\fR [\-l, \-\-list] [\-\-no\-email]
.
.SH "DESCRIPTION"
Populates the file matching \fIauthors|contributors \-i\fR with the authors of commits, according to the number of commits per author\. Opens the file in \fB$EDITOR\fR when set\.
.
.TS
allbox;
Populates the file matching _authors contributors \-i_ with the authors of commits, according to the number of commits per author\.
.TE
.P
See the "MAPPING AUTHORS" section of \fBgit\-shortlog\fR(1) to coalesce together commits by the same person\.
.
.SH "OPTIONS"
\-l, \-\-list
.
.P
Show authors\.
.
.P
\-\-no\-email
.
.P
Don\'t show authors\' email\.
.
.SH "EXAMPLES"
.
.TP
Updating AUTHORS file:
.
.IP
.IP "" 4
.nf
$ git authors
.
.TP
.fi
.IP "" 0
.P
Listing authors:
.
.IP
$ git authors \-\-list
.
.IP "" 4
.
.nf

$ git authors \-\-list
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>
.
.fi
.
.IP "" 0

.
.TP
.P
Listing authors without email:
.
.IP
$ git authors \-\-list \-\-no\-email
.
.IP "" 4
.
.nf

$ git authors \-\-list \-\-no\-email
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"
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
.
.SH "SEE ALSO"
<\fIhttps://github\.com/tj/git\-extras\fR>
58 changes: 31 additions & 27 deletions man/git-authors.html

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

13 changes: 3 additions & 10 deletions man/git-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ git-authors(1) -- Generate authors report
## DESCRIPTION

Populates the file matching _authors|contributors -i_ with the authors of commits, according to the number of commits per author.
Opens the file in **$EDITOR** when set.

See the "MAPPING AUTHORS" section of **git-shortlog**(1) to coalesce together commits by the same person.

Expand All @@ -24,33 +23,27 @@ git-authors(1) -- Generate authors report

## EXAMPLES

* Updating AUTHORS file:
Updating AUTHORS file:

$ git authors

* Listing authors:
Listing authors:

$ git authors --list

```
TJ Holowaychuk <[email protected]>
hemanth.hm <[email protected]>
Jonhnny Weslley <[email protected]>
nickl- <[email protected]>
Leila Muhtasib <[email protected]>
```

* Listing authors without email:
Listing authors without email:

$ git authors --list --no-email
```
TJ Holowaychuk
hemanth.hm
Jonhnny Weslley
nickl-
Leila Muhtasib
```

## AUTHOR

Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ def named_temp_repo(request):
dirname = request.param
repo = create_repo(dirname)
init_repo_git_status(repo)
return repo
yield repo
repo.teardown()
28 changes: 14 additions & 14 deletions tests/helper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import os, subprocess, stat, shutil, tempfile, git
import os, subprocess, shutil, tempfile
from git import Repo

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
GIT_EXTRAS_BIN = os.path.join(CURRENT_DIR, "..", "bin")
GIT_EXTRAS_HELPER = os.path.join(CURRENT_DIR, "..", "helper")

def invoke_git_extras_command(name, *params):
script = [os.path.join(GIT_EXTRAS_BIN, name), *params]
print(f"Run the script \"{script}\"")
return subprocess.run(script, capture_output=True)
GIT_EXTRAS_BIN = os.path.abspath(os.path.join(CURRENT_DIR, "..", "bin"))
GIT_EXTRAS_HELPER = os.path.abspath(os.path.join(CURRENT_DIR, "..", "helper"))

class TempRepository:
def __init__(self, repo_work_dir = None):
Expand All @@ -18,7 +14,7 @@ def __init__(self, repo_work_dir = None):
repo_work_dir = os.path.join(self._system_tmpdir, repo_work_dir)
self._cwd = repo_work_dir
self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:]
self._git_repo = git.Repo.init(repo_work_dir, b="default")
self._git_repo = Repo.init(repo_work_dir, b="default")
self._files = []

def switch_cwd_under_repo(self):
Expand Down Expand Up @@ -69,12 +65,14 @@ def teardown(self):

def invoke_extras_command(self, name, *params):
command_name = "git-" + name
print(f"Invoke the git-extras command - {command_name}")
return invoke_git_extras_command(command_name, *params)
print(f"Invoke the git-extras command - {command_name} at {self._cwd}")
script = [os.path.join(GIT_EXTRAS_BIN, command_name), *list(params)]
print(f"Run the script \"{' '.join(script)}\"")
return subprocess.run(script, capture_output=True)

def invoke_installed_extras_command(self, name, *params):
command_name = "git-" + name
print(f"Invoke the git-extras command - {command_name}")
print(f"Invoke the git-extras command - {command_name} at {self._cwd}")
origin_extras_command = os.path.join(GIT_EXTRAS_BIN, command_name)
temp_extras_command = os.path.join(self._cwd, command_name)
helpers = [
Expand All @@ -94,7 +92,9 @@ def invoke_installed_extras_command(self, name, *params):
whole.extend(rest)
whole.insert(0, first)
t.write("\n".join(whole))
print("Update file {temp_extras_command}:\n{t.read()}")
print(f"Update file {temp_extras_command}")
os.chmod(temp_extras_command, 0o775)

return subprocess.run([temp_extras_command, *params], capture_output=True)
script = [temp_extras_command, *params]
print(f"Run the script \"{script}\"")
return subprocess.run(script, capture_output=True)
Loading

0 comments on commit cf9fe2d

Please sign in to comment.