Skip to content

Commit

Permalink
Merge pull request #20 from Neoteroi/v0.1.2
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
RobertoPrevato authored Oct 4, 2022
2 parents 4a2229f + 15c4e26 commit ebeec63
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ styles/*.css
styles/*.css.map
__*.html
__file_out.py

docs/res/contribs-new.html
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/res/contribs-01.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hello World

Some sentence.
4 changes: 3 additions & 1 deletion neoteroi/contribs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,10 +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:
except (CalledProcessError, ValueError) as operation_error:
logger.error(
"Failed to display contributors list for page: %s",
kwargs["page"].title,
exc_info=operation_error,
)
pass
return markdown
8 changes: 4 additions & 4 deletions neoteroi/contribs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class GitContributionsReader(ContributionsReader):

_name_email_rx = re.compile(r"(?P<name>[\w\s]+)\s<(?P<email>[^\>]+)>")
_name_email_rx = re.compile(r"(?P<name>[^\<]+)<(?P<email>[^\>]+)>")

def _decode(self, value: bytes) -> str:
try:
Expand All @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
108 changes: 108 additions & 0 deletions tests/test_contribs.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -11,6 +16,10 @@
" 2\tRoberto Prevato <[email protected]>\n",
[Contributor("Roberto Prevato", "[email protected]", 2)],
],
[
" 2\tRoberto Prevato (RP) <[email protected]>\n",
[Contributor("Roberto Prevato (RP)", "[email protected]", 2)],
],
[
(
" 14\tRoberto Prevato <[email protected]>\n"
Expand All @@ -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(
"""
# 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.
<div class="nt-contribs"><p class="nt-mod-time">Last modified on: 2022-10-04 21"""
+ """:01:05</p><div class="nt-contributors"><div class="nt-contributor """
+ """nt-group-0" title="Charlie Brown &lt;[email protected]&gt;"""
+ """ (1)"><span class="nt-initials">CB</span></div><div class="nt-cont"""
+ """ributor nt-group-1" title="Sally Brown (SB) &lt;sally.brown@exampl"""
+ """e.org&gt; (1)"><span class="nt-initials">SB</span></div></div></di"""
+ """v>"""
)


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(
"""
# 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

0 comments on commit ebeec63

Please sign in to comment.