Skip to content

Commit

Permalink
complete unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Dec 23, 2019
1 parent 6945b0f commit 130d4de
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

[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`).

If you want to display authors' github user profiles, see [mkdocs-git-committers-plugin](https://github.com/byrnereese/mkdocs-git-committers-plugin).
Other MkDocs plugins that use information from git:

- [mkdocs-git-committers-plugin](https://github.com/byrnereese/mkdocs-git-committers-plugin) for displaying authors' github user profiles
- [mkdocs-git-revision-date-localized-plugin](https://github.com/timvink/mkdocs-git-revision-date-localized-plugin) for displaying the last revision date

## Setup

Expand Down
2 changes: 1 addition & 1 deletion mkdocs_git_authors_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_authors(self, path: str, type: str = 'authors'):

# Update existing author
if authors.get(key):
authors[key]['lines'] += len(lines)
authors[key]['lines'] = authors[key]['lines'] + len(lines)
current_dt = authors.get(key,{}).get('last_datetime')
if commit.committed_datetime > current_dt:
authors[key]['last_datetime'] = commit.committed_datetime
Expand Down
3 changes: 3 additions & 0 deletions test/basic_setup/docs/page_without_tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# test page

this page has no git authors tag
72 changes: 68 additions & 4 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@
import os
from git import Actor, Repo

def test_retrieve_authors(tmp_path):
def test_empty_file(tmp_path):

# Create empty file
file_name = os.path.join(tmp_path, 'new-file')
open(file_name, 'a').close()

# Get authors of empty, uncommitted file
r = Repo.init(tmp_path)
instance = util.Util(tmp_path)
authors = instance.get_authors(path = file_name)
assert authors == ""

# Create empty file
# Get authors of empty, committed file
r.index.add([file_name])
author = Actor('Tim', '[email protected]')
r.index.commit("initial commit", author = author)
authors = instance.get_authors(path = file_name)
assert authors == ""

def test_retrieve_authors(tmp_path):

tmp_path = "/Users/ue86yw/workspace/testrepo"

# Create file
file_name = os.path.join(tmp_path, 'new-file')
with open(file_name, 'w') as the_file:
the_file.write('Hello\n')

# Commit

# Create git repo and commit file
r = Repo.init(tmp_path)
r.index.add([file_name])
author = Actor('Tim', '[email protected]')
r.index.commit("initial commit", author = author)
Expand All @@ -27,6 +47,50 @@ def test_retrieve_authors(tmp_path):
'lines' : 1,
'contribution' : '100.0%'
}]

# Now add a line to the file
# From a second author with same email
with open(file_name, 'a+') as the_file:
the_file.write('World\n')
r.index.add([file_name])
author = Actor('Tim2', '[email protected]')
r.index.commit("another commit", author = author)

authors = instance.get_authors(path = file_name)
authors[0]['last_datetime'] = None

assert authors == [{
'name' : "Tim",
'email' : "[email protected]",
'last_datetime' : None,
'lines' : 2,
'contribution' : '100.0%'
}]

# Then a third commit from a new author
with open(file_name, 'a+') as the_file:
the_file.write('A new line\n')
r.index.add([file_name])
author = Actor('John', '[email protected]')
r.index.commit("third commit", author = author)

authors = instance.get_authors(path = file_name)
authors[0]['last_datetime'] = None
authors[1]['last_datetime'] = None

assert authors == [{
'name' : "John",
'email' : "[email protected]",
'last_datetime' : None,
'lines' : 1,
'contribution' : '33.33%'
},{
'name' : "Tim",
'email' : "[email protected]",
'last_datetime' : None,
'lines' : 2,
'contribution' : '66.67%'
}]

def test_summarize_authors():

Expand Down

0 comments on commit 130d4de

Please sign in to comment.