-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_catalog.py
More file actions
183 lines (139 loc) · 6.15 KB
/
Copy pathtest_catalog.py
File metadata and controls
183 lines (139 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from __future__ import annotations
from copy import deepcopy
from datetime import date
from pathlib import Path
import pytest
import yaml
from tools.catalog import (
CatalogLoadError,
canonical_hostname,
load_catalog,
normalize_url,
validate_catalog,
)
from tools.validate_catalog import run
def test_valid_catalog_passes(valid_catalog: dict) -> None:
assert validate_catalog(valid_catalog, today=date(2026, 8, 31)) == []
@pytest.mark.parametrize("language", ["en", "zh", "multilingual"])
def test_supported_language_values_pass(valid_catalog: dict, language: str) -> None:
data = deepcopy(valid_catalog)
data["resources"][0]["language"] = language
assert validate_catalog(data, today=date(2026, 8, 31)) == []
def test_loader_rejects_duplicate_yaml_keys(tmp_path) -> None:
catalog = tmp_path / "resources.yml"
catalog.write_text("catalog: {}\ncatalog: {}\nresources: []\n", encoding="utf-8")
with pytest.raises(CatalogLoadError, match="duplicate key"):
load_catalog(catalog)
def test_loader_rejects_non_string_mapping_keys(tmp_path) -> None:
catalog = tmp_path / "resources.yml"
catalog.write_text("? [catalog]\n: {}\nresources: []\n", encoding="utf-8")
with pytest.raises(CatalogLoadError, match="mapping keys must be strings"):
load_catalog(catalog)
def test_schema_duplicate_https_date_and_parity_errors(valid_catalog: dict) -> None:
data = deepcopy(valid_catalog)
data["resources"][0]["url"] = "http://example.invalid/docs"
data["resources"][1]["url"] = "https://example.invalid/docs/"
data["resources"][2]["url"] = "https://example.invalid/docs"
data["resources"][2]["reviewed_on"] = "2025-01-01"
data["resources"][3]["why_zh"] = ""
issues = validate_catalog(data, today=date(2026, 8, 31), max_review_age_days=366)
codes = {issue.code for issue in issues}
assert {"https-required", "duplicate-url", "stale-review", "invalid-text"} <= codes
def test_resource_ids_and_urls_reject_unsafe_forms(valid_catalog: dict) -> None:
data = deepcopy(valid_catalog)
data["resources"][0]["id"] = "Not A Slug"
data["resources"][0]["url"] = "https://user:secret@example.com/docs"
codes = {
issue.code for issue in validate_catalog(data, today=date(2026, 8, 31))
}
assert {"invalid-id", "url-credentials"} <= codes
def test_path_orders_must_be_consecutive(valid_catalog: dict) -> None:
data = deepcopy(valid_catalog)
data["catalog"]["paths"][-1]["order"] = 5
codes = {
issue.code for issue in validate_catalog(data, today=date(2026, 8, 31))
}
assert "order-parity" in codes
def test_resource_orders_must_be_unique_and_consecutive(valid_catalog: dict) -> None:
data = deepcopy(valid_catalog)
duplicate = deepcopy(data["resources"][0])
duplicate["id"] = "another-foundation-resource"
duplicate["url"] = "https://another.example.com/docs/"
data["resources"].append(duplicate)
codes = {
issue.code for issue in validate_catalog(data, today=date(2026, 8, 31))
}
assert {"duplicate-resource-order", "resource-order-parity"} <= codes
def test_directory_loader_composes_catalog_sources(
tmp_path: Path, valid_catalog: dict
) -> None:
catalog_dir = tmp_path / "catalog"
resources_dir = catalog_dir / "resources"
resources_dir.mkdir(parents=True)
metadata = {
"reviewed_on": valid_catalog["catalog"]["reviewed_on"],
"status": valid_catalog["catalog"]["status"],
}
(catalog_dir / "catalog.yml").write_text(
yaml.safe_dump(metadata, sort_keys=False), encoding="utf-8"
)
(catalog_dir / "paths.yml").write_text(
yaml.safe_dump(valid_catalog["catalog"]["paths"], sort_keys=False),
encoding="utf-8",
)
for resource in reversed(valid_catalog["resources"]):
(resources_dir / f"{resource['id']}.yml").write_text(
yaml.safe_dump(resource, sort_keys=False), encoding="utf-8"
)
loaded = load_catalog(catalog_dir)
assert loaded == valid_catalog
def test_directory_loader_requires_resource_id_to_match_filename(
tmp_path: Path, valid_catalog: dict
) -> None:
catalog_dir = tmp_path / "catalog"
resources_dir = catalog_dir / "resources"
resources_dir.mkdir(parents=True)
(catalog_dir / "catalog.yml").write_text(
"reviewed_on: 2026-08-31\nstatus: active\n", encoding="utf-8"
)
(catalog_dir / "paths.yml").write_text("[]\n", encoding="utf-8")
(resources_dir / "wrong-name.yml").write_text(
yaml.safe_dump(valid_catalog["resources"][0], sort_keys=False),
encoding="utf-8",
)
with pytest.raises(CatalogLoadError, match="resource id must match filename"):
load_catalog(catalog_dir)
@pytest.mark.parametrize(
("url", "expected"),
[
(
"HTTPS://[2001:4860:4860::8888]:443/docs/",
"https://[2001:4860:4860::8888]/docs",
),
(
"https://[2001:4860:4860::8888]:8443/docs/",
"https://[2001:4860:4860::8888]:8443/docs",
),
],
)
def test_normalize_url_preserves_ipv6_brackets(url: str, expected: str) -> None:
assert normalize_url(url) == expected
def test_hostname_canonicalization_handles_idna_and_trailing_dot() -> None:
assert canonical_hostname("BÜCHER.example.") == "xn--bcher-kva.example"
def test_validator_detects_idna_equivalent_duplicate_urls(valid_catalog: dict) -> None:
data = deepcopy(valid_catalog)
data["resources"][0]["url"] = "https://bücher.example/docs/"
data["resources"][1]["url"] = "https://xn--bcher-kva.example/docs"
codes = {
issue.code for issue in validate_catalog(data, today=date(2026, 8, 31))
}
assert "duplicate-url" in codes
def test_validator_reports_non_string_mapping_keys(valid_catalog: dict) -> None:
data = deepcopy(valid_catalog)
data["catalog"][1] = "unexpected"
issues = validate_catalog(data, today=date(2026, 8, 31))
assert any(issue.code == "invalid-key" for issue in issues)
def test_validator_exit_code_for_invalid_catalog(tmp_path) -> None:
catalog = tmp_path / "resources.yml"
catalog.write_text("catalog: {}\nresources: []\n", encoding="utf-8")
assert run(["--catalog", str(catalog)]) == 1