Skip to content

Commit 130d4de

Browse files
committed
complete unit test coverage
1 parent 6945b0f commit 130d4de

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
[MkDocs](https://www.mkdocs.org/) plugin to display git authors of a page. Only considers authors of the current lines in the page ('surviving code' using `git blame`).
1010

11-
If you want to display authors' github user profiles, see [mkdocs-git-committers-plugin](https://github.com/byrnereese/mkdocs-git-committers-plugin).
11+
Other MkDocs plugins that use information from git:
12+
13+
- [mkdocs-git-committers-plugin](https://github.com/byrnereese/mkdocs-git-committers-plugin) for displaying authors' github user profiles
14+
- [mkdocs-git-revision-date-localized-plugin](https://github.com/timvink/mkdocs-git-revision-date-localized-plugin) for displaying the last revision date
1215

1316
## Setup
1417

mkdocs_git_authors_plugin/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def get_authors(self, path: str, type: str = 'authors'):
3434

3535
# Update existing author
3636
if authors.get(key):
37-
authors[key]['lines'] += len(lines)
37+
authors[key]['lines'] = authors[key]['lines'] + len(lines)
3838
current_dt = authors.get(key,{}).get('last_datetime')
3939
if commit.committed_datetime > current_dt:
4040
authors[key]['last_datetime'] = commit.committed_datetime
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# test page
2+
3+
this page has no git authors tag

test/test_util.py

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22
import os
33
from git import Actor, Repo
44

5-
def test_retrieve_authors(tmp_path):
5+
def test_empty_file(tmp_path):
66

7+
# Create empty file
8+
file_name = os.path.join(tmp_path, 'new-file')
9+
open(file_name, 'a').close()
10+
11+
# Get authors of empty, uncommitted file
712
r = Repo.init(tmp_path)
13+
instance = util.Util(tmp_path)
14+
authors = instance.get_authors(path = file_name)
15+
assert authors == ""
816

9-
# Create empty file
17+
# Get authors of empty, committed file
18+
r.index.add([file_name])
19+
author = Actor('Tim', '[email protected]')
20+
r.index.commit("initial commit", author = author)
21+
authors = instance.get_authors(path = file_name)
22+
assert authors == ""
23+
24+
def test_retrieve_authors(tmp_path):
25+
26+
tmp_path = "/Users/ue86yw/workspace/testrepo"
27+
28+
# Create file
1029
file_name = os.path.join(tmp_path, 'new-file')
1130
with open(file_name, 'w') as the_file:
1231
the_file.write('Hello\n')
13-
14-
# Commit
32+
33+
# Create git repo and commit file
34+
r = Repo.init(tmp_path)
1535
r.index.add([file_name])
1636
author = Actor('Tim', '[email protected]')
1737
r.index.commit("initial commit", author = author)
@@ -27,6 +47,50 @@ def test_retrieve_authors(tmp_path):
2747
'lines' : 1,
2848
'contribution' : '100.0%'
2949
}]
50+
51+
# Now add a line to the file
52+
# From a second author with same email
53+
with open(file_name, 'a+') as the_file:
54+
the_file.write('World\n')
55+
r.index.add([file_name])
56+
author = Actor('Tim2', '[email protected]')
57+
r.index.commit("another commit", author = author)
58+
59+
authors = instance.get_authors(path = file_name)
60+
authors[0]['last_datetime'] = None
61+
62+
assert authors == [{
63+
'name' : "Tim",
64+
'email' : "[email protected]",
65+
'last_datetime' : None,
66+
'lines' : 2,
67+
'contribution' : '100.0%'
68+
}]
69+
70+
# Then a third commit from a new author
71+
with open(file_name, 'a+') as the_file:
72+
the_file.write('A new line\n')
73+
r.index.add([file_name])
74+
author = Actor('John', '[email protected]')
75+
r.index.commit("third commit", author = author)
76+
77+
authors = instance.get_authors(path = file_name)
78+
authors[0]['last_datetime'] = None
79+
authors[1]['last_datetime'] = None
80+
81+
assert authors == [{
82+
'name' : "John",
83+
'email' : "[email protected]",
84+
'last_datetime' : None,
85+
'lines' : 1,
86+
'contribution' : '33.33%'
87+
},{
88+
'name' : "Tim",
89+
'email' : "[email protected]",
90+
'last_datetime' : None,
91+
'lines' : 2,
92+
'contribution' : '66.67%'
93+
}]
3094

3195
def test_summarize_authors():
3296

0 commit comments

Comments
 (0)