Skip to content

Commit dc5ee39

Browse files
committed
add unit tests
1 parent e3a5cc9 commit dc5ee39

File tree

10 files changed

+212
-0
lines changed

10 files changed

+212
-0
lines changed

test/basic_setup/docs/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Test page
2+
3+
Markdown tag: {{ git_revision_date_localized }}
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/basic_setup/mkdocs.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-revision-date-localized

test/basic_setup/mkdocs_locale.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
locale: nl
5+
6+
plugins:
7+
- search
8+
- git-revision-date-localized
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-revision-date-localized:
7+
locale: nl
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
theme:
5+
name: 'material'
6+
language: nl
7+
8+
plugins:
9+
- search
10+
- git-revision-date-localized
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-revision-date-localized:
7+
type: timeago

test/test_basic.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import re
2+
import yaml
3+
from click.testing import CliRunner
4+
from mkdocs.__main__ import build_command
5+
from pytest import mark
6+
7+
def load_config(mkdocs_path):
8+
return yaml.load(open(mkdocs_path, 'rb'), Loader=yaml.Loader)
9+
10+
def build_docs_setup(mkdocs_path, output_path):
11+
runner = CliRunner()
12+
return runner.invoke(build_command,
13+
['--config-file',
14+
mkdocs_path,
15+
'--site-dir',
16+
str(output_path)])
17+
18+
def built_site_tests(path):
19+
# Make sure there is proper output
20+
index_file = path/'index.html'
21+
assert index_file.exists(), f"{index_file} does not exist"
22+
23+
# Make sure there is some output for the tag
24+
contents = index_file.read_text()
25+
assert re.search(r"Markdown tag\:\s[\w].+", contents)
26+
27+
def test_basic_working(tmp_path):
28+
29+
# No config
30+
result = build_docs_setup('test/basic_setup/mkdocs.yml', tmp_path)
31+
assert result.exit_code == 0, "'mkdocs build' command failed"
32+
built_site_tests(tmp_path)
33+
34+
# Locales set
35+
result = build_docs_setup('test/basic_setup/mkdocs_plugin_locale.yml', tmp_path)
36+
assert result.exit_code == 0, "'mkdocs build' command failed"
37+
built_site_tests(tmp_path)
38+
39+
result = build_docs_setup('test/basic_setup/mkdocs_locale.yml', tmp_path)
40+
assert result.exit_code == 0, "'mkdocs build' command failed"
41+
built_site_tests(tmp_path)
42+
43+
# With the mkdocs-material theme:
44+
result = build_docs_setup('test/basic_setup/mkdocs_theme_locale.yml', tmp_path)
45+
assert result.exit_code == 0, "'mkdocs build' command failed"
46+
built_site_tests(tmp_path)
47+
48+
# In mkdocs-material, a 'last update' should appear
49+
# in Dutch because locale is set to nl
50+
index_file = tmp_path/'index.html'
51+
built_site_tests(tmp_path)
52+
contents = index_file.read_text()
53+
assert re.search(r"Laatst geüpdatet op\:\s[\w].+", contents)
54+
55+
# Test timeago
56+
result = build_docs_setup('test/basic_setup/mkdocs_timeago.yml', tmp_path)
57+
assert result.exit_code == 0, "'mkdocs build' command failed"
58+
index_file = tmp_path/'index.html'
59+
contents = index_file.read_text()
60+
assert re.search("<span class='timeago'", contents)
61+

test/test_requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pytest
2+
pytest-cov
3+
codecov
4+
click
5+
mkdocs-material

test/test_util.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from mkdocs_git_authors_plugin import util
2+
import os
3+
from git import Actor, Repo
4+
5+
# def test_empty_file(tmp_path):
6+
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
12+
# r = Repo.init(tmp_path)
13+
# instance = util.Util(tmp_path)
14+
# authors = instance.get_authors(path = file_name)
15+
# assert authors == ""
16+
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+
# # Create file
27+
# file_name = os.path.join(tmp_path, 'new-file')
28+
# with open(file_name, 'w') as the_file:
29+
# the_file.write('Hello\n')
30+
31+
# # Create git repo and commit file
32+
# r = Repo.init(tmp_path)
33+
# r.index.add([file_name])
34+
# author = Actor('Tim', '[email protected]')
35+
# r.index.commit("initial commit", author = author)
36+
37+
# instance = util.Util(tmp_path)
38+
# authors = instance.get_authors(path = file_name)
39+
# authors[0]['last_datetime'] = None
40+
41+
# assert authors == [{
42+
# 'name' : "Tim",
43+
# 'email' : "[email protected]",
44+
# 'last_datetime' : None,
45+
# 'lines' : 1,
46+
# 'contribution' : '100.0%'
47+
# }]
48+
49+
# # Now add a line to the file
50+
# # From a second author with same email
51+
# with open(file_name, 'a+') as the_file:
52+
# the_file.write('World\n')
53+
# r.index.add([file_name])
54+
# author = Actor('Tim2', '[email protected]')
55+
# r.index.commit("another commit", author = author)
56+
57+
# authors = instance.get_authors(path = file_name)
58+
# authors[0]['last_datetime'] = None
59+
60+
# assert authors == [{
61+
# 'name' : "Tim",
62+
# 'email' : "[email protected]",
63+
# 'last_datetime' : None,
64+
# 'lines' : 2,
65+
# 'contribution' : '100.0%'
66+
# }]
67+
68+
# # Then a third commit from a new author
69+
# with open(file_name, 'a+') as the_file:
70+
# the_file.write('A new line\n')
71+
# r.index.add([file_name])
72+
# author = Actor('John', '[email protected]')
73+
# r.index.commit("third commit", author = author)
74+
75+
# authors = instance.get_authors(path = file_name)
76+
# authors[0]['last_datetime'] = None
77+
# authors[1]['last_datetime'] = None
78+
79+
# assert authors == [{
80+
# 'name' : "John",
81+
# 'email' : "[email protected]",
82+
# 'last_datetime' : None,
83+
# 'lines' : 1,
84+
# 'contribution' : '33.33%'
85+
# },{
86+
# 'name' : "Tim",
87+
# 'email' : "[email protected]",
88+
# 'last_datetime' : None,
89+
# 'lines' : 2,
90+
# 'contribution' : '66.67%'
91+
# }]
92+
93+
# def test_summarize_authors():
94+
95+
# authors = [
96+
# {'name' : 'Tim',
97+
# 'email' : '[email protected]'
98+
# }
99+
# ]
100+
101+
# summary = util.Util().summarize(authors)
102+
# assert summary == "<span class='git-authors'><a href='mailto:[email protected]'>Tim</a></span>"

0 commit comments

Comments
 (0)