Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infra: add tests #2545

Merged
merged 15 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Test

on:
push:
paths:
- ".github/workflows/test.yml"
- "pep_sphinx_extensions/**"
- "tox.ini"
pull_request:
paths:
- ".github/workflows/test.yml"
- "pep_sphinx_extensions/**"
- "tox.ini"
workflow_dispatch:

env:
FORCE_COLOR: 1

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10"]
os: [macos-latest, ubuntu-latest]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test on multiple OSes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably just do Ubuntu?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to either only test on one platform, or test on Ubuntu, macOS and Windows. I'd prefer the latter, since these checks only run when we make build system changes and we may as well make sure things aren't broken on a specific platform (though it seems relatively unlikely).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's add Windows.


steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
cache: pip

- name: Install dependencies
run: |
python -m pip install -U pip
python -m pip install -U wheel
python -m pip install -U tox

- name: tox tests
run: |
tox -e py

- name: Upload coverage
uses: codecov/codecov-action@v3
with:
flags: ${{ matrix.os }}
name: ${{ matrix.os }} Python ${{ matrix.python-version }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ __pycache__
*.pyo
*~
*env
.coverage
.tox
.vscode
*.swp
/build
Expand Down
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ repos:
- id: check-yaml
name: "Check YAML"

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
args: [--target-version=py39]
files: "pep_sphinx_extensions/tests/.*"

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
args: [--profile=black]
files: "pep_sphinx_extensions/tests/.*"

- repo: https://github.com/tox-dev/tox-ini-fmt
rev: 0.5.2
hooks:
- id: tox-ini-fmt

# RST checks
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ lint: venv
$(VENVDIR)/bin/python3 -m pre_commit --version > /dev/null || $(VENVDIR)/bin/python3 -m pip install pre-commit
$(VENVDIR)/bin/python3 -m pre_commit run --all-files

test: venv
$(VENVDIR)/bin/python3 -m pytest --cov pep_sphinx_extensions --cov-report html --cov-report term --cov-report xml

spellcheck: venv
$(VENVDIR)/bin/python3 -m pre_commit --version > /dev/null || $(VENVDIR)/bin/python3 -m pip install pre-commit
$(VENVDIR)/bin/python3 -m pre_commit run --all-files --hook-stage manual codespell
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _process_pretty_url(url: str) -> tuple[str, str]:
item_name, item_type = LINK_PRETTIFIERS[parts[2]](parts)
except KeyError as error:
raise ValueError(
"{url} not a link to a recognized domain to prettify") from error
f"{url} not a link to a recognized domain to prettify") from error
item_name = item_name.title().replace("Sig", "SIG")
return item_name, item_type

