mkdocs-git-authors-plugin 0.7.2__tar.gz → 0.9.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.9.0}/LICENSE +1 -1
  2. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/PKG-INFO +3 -2
  3. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/git/page.py +10 -3
  4. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/plugin.py +30 -4
  5. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/util.py +6 -2
  6. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin.egg-info/PKG-INFO +3 -2
  7. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin.egg-info/SOURCES.txt +3 -1
  8. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/setup.py +2 -2
  9. mkdocs_git_authors_plugin-0.9.0/tests/test_basic.py +324 -0
  10. mkdocs_git_authors_plugin-0.9.0/tests/test_util.py +513 -0
  11. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/README.md +0 -0
  12. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/__init__.py +0 -0
  13. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/ci.py +0 -0
  14. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/exclude.py +0 -0
  15. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/git/__init__.py +0 -0
  16. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/git/author.py +0 -0
  17. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/git/command.py +0 -0
  18. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/git/commit.py +0 -0
  19. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.0}/mkdocs_git_authors_plugin/git/repo.py +0 -0
  20. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.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.9.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.9.0}/mkdocs_git_authors_plugin.egg-info/requires.txt +0 -0
  23. {mkdocs-git-authors-plugin-0.7.2 → mkdocs_git_authors_plugin-0.9.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.9.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.9.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
 
@@ -146,7 +146,13 @@ class Page(AbstractRepoObject):
146
146
 
147
147
  re_sha = re.compile(r"^\w{40}")
148
148
 
149
- cmd = GitCommand("blame", ["--porcelain", str(self._path)])
149
+ args = []
150
+ if self.repo().config("ignore_commits"):
151
+ args.append("--ignore-revs-file")
152
+ args.append(self.repo().config("ignore_commits"))
153
+ args.append("--porcelain")
154
+ args.append(str(self._path))
155
+ cmd = GitCommand("blame", args)
150
156
  cmd.run()
151
157
 
152
158
  lines = cmd.stdout()
@@ -155,6 +161,7 @@ class Page(AbstractRepoObject):
155
161
  if len(lines) == 0:
156
162
  raise GitCommandError
157
163
 
164
+ ignore_authors = self.repo().config("ignore_authors")
158
165
  commit_data = {}
159
166
  for line in lines:
160
167
  key = line.split(" ")[0]
@@ -183,7 +190,7 @@ class Page(AbstractRepoObject):
183
190
  author_tz=commit_data.get("author-tz"),
184
191
  summary=commit_data.get("summary"),
185
192
  )
186
- if len(line) > 1 or self.repo().config("count_empty_lines"):
193
+ if commit.author().email() not in ignore_authors and (len(line) > 1 or self.repo().config("count_empty_lines")):
187
194
  author = commit.author()
188
195
  if author not in self._authors:
189
196
  self._authors.append(author)
@@ -17,10 +17,14 @@ 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=[])),
24
+ ("ignore_commits", config_options.Type(str, default=None)),
25
+ ("ignore_authors", config_options.Type(list, default=[])),
23
26
  ("enabled", config_options.Type(bool, default=True)),
27
+ ("enabled_on_serve", config_options.Type(bool, default=True)),
24
28
  ("sort_authors_by", config_options.Type(str, default="name")),
25
29
  ("authorship_threshold_percent", config_options.Type(int, default=0)),
26
30
  ("strict", config_options.Type(bool, default=True)),
@@ -31,6 +35,10 @@ class GitAuthorsPlugin(BasePlugin):
31
35
  def __init__(self):
32
36
  self._repo = None
33
37
  self._fallback = False
38
+ self.is_serve = False
39
+
40
+ def on_startup(self, command, dirty):
41
+ self.is_serve = command == "serve"
34
42
 
35
43
  def on_config(self, config, **kwargs):
36
44
  """
@@ -51,7 +59,8 @@ class GitAuthorsPlugin(BasePlugin):
51
59
  Returns:
52
60
  (updated) configuration object
53
61
  """
54
- if not self.config.get("enabled"):
62
+
63
+ if not self._is_enabled():
55
64
  return config
56
65
 
57
66
  assert self.config["authorship_threshold_percent"] >= 0
@@ -99,7 +108,7 @@ class GitAuthorsPlugin(BasePlugin):
99
108
  Returns:
