Repository navigation
Expand file tree
/
Copy pathtest_catalog_resolve.py
More file actions
282 lines (245 loc) · 8.53 KB
/
Copy pathtest_catalog_resolve.py
File metadata and controls
282 lines (245 loc) · 8.53 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""Catalog slug resolution tests."""
from __future__ import annotations
import pytest
from create_awesome_python_app.catalog import (
CUSTOM_TEMPLATE_SENTINEL,
CatalogResolutionError,
IncompatibleExtensionsError,
build_extension_choices,
build_template_choices,
find_incompatible_pairs,
group_extension_choices,
is_url_like,
resolve_catalog_spec,
resolve_catalog_specs,
short_category_label,
validate_extension_compatibility,
)
SAMPLE_CATALOG = {
"templates": [
{
"slug": "fastapi-starter",
"url": "https://github.com/Create-Python-App/cpa-templates?subdir=templates/fastapi-starter",
}
],
"extensions": [
{
"slug": "github-setup",
"url": "https://github.com/Create-Python-App/cpa-templates?subdir=extensions/github-setup",
}
],
}
def test_is_url_like() -> None:
assert is_url_like("https://github.com/org/repo")
assert is_url_like("file:///tmp/foo")
assert is_url_like("git@github.com:org/repo.git")
assert not is_url_like("fastapi-starter")
def test_resolve_template_slug() -> None:
url = resolve_catalog_spec("fastapi-starter", catalog=SAMPLE_CATALOG)
assert "cpa-templates" in url
assert "fastapi-starter" in url
def test_resolve_extension_slug() -> None:
url = resolve_catalog_spec("github-setup", catalog=SAMPLE_CATALOG)
assert "github-setup" in url
def test_resolve_url_unchanged() -> None:
spec = "file:///tmp/template?subdir=foo"
assert resolve_catalog_spec(spec, catalog=SAMPLE_CATALOG) == spec
def test_unknown_slug_raises() -> None:
with pytest.raises(CatalogResolutionError, match="unknown-slug"):
resolve_catalog_spec("unknown-slug", catalog=SAMPLE_CATALOG)
def test_resolve_catalog_specs_batch() -> None:
resolved = resolve_catalog_specs(
["github-setup", "file:///ext"],
catalog=SAMPLE_CATALOG,
)
assert len(resolved) == 2
assert resolved[1] == "file:///ext"
def test_short_category_label_matches_cna_style() -> None:
assert short_category_label("Backend Applications") == "Backend"
assert short_category_label("User Acceptance Testing") == "UAT"
def test_build_template_choices_are_searchable() -> None:
catalog = {
"categories": [
{
"slug": "backend-applications",
"name": "Backend Applications",
}
],
"templates": [
{
"slug": "fastapi-starter",
"name": "FastAPI Starter",
"description": "Async API with OpenAPI docs",
"url": "file:///templates/fastapi",
"category": "backend-applications",
"labels": ["FastAPI", "API", "uv"],
}
],
}
choices = build_template_choices(catalog)
first = choices[0]
assert first.value == "file:///templates/fastapi"
assert "FastAPI Starter" in first.title
assert "OpenAPI" in first.title
assert "uv" in first.title
assert "\033" not in first.title
assert "openapi" in first.search
assert "backend" in first.search
assert "uv" in first.search
assert choices[-1].value == CUSTOM_TEMPLATE_SENTINEL
def test_template_choice_titles_are_html_safe_for_questionary() -> None:
"""questionary.autocomplete formats choice text as HTML (match underline)."""
from prompt_toolkit.formatted_text import HTML
catalog = {
"categories": [
{"slug": "backend-applications", "name": "Backend Applications"}
],
"templates": [
{
"slug": "fastapi-starter",
"name": "FastAPI Starter",
"description": "Async API with OpenAPI docs",
"url": "file:///templates/fastapi",
"category": "backend-applications",
"labels": ["FastAPI"],
}
],
}
title = build_template_choices(catalog)[0].title
# Must not raise "not well-formed (invalid token)" from ANSI escapes.
HTML("{}<b><u>{}</u></b>{}").format(title[:3], title[3:6], title[6:])
def test_build_extension_choices_filters_by_template_type() -> None:
catalog = {
"categories": [
{"slug": "ci", "name": "CI"},
{"slug": "data", "name": "Data"},
],
"templates": [
{
"slug": "fastapi-starter",
"name": "FastAPI Starter",
"url": "file:///templates/fastapi",
"type": "fastapi-backend",
"category": "backend-applications",
}
],
"extensions": [
{
"slug": "github-setup",
"name": "GitHub Setup",
"description": "Actions and Dependabot",
"url": "file:///extensions/github",
"type": ["fastapi-backend"],
"category": "ci",
"labels": ["GitHub", "CI"],
},
{
"slug": "all-projects",
"name": "All Projects",
"url": "file:///extensions/all",
"type": ["all"],
"category": "data",
},
{
"slug": "django-only",
"name": "Django Only",
"url": "file:///extensions/django",
"type": ["django"],
"category": "ci",
},
],
}
choices = build_extension_choices(catalog, "file:///templates/fastapi")
assert [choice.value for choice in choices] == [
"file:///extensions/github",
"file:///extensions/all",
]
assert "dependabot" in choices[0].search
assert "github" in choices[0].title.lower()
def test_group_extension_choices_preserves_category_order() -> None:
catalog = {
"categories": [
{"slug": "ci", "name": "CI"},
{"slug": "data", "name": "Data"},
],
"templates": [
{
"slug": "fastapi-starter",
"name": "FastAPI Starter",
"url": "file:///templates/fastapi",
"type": "fastapi-backend",
"category": "backend-applications",
}
],
"extensions": [
{
"slug": "postgres",
"name": "Postgres",
"url": "file:///extensions/postgres",
"type": ["fastapi-backend"],
"category": "data",
},
{
"slug": "github",
"name": "GitHub",
"url": "file:///extensions/github",
"type": ["fastapi-backend"],
"category": "ci",
},
],
}
grouped = group_extension_choices(
build_extension_choices(catalog, "file:///templates/fastapi")
)
assert list(grouped) == ["ci", "data"]
assert grouped["ci"][0].value == "file:///extensions/github"
def test_validate_extension_compatibility_ok() -> None:
catalog = {
"extensions": [
{
"slug": "github-setup",
"url": "file:///ext/github",
"incompatibleWith": ["other"],
},
{"slug": "python-docker", "url": "file:///ext/docker"},
]
}
validate_extension_compatibility(["github-setup", "python-docker"], catalog=catalog)
def test_validate_extension_compatibility_fails_on_pair() -> None:
catalog = {
"extensions": [
{
"slug": "react-redux-saga",
"url": "file:///ext/saga",
"incompatibleWith": ["react-redux-thunk"],
},
{
"slug": "react-redux-thunk",
"url": "file:///ext/thunk",
"incompatibleWith": ["react-redux-saga"],
},
]
}
with pytest.raises(IncompatibleExtensionsError, match="react-redux-saga") as ei:
validate_extension_compatibility(
["react-redux-saga", "file:///ext/thunk"],
catalog=catalog,
)
assert ei.value.pairs == [("react-redux-saga", "react-redux-thunk")]
def test_find_incompatible_pairs_dedupes_symmetric_edges() -> None:
catalog = {
"extensions": [
{
"slug": "a",
"url": "file:///a",
"incompatibleWith": ["b"],
},
{
"slug": "b",
"url": "file:///b",
"incompatibleWith": ["a"],
},
]
}
pairs = find_incompatible_pairs(["a", "b"], catalog=catalog)
assert pairs == [("a", "b")]