From 188a35fac7537658345235e7e3311666cabaf5eb Mon Sep 17 00:00:00 2001 From: Roberto Prevato Date: Tue, 4 Oct 2022 18:13:04 +0200 Subject: [PATCH 1/7] Fix two bugs related to name email handling in contribs plugin --- neoteroi/contribs/__init__.py | 3 ++- neoteroi/contribs/git.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/neoteroi/contribs/__init__.py b/neoteroi/contribs/__init__.py index 83f5e8c..7752c9f 100644 --- a/neoteroi/contribs/__init__.py +++ b/neoteroi/contribs/__init__.py @@ -125,10 +125,11 @@ def _set_contributors(self, markdown: str, page: Page) -> str: def on_page_markdown(self, markdown, *args, **kwargs): try: markdown = self._set_contributors(markdown, kwargs["page"]) - except ValueError: + except ValueError as value_error: logger.error( "Failed to display contributors list for page: %s", kwargs["page"].title, + exc_info=value_error ) pass return markdown diff --git a/neoteroi/contribs/git.py b/neoteroi/contribs/git.py index fd8eb37..6ae9adc 100644 --- a/neoteroi/contribs/git.py +++ b/neoteroi/contribs/git.py @@ -11,7 +11,7 @@ class GitContributionsReader(ContributionsReader): - _name_email_rx = re.compile(r"(?P[\w\s]+)\s<(?P[^\>]+)>") + _name_email_rx = re.compile(r"(?P[^\<]+)<(?P[^\>]+)>") def _decode(self, value: bytes) -> str: try: @@ -22,10 +22,10 @@ def _decode(self, value: bytes) -> str: def _parse_name_and_email(self, name_and_email) -> Tuple[str, str]: match = self._name_email_rx.search(name_and_email) if match: - name = match.groupdict()["name"] - email = match.groupdict()["email"] + name = match.groupdict()["name"].strip() + email = match.groupdict()["email"].strip() else: - name, email = "" + name, email = ("", "") return name, email def parse_committers(self, output: str) -> Iterable[Contributor]: From 662b06f76968a6eca5fdd6efae3023961297c830 Mon Sep 17 00:00:00 2001 From: Charlie Brown Date: Tue, 4 Oct 2022 20:57:17 +0200 Subject: [PATCH 2/7] Add example commit --- docs/rest/contribs-01.html | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/rest/contribs-01.html diff --git a/docs/rest/contribs-01.html b/docs/rest/contribs-01.html new file mode 100644 index 0000000..2965834 --- /dev/null +++ b/docs/rest/contribs-01.html @@ -0,0 +1 @@ +# Hello World From 625503010c7c6b4bfb15dc3c2db853027f3a99e0 Mon Sep 17 00:00:00 2001 From: "Sally Brown (SB)" Date: Tue, 4 Oct 2022 20:57:57 +0200 Subject: [PATCH 3/7] Add example commit 2 --- docs/rest/contribs-01.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/rest/contribs-01.html b/docs/rest/contribs-01.html index 2965834..d72ece2 100644 --- a/docs/rest/contribs-01.html +++ b/docs/rest/contribs-01.html @@ -1 +1,3 @@ # Hello World + +Some sentence. From ecb9ca0cc0066ddd495a164be0fa51652be2fb75 Mon Sep 17 00:00:00 2001 From: Charlie Brown Date: Tue, 4 Oct 2022 21:00:50 +0200 Subject: [PATCH 4/7] Add example commit --- docs/res/contribs-01.html | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/res/contribs-01.html diff --git a/docs/res/contribs-01.html b/docs/res/contribs-01.html new file mode 100644 index 0000000..2965834 --- /dev/null +++ b/docs/res/contribs-01.html @@ -0,0 +1 @@ +# Hello World From 958f8c7b726143d3cf65cb16bcdd5caf3a062237 Mon Sep 17 00:00:00 2001 From: "Sally Brown (SB)" Date: Tue, 4 Oct 2022 21:01:05 +0200 Subject: [PATCH 5/7] Add example commit 2 --- docs/res/contribs-01.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/res/contribs-01.html b/docs/res/contribs-01.html index 2965834..d72ece2 100644 --- a/docs/res/contribs-01.html +++ b/docs/res/contribs-01.html @@ -1 +1,3 @@ # Hello World + +Some sentence. From 6f5bcc676d4c6db918aed9b18a9ab197bbd5d301 Mon Sep 17 00:00:00 2001 From: Roberto Prevato Date: Tue, 4 Oct 2022 21:44:04 +0200 Subject: [PATCH 6/7] More improvements for contribs --- .gitignore | 2 + CHANGELOG.md | 6 +- docs/rest/contribs-01.html | 3 - neoteroi/contribs/__init__.py | 5 +- setup.py | 2 +- tests/test_contribs.py | 108 ++++++++++++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 7 deletions(-) delete mode 100644 docs/rest/contribs-01.html diff --git a/.gitignore b/.gitignore index 82ccece..fefb65d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ styles/*.css styles/*.css.map __*.html __file_out.py + +docs/res/contribs-new.html diff --git a/CHANGELOG.md b/CHANGELOG.md index c210a7b..c66b7ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.1.2] - 2022-10-04 +- Corrects the pattern handling name and email for the `contribs` plugin +- Adds tests for the `contribs` plugin + ## [0.1.1] - 2022-10-04 -- Corrects bug in the `contributors` plugin, causing failures in certain CI/CD +- Corrects bug in the `contribs` plugin, causing failures in certain CI/CD solutions and improves its safety and performance (removes `shell=True`!) - Adds option to apply a class to specific contributors, for simpler styling - Minor improvements to styles diff --git a/docs/rest/contribs-01.html b/docs/rest/contribs-01.html deleted file mode 100644 index d72ece2..0000000 --- a/docs/rest/contribs-01.html +++ /dev/null @@ -1,3 +0,0 @@ -# Hello World - -Some sentence. diff --git a/neoteroi/contribs/__init__.py b/neoteroi/contribs/__init__.py index 7752c9f..28a8549 100644 --- a/neoteroi/contribs/__init__.py +++ b/neoteroi/contribs/__init__.py @@ -9,6 +9,7 @@ import logging from datetime import datetime from pathlib import Path +from subprocess import CalledProcessError from typing import List, Optional from mkdocs.config import config_options as c @@ -125,11 +126,11 @@ def _set_contributors(self, markdown: str, page: Page) -> str: def on_page_markdown(self, markdown, *args, **kwargs): try: markdown = self._set_contributors(markdown, kwargs["page"]) - except ValueError as value_error: + except (CalledProcessError, ValueError) as operation_error: logger.error( "Failed to display contributors list for page: %s", kwargs["page"].title, - exc_info=value_error + exc_info=operation_error, ) pass return markdown diff --git a/setup.py b/setup.py index 1a18f8d..730ccf5 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def readme(): setup( name="neoteroi-mkdocs", - version="0.1.1", + version="0.1.2", description="Plugins for MkDocs and Python Markdown", long_description=readme(), long_description_content_type="text/markdown", diff --git a/tests/test_contribs.py b/tests/test_contribs.py index cbf5f5b..c315954 100644 --- a/tests/test_contribs.py +++ b/tests/test_contribs.py @@ -1,5 +1,10 @@ +import textwrap + import pytest +from mkdocs.structure.files import File +from mkdocs.structure.pages import Page +from neoteroi.contribs import ContribsPlugin from neoteroi.contribs.domain import Contributor from neoteroi.contribs.git import GitContributionsReader @@ -11,6 +16,10 @@ " 2\tRoberto Prevato \n", [Contributor("Roberto Prevato", "roberto.prevato@example.com", 2)], ], + [ + " 2\tRoberto Prevato (RP) \n", + [Contributor("Roberto Prevato (RP)", "roberto.prevato@example.com", 2)], + ], [ ( " 14\tRoberto Prevato \n" @@ -27,3 +36,102 @@ def test_parse_contributors(value, expected_result): reader = GitContributionsReader() contributors = list(reader.parse_committers(value)) assert contributors == expected_result + + +def _get_contribs_config(): + return { + "contributors_label": "Contributors", + "last_modified_label": "Last modified on", + "show_last_modified_time": True, + "show_contributors_title": False, + "time_format": "%Y-%m-%d %H:%M:%S", + } + + +def test_contribs_plugin_success(): + """ + Tests a successful scenario. + """ + handler = ContribsPlugin() + handler.config = _get_contribs_config() + + example = textwrap.dedent( + f""" + # Hello World! + + Lorem ipsum dolor sit amet. + + """.strip( + "\n" + ) + ) + + result = handler.on_page_markdown( + example, + page=Page( + "Example", + File( + path="res/contribs-01.html", + src_dir="tests/res", + dest_dir="tests", + use_directory_urls=True, + ), + {}, + ), + ) + + assert result is not None + assert ( + result + == """# Hello World! + +Lorem ipsum dolor sit amet. + +

