Skip to content

Commit 50c01e2

Browse files
authored
Merge pull request #33 from timvink/custom-git-path
Custom git path
2 parents af27821 + 11b4c99 commit 50c01e2

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
1818
("type", config_options.Type(str, default="date")),
1919
)
2020

21-
def __init__(self):
22-
self.util = Util()
23-
2421
def on_config(self, config: config_options.Config) -> dict:
2522
"""
2623
Determine which locale to use.
@@ -36,6 +33,7 @@ def on_config(self, config: config_options.Config) -> dict:
3633
Returns:
3734
dict: global configuration object
3835
"""
36+
self.util = Util(path=config["docs_dir"])
3937

4038
# Get locale settings - might be added in future mkdocs versions
4139
# see: https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/24
@@ -161,11 +159,3 @@ def on_page_markdown(
161159
markdown,
162160
flags=re.IGNORECASE,
163161
)
164-
165-
166-
# ##############################################################################
167-
# ##### Stand alone program ########
168-
# ##################################
169-
if __name__ == "__main__":
170-
"""Standalone execution and quick tests."""
171-
pass

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
# 3rd party
88
from babel.dates import format_date
9-
from git import Git, GitCommandError, GitCommandNotFound
9+
from git import Repo, Git, GitCommandError, GitCommandNotFound
1010

1111

1212
class Util:
1313
def __init__(self, path: str = "."):
14-
self.repo = Git(path)
14+
git_repo = Repo(path, search_parent_directories=True)
15+
self.repo = git_repo.git
1516

1617
# Checks when running builds on CI
1718
# See https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/10

tests/test_basic.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def build_docs_setup(testproject_path):
177177
raise
178178

179179

180-
def validate_build(testproject_path, plugin_config: dict):
180+
def validate_build(testproject_path, plugin_config: dict = {}):
181181
"""
182182
Validates a build from a testproject
183183
@@ -196,7 +196,7 @@ def validate_build(testproject_path, plugin_config: dict):
196196
contents = page_with_tag.read_text(encoding="utf8")
197197
assert re.search(r"Markdown tag\:\s[<span>|\w].+", contents)
198198

199-
repo = Util(str(testproject_path))
199+
repo = Util(str(testproject_path / "docs"))
200200
date_formats = repo.get_revision_date_for_file(
201201
path=str(testproject_path / "docs/page_with_tag.md"),
202202
locale=plugin_config.get("locale"),
@@ -231,9 +231,11 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
231231
return testproject_path
232232

233233

234-
# #############################################################################
234+
# ##################################
235235
# ########### Tests ################
236236
# ##################################
237+
238+
237239
def test_date_formats():
238240
u = Util()
239241
assert u._date_formats(1582397529) == {
@@ -321,6 +323,44 @@ def test_type_unknown(tmp_path):
321323
validate_mkdocs_file(tmp_path, "tests/basic_setup/mkdocs_unknown_type.yml")
322324

323325

326+
def test_git_in_docs_dir(tmp_path):
327+
"""
328+
In https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/pull/31
329+
a use case is described where `.git` dir lives in `docs/`
330+
"""
331+
332+
testproject_path = setup_clean_mkdocs_folder(
333+
"tests/basic_setup/mkdocs.yml", tmp_path
334+
)
335+
336+
# Setup git repo in the 'docs' dir
337+
testproject_docs = str(testproject_path / "docs")
338+
repo = git.Repo.init(testproject_docs, bare=False)
339+
author = "Test Person <[email protected]>"
340+
341+
# Change the working directory
342+
cwd = os.getcwd()
343+
os.chdir(testproject_docs)
344+
345+
try:
346+
repo.git.add("page_with_tag.md")
347+
repo.git.commit(message="homepage", author=author)
348+
os.chdir(cwd)
349+
except:
350+
os.chdir(cwd)
351+
raise
352+
353+
# Build project
354+
result = build_docs_setup(testproject_path)
355+
assert result.exit_code == 0
356+
validate_build(
357+
testproject_path,
358+
plugin_config=get_plugin_config_from_mkdocs(
359+
str(testproject_path / "mkdocs.yml")
360+
),
361+
)
362+
363+
324364
def test_low_fetch_depth(tmp_path, caplog):
325365
"""
326366
On gitlab and github runners, a GIT might have a low fetch

0 commit comments

Comments
 (0)