Skip to content

Commit 7bc026e

Browse files
twsltimvink
authored andcommitted
Add type hints to tests
1 parent ca568e3 commit 7bc026e

File tree

2 files changed

+72
-73
lines changed

2 files changed

+72
-73
lines changed

tests/test_basic.py

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Note that pytest offers a `tmp_path`.
2+
Note that pytest offers a `tmp_path`.
33
You can reproduce locally with
44
55
```python
@@ -16,16 +16,17 @@
1616
```
1717
"""
1818

19+
import os
1920
import re
2021
import shutil
21-
import os
22-
import pytest
22+
from contextlib import contextmanager
23+
from typing import Any, Generator
2324

24-
from click.testing import CliRunner
25-
from mkdocs.__main__ import build_command
26-
from git import Repo
2725
import git as gitpython
28-
from contextlib import contextmanager
26+
import pytest
27+
from click.testing import CliRunner, Result
28+
from git import Repo
29+
from mkdocs.__main__ import build_command
2930

3031
SITES_THAT_SHOULD_SUCCEED = [
3132
"mkdocs.yml",
@@ -40,8 +41,9 @@
4041
"mkdocs_w_macros2.yml",
4142
]
4243

44+
4345
@contextmanager
44-
def working_directory(path):
46+
def working_directory(path) -> Generator[None, Any, None]:
4547
"""
4648
Temporarily change working directory.
4749
A context manager which changes the working directory to the given
@@ -62,20 +64,21 @@ def working_directory(path):
6264
os.chdir(prev_cwd)
6365

6466

65-
def build_docs_setup(mkdocs_path, output_path):
67+
def build_docs_setup(mkdocs_path, output_path) -> Result:
6668
runner = CliRunner()
6769
return runner.invoke(
6870
build_command, ["--config-file", mkdocs_path, "--site-dir", str(output_path)]
6971
)
7072

