|
20 | 20 | import re |
21 | 21 | import shutil |
22 | 22 | import sys |
23 | | -from contextlib import contextmanager |
24 | 23 | from packaging.version import Version |
25 | | -from typing import Any, Generator |
26 | 24 |
|
27 | 25 | import git as gitpython |
28 | 26 | import mkdocs |
|
31 | 29 | from git import Repo |
32 | 30 | from mkdocs.__main__ import build_command |
33 | 31 |
|
| 32 | +from mkdocs_git_authors_plugin.util import working_directory |
| 33 | + |
34 | 34 | SITES_THAT_SHOULD_SUCCEED = [ |
35 | 35 | "mkdocs.yml", |
36 | 36 | "mkdocs_complete_material.yml", |
|
45 | 45 | ] |
46 | 46 |
|
47 | 47 |
|
48 | | -@contextmanager |
49 | | -def working_directory(path) -> Generator[None, Any, None]: |
50 | | - """ |
51 | | - Temporarily change working directory. |
52 | | - A context manager which changes the working directory to the given |
53 | | - path, and then changes it back to its previous value on exit. |
54 | | - Usage: |
55 | | - ```python |
56 | | - # Do something in original directory |
57 | | - with working_directory('/my/new/path'): |
58 | | - # Do something in new directory |
59 | | - # Back to old directory |
60 | | - ``` |
61 | | - """ |
62 | | - prev_cwd = os.getcwd() |
63 | | - os.chdir(path) |
64 | | - try: |
65 | | - yield |
66 | | - finally: |
67 | | - os.chdir(prev_cwd) |
68 | 48 |
|
69 | 49 |
|
70 | 50 | def build_docs_setup(mkdocs_path, output_path) -> Result: |
@@ -373,3 +353,31 @@ def test_mkapi_v1(tmp_path) -> None: |
373 | 353 | r'<a href="\$api:src/mkdocs_git_authors_plugin.*" class="nav-link">API</a>', |
374 | 354 | contents, |
375 | 355 | ) |
| 356 | + |
| 357 | +def test_custom_docs_dir(tmp_path): |
| 358 | + |
| 359 | + testproject_path = tmp_path / "testproject" |
| 360 | + shutil.copytree("tests/custom_docs_dir", testproject_path) |
| 361 | + |
| 362 | + |
| 363 | + # init git inside the docs directory |
| 364 | + with working_directory(str(testproject_path / "documentation")): |
| 365 | + # setup git history |
| 366 | + repo = Repo.init(".") |
| 367 | + assert not repo.bare |
| 368 | + repo.git.add(all=True) |
| 369 | + repo.index.commit("first commit") |
| 370 | + |
| 371 | + with working_directory(str(testproject_path)): |
| 372 | + result = build_docs_setup("mkdocs.yml", str(tmp_path / "site")) |
| 373 | + |
| 374 | + assert ( |
| 375 | + result.exit_code == 0 |
| 376 | + ), f"'mkdocs build' command failed. Error: {result.stdout}" |
| 377 | + |
| 378 | + index_file = tmp_path / "site" / "index.html" |
| 379 | + assert index_file.exists(), f"{index_file} does not exist" |
| 380 | + |
| 381 | + contents = index_file.read_text() |
| 382 | + assert re.search("<span class='git-page-authors", contents) |
| 383 | + |
0 commit comments