Last modified on: 2022-10-04 21""" + + """:01:05

CB
SB
""" + ) + + +def test_contribs_plugin_new_file_ignore(): + """ + Tests the scenario of a new file that is created while developing and is not + committed, yet. + """ + handler = ContribsPlugin() + handler.config = _get_contribs_config() + + example = textwrap.dedent( + f""" + # Hello World! + + Lorem ipsum dolor sit amet. + + """.strip( + "\n" + ) + ) + + with open("docs/res/contribs-new.html", mode="wt", encoding="utf8") as new_file: + new_file.write(example) + + result = handler.on_page_markdown( + example, + page=Page( + "Example", + File( + path="res/contribs-new.html", + src_dir="tests/res", + dest_dir="tests", + use_directory_urls=True, + ), + {}, + ), + ) + + assert result is not None + assert result == example From 15c4e26110d0e725f3c93e4160aa1bbd156a1dd2 Mon Sep 17 00:00:00 2001 From: Roberto Prevato Date: Tue, 4 Oct 2022 22:08:59 +0200 Subject: [PATCH 7/7] Update test_contribs.py --- tests/test_contribs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_contribs.py b/tests/test_contribs.py index c315954..5393478 100644 --- a/tests/test_contribs.py +++ b/tests/test_contribs.py @@ -56,7 +56,7 @@ def test_contribs_plugin_success(): handler.config = _get_contribs_config() example = textwrap.dedent( - f""" + """ # Hello World! Lorem ipsum dolor sit amet. @@ -106,7 +106,7 @@ def test_contribs_plugin_new_file_ignore(): handler.config = _get_contribs_config() example = textwrap.dedent( - f""" + """ # Hello World! Lorem ipsum dolor sit amet.