Skip to content

Commit 0b0dd6d

Browse files
hugovkAA-TurnerCAM-Gerlach
authored
Add tests for pep_sphinx_extensions (#2545)
Co-authored-by: Adam Turner <[email protected]> Co-authored-by: CAM Gerlach <[email protected]>
1 parent 73d438c commit 0b0dd6d

21 files changed

+545
-8
lines changed

.github/workflows/render.yml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
- name: 👷‍ Install dependencies
2323
run: |
2424
python -m pip install --upgrade pip
25-
python -m pip install --upgrade -r requirements.txt
2625
2726
- name: 🔧 Render PEPs
2827
run: make pages -j$(nproc)

.github/workflows/test.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Test Sphinx Extensions
2+
3+
on:
4+
push:
5+
paths:
6+
- ".github/workflows/test.yml"
7+
- "pep_sphinx_extensions/**"
8+
- "tox.ini"
9+
pull_request:
10+
paths:
11+
- ".github/workflows/test.yml"
12+
- "pep_sphinx_extensions/**"
13+
- "tox.ini"
14+
workflow_dispatch:
15+
16+
env:
17+
FORCE_COLOR: 1
18+
19+
jobs:
20+
test:
21+
runs-on: ${{ matrix.os }}
22+
strategy:
23+
fail-fast: false
24+
matrix:
25+
python-version: ["3.9", "3.10"]
26+
os: [windows-latest, macos-latest, ubuntu-latest]
27+
28+
steps:
29+
- uses: actions/checkout@v3
30+
31+
- name: Set up Python ${{ matrix.python-version }}
32+
uses: actions/setup-python@v3
33+
with:
34+
python-version: ${{ matrix.python-version }}
35+
cache: pip
36+
37+
- name: Install dependencies
38+
run: |
39+
python -m pip install -U pip
40+
python -m pip install -U wheel
41+
python -m pip install -U tox
42+
43+
- name: Run tests with tox
44+
run: |
45+
tox -e py -- -v --cov-report term
46+
47+
- name: Upload coverage
48+
uses: codecov/codecov-action@v3
49+
with:
50+
flags: ${{ matrix.os }}
51+
name: ${{ matrix.os }} Python ${{ matrix.python-version }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ __pycache__
77
*.pyo
88
*~
99
*env
10+
.coverage
11+
.tox
1012
.vscode
1113
*.swp
1214
/build

.pre-commit-config.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ repos:
4242
- id: check-yaml
4343
name: "Check YAML"
4444

45+
- repo: https://github.com/psf/black
46+
rev: 22.3.0
47+
hooks:
48+
- id: black
49+
name: "Format with Black"
50+
args:
51+
- '--target-version=py39'
52+
- '--target-version=py310'
53+
files: 'pep_sphinx_extensions/tests/.*'
54+
55+
- repo: https://github.com/PyCQA/isort
56+
rev: 5.10.1
57+
hooks:
58+
- id: isort
59+
name: "Sort imports with isort"
60+
args: ['--profile=black', '--atomic']
61+
files: 'pep_sphinx_extensions/tests/.*'
62+
63+
- repo: https://github.com/tox-dev/tox-ini-fmt
64+
rev: 0.5.2
65+
hooks:
66+
- id: tox-ini-fmt
67+
name: "Format tox.ini"
68+
4569
# RST checks
4670
- repo: https://github.com/pre-commit/pygrep-hooks
4771
rev: v1.9.0

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ lint: venv
4141
$(VENVDIR)/bin/python3 -m pre_commit --version > /dev/null || $(VENVDIR)/bin/python3 -m pip install pre-commit
4242
$(VENVDIR)/bin/python3 -m pre_commit run --all-files
4343

44+
test: venv
45+
$(VENVDIR)/bin/python3 -bb -X dev -W error -m pytest
46+
4447
spellcheck: venv
4548
$(VENVDIR)/bin/python3 -m pre_commit --version > /dev/null || $(VENVDIR)/bin/python3 -m pip install pre-commit
4649
$(VENVDIR)/bin/python3 -m pre_commit run --all-files --hook-stage manual codespell

pep_sphinx_extensions/pep_processor/transforms/pep_headers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ def _process_pretty_url(url: str) -> tuple[str, str]:
204204
item_name, item_type = LINK_PRETTIFIERS[parts[2]](parts)
205205
except KeyError as error:
206206
raise ValueError(
207-
"{url} not a link to a recognized domain to prettify") from error
208-
item_name = item_name.title().replace("Sig", "SIG")
207+
f"{url} not a link to a recognized domain to prettify") from error
208+
item_name = item_name.title().replace("Sig", "SIG").replace("Pep", "PEP")
209209
return item_name, item_type
210210

211211

pep_sphinx_extensions/pep_zero_generator/author.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ def parse_author_email(author_email_tuple: tuple[str, str], authors_overrides: d
3333
if name_parts.mononym is not None:
3434
return Author(name_parts.mononym, name_parts.mononym, email)
3535

36-
if name_parts.surname[1] == ".":
37-
# Add an escape to avoid docutils turning `v.` into `22.`.
38-
name_parts.surname = f"\\{name_parts.surname}"
39-
4036
if name_parts.suffix:
4137
last_first = f"{name_parts.surname}, {name_parts.forename}, {name_parts.suffix}"
4238
return Author(last_first, name_parts.surname, email)
@@ -63,7 +59,7 @@ def _parse_name(full_name: str) -> _Name:
6359
num_parts = len(name_parts)
6460
suffix = raw_suffix.strip()
6561

66-
if num_parts == 0:
62+
if name_parts == [""]:
6763
raise ValueError("Name is empty!")
6864
elif num_parts == 1:
6965
return _Name(mononym=name_parts[0], suffix=suffix)

pep_sphinx_extensions/tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from pathlib import Path
2+
3+
from pep_sphinx_extensions.pep_processor.transforms import pep_footer
4+
5+
6+
def test_add_source_link():
7+
out = pep_footer._add_source_link(Path("pep-0008.txt"))
8+
9+
assert "https://github.com/python/peps/blob/main/pep-0008.txt" in str(out)
10+
11+
12+
def test_add_commit_history_info():
13+
out = pep_footer._add_commit_history_info(Path("pep-0008.txt"))
14+
15+
assert str(out).startswith(
16+
"<paragraph>Last modified: "
17+
'<reference refuri="https://github.com/python/peps/commits/main/pep-0008.txt">'
18+
)
19+
# A variable timestamp comes next, don't test that
20+
assert str(out).endswith("</reference></paragraph>")
21+
22+
23+
def test_add_commit_history_info_invalid():
24+
out = pep_footer._add_commit_history_info(Path("pep-not-found.txt"))
25+
26+
assert str(out) == "<paragraph/>"
27+
28+
29+
def test_get_last_modified_timestamps():
30+
out = pep_footer._get_last_modified_timestamps()
31+
32+
assert len(out) >= 585
33+
# Should be a Unix timestamp and at least this
34+
assert out["pep-0008.txt"] >= 1643124055
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import pytest
2+
3+
from pep_sphinx_extensions.pep_processor.transforms import pep_headers
4+
5+
6+
@pytest.mark.parametrize(
7+
"test_input, expected",
8+
[
9+
10+
("[email protected]", "https://groups.google.com/g/python-tulip"),
11+
("[email protected]", "https://mail.python.org/mailman/listinfo/db-sig"),
12+
("[email protected]", "https://mail.python.org/pipermail/import-sig/"),
13+
(
14+
15+
"https://mail.python.org/archives/list/[email protected]/",
16+
),
17+
],
18+
)
19+
def test_generate_list_url(test_input, expected):
20+
out = pep_headers._generate_list_url(test_input)
21+
22+
assert out == expected
23+
24+
25+
@pytest.mark.parametrize(
26+
"test_input, expected",
27+
[
28+
(
29+
"https://mail.python.org/pipermail/python-3000/2006-November/004190.html",
30+
("Python-3000", "message"),
31+
),
32+
(
33+
"https://mail.python.org/archives/list/[email protected]/thread/HW2NFOEMCVCTAFLBLC3V7MLM6ZNMKP42/",
34+
("Python-Dev", "thread"),
35+
),
36+
(
37+
"https://mail.python.org/mailman3/lists/capi-sig.python.org/",
38+
("Capi-SIG", "list"),
39+
),
40+
(
41+
"https://mail.python.org/mailman/listinfo/web-sig",
42+
("Web-SIG", "list"),
43+
),
44+
(
45+
"https://discuss.python.org/t/pep-643-metadata-for-package-source-distributions/5577",
46+
("Discourse", "thread"),
47+
),
48+
(
49+
"https://discuss.python.org/c/peps/",
50+
("PEPs Discourse", "category"),
51+
),
52+
],
53+
)
54+
def test_process_pretty_url(test_input, expected):
55+
out = pep_headers._process_pretty_url(test_input)
56+
57+
assert out == expected
58+
59+
60+
@pytest.mark.parametrize(
61+
"test_input, expected",
62+
[
63+
(
64+
"https://example.com/",
65+
"https://example.com/ not a link to a recognized domain to prettify",
66+
),
67+
(
68+
"https://mail.python.org",
69+
"https://mail.python.org not a link to a list, message or thread",
70+
),
71+
(
72+
"https://discuss.python.org/",
73+
"https://discuss.python.org not a link to a Discourse thread or category",
74+
),
75+
],
76+
)
77+
def test_process_pretty_url_invalid(test_input, expected):
78+
with pytest.raises(ValueError, match=expected):
79+
pep_headers._process_pretty_url(test_input)
80+
81+
82+
@pytest.mark.parametrize(
83+
"test_input, expected",
84+
[
85+
(
86+
"https://mail.python.org/pipermail/python-3000/2006-November/004190.html",
87+
"Python-3000 message",
88+
),
89+
(
90+
"https://mail.python.org/archives/list/[email protected]/thread/HW2NFOEMCVCTAFLBLC3V7MLM6ZNMKP42/",
91+
"Python-Dev thread",
92+
),
93+
(
94+
"https://mail.python.org/mailman3/lists/capi-sig.python.org/",
95+
"Capi-SIG list",
96+
),
97+
(
98+
"https://mail.python.org/mailman/listinfo/web-sig",
99+
"Web-SIG list",
100+
),
101+
(
102+
"https://discuss.python.org/t/pep-643-metadata-for-package-source-distributions/5577",
103+
"Discourse thread",
104+
),
105+
(
106+
"https://discuss.python.org/c/peps/",
107+
"PEPs Discourse category",
108+
),
109+
],
110+
)
111+
def test_make_link_pretty(test_input, expected):
112+
out = pep_headers._make_link_pretty(test_input)
113+
114+
assert out == expected
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
from docutils import nodes
3+
4+
from pep_sphinx_extensions.pep_processor.transforms import pep_zero
5+
6+
7+
@pytest.mark.parametrize(
8+
"test_input, expected",
9+
[
10+
(
11+
nodes.reference(
12+
"", text="[email protected]", refuri="mailto:[email protected]"
13+
),
14+
'<raw format="html" xml:space="preserve">user&#32;&#97;t&#32;example.com</raw>',
15+
),
16+
(
17+
nodes.reference("", text="Introduction", refid="introduction"),
18+
'<reference refid="introduction">Introduction</reference>',
19+
),
20+
],
21+
)
22+
def test_generate_list_url(test_input, expected):
23+
out = pep_zero._mask_email(test_input)
24+
25+
assert str(out) == expected
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import pytest
2+
3+
from pep_sphinx_extensions.pep_zero_generator import author
4+
from pep_sphinx_extensions.tests.utils import AUTHORS_OVERRIDES
5+
6+
7+
@pytest.mark.parametrize(
8+
"test_input, expected",
9+
[
10+
(
11+
("First Last", "[email protected]"),
12+
author.Author(
13+
last_first="Last, First", nick="Last", email="[email protected]"
14+
),
15+
),
16+
(
17+
("Guido van Rossum", "[email protected]"),
18+
author.Author(
19+
last_first="van Rossum, Guido (GvR)",
20+
nick="GvR",
21+
22+
),
23+
),
24+
(
25+
("Hugo van Kemenade", "[email protected]"),
26+
author.Author(
27+
last_first="van Kemenade, Hugo",
28+
nick="van Kemenade",
29+
30+
),
31+
),
32+
(
33+
("Eric N. Vander Weele", "[email protected]"),
34+
author.Author(
35+
last_first="Vander Weele, Eric N.",
36+
nick="Vander Weele",
37+
38+
),
39+
),
40+
(
41+
("Mariatta", "[email protected]"),
42+
author.Author(
43+
last_first="Mariatta", nick="Mariatta", email="[email protected]"
44+
),
45+
),
46+
(
47+
("First Last Jr.", "[email protected]"),
48+
author.Author(
49+
last_first="Last, First, Jr.", nick="Last", email="[email protected]"
50+
),
51+
),
52+
pytest.param(
53+
("First Last", "first at example.com"),
54+
author.Author(
55+
last_first="Last, First", nick="Last", email="[email protected]"
56+
),
57+
marks=pytest.mark.xfail,
58+
),
59+
],
60+
)
61+
def test_parse_author_email(test_input, expected):
62+
out = author.parse_author_email(test_input, AUTHORS_OVERRIDES)
63+
64+
assert out == expected
65+
66+
67+
def test_parse_author_email_empty_name():
68+
with pytest.raises(ValueError, match="Name is empty!"):
69+
author.parse_author_email(("", "[email protected]"), AUTHORS_OVERRIDES)

0 commit comments

Comments
 (0)