Expand Down
9 changes: 7 additions & 2 deletions pep_sphinx_extensions/pep_zero_generator/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ def parse_author_email(author_email_tuple: tuple[str, str], authors_overrides: d

if name_parts.surname[1] == ".":
# Add an escape to avoid docutils turning `v.` into `22.`.
name_parts.surname = f"\\{name_parts.surname}"
name_parts = _Name(
mononym=name_parts.mononym,
forename=name_parts.forename,
surname=f"\\{name_parts.surname}",
suffix=name_parts.suffix,
)

if name_parts.suffix:
last_first = f"{name_parts.surname}, {name_parts.forename}, {name_parts.suffix}"
Expand Down Expand Up @@ -63,7 +68,7 @@ def _parse_name(full_name: str) -> _Name:
num_parts = len(name_parts)
suffix = raw_suffix.strip()

if num_parts == 0:
if name_parts == [""]:
raise ValueError("Name is empty!")
elif num_parts == 1:
return _Name(mononym=name_parts[0], suffix=suffix)
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from pathlib import Path

from pep_sphinx_extensions.pep_processor.transforms import pep_footer


def test_add_source_link():
out = pep_footer._add_source_link(Path("pep-0008.txt"))

assert (
str(out) == "<paragraph>Source: "
'<reference refuri="https://github.com/python/peps/blob/main/pep-0008.txt">'
"https://github.com/python/peps/blob/main/pep-0008.txt</reference>"
"</paragraph>"
)


def test_add_commit_history_info():
out = pep_footer._add_commit_history_info(Path("pep-0008.txt"))

assert str(out).startswith(
"<paragraph>Last modified: "
'<reference refuri="https://github.com/python/peps/commits/main/pep-0008.txt">'
)
# A variable timestamp comes next, don't test that
assert str(out).endswith("</reference></paragraph>")


def test_add_commit_history_info_invalid():
out = pep_footer._add_commit_history_info(Path("pep-not-found.txt"))

assert str(out) == "<paragraph/>"


def test_get_last_modified_timestamps():
out = pep_footer._get_last_modified_timestamps()

assert len(out) >= 585
assert out["pep-0008.txt"] >= 1643124055
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import pytest

from pep_sphinx_extensions.pep_processor.transforms import pep_headers


@pytest.mark.parametrize(
"test_input, expected",
[
("[email protected]", "[email protected]"),
("[email protected]", "https://groups.google.com/g/python-tulip"),
("[email protected]", "https://mail.python.org/mailman/listinfo/db-sig"),
("[email protected]", "https://mail.python.org/pipermail/import-sig/"),
(
"[email protected]",
"https://mail.python.org/archives/list/[email protected]/",
),
],
)
def test_generate_list_url(test_input, expected):
out = pep_headers._generate_list_url(test_input)

assert out == expected


@pytest.mark.parametrize(
"test_input, expected",
[
(
"https://mail.python.org/pipermail/python-3000/2006-November/004190.html",
("Python-3000", "message"),
),
(
"https://mail.python.org/archives/list/[email protected]/thread/HW2NFOEMCVCTAFLBLC3V7MLM6ZNMKP42/",
("Python-Dev", "thread"),
),
(
"https://mail.python.org/mailman3/lists/capi-sig.python.org/",
("Capi-SIG", "list"),
),
(
"https://mail.python.org/mailman/listinfo/web-sig",
("Web-SIG", "list"),
),
(
"https://discuss.python.org/t/pep-643-metadata-for-package-source-distributions/5577",
("Discourse", "thread"),
),
(
"https://discuss.python.org/c/peps/",
("Peps Discourse", "category"),
),
],
)
def test_process_pretty_url(test_input, expected):
out = pep_headers._process_pretty_url(test_input)

assert out == expected


@pytest.mark.parametrize(
"test_input, expected",
[
(
"https://example.com/",
"https://example.com/ not a link to a recognized domain to prettify",
),
(
"https://mail.python.org",
"https://mail.python.org not a link to a list, message or thread",
),
(
"https://discuss.python.org/",
"https://discuss.python.org not a link to a Discourse thread or category",
),
],
)
def test_process_pretty_url_invalid(test_input, expected):
with pytest.raises(ValueError, match=expected):
pep_headers._process_pretty_url(test_input)


@pytest.mark.parametrize(
"test_input, expected",
[
(
"https://mail.python.org/pipermail/python-3000/2006-November/004190.html",
"Python-3000 message",
),
(
"https://mail.python.org/archives/list/[email protected]/thread/HW2NFOEMCVCTAFLBLC3V7MLM6ZNMKP42/",
"Python-Dev thread",
),
(
"https://mail.python.org/mailman3/lists/capi-sig.python.org/",
"Capi-SIG list",
),
(
"https://mail.python.org/mailman/listinfo/web-sig",
"Web-SIG list",
),
(
"https://discuss.python.org/t/pep-643-metadata-for-package-source-distributions/5577",
"Discourse thread",
),
(
"https://discuss.python.org/c/peps/",
"Peps Discourse category",
),
],
)
def test_make_link_pretty(test_input, expected):
out = pep_headers._make_link_pretty(test_input)

assert out == expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest
from docutils import nodes

from pep_sphinx_extensions.pep_processor.transforms import pep_zero


@pytest.mark.parametrize(
"test_input, expected",
[
(
nodes.reference(
"", text="[email protected]", refuri="mailto:[email protected]"
),
'<raw format="html" xml:space="preserve">user&#32;&#97;t&#32;example.com</raw>',
),
(
nodes.reference("", text="Introduction", refid="introduction"),
'<reference refid="introduction">Introduction</reference>',
),
],
)
def test_generate_list_url(test_input, expected):
out = pep_zero._mask_email(test_input)

assert str(out) == expected
70 changes: 70 additions & 0 deletions pep_sphinx_extensions/tests/pep_zero_generator/test_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest

from pep_sphinx_extensions.pep_zero_generator import author
from pep_sphinx_extensions.tests.utils import AUTHORS_OVERRIDES


@pytest.mark.parametrize(
"test_input, expected",
[
(
("First Last", "[email protected]"),
author.Author(
last_first="Last, First", nick="Last", email="[email protected]"
),
),
(
("Guido van Rossum", "[email protected]"),
author.Author(
last_first="van Rossum, Guido (GvR)",
nick="GvR",
email="[email protected]",
),
),
(
("Hugo van Kemenade", "[email protected]"),
author.Author(
last_first="van Kemenade, Hugo",
nick="van Kemenade",
email="[email protected]",
),
),
(
("Eric N. Vander Weele", "[email protected]"),
author.Author(
last_first="Vander Weele, Eric N.",
nick="Vander Weele",
email="[email protected]",
),
),
(
("Bob v. Smith", "[email protected]"),
author.Author(
last_first="\\v. Smith, Bob",
nick="\\v. Smith",
email="[email protected]",
),
),
(
("Mariatta", "[email protected]"),
author.Author(
last_first="Mariatta", nick="Mariatta", email="[email protected]"
),
),
(
("First Last Jr.", "[email protected]"),
author.Author(
last_first="Last, First, Jr.", nick="Last", email="[email protected]"
),
),
],
)
def test_parse_author_email(test_input, expected):
out = author.parse_author_email(test_input, AUTHORS_OVERRIDES)

assert out == expected


def test_parse_author_email_empty_name():
with pytest.raises(ValueError, match="Name is empty!"):
author.parse_author_email(("", "[email protected]"), AUTHORS_OVERRIDES)
Loading