test(git-authors): add unit test (#1098)

This commit is contained in:
Leroy 2023-11-18 10:20:19 +08:00 committed by GitHub
parent c13ab52138
commit cf9fe2d23f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 115 additions and 103 deletions

View file

@ -2,3 +2,4 @@
minversion = 7.4 minversion = 7.4
addopts = -ra -q addopts = -ra -q
testpaths = tests testpaths = tests
faulthandler_timeout = 5

View file

@ -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. 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. 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: Updating AUTHORS file:

View file

@ -3,7 +3,6 @@
LIST=false LIST=false
NO_EMAIL=false NO_EMAIL=false
FILE="" FILE=""
EDITOR=$(git var GIT_EDITOR)
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
@ -37,10 +36,10 @@ fi
authors() { authors() {
if $NO_EMAIL; then if $NO_EMAIL; then
# email will be used to uniq authors. # 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}' | awk -F'<' '{gsub(/ +$/, "", $1); print $1}'
else 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 fi
} }
@ -52,5 +51,4 @@ if $LIST; then
authors authors
else else
authors >> "$FILE" authors >> "$FILE"
test -n "$EDITOR" && $EDITOR "$FILE"
fi fi

View file

@ -1,87 +1,59 @@
.\" generated with Ronn/v0.7.3 .\" generated with Ronn-NG/v0.9.1
.\" http://github.com/rtomayko/ronn/tree/0.7.3 .\" http://github.com/apjanke/ronn-ng/tree/0.9.1
. .TH "GIT\-AUTHORS" "1" "November 2023" "" "Git Extras"
.TH "GIT\-AUTHORS" "1" "October 2017" "" "Git Extras"
.
.SH "NAME" .SH "NAME"
\fBgit\-authors\fR \- Generate authors report \fBgit\-authors\fR \- Generate authors report
.
.SH "SYNOPSIS" .SH "SYNOPSIS"
\fBgit\-authors\fR [\-l, \-\-list] [\-\-no\-email] \fBgit\-authors\fR [\-l, \-\-list] [\-\-no\-email]
.
.SH "DESCRIPTION" .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 .P
See the "MAPPING AUTHORS" section of \fBgit\-shortlog\fR(1) to coalesce together commits by the same person\. See the "MAPPING AUTHORS" section of \fBgit\-shortlog\fR(1) to coalesce together commits by the same person\.
.
.SH "OPTIONS" .SH "OPTIONS"
\-l, \-\-list \-l, \-\-list
.
.P .P
Show authors\. Show authors\.
.
.P .P
\-\-no\-email \-\-no\-email
.
.P .P
Don\'t show authors\' email\. Don\'t show authors\' email\.
.
.SH "EXAMPLES" .SH "EXAMPLES"
.
.TP
Updating AUTHORS file: Updating AUTHORS file:
.
.IP
$ git authors
.
.TP
Listing authors:
.
.IP
$ git authors \-\-list
.
.IP "" 4 .IP "" 4
.
.nf .nf
$ git authors
.fi
.IP "" 0
.P
Listing authors:
.IP "" 4
.nf
$ git authors \-\-list
TJ Holowaychuk <tj@vision\-media\.ca> TJ Holowaychuk <tj@vision\-media\.ca>
hemanth\.hm <hemanth\.hm@gmail\.com> hemanth\.hm <hemanth\.hm@gmail\.com>
Jonhnny Weslley <jw@jonhnnyweslley\.net> Jonhnny Weslley <jw@jonhnnyweslley\.net>
nickl\- <github@jigsoft\.co\.za> nickl\- <github@jigsoft\.co\.za>
Leila Muhtasib <muhtasib@gmail\.com> Leila Muhtasib <muhtasib@gmail\.com>
.
.fi .fi
.
.IP "" 0 .IP "" 0
.P
.
.TP
Listing authors without email: Listing authors without email:
.
.IP
$ git authors \-\-list \-\-no\-email
.
.IP "" 4 .IP "" 4
.
.nf .nf
$ git authors \-\-list \-\-no\-email
TJ Holowaychuk TJ Holowaychuk
hemanth\.hm hemanth\.hm
Jonhnny Weslley Jonhnny Weslley
nickl\- nickl\-
Leila Muhtasib Leila Muhtasib
.
.fi .fi
.
.IP "" 0 .IP "" 0
.
.SH "AUTHOR" .SH "AUTHOR"
Written by Titus Wormer <\fItituswormer@gmail\.com\fR> Written by Titus Wormer <\fItituswormer@gmail\.com\fR>
.
.SH "REPORTING BUGS" .SH "REPORTING BUGS"
<\fIhttps://github\.com/tj/git\-extras/issues\fR> <\fIhttps://github\.com/tj/git\-extras/issues\fR>
.
.SH "SEE ALSO" .SH "SEE ALSO"
<\fIhttps://github\.com/tj/git\-extras\fR> <\fIhttps://github\.com/tj/git\-extras\fR>

View file

@ -1,8 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta http-equiv='content-type' value='text/html;charset=utf8'> <meta http-equiv='content-type' content='text/html;charset=utf8'>
<meta name='generator' value='Ronn/v0.7.3 (http://github.com/rtomayko/ronn/tree/0.7.3)'> <meta name='generator' content='Ronn-NG/v0.9.1 (http://github.com/apjanke/ronn-ng/tree/0.9.1)'>
<title>git-authors(1) - Generate authors report</title> <title>git-authors(1) - Generate authors report</title>
<style type='text/css' media='all'> <style type='text/css' media='all'>
/* style: man */ /* style: man */
@ -69,64 +69,69 @@
<li class='tr'>git-authors(1)</li> <li class='tr'>git-authors(1)</li>
</ol> </ol>
<h2 id="NAME">NAME</h2>
<h2 id="NAME">NAME</h2>
<p class="man-name"> <p class="man-name">
<code>git-authors</code> - <span class="man-whatis">Generate authors report</span> <code>git-authors</code> - <span class="man-whatis">Generate authors report</span>
</p> </p>
<h2 id="SYNOPSIS">SYNOPSIS</h2> <h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>git-authors</code> [-l, --list] [--no-email]</p> <p><code>git-authors</code> [-l, --list] [--no-email]</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2> <h2 id="DESCRIPTION">DESCRIPTION</h2>
<p> Populates the file matching <em>authors|contributors -i</em> with the authors of commits, according to the number of commits per author. <table>
Opens the file in <strong>$EDITOR</strong> when set.</p> <tbody>
<tr>
<td>Populates the file matching _authors</td>
<td>contributors -i_ with the authors of commits, according to the number of commits per author.</td>
</tr>
</tbody>
</table>
<p> See the "MAPPING AUTHORS" section of <strong>git-shortlog</strong>(1) to coalesce together commits by the same person.</p> <p>See the "MAPPING AUTHORS" section of <strong>git-shortlog</strong>(1) to coalesce together commits by the same person.</p>
<h2 id="OPTIONS">OPTIONS</h2> <h2 id="OPTIONS">OPTIONS</h2>
<p> -l, --list</p> <p>-l, --list</p>
<p> Show authors.</p> <p>Show authors.</p>
<p> --no-email</p> <p>--no-email</p>
<p> Don't show authors' email.</p> <p>Don't show authors' email.</p>
<h2 id="EXAMPLES">EXAMPLES</h2> <h2 id="EXAMPLES">EXAMPLES</h2>
<dl> <p>Updating AUTHORS file:</p>
<dt>Updating AUTHORS file:</dt><dd><p></p>
<p>$ git authors</p></dd> <pre><code>$ git authors
<dt>Listing authors:</dt><dd><p></p> </code></pre>
<p>$ git authors --list</p> <p>Listing authors:</p>
<pre><code>TJ Holowaychuk &lt;tj@vision-media.ca&gt; <pre><code>$ git authors --list
TJ Holowaychuk &lt;tj@vision-media.ca&gt;
hemanth.hm &lt;hemanth.hm@gmail.com&gt; hemanth.hm &lt;hemanth.hm@gmail.com&gt;
Jonhnny Weslley &lt;jw@jonhnnyweslley.net&gt; Jonhnny Weslley &lt;jw@jonhnnyweslley.net&gt;
nickl- &lt;github@jigsoft.co.za&gt; nickl- &lt;github@jigsoft.co.za&gt;
Leila Muhtasib &lt;muhtasib@gmail.com&gt; Leila Muhtasib &lt;muhtasib@gmail.com&gt;
</code></pre></dd> </code></pre>
<dt>Listing authors without email:</dt><dd><p></p>
<p>$ git authors --list --no-email</p> <p>Listing authors without email:</p>
<pre><code>TJ Holowaychuk <pre><code>$ git authors --list --no-email
TJ Holowaychuk
hemanth.hm hemanth.hm
Jonhnny Weslley Jonhnny Weslley
nickl- nickl-
Leila Muhtasib Leila Muhtasib
</code></pre></dd> </code></pre>
</dl>
<h2 id="AUTHOR">AUTHOR</h2> <h2 id="AUTHOR">AUTHOR</h2>
<p>Written by Titus Wormer &lt;<a href="&#x6d;&#x61;&#x69;&#x6c;&#116;&#111;&#58;&#x74;&#105;&#116;&#x75;&#x73;&#x77;&#x6f;&#x72;&#x6d;&#x65;&#114;&#64;&#103;&#109;&#97;&#x69;&#108;&#46;&#99;&#111;&#x6d;" data-bare-link="true">&#x74;&#105;&#116;&#x75;&#x73;&#x77;&#111;&#114;&#109;&#101;&#x72;&#64;&#103;&#x6d;&#x61;&#x69;&#108;&#x2e;&#99;&#x6f;&#x6d;</a>&gt;</p> <p>Written by Titus Wormer &lt;<a href="mailto:tituswormer@gmail.com" data-bare-link="true">tituswormer@gmail.com</a>&gt;</p>
<h2 id="REPORTING-BUGS">REPORTING BUGS</h2> <h2 id="REPORTING-BUGS">REPORTING BUGS</h2>
@ -136,10 +141,9 @@ Leila Muhtasib
<p>&lt;<a href="https://github.com/tj/git-extras" data-bare-link="true">https://github.com/tj/git-extras</a>&gt;</p> <p>&lt;<a href="https://github.com/tj/git-extras" data-bare-link="true">https://github.com/tj/git-extras</a>&gt;</p>
<ol class='man-decor man-foot man foot'> <ol class='man-decor man-foot man foot'>
<li class='tl'></li> <li class='tl'></li>
<li class='tc'>October 2017</li> <li class='tc'>November 2023</li>
<li class='tr'>git-authors(1)</li> <li class='tr'>git-authors(1)</li>
</ol> </ol>

View file

@ -8,7 +8,6 @@ git-authors(1) -- Generate authors report
## DESCRIPTION ## DESCRIPTION
Populates the file matching _authors|contributors -i_ with the authors of commits, according to the number of commits per author. 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. See the "MAPPING AUTHORS" section of **git-shortlog**(1) to coalesce together commits by the same person.
@ -24,33 +23,27 @@ git-authors(1) -- Generate authors report
## EXAMPLES ## EXAMPLES
* Updating AUTHORS file: Updating AUTHORS file:
$ git authors $ git authors
* Listing authors: Listing authors:
$ git authors --list $ git authors --list
```
TJ Holowaychuk <tj@vision-media.ca> TJ Holowaychuk <tj@vision-media.ca>
hemanth.hm <hemanth.hm@gmail.com> hemanth.hm <hemanth.hm@gmail.com>
Jonhnny Weslley <jw@jonhnnyweslley.net> Jonhnny Weslley <jw@jonhnnyweslley.net>
nickl- <github@jigsoft.co.za> nickl- <github@jigsoft.co.za>
Leila Muhtasib <muhtasib@gmail.com> Leila Muhtasib <muhtasib@gmail.com>
```
* Listing authors without email: Listing authors without email:
$ git authors --list --no-email $ git authors --list --no-email
```
TJ Holowaychuk TJ Holowaychuk
hemanth.hm hemanth.hm
Jonhnny Weslley Jonhnny Weslley
nickl- nickl-
Leila Muhtasib Leila Muhtasib
```
## AUTHOR ## AUTHOR

View file

@ -29,4 +29,5 @@ def named_temp_repo(request):
dirname = request.param dirname = request.param
repo = create_repo(dirname) repo = create_repo(dirname)
init_repo_git_status(repo) init_repo_git_status(repo)
return repo yield repo
repo.teardown()

View file

@ -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__)) CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
GIT_EXTRAS_BIN = os.path.join(CURRENT_DIR, "..", "bin") GIT_EXTRAS_BIN = os.path.abspath(os.path.join(CURRENT_DIR, "..", "bin"))
GIT_EXTRAS_HELPER = os.path.join(CURRENT_DIR, "..", "helper") GIT_EXTRAS_HELPER = os.path.abspath(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)
class TempRepository: class TempRepository:
def __init__(self, repo_work_dir = None): def __init__(self, repo_work_dir = None):
@ -18,7 +14,7 @@ class TempRepository:
repo_work_dir = os.path.join(self._system_tmpdir, repo_work_dir) repo_work_dir = os.path.join(self._system_tmpdir, repo_work_dir)
self._cwd = repo_work_dir self._cwd = repo_work_dir
self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:] 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 = [] self._files = []
def switch_cwd_under_repo(self): def switch_cwd_under_repo(self):
@ -69,12 +65,14 @@ class TempRepository:
def invoke_extras_command(self, name, *params): def invoke_extras_command(self, name, *params):
command_name = "git-" + name 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}")
return invoke_git_extras_command(command_name, *params) 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): def invoke_installed_extras_command(self, name, *params):
command_name = "git-" + name 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) origin_extras_command = os.path.join(GIT_EXTRAS_BIN, command_name)
temp_extras_command = os.path.join(self._cwd, command_name) temp_extras_command = os.path.join(self._cwd, command_name)
helpers = [ helpers = [
@ -94,7 +92,9 @@ class TempRepository:
whole.extend(rest) whole.extend(rest)
whole.insert(0, first) whole.insert(0, first)
t.write("\n".join(whole)) 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) 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)

45
tests/test_authors.py Normal file
View file

@ -0,0 +1,45 @@
import os, subprocess
expected_authors_list = "test <test@git-extras.com>\ntestagain <testagain@git-extras.com>\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", "testagain@git-extras.com")
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