mkdocs-git-authors-plugin 0.7.2__tar.gz → 0.8.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/LICENSE +1 -1
  2. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/PKG-INFO +3 -2
  3. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/git/page.py +1 -1
  4. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/plugin.py +28 -4
  5. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/util.py +6 -2
  6. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin.egg-info/PKG-INFO +3 -2
  7. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin.egg-info/SOURCES.txt +3 -1
  8. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/setup.py +2 -2
  9. mkdocs-git-authors-plugin-0.8.0/tests/test_basic.py +307 -0
  10. mkdocs-git-authors-plugin-0.8.0/tests/test_util.py +370 -0
  11. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/README.md +0 -0
  12. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/__init__.py +0 -0
  13. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/ci.py +0 -0
  14. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/exclude.py +0 -0
  15. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/git/__init__.py +0 -0
  16. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/git/author.py +0 -0
  17. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/git/command.py +0 -0
  18. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/git/commit.py +0 -0
  19. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin/git/repo.py +0 -0
  20. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin.egg-info/dependency_links.txt +0 -0
  21. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin.egg-info/entry_points.txt +0 -0
  22. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin.egg-info/requires.txt +0 -0
  23. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/mkdocs_git_authors_plugin.egg-info/top_level.txt +0 -0
  24. {mkdocs-git-authors-plugin-0.7.2 → mkdocs-git-authors-plugin-0.8.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 Tim Vink
3
+ Copyright (c) 2019-2024 Tim Vink
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mkdocs-git-authors-plugin
3
- Version: 0.7.2
3
+ Version: 0.8.0
4
4
  Summary: Mkdocs plugin to display git authors of a page
5
5
  Home-page: https://github.com/timvink/mkdocs-git-authors-plugin
6
6
  Author: Tim Vink
@@ -10,10 +10,10 @@ Keywords: mkdocs git contributors committers authors plugin
10
10
  Classifier: Operating System :: OS Independent
11
11
  Classifier: Programming Language :: Python
12
12
  Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.7
14
13
  Classifier: Programming Language :: Python :: 3.8
15
14
  Classifier: Programming Language :: Python :: 3.9
16
15
  Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3 :: Only
18
18
  Classifier: License :: OSI Approved :: MIT License
19
19
  Classifier: Topic :: Documentation
@@ -21,6 +21,7 @@ Classifier: Topic :: Text Processing
21
21
  Requires-Python: >=3.7
22
22
  Description-Content-Type: text/markdown
23
23
  License-File: LICENSE
24
+ Requires-Dist: mkdocs>=1.0
24
25
 
25
26
  [![Actions Status](https://github.com/timvink/mkdocs-git-authors-plugin/workflows/pytest/badge.svg)](https://github.com/timvink/mkdocs-git-authors-plugin/actions)
26
27
  ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/mkdocs-git-authors-plugin)
@@ -82,7 +82,7 @@ class Page(AbstractRepoObject):
82
82
  self._authors = [
83
83
  a
84
84
  for a in self._authors
85
- if a.contribution() * 100 > author_threshold
85
+ if a.contribution(self._path) * 100 > author_threshold
86
86
  ]
87
87
  return self._authors
88
88
 
@@ -17,10 +17,12 @@ class GitAuthorsPlugin(BasePlugin):
17
17
  ("show_contribution", config_options.Type(bool, default=False)),
18
18
  ("show_line_count", config_options.Type(bool, default=False)),
19
19
  ("show_email_address", config_options.Type(bool, default=True)),
20
+ ("href", config_options.Type(str, default='mailto:{email}')),
20
21
  ("count_empty_lines", config_options.Type(bool, default=True)),
21
22
  ("fallback_to_empty", config_options.Type(bool, default=False)),
22
23
  ("exclude", config_options.Type(list, default=[])),
23
24
  ("enabled", config_options.Type(bool, default=True)),
25
+ ("enabled_on_serve", config_options.Type(bool, default=True)),
24
26
  ("sort_authors_by", config_options.Type(str, default="name")),
25
27
  ("authorship_threshold_percent", config_options.Type(int, default=0)),
26
28
  ("strict", config_options.Type(bool, default=True)),
@@ -31,6 +33,10 @@ class GitAuthorsPlugin(BasePlugin):
31
33
  def __init__(self):
32
34
  self._repo = None
33
35
  self._fallback = False
36
+ self.is_serve = False
37
+
38
+ def on_startup(self, command, dirty):
39
+ self.is_serve = command == "serve"
34
40
 
35
41
  def on_config(self, config, **kwargs):
36
42
  """
@@ -51,7 +57,8 @@ class GitAuthorsPlugin(BasePlugin):
51
57
  Returns:
52
58
  (updated) configuration object
53
59
  """
54
- if not self.config.get("enabled"):
60
+
61
+ if not self._is_enabled():
55
62
  return config
56
63
 
57
64
  assert self.config["authorship_threshold_percent"] >= 0
@@ -99,7 +106,7 @@ class GitAuthorsPlugin(BasePlugin):
99
106
  Returns:
100
107
  global files collection
101
108
  """
102
- if not self.config.get("enabled"):
109
+ if not self._is_enabled():
103
110
  return
104
111
  if self._fallback:
105
112
  return
@@ -138,7 +145,7 @@ class GitAuthorsPlugin(BasePlugin):
138
145
  Returns:
139
146
  str: HTML text of page as string
140
147
  """
141
- if not self.config.get("enabled"):
148
+ if not self._is_enabled():
142
149
  return html
143
150
 
144
151
  # Exclude pages specified in config
@@ -195,7 +202,7 @@ class GitAuthorsPlugin(BasePlugin):
195
202
  Returns:
196
203
  dict: template context variables
197
204
  """
198
- if not self.config.get("enabled"):
205
+ if not self._is_enabled():
199
206
  return context
200
207
  if self._fallback:
201
208
  return context
@@ -235,3 +242,20 @@ class GitAuthorsPlugin(BasePlugin):
235
242
  Reference to the Repo object of the current project.
236
243
  """
237
244
  return self._repo
245
+
246
+ def _is_enabled(self):
247
+ """
248
+ Consider this plugin to be disabled in the following two conditions:
249
+ * config.enabled is false
250
+ * config.enabled is true and
251
+ config.enabled_on_serve is false and
252
+ executed via `serve` command
253
+ """
254
+ is_enabled = True
255
+
256
+ if not self.config.get("enabled"):
257
+ is_enabled = False
258
+ elif self.is_serve and not self.config.get("enabled_on_serve"):
259
+ is_enabled = False
260
+
261
+ return is_enabled
@@ -59,7 +59,9 @@ def page_authors_summary(page, config: dict):
59
59
  else ""
60
60
  )
61
61
  if page.repo().config("show_email_address"):
62
- author_name = "<a href='mailto:%s'>%s</a>" % (author.email(), author.name())
62
+ href = page.repo().config("href").format(email=author.email(),
63
+ name=author.name())
64
+ author_name = "<a href='%s'>%s</a>" % (href, author.name())
63
65
  else:
64
66
  author_name = author.name()
65
67
  authors_summary.append("%s%s" % (author_name, contrib))
@@ -105,7 +107,9 @@ def site_authors_summary(authors, config: dict):
105
107
  lines = ": %s lines" % author.lines() if show_line_count else ""
106
108
  author_name = ""
107
109
  if show_email_address:
108
- author_name = '<a href="mailto:%s">%s</a>' % (author.email(), author.name())
110
+ href = config["href"].format(email=author.email(),
111
+ name=author.name())
112
+ author_name = '<a href="%s">%s</a>' % (href, author.name())
109
113
  else:
110
114
  author_name = author.name()
111
115
  result += """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mkdocs-git-authors-plugin
3
- Version: 0.7.2
3
+ Version: 0.8.0
4
4
  Summary: Mkdocs plugin to display git authors of a page
5
5
  Home-page: https://github.com/timvink/mkdocs-git-authors-plugin
6
6
  Author: Tim Vink
@@ -10,10 +10,10 @@ Keywords: mkdocs git contributors committers authors plugin
10
10
  Classifier: Operating System :: OS Independent
11
11
  Classifier: Programming Language :: Python
12
12
  Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.7
14
13
  Classifier: Programming Language :: Python :: 3.8
15
14
  Classifier: Programming Language :: Python :: 3.9
16
15
  Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3 :: Only
18
18
  Classifier: License :: OSI Approved :: MIT License
19
19
  Classifier: Topic :: Documentation
@@ -21,6 +21,7 @@ Classifier: Topic :: Text Processing
21
21
  Requires-Python: >=3.7
22
22
  Description-Content-Type: text/markdown
23
23
  License-File: LICENSE
24
+ Requires-Dist: mkdocs>=1.0
24
25
 
25
26
  [![Actions Status](https://github.com/timvink/mkdocs-git-authors-plugin/workflows/pytest/badge.svg)](https://github.com/timvink/mkdocs-git-authors-plugin/actions)
26
27
  ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/mkdocs-git-authors-plugin)
@@ -17,4 +17,6 @@ mkdocs_git_authors_plugin/git/author.py
17
17
  mkdocs_git_authors_plugin/git/command.py
18
18
  mkdocs_git_authors_plugin/git/commit.py
19
19
  mkdocs_git_authors_plugin/git/page.py
20
- mkdocs_git_authors_plugin/git/repo.py
20
+ mkdocs_git_authors_plugin/git/repo.py
21
+ tests/test_basic.py
22
+ tests/test_util.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setup(
7
7
  name="mkdocs-git-authors-plugin",
8
- version="0.7.2",
8
+ version="0.8.0",
9
9
  description="Mkdocs plugin to display git authors of a page",
10
10
  long_description=long_description,
11
11
  long_description_content_type="text/markdown",
@@ -19,10 +19,10 @@ setup(
19
19
  "Operating System :: OS Independent",
20
20
  'Programming Language :: Python',
21
21
  'Programming Language :: Python :: 3',
22
- 'Programming Language :: Python :: 3.7',
23
22
  'Programming Language :: Python :: 3.8',
24
23
  'Programming Language :: Python :: 3.9',
25
24
  'Programming Language :: Python :: 3.10',
25
+ 'Programming Language :: Python :: 3.11',
26
26
  'Programming Language :: Python :: 3 :: Only',
27
27
  "License :: OSI Approved :: MIT License",
28
28
  'Topic :: Documentation',
@@ -0,0 +1,307 @@
1
+ """
2
+ Note that pytest offers a `tmp_path`.
3
+ You can reproduce locally with
4
+
5
+ ```python
6
+ %load_ext autoreload
7
+ %autoreload 2
8
+ import os
9
+ import tempfile
10
+ import shutil
11
+ from pathlib import Path
12
+ tmp_path = Path(tempfile.gettempdir()) / 'pytest-retrieve-authors'
13
+ if os.path.exists(tmp_path):
14
+ shutil.rmtree(tmp_path)
15
+ os.mkdir(tmp_path)
16
+ ```
17
+ """
18
+
19
+ import re
20
+ import shutil
21
+ import os
22
+ import pytest
23
+
24
+ from click.testing import CliRunner
25
+ from mkdocs.__main__ import build_command
26
+ from git import Repo
27
+ import git as gitpython
28
+ from contextlib import contextmanager
29
+
30
+ SITES_THAT_SHOULD_SUCCEED = [
31
+ "mkdocs.yml",
32
+ "mkdocs_custom_href.yml",
33
+ "mkdocs_complete_material_disabled.yml",
34
+ "mkdocs_complete_material.yml",
35
+ "mkdocs_exclude.yml",
36
+ "mkdocs_fallback.yml",
37
+ "mkdocs_genfiles.yml",
38
+ "mkdocs_no_email.yml",
39
+ "mkdocs_w_contribution_and_author_threshold.yml",
40
+ "mkdocs_w_contribution_sort_and_author_threshold.yml",
41
+ "mkdocs_w_contribution.yml",
42
+ "mkdocs_w_macros.yml",
43
+ "mkdocs_w_macros2.yml",
44
+ ]
45
+
46
+ @contextmanager
47
+ def working_directory(path):
48
+ """
49
+ Temporarily change working directory.
50
+ A context manager which changes the working directory to the given
51
+ path, and then changes it back to its previous value on exit.
52
+ Usage:
53
+ ```python
54
+ # Do something in original directory
55
+ with working_directory('/my/new/path'):
56
+ # Do something in new directory
57
+ # Back to old directory
58
+ ```
59
+ """
60
+ prev_cwd = os.getcwd()
61
+ os.chdir(path)
62
+ try:
63
+ yield
64
+ finally:
65
+ os.chdir(prev_cwd)
66
+
67
+
68
+ def build_docs_setup(mkdocs_path, output_path):
69
+ runner = CliRunner()
70
+ return runner.invoke(
71
+ build_command, ["--config-file", mkdocs_path, "--site-dir", str(output_path)]
72
+ )
73
+
74
+ @pytest.mark.parametrize(
75
+ "mkdocs_file",
76
+ SITES_THAT_SHOULD_SUCCEED,
77
+ )
78
+ def test_basic_working(tmp_path, mkdocs_file):
79
+ """
80
+ combination with mkdocs-macros-plugin lead to error.
81
+ See https://github.com/timvink/mkdocs-git-authors-plugin/issues/60
82
+ """
83
+ result = build_docs_setup("tests/basic_setup/mkdocs.yml", tmp_path)
84
+ assert result.exit_code == 0, (
85
+ "'mkdocs build' command failed. Error: %s" % result.stdout
86
+ )
87
+
88
+ index_file = tmp_path / "index.html"
89
+ assert index_file.exists(), "%s does not exist" % index_file
90
+
91
+ contents = index_file.read_text()
92
+ assert re.search("<span class='git-page-authors", contents)
93
+ assert re.search('<a href="mailto:vinktim@gmail.com">Tim Vink</a>', contents)
94
+
95
+
96
+ def test_custom_href(tmp_path):
97
+ """
98
+ """
99
+ result = build_docs_setup("tests/basic_setup/mkdocs_custom_href.yml", tmp_path)
100
+ assert result.exit_code == 0, (
101
+ "'mkdocs build' command failed. Error: %s" % result.stdout
102
+ )
103
+
104
+ index_file = tmp_path / "index.html"
105
+ assert index_file.exists(), "%s does not exist" % index_file
106
+
107
+ contents = index_file.read_text()
108
+ assert re.search("<span class='git-page-authors", contents)
109
+ # Checking Page Authors
110
+ assert re.search((r"<p>Page authors:.*<a href='https://teams.microsoft.com/l/chat/0/0\?"
111
+ "users=vinktim@gmail.com'>Tim Vink</a>.*<\/p>"), contents)
112
+ # Checking Site Authors
113
+ assert re.search(('<li><a href="https://teams.microsoft.com/l/chat/0/0\?'
114
+ 'users=vinktim@gmail.com">Tim Vink</a><\/li>'), contents)
115
+
116
+
117
+
118
+ def test_no_email(tmp_path):
119
+
120
+ result = build_docs_setup("tests/basic_setup/mkdocs_no_email.yml", tmp_path)
121
+ assert result.exit_code == 0, (
122
+ "'mkdocs build' command failed. Error: %s" % result.stdout
123
+ )
124
+
125
+ index_file = tmp_path / "index.html"
126
+ assert index_file.exists(), "%s does not exist" % index_file
127
+
128
+ contents = index_file.read_text()
129
+ assert re.search("<span class='git-page-authors", contents)
130
+ assert re.search("<li>Tim Vink</li>", contents)
131
+
132
+
133
+
134
+ def test_exclude_working(tmp_path):
135
+
136
+ result = build_docs_setup("tests/basic_setup/mkdocs_exclude.yml", tmp_path)
137
+ assert result.exit_code == 0, (
138
+ "'mkdocs build' command failed. Error: %s" % result.stdout
139
+ )
140
+
141
+ page_file = tmp_path / "page_with_tag/index.html"
142
+ assert page_file.exists(), "%s does not exist" % page_file
143
+
144
+ contents = page_file.read_text()
145
+ assert not re.search("<span class='git-page-authors", contents)
146
+
147
+
148
+
149
+ def test_exclude_working_with_genfiles(tmp_path):
150
+ """
151
+ A warning for uncommited files should not show up
152
+ when those uncommited files are excluded.
153
+ """
154
+
155
+ testproject_path = tmp_path / "testproject_genfiles"
156
+
157
+ shutil.copytree(
158
+ "tests/basic_setup/docs", str(testproject_path / "docs")
159
+ )
160
+ shutil.copyfile(
161
+ "tests/basic_setup/mkdocs_genfiles.yml",
162
+ str(testproject_path / "mkdocs.yml"),
163
+ )
164
+
165
+ with working_directory(str(testproject_path)):
166
+ # setup git history
167
+ repo = Repo.init(testproject_path)
168
+ assert not repo.bare
169
+ repo.git.add(all=True)
170
+ repo.index.commit("first commit")
171
+
172
+ # generate a file manually, do not commit
173
+ with open(testproject_path / "docs" / "manually_created.md", "w") as f:
174
+ f.write("Hello, world!")
175
+
176
+ # generate another file manually, do not commit
177
+ another_path = testproject_path / "docs" / "somefolder"
178
+ os.mkdir(another_path)
179
+ with open(another_path / "manually_created_infolder.md", "w") as f:
180
+ f.write("Hello, world!")
181
+
182
+ # mkdocs build
183
+ # mkdocs.yml has exclusions for the created files
184
+ result = build_docs_setup(
185
+ str(testproject_path / "mkdocs.yml"), str(testproject_path / "site")
186
+ )
187
+ assert result.exit_code == 0, (
188
+ "'mkdocs build' command failed. Error: %s" % result.stdout
189
+ )
190
+
191
+ # files generated ourselves right before build but not committed, should not generate warnings
192
+ assert "manually_created.md has not been committed yet." not in result.stdout
193
+ assert "manually_created_infolder.md has not been committed yet." not in result.stdout
194
+
195
+
196
+
197
+ def test_enabled_working(tmp_path):
198
+
199
+ result = build_docs_setup("tests/basic_setup/mkdocs_complete_material_disabled.yml", tmp_path)
200
+ assert result.exit_code == 0, (
201
+ "'mkdocs build' command failed. Error: %s" % result.stdout
202
+ )
203
+
204
+ page_file = tmp_path / "page_with_tag/index.html"
205
+ assert page_file.exists(), "%s does not exist" % page_file
206
+
207
+ contents = page_file.read_text()
208
+ assert not re.search("<span class='git-page-authors", contents)
209
+
210
+
211
+
212
+ def test_project_with_no_commits(tmp_path):
213
+ """
214
+ Structure:
215
+
216
+ tmp_path/testproject
217
+ website/
218
+ ├── docs/
219
+ └── mkdocs.yml"""
220
+ testproject_path = tmp_path / "testproject"
221
+
222
+ shutil.copytree(
223
+ "tests/basic_setup/docs", str(testproject_path / "website" / "docs")
224
+ )
225
+ shutil.copyfile(
226
+ "tests/basic_setup/mkdocs_w_contribution.yml",
227
+ str(testproject_path / "website" / "mkdocs.yml"),
228
+ )
229
+
230
+ with working_directory(str(testproject_path)):
231
+ # run 'git init'
232
+ gitpython.Repo.init(testproject_path, bare=False)
233
+
234
+ result = build_docs_setup(
235
+ str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
236
+ )
237
+ assert result.exit_code == 0, (
238
+ "'mkdocs build' command failed. Error: %s" % result.stdout
239
+ )
240
+
241
+
242
+
243
+
244
+ def test_building_empty_site(tmp_path):
245
+ """
246
+ Structure:
247
+
248
+ ```
249
+ tmp_path/testproject
250
+ website/
251
+ ├── docs/
252
+ └── mkdocs.yml
253
+ ````
254
+ """
255
+ testproject_path = tmp_path / "testproject"
256
+
257
+ shutil.copytree(
258
+ "tests/basic_setup/empty_site", str(testproject_path / "website" / "docs")
259
+ )
260
+ shutil.copyfile(
261
+ "tests/basic_setup/mkdocs_w_contribution.yml",
262
+ str(testproject_path / "website" / "mkdocs.yml"),
263
+ )
264
+
265
+ with working_directory(str(testproject_path)):
266
+ # run 'git init'
267
+ gitpython.Repo.init(testproject_path, bare=False)
268
+
269
+ result = build_docs_setup(
270
+ str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
271
+ )
272
+ assert result.exit_code == 0, (
273
+ "'mkdocs build' command failed. Error: %s" % result.stdout
274
+ )
275
+
276
+
277
+
278
+ def test_fallback(tmp_path):
279
+ """
280
+ Structure:
281
+
282
+ ```
283
+ tmp_path/testproject
284
+ website/
285
+ ├── docs/
286
+ └── mkdocs.yml
287
+ ````
288
+ """
289
+ testproject_path = tmp_path / "testproject"
290
+
291
+ shutil.copytree(
292
+ "tests/basic_setup/docs", str(testproject_path / "website" / "docs")
293
+ )
294
+ shutil.copyfile(
295
+ "tests/basic_setup/mkdocs_fallback.yml",
296
+ str(testproject_path / "website" / "mkdocs.yml"),
297
+ )
298
+
299
+ with working_directory(str(testproject_path)):
300
+
301
+ result = build_docs_setup(
302
+ str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
303
+ )
304
+ # import pdb; pdb.set_trace()
305
+ assert result.exit_code == 0, (
306
+ "'mkdocs build' command failed. Error: %s" % result.stdout
307
+ )
@@ -0,0 +1,370 @@
1
+ """
2
+ Note that pytest offers a `tmp_path`.
3
+ You can reproduce locally with
4
+
5
+ ```python
6
+ %load_ext autoreload
7
+ %autoreload 2
8
+ import os
9
+ import tempfile
10
+ import shutil
11
+ from pathlib import Path
12
+ tmp_path = Path(tempfile.gettempdir()) / 'pytest-retrieve-authors'
13
+ if os.path.exists(tmp_path):
14
+ shutil.rmtree(tmp_path)
15
+ os.mkdir(tmp_path)
16
+ ```
17
+ """
18
+
19
+ import os
20
+ import shutil
21
+ import logging
22
+
23
+ from mkdocs_git_authors_plugin import util
24
+ from mkdocs_git_authors_plugin.git import repo
25
+
26
+ # GitPython
27
+ import git as gitpython
28
+
29
+ DEFAULT_CONFIG = {
30
+ "show_contribution": False,
31
+ "show_line_count": False,
32
+ "show_email_address": True,
33
+ "count_empty_lines": True,
34
+ "sort_authors_by_name": True,
35
+ "sort_reverse": False,
36
+ "sort_authors_by": "name",
37
+ "authorship_threshold_percent": 0,
38
+ }
39
+
40
+ #### Helpers ####
41
+
42
+
43
+ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
44
+ """
45
+ Sets up a clean mkdocs directory
46
+
47
+ outputpath/testproject
48
+ ├── docs/
49
+ └── mkdocs.yml
50
+
51
+ Args:
52
+ mkdocs_yml_path (Path): Path of mkdocs.yml file to use
53
+ output_path (Path): Path of folder in which to create mkdocs project
54
+
55
+ Returns:
56
+ testproject_path (Path): Path to test project
57
+ """
58
+
59
+ testproject_path = output_path / "testproject"
60
+
61
+ # Create empty 'testproject' folder
62
+ if os.path.exists(testproject_path):
63
+ logging.warning(
64
+ """This command does not work on windows.
65
+ Refactor your test to use setup_clean_mkdocs_folder() only once"""
66
+ )
67
+ shutil.rmtree(testproject_path)
68
+
69
+ # Copy correct mkdocs.yml file and our test 'docs/'
70
+ shutil.copytree("tests/basic_setup/docs", testproject_path / "docs")
71
+ shutil.copyfile(mkdocs_yml_path, testproject_path / "mkdocs.yml")
72
+
73
+ return testproject_path
74
+
75
+
76
+ def setup_commit_history(testproject_path):
77
+ """
78
+ Initializes and creates a git commit history
79
+ in a new mkdocs testproject.
80
+
81
+ We commit the pages one by one in order
82
+ to create some git depth.
83
+
84
+ Args:
85
+ testproject_path (Path): Path to test project
86
+
87
+ Returns:
88
+ repo (repo): git.Repo object
89
+ """
90
+ assert not os.path.exists(testproject_path / ".git")
91
+
92
+ repo = gitpython.Repo.init(testproject_path, bare=False)
93
+ author = "Test Person <testtest@gmail.com>"
94
+
95
+ # Change the working directory
96
+ cwd = os.getcwd()
97
+ os.chdir(str(testproject_path))
98
+
99
+ try:
100
+ repo.git.add("mkdocs.yml")
101
+ repo.git.commit(message="add mkdocs", author=author)
102
+
103
+ repo.git.add("docs/first_page.md")
104
+ repo.git.commit(message="first page", author=author)
105
+ file_name = testproject_path / "docs/first_page.md"
106
+ with open(file_name, "w+") as the_file:
107
+ the_file.write("Hello\n")
108
+ repo.git.add("docs/first_page.md")
109
+ repo.git.commit(message="first page update 1", author=author)
110
+ with open(file_name, "w") as the_file:
111
+ the_file.write("# First Test Page Edited\n\nSome Lorem text")
112
+ repo.git.add("docs/first_page.md")
113
+ repo.git.commit(message="first page update 2", author=author)
114
+
115
+ repo.git.add("docs/second_page.md")
116
+ repo.git.commit(message="second page", author=author)
117
+ repo.git.add("docs/index.md")
118
+ repo.git.commit(message="homepage", author=author)
119
+ repo.git.add("docs/page_with_tag.md")
120
+ repo.git.commit(message="homepage", author=author)
121
+ os.chdir(str(cwd))
122
+ except:
123
+ os.chdir(str(cwd))
124
+ raise
125
+
126
+ os.chdir(cwd)
127
+ return repo
128
+
129
+
130
+ #### Tests ####
131
+
132
+
133
+ def test_empty_file(tmp_path):
134
+
135
+ # Change working directory
136
+ cwd = os.getcwd()
137
+ os.chdir(str(tmp_path))
138
+
139
+ # Create empty file
140
+ file_name = str(tmp_path / "new-file")
141
+ open(file_name, "a").close()
142
+
143
+ # Get authors of empty, uncommitted file
144
+ r = gitpython.Repo.init(tmp_path)
145
+
146
+ repo_instance = repo.Repo()
147
+ repo_instance.set_config(DEFAULT_CONFIG)
148
+ # TODO: should throw an error?
149
+ repo_instance.page(file_name)
150
+
151
+ authors = repo_instance.get_authors()
152
+ assert authors == []
153
+
154
+ # Get authors of empty but committed file
155
+ r.index.add([file_name])
156
+ author = gitpython.Actor("Tim", "abc@abc.com")
157
+ r.index.commit("initial commit", author=author)
158
+
159
+ repo_instance.page(file_name)
160
+ authors = repo_instance.get_authors()
161
+ assert authors == []
162
+
163
+ os.chdir(cwd)
164
+ ## TODO
165
+ # When the first instance of a commit on a page is skipped as an empty line,
166
+ # the second instance will not have the commit metadata available
167
+
168
+
169
+ def test_retrieve_authors(tmp_path):
170
+ """
171
+ Builds a fake git project with some commits.
172
+
173
+ Args:
174
+ tmp_path (PosixPath): Directory of a tempdir
175
+ """
176
+ cwd = os.getcwd()
177
+ os.chdir(str(tmp_path))
178
+
179
+ # Create file
180
+ file_name = str(tmp_path / "new-file")
181
+ with open(file_name, "w") as the_file:
182
+ the_file.write("Hello\n")
183
+
184
+ # Create git repo and commit file
185
+ r = gitpython.Repo.init(tmp_path)
186
+ r.index.add([file_name])
187
+ author = gitpython.Actor("Tim", "abc@abc.com")
188
+ r.index.commit("initial commit", author=author)
189
+
190
+ # Test retrieving author
191
+ repo_instance = repo.Repo()
192
+ repo_instance.set_config(DEFAULT_CONFIG)
193
+ repo_instance.page(file_name)
194
+
195
+ authors = repo_instance.get_authors()
196
+ assert len(authors) == 1
197
+ # We don't want to test datetime
198
+ authors = util.page_authors(authors, file_name)
199
+ authors[0]["last_datetime"] = None
200
+
201
+ assert authors == [
202
+ {
203
+ "name": "Tim",
204
+ "email": "abc@abc.com",
205
+ "last_datetime": None,
206
+ "lines": 1,
207
+ "lines_all_pages": 1,
208
+ "contribution": "100.0%",
209
+ "contribution_all_pages": "100.0%",
210
+ }
211
+ ]
212
+
213
+ # Now add a line to the file
214
+ # From a second author with same email
215
+ with open(file_name, "a+") as the_file:
216
+ the_file.write("World\n")
217
+ r.index.add([file_name])
218
+ author = gitpython.Actor("Tim2", "abc@abc.com")
219
+ r.index.commit("another commit", author=author)
220
+
221
+ repo_instance = repo.Repo()
222
+ repo_instance.set_config(DEFAULT_CONFIG)
223
+ repo_instance.page(file_name)
224
+ authors = repo_instance.get_authors()
225
+ authors = util.page_authors(authors, file_name)
226
+ authors[0]["last_datetime"] = None
227
+
228
+ assert authors == [
229
+ {
230
+ "name": "Tim",
231
+ "email": "abc@abc.com",
232
+ "last_datetime": None,
233
+ "lines": 2,
234
+ "lines_all_pages": 2,
235
+ "contribution": "100.0%",
236
+ "contribution_all_pages": "100.0%",
237
+ }
238
+ ]
239
+
240
+ # Then a third commit from a new author
241
+ with open(file_name, "a+") as the_file:
242
+ the_file.write("A new line\n")
243
+ r.index.add([file_name])
244
+ author = gitpython.Actor("John", "john@abc.com")
245
+ r.index.commit("third commit", author=author)
246
+
247
+ repo_instance = repo.Repo()
248
+ repo_instance.set_config(DEFAULT_CONFIG)
249
+ repo_instance.page(file_name)
250
+ authors = repo_instance.get_authors()
251
+ authors = util.page_authors(authors, file_name)
252
+ authors[0]["last_datetime"] = None
253
+ authors[1]["last_datetime"] = None
254
+
255
+ assert authors == [
256
+ {
257
+ "name": "John",
258
+ "email": "john@abc.com",
259
+ "last_datetime": None,
260
+ "lines": 1,
261
+ "lines_all_pages": 1,
262
+ "contribution": "33.33%",
263
+ "contribution_all_pages": "33.33%",
264
+ },
265
+ {
266
+ "name": "Tim",
267
+ "email": "abc@abc.com",
268
+ "last_datetime": None,
269
+ "lines": 2,
270
+ "lines_all_pages": 2,
271
+ "contribution": "66.67%",
272
+ "contribution_all_pages": "66.67%",
273
+ },
274
+ ]
275
+ os.chdir(cwd)
276
+
277
+
278
+ def test_mkdocs_in_git_subdir(tmp_path):
279
+ """
280
+ Sometimes `mkdocs.yml` is not in the root of the repo.
281
+ We need to make sure things still work in this edge case.
282
+
283
+ tmp_path/testproject
284
+ website/
285
+ ├── docs/
286
+ └── mkdocs.yml
287
+ """
288
+ testproject_path = tmp_path / "testproject"
289
+
290
+ shutil.copytree(
291
+ "tests/basic_setup/docs", str(testproject_path / "website" / "docs")
292
+ )
293
+ shutil.copyfile(
294
+ "tests/basic_setup/mkdocs.yml", str(testproject_path / "website" / "mkdocs.yml")
295
+ )
296
+
297
+ cwd = os.getcwd()
298
+ os.chdir(str(testproject_path))
299
+
300
+ # Create file
301
+ file_name = str(testproject_path / "website" / "new-file")
302
+ with open(file_name, "w") as the_file:
303
+ the_file.write("Hello\n")
304
+
305
+ # Create git repo and commit file
306
+ r = gitpython.Repo.init(testproject_path)
307
+ r.index.add([file_name])
308
+ author = gitpython.Actor("Tim", "abc@abc.com")
309
+ r.index.commit("initial commit", author=author)
310
+
311
+ # Test retrieving author
312
+ repo_instance = repo.Repo()
313
+ repo_instance.set_config(DEFAULT_CONFIG)
314
+ repo_instance.page(file_name)
315
+
316
+ authors = repo_instance.get_authors()
317
+ assert len(authors) == 1
318
+ # We don't want to test datetime
319
+ authors = util.page_authors(authors, file_name)
320
+ authors[0]["last_datetime"] = None
321
+
322
+ assert authors == [
323
+ {
324
+ "name": "Tim",
325
+ "email": "abc@abc.com",
326
+ "last_datetime": None,
327
+ "lines": 1,
328
+ "lines_all_pages": 1,
329
+ "contribution": "100.0%",
330
+ "contribution_all_pages": "100.0%",
331
+ }
332
+ ]
333
+
334
+ os.chdir(cwd)
335
+
336
+ def test_summarize_authors():
337
+ """
338
+ Test summary functions.
339
+ TODO
340
+ """
341
+ pass
342
+ # authors = [
343
+ # {'name' : 'Tim',
344
+ # 'email' : 'abc@abc.com',
345
+ # 'contribution' : '64.23%'
346
+ # }
347
+ # ]
348
+
349
+ # # Default case: don't show contribution
350
+ # config = { 'show_contribution' : False }
351
+ # summary = util.Util().summarize(authors, config)
352
+ # assert summary == "<span class='git-authors'><a href='mailto:abc@abc.com'>Tim</a></span>"
353
+
354
+ # # Do show contribution,
355
+ # # but hide it because there's only one author
356
+ # config = { 'show_contribution' : True }
357
+ # summary = util.Util().summarize(authors, config)
358
+ # assert summary == "<span class='git-authors'><a href='mailto:abc@abc.com'>Tim</a></span>"
359
+
360
+ # # Add another author
361
+ # authors.append({
362
+ # 'name' : 'Tom',
363
+ # 'email' : 'efg@efg.org',
364
+ # 'contribution' : '35.77%'
365
+ # })
366
+ # # Now contribution is displayed
367
+ # summary = util.Util().summarize(authors, config)
368
+ # assert summary == "<span class='git-authors'><a href='mailto:abc@abc.com'>Tim</a> (64.23%), <a href='mailto:efg@efg.org'>Tom</a> (35.77%)</span>"
369
+
370
+ # TODO: test authors threshold with commits