Skip to content

Commit 3744f61

Browse files
committed
add unit tests for case when mkdocs.yml is not in git repo root dir, closes #1
1 parent 49253f0 commit 3744f61

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

tests/test_basic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ def test_basic_working(tmp_path):
2020
assert index_file.exists(), "%s does not exist" % index_file
2121

2222
contents = index_file.read_text()
23-
assert re.search("<span class='git-authors'>", contents)
23+
assert re.search("<span class='git-authors'>", contents)
24+
25+

tests/test_util.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,59 @@ def test_retrieve_authors(tmp_path):
259259
}]
260260
os.chdir(cwd)
261261

262+
def test_mkdocs_in_git_subdir(tmp_path):
263+
"""
264+
Sometimes `mkdocs.yml` is not in the root of the repo.
265+
We need to make sure things still work in this edge case.
266+
267+
tmp_path/testproject
268+
website/
269+
├── docs/
270+
└── mkdocs.yml
271+
"""
272+
testproject_path = tmp_path / 'testproject'
273+
274+
shutil.copytree('tests/basic_setup/docs', testproject_path / 'website' / 'docs')
275+
shutil.copyfile("tests/basic_setup/mkdocs.yml", testproject_path / 'website' / 'mkdocs.yml')
276+
277+
cwd = os.getcwd()
278+
os.chdir(str(testproject_path))
279+
280+
# Create file
281+
file_name = str(testproject_path / 'website' / 'new-file')
282+
with open(file_name, 'w') as the_file:
283+
the_file.write('Hello\n')
284+
285+
# Create git repo and commit file
286+
r = gitpython.Repo.init(testproject_path)
287+
r.index.add([file_name])
288+
author = gitpython.Actor('Tim', '[email protected]')
289+
r.index.commit("initial commit", author = author)
290+
291+
# Test retrieving author
292+
repo_instance = repo.Repo()
293+
repo_instance.set_config(DEFAULT_CONFIG)
294+
repo_instance.page(file_name)
295+
296+
authors = repo_instance.get_authors()
297+
assert len(authors) == 1
298+
# We don't want to test datetime
299+
authors = util.page_authors(authors, file_name)
300+
authors[0]['last_datetime'] = None
301+
302+
assert authors == [{
303+
'name' : "Tim",
304+
'email' : "[email protected]",
305+
'last_datetime' : None,
306+
'lines' : 1,
307+
'lines_all_pages': 1,
308+
'contribution' : '100.0%',
309+
'contribution_all_pages': '100.0%'
310+
}]
311+
312+
os.chdir(cwd)
313+
314+
262315
def test_summarize_authors():
263316
"""
264317
Test summary functions.
@@ -292,3 +345,4 @@ def test_summarize_authors():
292345
# # Now contribution is displayed
293346
# summary = util.Util().summarize(authors, config)
294347
# assert summary == "<span class='git-authors'><a href='mailto:[email protected]'>Tim</a> (64.23%), <a href='mailto:[email protected]'>Tom</a> (35.77%)</span>"
348+

0 commit comments

Comments
 (0)