73+
7174
@pytest.mark.parametrize(
7275
"mkdocs_file",
7376
SITES_THAT_SHOULD_SUCCEED,
7477
)
75-
def test_basic_working(tmp_path, mkdocs_file):
78+
def test_basic_working(tmp_path, mkdocs_file: str) -> None:
7679
"""
7780
combination with mkdocs-macros-plugin lead to error.
78-
See https://github.com/timvink/mkdocs-git-authors-plugin/issues/60
81+
See https://github.com/timvink/mkdocs-git-authors-plugin/issues/60
7982
"""
8083
result = build_docs_setup(f"tests/basic_setup/{mkdocs_file}", tmp_path)
8184
assert result.exit_code == 0, (
@@ -90,12 +93,11 @@ def test_basic_working(tmp_path, mkdocs_file):
9093
assert re.search('<a href="mailto:[email protected]">Tim Vink</a>', contents)
9194

9295

93-
def test_custom_href(tmp_path):
94-
"""
95-
"""
96+
def test_custom_href(tmp_path) -> None:
97+
""" """
9698
result = build_docs_setup("tests/basic_setup/mkdocs_custom_href.yml", tmp_path)
9799
assert result.exit_code == 0, (
98-
"'mkdocs build' command failed. Error: %s" % result.stdout
100+
"'mkdocs build' command failed. Error: %s" % result.stdout
99101
)
100102

101103
index_file = tmp_path / "index.html"
@@ -104,16 +106,20 @@ def test_custom_href(tmp_path):
104106
contents = index_file.read_text()
105107
assert re.search("<span class='git-page-authors", contents)
106108
# Checking Page Authors
107-
assert re.search((r"<p>Page authors:.*<a href='https://teams.microsoft.com/l/chat/0/0\?"
108-
"[email protected]'>Tim Vink</a>.*<\/p>"), contents)
109+
assert re.search(
110+
r"<p>Page authors:.*<a href='https://teams.microsoft.com/l/chat/0/0\?"
111+
r"[email protected]'>Tim Vink</a>.*</p>",
112+
contents,
113+
)
109114
# Checking Site Authors
110-
assert re.search(('<li><a href="https://teams.microsoft.com/l/chat/0/0\?'
111-
'[email protected]">Tim Vink</a><\/li>'), contents)
112-
113-
115+
assert re.search(
116+
r'<li><a href="https://teams.microsoft.com/l/chat/0/0\?'
117+
r'[email protected]">Tim Vink</a></li>',
118+
contents,
119+
)
114120

115-
def test_no_email(tmp_path):
116121

122+
def test_no_email(tmp_path) -> None:
117123
result = build_docs_setup("tests/basic_setup/mkdocs_no_email.yml", tmp_path)
118124
assert result.exit_code == 0, (
119125
"'mkdocs build' command failed. Error: %s" % result.stdout
@@ -127,9 +133,7 @@ def test_no_email(tmp_path):
127133
assert re.search("<li>Tim Vink</li>", contents)
128134

129135

130-
131-
def test_exclude_working(tmp_path):
132-
136+
def test_exclude_working(tmp_path) -> None:
133137
result = build_docs_setup("tests/basic_setup/mkdocs_exclude.yml", tmp_path)
134138
assert result.exit_code == 0, (
135139
"'mkdocs build' command failed. Error: %s" % result.stdout
@@ -142,12 +146,10 @@ def test_exclude_working(tmp_path):
142146
assert not re.search("<span class='git-page-authors", contents)
143147

144148

145-
146-
def test_ignore_authors_working(tmp_path):
147-
149+
def test_ignore_authors_working(tmp_path) -> None:
148150
result = build_docs_setup("tests/basic_setup/mkdocs_ignore_authors.yml", tmp_path)
149151
assert result.exit_code == 0, (
150-
"'mkdocs build' command failed. Error: %s" % result.stdout
152+
"'mkdocs build' command failed. Error: %s" % result.stdout
151153
)
152154

153155
page_file = tmp_path / "page_with_tag/index.html"
@@ -159,18 +161,15 @@ def test_ignore_authors_working(tmp_path):
159161
assert not re.search("Julien", contents)
160162

161163

162-
163-
def test_exclude_working_with_genfiles(tmp_path):
164+
def test_exclude_working_with_genfiles(tmp_path) -> None:
164165
"""
165166
A warning for uncommited files should not show up
166167
when those uncommited files are excluded.
167168
"""
168-
169+
169170
testproject_path = tmp_path / "testproject_genfiles"
170171

171-
shutil.copytree(
172-
"tests/basic_setup/docs", str(testproject_path / "docs")
173-
)
172+
shutil.copytree("tests/basic_setup/docs", str(testproject_path / "docs"))
174173
shutil.copyfile(
175174
"tests/basic_setup/mkdocs_genfiles.yml",
176175
str(testproject_path / "mkdocs.yml"),
@@ -199,18 +198,21 @@ def test_exclude_working_with_genfiles(tmp_path):
199198
str(testproject_path / "mkdocs.yml"), str(testproject_path / "site")
200199
)
201200
assert result.exit_code == 0, (
202-
"'mkdocs build' command failed. Error: %s" % result.stdout
201+
"'mkdocs build' command failed. Error: %s" % result.stdout
203202
)
204203

205204
# files generated ourselves right before build but not committed, should not generate warnings
206205
assert "manually_created.md has not been committed yet." not in result.stdout
207-
assert "manually_created_infolder.md has not been committed yet." not in result.stdout
208-
209-
206+
assert (
207+
"manually_created_infolder.md has not been committed yet."
208+
not in result.stdout
209+
)
210210

211-
def test_enabled_working(tmp_path):
212211

213-
result = build_docs_setup("tests/basic_setup/mkdocs_complete_material_disabled.yml", tmp_path)
212+
def test_enabled_working(tmp_path) -> None:
213+
result = build_docs_setup(
214+
"tests/basic_setup/mkdocs_complete_material_disabled.yml", tmp_path
215+
)
214216
assert result.exit_code == 0, (
215217
"'mkdocs build' command failed. Error: %s" % result.stdout
216218
)
@@ -222,11 +224,10 @@ def test_enabled_working(tmp_path):
222224
assert not re.search("<span class='git-page-authors", contents)
223225

224226

225-
226-
def test_project_with_no_commits(tmp_path):
227+
def test_project_with_no_commits(tmp_path) -> None:
227228
"""
228229
Structure:
229-
230+
230231
tmp_path/testproject
231232
website/
232233
├── docs/
@@ -253,12 +254,10 @@ def test_project_with_no_commits(tmp_path):
253254
)
254255

255256

256-
257-
258-
def test_building_empty_site(tmp_path):
257+
def test_building_empty_site(tmp_path) -> None:
259258
"""
260259
Structure:
261-
260+
262261
```
263262
tmp_path/testproject
264263
website/
@@ -288,11 +287,10 @@ def test_building_empty_site(tmp_path):
288287
)
289288

290289

291-
292-
def test_fallback(tmp_path):
290+
def test_fallback(tmp_path) -> None:
293291
"""
294292
Structure:
295-
293+
296294
```
297295
tmp_path/testproject
298296
website/
@@ -311,7 +309,6 @@ def test_fallback(tmp_path):
311309
)
312310

313311
with working_directory(str(testproject_path)):
314-
315312
result = build_docs_setup(
316313
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
317314
)

tests/test_util.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Note that pytest offers a `tmp_path`.
2+
Note that pytest offers a `tmp_path`.
33
You can reproduce locally with
44
55
```python
@@ -16,16 +16,17 @@
1616
```
1717
"""
1818

19+
import logging
1920
import os
2021
import shutil
21-
import logging
22-
23-
from mkdocs_git_authors_plugin import util
24-
from mkdocs_git_authors_plugin.git import repo
22+
from typing import Any
2523

2624
# GitPython
2725
import git as gitpython
2826

27+
from mkdocs_git_authors_plugin import util
28+
from mkdocs_git_authors_plugin.git import repo
29+
2930
DEFAULT_CONFIG = {
3031
"show_contribution": False,
3132
"show_line_count": False,
@@ -41,18 +42,18 @@
4142
#### Helpers ####
4243

4344

44-
def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
45+
def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path) -> Any:
4546
"""
4647
Sets up a clean mkdocs directory
47-
48+
4849
outputpath/testproject
4950
├── docs/
5051
└── mkdocs.yml
51-
52+
5253
Args:
5354
mkdocs_yml_path (Path): Path of mkdocs.yml file to use
5455
output_path (Path): Path of folder in which to create mkdocs project
55-
56+
5657
Returns:
5758
testproject_path (Path): Path to test project
5859
"""
@@ -77,14 +78,14 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
7778
def setup_commit_history(testproject_path):
7879
"""
7980
Initializes and creates a git commit history
80-
in a new mkdocs testproject.
81-
82-
We commit the pages one by one in order
81+
in a new mkdocs testproject.
82+
83+
We commit the pages one by one in order
8384
to create some git depth.
84-
85+
8586
Args:
8687
testproject_path (Path): Path to test project
87-
88+
8889
Returns:
8990
repo (repo): git.Repo object
9091
"""
@@ -131,8 +132,7 @@ def setup_commit_history(testproject_path):
131132
#### Tests ####
132133

133134

134-
def test_empty_file(tmp_path):
135-
135+
def test_empty_file(tmp_path) -> None:
136136
# Change working directory
137137
cwd = os.getcwd()
138138
os.chdir(str(tmp_path))
@@ -331,7 +331,7 @@ def test_retrieve_authors_ignoring_commits(tmp_path):
331331
the_file.write(commit.hexsha + "\n")
332332
repo_instance = repo.Repo()
333333
config = DEFAULT_CONFIG.copy()
334-
config['ignore_commits'] = ignored_commits_files
334+
config["ignore_commits"] = ignored_commits_files
335335
repo_instance.set_config(config)
336336
repo_instance.page(file_name)
337337
authors = repo_instance.get_authors()
@@ -386,7 +386,7 @@ def test_retrieve_authors_ignoring_emails(tmp_path):
386386
# Get the authors while ignoring [email protected] user
387387
repo_instance = repo.Repo()
388388
config = DEFAULT_CONFIG.copy()
389-
config['ignore_authors'] = ['[email protected]']
389+
config["ignore_authors"] = ["[email protected]"]
390390
repo_instance.set_config(config)
391391
repo_instance.page(file_name)
392392
authors = repo_instance.get_authors()
@@ -402,7 +402,7 @@ def test_retrieve_authors_ignoring_emails(tmp_path):
402402
"last_datetime": None,
403403
"lines": 0,
404404
"lines_all_pages": 0,
405-
"name": "John"
405+
"name": "John",
406406
},
407407
{
408408
"name": "Tim",
@@ -476,9 +476,10 @@ def test_mkdocs_in_git_subdir(tmp_path):
476476

477477
os.chdir(cwd)
478478

479+
479480
def test_summarize_authors():
480481
"""
481-
Test summary functions.
482+
Test summary functions.
482483
TODO
483484
"""
484485
pass
@@ -510,4 +511,5 @@ def test_summarize_authors():
510511
# summary = util.Util().summarize(authors, config)
511512
# 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>"
512513

513-
# TODO: test authors threshold with commits
514+
515+
# TODO: test authors threshold with commits

0 commit comments

Comments
 (0)