Skip to content

Commit 6e47986

Browse files
committed
Infra: add tests
1 parent e0d4ae5 commit 6e47986

File tree

19 files changed

+537
-3
lines changed

19 files changed

+537
-3
lines changed

.github/workflows/test.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Test
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: [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: tox tests
44+
run: |
45+
tox -e py
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

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ 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+
args: [--target-version=py39]
50+
files: "pep_sphinx_extensions/tests/.*"
51+
52+
- repo: https://github.com/PyCQA/isort
53+
rev: 5.10.1
54+
hooks:
55+
- id: isort
56+
args: [--profile=black]
57+
files: "pep_sphinx_extensions/tests/.*"
58+
59+
- repo: https://github.com/tox-dev/tox-ini-fmt
60+
rev: 0.5.2
61+
hooks:
62+
- id: tox-ini-fmt
63+
4564
# RST checks
4665
- repo: https://github.com/pre-commit/pygrep-hooks
4766
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 -m pytest --cov pep_sphinx_extensions --cov-report html --cov-report term --cov-report xml
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ 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
207+
f"{url} not a link to a recognized domain to prettify") from error
208208
item_name = item_name.title().replace("Sig", "SIG")
209209
return item_name, item_type
210210

pep_sphinx_extensions/pep_zero_generator/author.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ def parse_author_email(author_email_tuple: tuple[str, str], authors_overrides: d
3535

3636
if name_parts.surname[1] == ".":
3737
# Add an escape to avoid docutils turning `v.` into `22.`.
38-
name_parts.surname = f"\\{name_parts.surname}"
38+
name_parts = _Name(
39+
mononym=name_parts.mononym,
40+
forename=name_parts.forename,
41+
surname=f"\\{name_parts.surname}",
42+
suffix=name_parts.suffix,
43+
)
3944

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

66-
if num_parts == 0:
71+
if name_parts == [""]:
6772
raise ValueError("Name is empty!")
6873
elif num_parts == 1:
6974
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,38 @@
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 (
10+
str(out) == "<paragraph>Source: "
11+
'<reference refuri="https://github.com/python/peps/blob/main/pep-0008.txt">'
12+
"https://github.com/python/peps/blob/main/pep-0008.txt</reference>"
13+
"</paragraph>"
14+
)
15+
16+
17+
def test_add_commit_history_info():
18+
out = pep_footer._add_commit_history_info(Path("pep-0008.txt"))
19+
20+
assert str(out).startswith(
21+
"<paragraph>Last modified: "
22+
'<reference refuri="https://github.com/python/peps/commits/main/pep-0008.txt">'
23+
)
24+
# A variable timestamp comes next, don't test that
25+
assert str(out).endswith("</reference></paragraph>")
26+
27+
28+
def test_add_commit_history_info_invalid():
29+
out = pep_footer._add_commit_history_info(Path("pep-not-found.txt"))
30+
31+
assert str(out) == "<paragraph/>"
32+
33+
34+
def test_get_last_modified_timestamps():
35+
out = pep_footer._get_last_modified_timestamps()
36+
37+
assert len(out) >= 585
38+
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,70 @@
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+
("Bob v. Smith", "[email protected]"),
42+
author.Author(
43+
last_first="\\v. Smith, Bob",
44+
nick="\\v. Smith",
45+
46+
),
47+
),
48+
(
49+
("Mariatta", "[email protected]"),
50+
author.Author(
51+
last_first="Mariatta", nick="Mariatta", email="[email protected]"
52+
),
53+
),
54+
(
55+
("First Last Jr.", "[email protected]"),
56+
author.Author(
57+
last_first="Last, First, Jr.", nick="Last", email="[email protected]"
58+
),
59+
),
60+
],
61+
)
62+
def test_parse_author_email(test_input, expected):
63+
out = author.parse_author_email(test_input, AUTHORS_OVERRIDES)
64+
65+
assert out == expected
66+
67+
68+
def test_parse_author_email_empty_name():
69+
with pytest.raises(ValueError, match="Name is empty!"):
70+
author.parse_author_email(("", "[email protected]"), AUTHORS_OVERRIDES)

0 commit comments

Comments
 (0)