100
109
  global files collection
101
110
  """
102
- if not self.config.get("enabled"):
111
+ if not self._is_enabled():
103
112
  return
104
113
  if self._fallback:
105
114
  return
@@ -138,7 +147,7 @@ class GitAuthorsPlugin(BasePlugin):
138
147
  Returns:
139
148
  str: HTML text of page as string
140
149
  """
141
- if not self.config.get("enabled"):
150
+ if not self._is_enabled():
142
151
  return html
143
152
 
144
153
  # Exclude pages specified in config
@@ -195,7 +204,7 @@ class GitAuthorsPlugin(BasePlugin):
195
204
  Returns:
196
205
  dict: template context variables
197
206
  """
198
- if not self.config.get("enabled"):
207
+ if not self._is_enabled():
199
208
  return context
200
209
  if self._fallback:
201
210
  return context
@@ -235,3 +244,20 @@ class GitAuthorsPlugin(BasePlugin):
235
244
  Reference to the Repo object of the current project.
236
245
  """
237
246
  return self._repo
247
+
248
+ def _is_enabled(self):
249
+ """
250
+ Consider this plugin to be disabled in the following two conditions:
251
+ * config.enabled is false
252
+ * config.enabled is true and
253
+ config.enabled_on_serve is false and
254
+ executed via `serve` command
255
+ """
256
+ is_enabled = True
257
+
258
+ if not self.config.get("enabled"):
259
+ is_enabled = False
260
+ elif self.is_serve and not self.config.get("enabled_on_serve"):
261
+ is_enabled = False
262
+
263
+ 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.9.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.9.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,324 @@
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_ignore_authors_working(tmp_path):
150
+
151
+ result = build_docs_setup("tests/basic_setup/mkdocs_ignore_authors.yml", tmp_path)
152
+ assert result.exit_code == 0, (
153
+ "'mkdocs build' command failed. Error: %s" % result.stdout
154
+ )
155
+
156
+ page_file = tmp_path / "page_with_tag/index.html"
157
+ assert page_file.exists(), "%s does not exist" % page_file
158
+
159
+ contents = page_file.read_text()
160
+ assert re.search("<span class='git-page-authors", contents)
161
+ assert re.search("<a href='mailto:vinktim@gmail.com'>Tim Vink</a>", contents)
162
+ assert not re.search("Julien", contents)
163
+
164
+
165
+
166
+ def test_exclude_working_with_genfiles(tmp_path):
167
+ """
168
+ A warning for uncommited files should not show up
169
+ when those uncommited files are excluded.
170
+ """
171
+
172
+ testproject_path = tmp_path / "testproject_genfiles"
173
+
174
+ shutil.copytree(
175
+ "tests/basic_setup/docs", str(testproject_path / "docs")
176
+ )
177
+ shutil.copyfile(
178
+ "tests/basic_setup/mkdocs_genfiles.yml",
179
+ str(testproject_path / "mkdocs.yml"),
180
+ )
181
+
182
+ with working_directory(str(testproject_path)):
183
+ # setup git history
184
+ repo = Repo.init(testproject_path)
185
+ assert not repo.bare
186
+ repo.git.add(all=True)
187
+ repo.index.commit("first commit")
188
+
189
+ # generate a file manually, do not commit
190
+ with open(testproject_path / "docs" / "manually_created.md", "w") as f:
191
+ f.write("Hello, world!")
192
+
193
+ # generate another file manually, do not commit
194
+ another_path = testproject_path / "docs" / "somefolder"
195
+ os.mkdir(another_path)
196
+ with open(another_path / "manually_created_infolder.md", "w") as f:
197
+ f.write("Hello, world!")
198
+
199
+ # mkdocs build
200
+ # mkdocs.yml has exclusions for the created files
201
+ result = build_docs_setup(
202
+ str(testproject_path / "mkdocs.yml"), str(testproject_path / "site")
203
+ )
204
+ assert result.exit_code == 0, (
205
+ "'mkdocs build' command failed. Error: %s" % result.stdout
206
+ )
207
+
208
+ # files generated ourselves right before build but not committed, should not generate warnings
209
+ assert "manually_created.md has not been committed yet." not in result.stdout
210
+ assert "manually_created_infolder.md has not been committed yet." not in result.stdout
211
+
212
+
213
+
214
+ def test_enabled_working(tmp_path):
215
+
216
+ result = build_docs_setup("tests/basic_setup/mkdocs_complete_material_disabled.yml", tmp_path)
217
+ assert result.exit_code == 0, (
218
+ "'mkdocs build' command failed. Error: %s" % result.stdout
219
+ )
220
+
221
+ page_file = tmp_path / "page_with_tag/index.html"
222
+ assert page_file.exists(), "%s does not exist" % page_file
223
+
224
+ contents = page_file.read_text()
225
+ assert not re.search("<span class='git-page-authors", contents)
226
+
227
+
228
+
229
+ def test_project_with_no_commits(tmp_path):
230
+ """
231
+ Structure:
232
+
233
+ tmp_path/testproject
234
+ website/
235
+ ├── docs/
236
+ └── mkdocs.yml"""
237
+ testproject_path = tmp_path / "testproject"
238
+
239
+ shutil.copytree(
240
+ "tests/basic_setup/docs", str(testproject_path / "website" / "docs")
241
+ )
242
+ shutil.copyfile(
243
+ "tests/basic_setup/mkdocs_w_contribution.yml",
244
+ str(testproject_path / "website" / "mkdocs.yml"),
245
+ )
246
+
247
+ with working_directory(str(testproject_path)):
248
+ # run 'git init'
249
+ gitpython.Repo.init(testproject_path, bare=False)
250
+
251
+ result = build_docs_setup(
252
+ str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
253
+ )
254
+ assert result.exit_code == 0, (
255
+ "'mkdocs build' command failed. Error: %s" % result.stdout
256
+ )
257
+
258
+
259
+
260
+
261
+ def test_building_empty_site(tmp_path):
262
+ """
263
+ Structure:
264
+
265
+ ```
266
+ tmp_path/testproject
267
+ website/
268
+ ├── docs/
269
+ └── mkdocs.yml
270
+ ````
271
+ """
272
+ testproject_path = tmp_path / "testproject"
273
+
274
+ shutil.copytree(
275
+ "tests/basic_setup/empty_site", str(testproject_path / "website" / "docs")
276
+ )
277
+ shutil.copyfile(
278
+ "tests/basic_setup/mkdocs_w_contribution.yml",
279
+ str(testproject_path / "website" / "mkdocs.yml"),
280
+ )
281
+
282
+ with working_directory(str(testproject_path)):
283
+ # run 'git init'
284
+ gitpython.Repo.init(testproject_path, bare=False)
285
+
286
+ result = build_docs_setup(
287
+ str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
288
+ )
289
+ assert result.exit_code == 0, (
290
+ "'mkdocs build' command failed. Error: %s" % result.stdout
291
+ )
292
+
293
+
294
+
295
+ def test_fallback(tmp_path):
296
+ """
297
+ Structure:
298
+
299
+ ```
300
+ tmp_path/testproject
301
+ website/
302
+ ├── docs/
303
+ └── mkdocs.yml
304
+ ````
305
+ """
306
+ testproject_path = tmp_path / "testproject"
307
+
308
+ shutil.copytree(
309
+ "tests/basic_setup/docs", str(testproject_path / "website" / "docs")
310
+ )
311
+ shutil.copyfile(
312
+ "tests/basic_setup/mkdocs_fallback.yml",
313
+ str(testproject_path / "website" / "mkdocs.yml"),
314
+ )
315
+
316
+ with working_directory(str(testproject_path)):
317
+
318
+ result = build_docs_setup(
319
+ str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
320
+ )
321
+ # import pdb; pdb.set_trace()
322
+ assert result.exit_code == 0, (
323
+ "'mkdocs build' command failed. Error: %s" % result.stdout
324
+ )
@@ -0,0 +1,513 @@
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
+ "ignore_authors": [],
39
+ }
40
+
41
+ #### Helpers ####
42
+
43
+
44
+ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
45
+ """
46
+ Sets up a clean mkdocs directory
47
+
48
+ outputpath/testproject
49
+ ├── docs/
50
+ └── mkdocs.yml
51
+
52
+ Args:
53
+ mkdocs_yml_path (Path): Path of mkdocs.yml file to use
54
+ output_path (Path): Path of folder in which to create mkdocs project
55
+
56
+ Returns:
57
+ testproject_path (Path): Path to test project
58
+ """
59
+
60
+ testproject_path = output_path / "testproject"
61
+
62
+ # Create empty 'testproject' folder
63
+ if os.path.exists(testproject_path):
64
+ logging.warning(
65
+ """This command does not work on windows.
66
+ Refactor your test to use setup_clean_mkdocs_folder() only once"""
67
+ )
68
+ shutil.rmtree(testproject_path)
69
+
70
+ # Copy correct mkdocs.yml file and our test 'docs/'
71
+ shutil.copytree("tests/basic_setup/docs", testproject_path / "docs")
72
+ shutil.copyfile(mkdocs_yml_path, testproject_path / "mkdocs.yml")
73
+
74
+ return testproject_path
75
+
76
+
77
+ def setup_commit_history(testproject_path):
78
+ """
79
+ Initializes and creates a git commit history
80
+ in a new mkdocs testproject.
81
+
82
+ We commit the pages one by one in order
83
+ to create some git depth.
84
+
85
+ Args:
86
+ testproject_path (Path): Path to test project
87
+
88
+ Returns:
89
+ repo (repo): git.Repo object
90
+ """
91
+ assert not os.path.exists(testproject_path / ".git")
92
+
93
+ repo = gitpython.Repo.init(testproject_path, bare=False)
94
+ author = "Test Person <testtest@gmail.com>"
95
+
96
+ # Change the working directory
97
+ cwd = os.getcwd()
98
+ os.chdir(str(testproject_path))
99
+
100
+ try:
101
+ repo.git.add("mkdocs.yml")
102
+ repo.git.commit(message="add mkdocs", author=author)
103
+
104
+ repo.git.add("docs/first_page.md")
105
+ repo.git.commit(message="first page", author=author)
106
+ file_name = testproject_path / "docs/first_page.md"
107
+ with open(file_name, "w+") as the_file:
108
+ the_file.write("Hello\n")
109
+ repo.git.add("docs/first_page.md")
110
+ repo.git.commit(message="first page update 1", author=author)
111
+ with open(file_name, "w") as the_file:
112
+ the_file.write("# First Test Page Edited\n\nSome Lorem text")
113
+ repo.git.add("docs/first_page.md")
114
+ repo.git.commit(message="first page update 2", author=author)
115
+
116
+ repo.git.add("docs/second_page.md")
117
+ repo.git.commit(message="second page", author=author)
118
+ repo.git.add("docs/index.md")
119
+ repo.git.commit(message="homepage", author=author)
120
+ repo.git.add("docs/page_with_tag.md")
121
+ repo.git.commit(message="homepage", author=author)
122
+ os.chdir(str(cwd))
123
+ except:
124
+ os.chdir(str(cwd))
125
+ raise
126
+
127
+ os.chdir(cwd)
128
+ return repo
129
+
130
+
131
+ #### Tests ####
132
+
133
+
134
+ def test_empty_file(tmp_path):
135
+
136
+ # Change working directory
137
+ cwd = os.getcwd()
138
+ os.chdir(str(tmp_path))
139
+
140
+ # Create empty file
141
+ file_name = str(tmp_path / "new-file")
142
+ open(file_name, "a").close()
143
+
144
+ # Get authors of empty, uncommitted file
145
+ r = gitpython.Repo.init(tmp_path)
146
+
147
+ repo_instance = repo.Repo()
148
+ repo_instance.set_config(DEFAULT_CONFIG)
149
+ # TODO: should throw an error?
150
+ repo_instance.page(file_name)
151
+
152
+ authors = repo_instance.get_authors()
153
+ assert authors == []
154
+
155
+ # Get authors of empty but committed file
156
+ r.index.add([file_name])
157
+ author = gitpython.Actor("Tim", "abc@abc.com")
158
+ r.index.commit("initial commit", author=author)
159
+
160
+ repo_instance.page(file_name)
161
+ authors = repo_instance.get_authors()
162
+ assert authors == []
163
+
164
+ os.chdir(cwd)
165
+ ## TODO
166
+ # When the first instance of a commit on a page is skipped as an empty line,
167
+ # the second instance will not have the commit metadata available
168
+
169
+
170
+ def test_retrieve_authors(tmp_path):
171
+ """
172
+ Builds a fake git project with some commits.
173
+
174
+ Args:
175
+ tmp_path (PosixPath): Directory of a tempdir
176
+ """
177
+ cwd = os.getcwd()
178
+ os.chdir(str(tmp_path))
179
+
180
+ # Create file
181
+ file_name = str(tmp_path / "new-file")
182
+ with open(file_name, "w") as the_file:
183
+ the_file.write("Hello\n")
184
+
185
+ # Create git repo and commit file
186
+ r = gitpython.Repo.init(tmp_path)
187
+ r.index.add([file_name])
188
+ author = gitpython.Actor("Tim", "abc@abc.com")
189
+ r.index.commit("initial commit", author=author)
190
+
191
+ # Test retrieving author
192
+ repo_instance = repo.Repo()
193
+ repo_instance.set_config(DEFAULT_CONFIG)
194
+ repo_instance.page(file_name)
195
+
196
+ authors = repo_instance.get_authors()
197
+ assert len(authors) == 1
198
+ # We don't want to test datetime
199
+ authors = util.page_authors(authors, file_name)
200
+ authors[0]["last_datetime"] = None
201
+
202
+ assert authors == [
203
+ {
204
+ "name": "Tim",
205
+ "email": "abc@abc.com",
206
+ "last_datetime": None,
207
+ "lines": 1,
208
+ "lines_all_pages": 1,
209
+ "contribution": "100.0%",
210
+ "contribution_all_pages": "100.0%",
211
+ }
212
+ ]
213
+
214
+ # Now add a line to the file
215
+ # From a second author with same email
216
+ with open(file_name, "a+") as the_file:
217
+ the_file.write("World\n")
218
+ r.index.add([file_name])
219
+ author = gitpython.Actor("Tim2", "abc@abc.com")
220
+ r.index.commit("another commit", author=author)
221
+
222
+ repo_instance = repo.Repo()
223
+ repo_instance.set_config(DEFAULT_CONFIG)
224
+ repo_instance.page(file_name)
225
+ authors = repo_instance.get_authors()
226
+ authors = util.page_authors(authors, file_name)
227
+ authors[0]["last_datetime"] = None
228
+
229
+ assert authors == [
230
+ {
231
+ "name": "Tim",
232
+ "email": "abc@abc.com",
233
+ "last_datetime": None,
234
+ "lines": 2,
235
+ "lines_all_pages": 2,
236
+ "contribution": "100.0%",
237
+ "contribution_all_pages": "100.0%",
238
+ }
239
+ ]
240
+
241
+ # Then a third commit from a new author
242
+ with open(file_name, "a+") as the_file:
243
+ the_file.write("A new line\n")
244
+ r.index.add([file_name])
245
+ author = gitpython.Actor("John", "john@abc.com")
246
+ r.index.commit("third commit", author=author)
247
+
248
+ repo_instance = repo.Repo()
249
+ repo_instance.set_config(DEFAULT_CONFIG)
250
+ repo_instance.page(file_name)
251
+ authors = repo_instance.get_authors()
252
+ authors = util.page_authors(authors, file_name)
253
+ authors[0]["last_datetime"] = None
254
+ authors[1]["last_datetime"] = None
255
+
256
+ assert authors == [
257
+ {
258
+ "name": "John",
259
+ "email": "john@abc.com",
260
+ "last_datetime": None,
261
+ "lines": 1,
262
+ "lines_all_pages": 1,
263
+ "contribution": "33.33%",
264
+ "contribution_all_pages": "33.33%",
265
+ },
266
+ {
267
+ "name": "Tim",
268
+ "email": "abc@abc.com",
269
+ "last_datetime": None,
270
+ "lines": 2,
271
+ "lines_all_pages": 2,
272
+ "contribution": "66.67%",
273
+ "contribution_all_pages": "66.67%",
274
+ },
275
+ ]
276
+ os.chdir(cwd)
277
+
278
+
279
+ def test_retrieve_authors_ignoring_commits(tmp_path):
280
+ """
281
+ Builds a fake git project with some commits.
282
+
283
+ Args:
284
+ tmp_path (PosixPath): Directory of a tempdir
285
+ """
286
+ cwd = os.getcwd()
287
+ os.chdir(str(tmp_path))
288
+
289
+ # Create file
290
+ file_name = str(tmp_path / "new-file")
291
+ with open(file_name, "w") as the_file:
292
+ the_file.write("line 1\n")
293
+ the_file.write("line 2\n")
294
+
295
+ # Create git repo and commit file
296
+ r = gitpython.Repo.init(tmp_path)
297
+ r.index.add([file_name])
298
+ author = gitpython.Actor("Tim", "abc@abc.com")
299
+ r.index.commit("initial commit", author=author)
300
+
301
+ # Update the file
302
+ with open(file_name, "w") as the_file:
303
+ the_file.write("line 1.1\n")
304
+ the_file.write("line 2.1\n")
305
+ r.index.add([file_name])
306
+ author = gitpython.Actor("John", "john@abc.com")
307
+ commit = r.index.commit("second commit", author=author)
308
+
309
+ repo_instance = repo.Repo()
310
+ repo_instance.set_config(DEFAULT_CONFIG)
311
+ repo_instance.page(file_name)
312
+ authors = repo_instance.get_authors()
313
+ authors = util.page_authors(authors, file_name)
314
+ authors[0]["last_datetime"] = None
315
+
316
+ assert authors == [
317
+ {
318
+ "name": "John",
319
+ "email": "john@abc.com",
320
+ "last_datetime": None,
321
+ "lines": 2,
322
+ "lines_all_pages": 2,
323
+ "contribution": "100.0%",
324
+ "contribution_all_pages": "100.0%",
325
+ }
326
+ ]
327
+
328
+ # Get the authors while ignoring the last commit
329
+ ignored_commits_files = str(tmp_path / "ignored_commits.txt")
330
+ with open(ignored_commits_files, "w") as the_file:
331
+ the_file.write(commit.hexsha + "\n")
332
+ repo_instance = repo.Repo()
333
+ config = DEFAULT_CONFIG.copy()
334
+ config['ignore_commits'] = ignored_commits_files
335
+ repo_instance.set_config(config)
336
+ repo_instance.page(file_name)
337
+ authors = repo_instance.get_authors()
338
+ authors = util.page_authors(authors, file_name)
339
+ authors[0]["last_datetime"] = None
340
+
341
+ assert authors == [
342
+ {
343
+ "name": "Tim",
344
+ "email": "abc@abc.com",
345
+ "last_datetime": None,
346
+ "lines": 2,
347
+ "lines_all_pages": 2,
348
+ "contribution": "100.0%",
349
+ "contribution_all_pages": "100.0%",
350
+ },
351
+ ]
352
+
353
+ os.chdir(cwd)
354
+
355
+
356
+ def test_retrieve_authors_ignoring_emails(tmp_path):
357
+ """
358
+ Builds a fake git project with some commits.
359
+
360
+ Args:
361
+ tmp_path (PosixPath): Directory of a tempdir
362
+ """
363
+ cwd = os.getcwd()
364
+ os.chdir(str(tmp_path))
365
+
366
+ # Create file
367
+ file_name = str(tmp_path / "new-file")
368
+ with open(file_name, "w") as the_file:
369
+ the_file.write("line 1\n")
370
+ the_file.write("line 2\n")
371
+
372
+ # Create git repo and commit file
373
+ r = gitpython.Repo.init(tmp_path)
374
+ r.index.add([file_name])
375
+ author = gitpython.Actor("Tim", "abc@abc.com")
376
+ r.index.commit("initial commit", author=author)
377
+
378
+ # Add more content
379
+ with open(file_name, "a+") as the_file:
380
+ the_file.write("line 3\n")
381
+ the_file.write("line 4\n")
382
+ r.index.add([file_name])
383
+ author = gitpython.Actor("John", "john@abc.com")
384
+ r.index.commit("second commit", author=author)
385
+
386
+ # Get the authors while ignoring john@abc.com user
387
+ repo_instance = repo.Repo()
388
+ config = DEFAULT_CONFIG.copy()
389
+ config['ignore_authors'] = ['john@abc.com']
390
+ repo_instance.set_config(config)
391
+ repo_instance.page(file_name)
392
+ authors = repo_instance.get_authors()
393
+ authors = util.page_authors(authors, file_name)
394
+ authors[0]["last_datetime"] = None
395
+ authors[1]["last_datetime"] = None
396
+
397
+ assert authors == [
398
+ {
399
+ "contribution": "0.0%",
400
+ "contribution_all_pages": "0.0%",
401
+ "email": "john@abc.com",
402
+ "last_datetime": None,
403
+ "lines": 0,
404
+ "lines_all_pages": 0,
405
+ "name": "John"
406
+ },
407
+ {
408
+ "name": "Tim",
409
+ "email": "abc@abc.com",
410
+ "last_datetime": None,
411
+ "lines": 2,
412
+ "lines_all_pages": 2,
413
+ "contribution": "100.0%",
414
+ "contribution_all_pages": "100.0%",
415
+ },
416
+ ]
417
+
418
+ os.chdir(cwd)
419
+
420
+
421
+ def test_mkdocs_in_git_subdir(tmp_path):
422
+ """
423
+ Sometimes `mkdocs.yml` is not in the root of the repo.
424
+ We need to make sure things still work in this edge case.
425
+
426
+ tmp_path/testproject
427
+ website/
428
+ ├── docs/
429
+ └── mkdocs.yml
430
+ """
431
+ testproject_path = tmp_path / "testproject"
432
+
433
+ shutil.copytree(
434
+ "tests/basic_setup/docs", str(testproject_path / "website" / "docs")
435
+ )
436
+ shutil.copyfile(
437
+ "tests/basic_setup/mkdocs.yml", str(testproject_path / "website" / "mkdocs.yml")
438
+ )
439
+
440
+ cwd = os.getcwd()
441
+ os.chdir(str(testproject_path))
442
+
443
+ # Create file
444
+ file_name = str(testproject_path / "website" / "new-file")
445
+ with open(file_name, "w") as the_file:
446
+ the_file.write("Hello\n")
447
+
448
+ # Create git repo and commit file
449
+ r = gitpython.Repo.init(testproject_path)
450
+ r.index.add([file_name])
451
+ author = gitpython.Actor("Tim", "abc@abc.com")
452
+ r.index.commit("initial commit", author=author)
453
+
454
+ # Test retrieving author
455
+ repo_instance = repo.Repo()
456
+ repo_instance.set_config(DEFAULT_CONFIG)
457
+ repo_instance.page(file_name)
458
+
459
+ authors = repo_instance.get_authors()
460
+ assert len(authors) == 1
461
+ # We don't want to test datetime
462
+ authors = util.page_authors(authors, file_name)
463
+ authors[0]["last_datetime"] = None
464
+
465
+ assert authors == [
466
+ {
467
+ "name": "Tim",
468
+ "email": "abc@abc.com",
469
+ "last_datetime": None,
470
+ "lines": 1,
471
+ "lines_all_pages": 1,
472
+ "contribution": "100.0%",
473
+ "contribution_all_pages": "100.0%",
474
+ }
475
+ ]
476
+
477
+ os.chdir(cwd)
478
+
479
+ def test_summarize_authors():
480
+ """
481
+ Test summary functions.
482
+ TODO
483
+ """
484
+ pass
485
+ # authors = [
486
+ # {'name' : 'Tim',
487
+ # 'email' : 'abc@abc.com',
488
+ # 'contribution' : '64.23%'
489
+ # }
490
+ # ]
491
+
492
+ # # Default case: don't show contribution
493
+ # config = { 'show_contribution' : False }
494
+ # summary = util.Util().summarize(authors, config)
495
+ # assert summary == "<span class='git-authors'><a href='mailto:abc@abc.com'>Tim</a></span>"
496
+
497
+ # # Do show contribution,
498
+ # # but hide it because there's only one author
499
+ # config = { 'show_contribution' : True }
500
+ # summary = util.Util().summarize(authors, config)
501
+ # assert summary == "<span class='git-authors'><a href='mailto:abc@abc.com'>Tim</a></span>"
502
+
503
+ # # Add another author
504
+ # authors.append({
505
+ # 'name' : 'Tom',
506
+ # 'email' : 'efg@efg.org',
507
+ # 'contribution' : '35.77%'
508
+ # })
509
+ # # Now contribution is displayed
510
+ # summary = util.Util().summarize(authors, config)
511
+ # 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>"
512
+
513
+ # TODO: test authors threshold with commits