codeplain 0.3.13.dev47__py3-none-any.whl → 0.3.13.dev48__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {codeplain-0.3.13.dev47.dist-info → codeplain-0.3.13.dev48.dist-info}/METADATA +2 -2
- {codeplain-0.3.13.dev47.dist-info → codeplain-0.3.13.dev48.dist-info}/RECORD +16 -19
- concept_utils.py +2 -220
- file_utils.py +6 -106
- plain2code.py +38 -5
- plain2code_exceptions.py +12 -42
- plain2code_nodes.py +2 -118
- plain2code_utils.py +1 -16
- plain_file.py +2 -870
- plain_spec.py +9 -413
- tests/test_file_utils.py +1 -70
- tests/test_render_range.py +42 -0
- tests/test_user_error_message.py +36 -0
- tests/test_imports.py +0 -53
- tests/test_plainfile.py +0 -271
- tests/test_plainfileparser.py +0 -775
- tests/test_plainspec.py +0 -103
- tests/test_requires.py +0 -28
- {codeplain-0.3.13.dev47.dist-info → codeplain-0.3.13.dev48.dist-info}/WHEEL +0 -0
- {codeplain-0.3.13.dev47.dist-info → codeplain-0.3.13.dev48.dist-info}/entry_points.txt +0 -0
- {codeplain-0.3.13.dev47.dist-info → codeplain-0.3.13.dev48.dist-info}/licenses/LICENSE +0 -0
plain_spec.py
CHANGED
|
@@ -1,403 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
import json
|
|
3
|
-
import uuid
|
|
4
|
-
from typing import Optional
|
|
1
|
+
"""Compatibility shim: the parser now lives in the ``plain-parser`` package.
|
|
5
2
|
|
|
6
|
-
|
|
3
|
+
The render-range helpers below stay here because they read codeplain's CLI
|
|
4
|
+
arguments (``--render-range`` / ``--render-from``); the FRID slicing they wrap
|
|
5
|
+
is ``plain_parser.plain_spec.get_frids_range``.
|
|
6
|
+
"""
|
|
7
7
|
|
|
8
|
-
from
|
|
9
|
-
|
|
10
|
-
DEFINITIONS = "definitions"
|
|
11
|
-
NON_FUNCTIONAL_REQUIREMENTS = "implementation reqs"
|
|
12
|
-
TEST_REQUIREMENTS = "test reqs"
|
|
13
|
-
FUNCTIONAL_REQUIREMENTS = "functional specs"
|
|
14
|
-
ACCEPTANCE_TESTS = "acceptance_tests"
|
|
15
|
-
ACCEPTANCE_TEST_HEADING = "acceptance tests"
|
|
16
|
-
|
|
17
|
-
ALLOWED_SPECIFICATION_HEADINGS = [
|
|
18
|
-
DEFINITIONS,
|
|
19
|
-
NON_FUNCTIONAL_REQUIREMENTS,
|
|
20
|
-
TEST_REQUIREMENTS,
|
|
21
|
-
FUNCTIONAL_REQUIREMENTS,
|
|
22
|
-
ACCEPTANCE_TEST_HEADING,
|
|
23
|
-
]
|
|
24
|
-
|
|
25
|
-
ALLOWED_IMPORT_SPECIFICATION_HEADINGS = [DEFINITIONS, NON_FUNCTIONAL_REQUIREMENTS, TEST_REQUIREMENTS]
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def has_acceptance_tests(plain_source_tree) -> bool:
|
|
29
|
-
"""Return True if any functional requirement in the plain source defines acceptance tests."""
|
|
30
|
-
functional_requirements = plain_source_tree.get(FUNCTIONAL_REQUIREMENTS) or []
|
|
31
|
-
for functional_requirement in functional_requirements:
|
|
32
|
-
if functional_requirement.get(ACCEPTANCE_TESTS):
|
|
33
|
-
return True
|
|
34
|
-
return False
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def collect_specification_linked_resources(specification, specification_heading, linked_resources_list):
|
|
38
|
-
linked_resources = []
|
|
39
|
-
if "linked_resources" in specification:
|
|
40
|
-
linked_resources.extend(specification["linked_resources"])
|
|
41
|
-
|
|
42
|
-
for resource in linked_resources:
|
|
43
|
-
resource_found = False
|
|
44
|
-
for resource_map in linked_resources_list:
|
|
45
|
-
if resource["text"] == resource_map["text"]:
|
|
46
|
-
if resource["target"] != resource_map["target"]:
|
|
47
|
-
raise Exception(
|
|
48
|
-
f"The file {resource['target']} is linked to multiple linked resources with the same text: {resource['text']}"
|
|
49
|
-
)
|
|
50
|
-
elif resource["target"] == resource_map["target"]:
|
|
51
|
-
if resource["text"] != resource_map["text"]:
|
|
52
|
-
raise Exception(
|
|
53
|
-
f"The linked resource {resource['text']} is linked to multiple files: {resource_map['target']}"
|
|
54
|
-
)
|
|
55
|
-
else:
|
|
56
|
-
continue
|
|
57
|
-
|
|
58
|
-
if resource_found:
|
|
59
|
-
raise Exception(
|
|
60
|
-
"Duplicate linked resource found: " + resource["text"] + " (" + resource["target"] + ")"
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
resource_found = True
|
|
64
|
-
resource_map["sections"].append(specification_heading)
|
|
65
|
-
|
|
66
|
-
if not resource_found:
|
|
67
|
-
linked_resources_list.append(
|
|
68
|
-
{"text": resource["text"], "target": resource["target"], "sections": [specification_heading]}
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def collect_linked_resources_in_section(
|
|
73
|
-
section, linked_resources_list, specifications_list, include_acceptance_tests, frid=Optional[str]
|
|
74
|
-
):
|
|
75
|
-
# When should we collect specification headings in the current section. Either when one of the following holds true:
|
|
76
|
-
# - frid wasn't specified
|
|
77
|
-
# - section has no ID (and that's exactly when it's the root section)
|
|
78
|
-
# - section has ID, frid was specified and the specified frid inside the current section tree
|
|
79
|
-
should_collect_specification_headings_in_current_section = (
|
|
80
|
-
frid is None or "ID" not in section or frid.startswith(section["ID"])
|
|
81
|
-
)
|
|
82
|
-
if should_collect_specification_headings_in_current_section:
|
|
83
|
-
specifications_to_collect = list(set([DEFINITIONS, NON_FUNCTIONAL_REQUIREMENTS, TEST_REQUIREMENTS]))
|
|
84
|
-
if specifications_list:
|
|
85
|
-
specifications_to_collect = list(set(specifications_to_collect) & set(specifications_list))
|
|
86
|
-
for specification_heading in specifications_to_collect:
|
|
87
|
-
if specification_heading in section:
|
|
88
|
-
for requirement in section[specification_heading]:
|
|
89
|
-
collect_specification_linked_resources(requirement, specification_heading, linked_resources_list)
|
|
90
|
-
|
|
91
|
-
if FUNCTIONAL_REQUIREMENTS in section and (
|
|
92
|
-
not specifications_list or FUNCTIONAL_REQUIREMENTS in specifications_list
|
|
93
|
-
):
|
|
94
|
-
functional_requirement_count = 0
|
|
95
|
-
for requirement in section[FUNCTIONAL_REQUIREMENTS]:
|
|
96
|
-
collect_specification_linked_resources(requirement, FUNCTIONAL_REQUIREMENTS, linked_resources_list)
|
|
97
|
-
|
|
98
|
-
functional_requirement_count += 1
|
|
99
|
-
section_id = section.get("ID", None)
|
|
100
|
-
current_frid = get_current_frid(section_id, functional_requirement_count)
|
|
101
|
-
|
|
102
|
-
if ACCEPTANCE_TESTS in requirement and include_acceptance_tests and (frid is None or frid == current_frid):
|
|
103
|
-
for acceptance_test in requirement[ACCEPTANCE_TESTS]:
|
|
104
|
-
collect_specification_linked_resources(
|
|
105
|
-
acceptance_test, FUNCTIONAL_REQUIREMENTS, linked_resources_list
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
if frid is not None and current_frid == frid:
|
|
109
|
-
# here, we rely on the fact that FRIDs are incrementing. And if we have reached the current FRID, we should
|
|
110
|
-
# not collect any FRIDs after the current one.
|
|
111
|
-
return True
|
|
112
|
-
|
|
113
|
-
if "sections" in section:
|
|
114
|
-
for subsection in section["sections"]:
|
|
115
|
-
if collect_linked_resources_in_section(
|
|
116
|
-
subsection, linked_resources_list, specifications_list, include_acceptance_tests, frid
|
|
117
|
-
):
|
|
118
|
-
return True
|
|
119
|
-
|
|
120
|
-
return False
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
# TODO: check if this function can be refactored to return the list of linked resources instead of modifying it in place
|
|
124
|
-
def collect_linked_resources(
|
|
125
|
-
plain_source_tree, linked_resources_list, specifications_list, include_acceptance_tests, frid=None
|
|
126
|
-
):
|
|
127
|
-
|
|
128
|
-
if not isinstance(plain_source_tree, dict):
|
|
129
|
-
raise ValueError("[plain_source_tree must be a dictionary.")
|
|
130
|
-
|
|
131
|
-
if frid is not None:
|
|
132
|
-
functional_requirements = get_frids(plain_source_tree)
|
|
133
|
-
if frid not in functional_requirements:
|
|
134
|
-
raise ValueError(f"frid {frid} does not exist.")
|
|
135
|
-
|
|
136
|
-
result = collect_linked_resources_in_section(
|
|
137
|
-
plain_source_tree, linked_resources_list, specifications_list, include_acceptance_tests, frid
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
# Sort linked_resources_list by the "text" field
|
|
141
|
-
linked_resources_list.sort(key=lambda x: x["text"])
|
|
142
|
-
|
|
143
|
-
return result
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
def get_frids(plain_source_tree):
|
|
147
|
-
if FUNCTIONAL_REQUIREMENTS in plain_source_tree:
|
|
148
|
-
for functional_requirement_id in range(1, len(plain_source_tree[FUNCTIONAL_REQUIREMENTS]) + 1):
|
|
149
|
-
if "ID" in plain_source_tree:
|
|
150
|
-
yield plain_source_tree["ID"] + "." + str(functional_requirement_id)
|
|
151
|
-
else:
|
|
152
|
-
yield str(functional_requirement_id)
|
|
153
|
-
|
|
154
|
-
if "sections" in plain_source_tree:
|
|
155
|
-
for section in plain_source_tree["sections"]:
|
|
156
|
-
yield from get_frids(section)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
def get_first_frid(plain_source_tree):
|
|
160
|
-
return next(get_frids(plain_source_tree), None)
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
def get_current_frid(section_id: Optional[str], functional_requirement_count: int) -> str:
|
|
164
|
-
if section_id is None:
|
|
165
|
-
return str(functional_requirement_count)
|
|
166
|
-
else:
|
|
167
|
-
return section_id + "." + str(functional_requirement_count)
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
def get_next_frid(plain_source_tree, frid):
|
|
171
|
-
functional_requirements = get_frids(plain_source_tree)
|
|
172
|
-
for temp_frid in functional_requirements:
|
|
173
|
-
if temp_frid == frid:
|
|
174
|
-
return next(functional_requirements, None)
|
|
175
|
-
|
|
176
|
-
raise Exception(f"Functionality {frid} does not exist.")
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
def get_previous_frid(plain_source_tree, frid):
|
|
180
|
-
previous_frid = None
|
|
181
|
-
for temp_frid in get_frids(plain_source_tree):
|
|
182
|
-
if temp_frid == frid:
|
|
183
|
-
return previous_frid
|
|
184
|
-
|
|
185
|
-
previous_frid = temp_frid
|
|
186
|
-
|
|
187
|
-
raise Exception(f"Functionality {frid} does not exist.")
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
def get_frids_before(plain_source_tree, target_frid: str) -> list[str]:
|
|
191
|
-
"""
|
|
192
|
-
Get all FRIDs that appear before the target FRID in the specification.
|
|
193
|
-
|
|
194
|
-
Args:
|
|
195
|
-
plain_source_tree: The plain source tree
|
|
196
|
-
target_frid: The FRID to find predecessors for
|
|
197
|
-
|
|
198
|
-
Returns:
|
|
199
|
-
List of FRIDs that appear before target_frid, in order
|
|
200
|
-
"""
|
|
201
|
-
previous_frids = []
|
|
202
|
-
for frid in get_frids(plain_source_tree):
|
|
203
|
-
if frid == target_frid:
|
|
204
|
-
break
|
|
205
|
-
previous_frids.append(frid)
|
|
206
|
-
|
|
207
|
-
return previous_frids
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
def get_specification_item_markdown(specification_item, code_variables, replace_code_variables):
|
|
211
|
-
markdown = specification_item["markdown"]
|
|
212
|
-
if "code_variables" in specification_item:
|
|
213
|
-
for code_variable in specification_item["code_variables"]:
|
|
214
|
-
if code_variable["name"] in code_variables:
|
|
215
|
-
if code_variables[code_variable["name"]] != code_variable["value"]:
|
|
216
|
-
raise Exception(
|
|
217
|
-
f"Code variable {code_variable['name']} has multiple values: {code_variables[code_variable['name']]} and {code_variable['value']}"
|
|
218
|
-
)
|
|
219
|
-
else:
|
|
220
|
-
code_variables[code_variable["name"]] = code_variable["value"]
|
|
221
|
-
|
|
222
|
-
if replace_code_variables:
|
|
223
|
-
markdown = markdown.replace(f"{{{{ {code_variable['name']} }}}}", code_variable["value"])
|
|
224
|
-
|
|
225
|
-
return markdown
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
def get_specifications_from_plain_source_tree(
|
|
229
|
-
frid,
|
|
230
|
-
plain_source_tree,
|
|
231
|
-
definitions,
|
|
232
|
-
non_functional_requirements,
|
|
233
|
-
test_requirements,
|
|
234
|
-
functional_requirements,
|
|
235
|
-
acceptance_tests,
|
|
236
|
-
code_variables,
|
|
237
|
-
replace_code_variables,
|
|
238
|
-
section_id=None,
|
|
239
|
-
):
|
|
240
|
-
return_frid = None
|
|
241
|
-
if FUNCTIONAL_REQUIREMENTS in plain_source_tree and len(plain_source_tree[FUNCTIONAL_REQUIREMENTS]) > 0:
|
|
242
|
-
functional_requirement_count = 0
|
|
243
|
-
for functional_requirement in plain_source_tree[FUNCTIONAL_REQUIREMENTS]:
|
|
244
|
-
functional_requirement_count += 1
|
|
245
|
-
if section_id is None:
|
|
246
|
-
current_frid = str(functional_requirement_count)
|
|
247
|
-
else:
|
|
248
|
-
current_frid = section_id + "." + str(functional_requirement_count)
|
|
249
|
-
|
|
250
|
-
functional_requirements.append(
|
|
251
|
-
get_specification_item_markdown(functional_requirement, code_variables, replace_code_variables)
|
|
252
|
-
)
|
|
253
|
-
|
|
254
|
-
if current_frid == frid:
|
|
255
|
-
if ACCEPTANCE_TESTS in functional_requirement:
|
|
256
|
-
for acceptance_test in functional_requirement[ACCEPTANCE_TESTS]:
|
|
257
|
-
acceptance_tests.append(
|
|
258
|
-
get_specification_item_markdown(acceptance_test, code_variables, replace_code_variables)
|
|
259
|
-
)
|
|
260
|
-
|
|
261
|
-
return_frid = current_frid
|
|
262
|
-
break
|
|
263
|
-
|
|
264
|
-
if "sections" in plain_source_tree:
|
|
265
|
-
for section in plain_source_tree["sections"]:
|
|
266
|
-
sub_frid = get_specifications_from_plain_source_tree(
|
|
267
|
-
frid,
|
|
268
|
-
section,
|
|
269
|
-
definitions,
|
|
270
|
-
non_functional_requirements,
|
|
271
|
-
test_requirements,
|
|
272
|
-
functional_requirements,
|
|
273
|
-
acceptance_tests,
|
|
274
|
-
code_variables,
|
|
275
|
-
replace_code_variables,
|
|
276
|
-
section["ID"],
|
|
277
|
-
)
|
|
278
|
-
if sub_frid is not None:
|
|
279
|
-
return_frid = sub_frid
|
|
280
|
-
break
|
|
281
|
-
|
|
282
|
-
if return_frid is not None:
|
|
283
|
-
if DEFINITIONS in plain_source_tree and plain_source_tree[DEFINITIONS] is not None:
|
|
284
|
-
definitions[0:0] = [
|
|
285
|
-
get_specification_item_markdown(specification, code_variables, replace_code_variables)
|
|
286
|
-
for specification in plain_source_tree[DEFINITIONS]
|
|
287
|
-
]
|
|
288
|
-
if (
|
|
289
|
-
NON_FUNCTIONAL_REQUIREMENTS in plain_source_tree
|
|
290
|
-
and plain_source_tree[NON_FUNCTIONAL_REQUIREMENTS] is not None
|
|
291
|
-
):
|
|
292
|
-
non_functional_requirements[0:0] = [
|
|
293
|
-
get_specification_item_markdown(specification, code_variables, replace_code_variables)
|
|
294
|
-
for specification in plain_source_tree[NON_FUNCTIONAL_REQUIREMENTS]
|
|
295
|
-
]
|
|
296
|
-
if TEST_REQUIREMENTS in plain_source_tree and plain_source_tree[TEST_REQUIREMENTS] is not None:
|
|
297
|
-
test_requirements[0:0] = [
|
|
298
|
-
get_specification_item_markdown(specification, code_variables, replace_code_variables)
|
|
299
|
-
for specification in plain_source_tree[TEST_REQUIREMENTS]
|
|
300
|
-
]
|
|
301
|
-
|
|
302
|
-
return return_frid
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
def get_specifications_for_frid(plain_source_tree, frid, replace_code_variables=True):
|
|
306
|
-
definitions = []
|
|
307
|
-
non_functional_requirements = []
|
|
308
|
-
test_requirements = []
|
|
309
|
-
functional_requirements = []
|
|
310
|
-
acceptance_tests = []
|
|
311
|
-
|
|
312
|
-
code_variables = {}
|
|
313
|
-
|
|
314
|
-
result = get_specifications_from_plain_source_tree(
|
|
315
|
-
frid,
|
|
316
|
-
plain_source_tree,
|
|
317
|
-
definitions,
|
|
318
|
-
non_functional_requirements,
|
|
319
|
-
test_requirements,
|
|
320
|
-
functional_requirements,
|
|
321
|
-
acceptance_tests,
|
|
322
|
-
code_variables,
|
|
323
|
-
replace_code_variables,
|
|
324
|
-
)
|
|
325
|
-
if result is None:
|
|
326
|
-
raise Exception(f"Functionality {frid} does not exist.")
|
|
327
|
-
|
|
328
|
-
specifications = {
|
|
329
|
-
DEFINITIONS: definitions,
|
|
330
|
-
NON_FUNCTIONAL_REQUIREMENTS: non_functional_requirements,
|
|
331
|
-
TEST_REQUIREMENTS: test_requirements,
|
|
332
|
-
FUNCTIONAL_REQUIREMENTS: functional_requirements,
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
if acceptance_tests:
|
|
336
|
-
specifications[ACCEPTANCE_TESTS] = acceptance_tests
|
|
337
|
-
|
|
338
|
-
if code_variables:
|
|
339
|
-
return specifications, code_variables
|
|
340
|
-
else:
|
|
341
|
-
return specifications, None
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
@with_context
|
|
345
|
-
def code_variable_liquid_filter(value, *, context):
|
|
346
|
-
if len(context.scope) == 0:
|
|
347
|
-
raise Exception("Invalid use of code_variable filter!")
|
|
348
|
-
|
|
349
|
-
if "code_variables" in context.globals:
|
|
350
|
-
code_variables = context.globals["code_variables"]
|
|
351
|
-
|
|
352
|
-
variable = next(iter(context.scope.items()))
|
|
353
|
-
|
|
354
|
-
unique_str = uuid.uuid4().hex
|
|
355
|
-
|
|
356
|
-
code_variables[unique_str] = {variable[0]: value}
|
|
357
|
-
|
|
358
|
-
return unique_str
|
|
359
|
-
else:
|
|
360
|
-
|
|
361
|
-
return value
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
@with_context
|
|
365
|
-
def prohibited_chars_liquid_filter(value, prohibited_chars, *, context):
|
|
366
|
-
if not isinstance(value, str):
|
|
367
|
-
value = str(value)
|
|
368
|
-
|
|
369
|
-
if len(context.scope) == 0:
|
|
370
|
-
raise Exception("Invalid use of prohibited_chars filter!")
|
|
371
|
-
|
|
372
|
-
variable = next(iter(context.scope.items()))
|
|
373
|
-
variable_name = variable[0]
|
|
374
|
-
|
|
375
|
-
for char in prohibited_chars:
|
|
376
|
-
if char in value:
|
|
377
|
-
raise InvalidLiquidVariableName(
|
|
378
|
-
f"'{char}' is not a valid character for variable '{variable_name}' (value: '{value}')."
|
|
379
|
-
)
|
|
380
|
-
|
|
381
|
-
return value
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
def hash_text(text):
|
|
385
|
-
return hashlib.sha256(text.encode()).hexdigest()
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
def get_hash_value(specifications):
|
|
389
|
-
return hash_text(json.dumps(specifications, indent=4))
|
|
8
|
+
from plain_parser.plain_spec import * # noqa: F401,F403
|
|
9
|
+
from plain_parser.plain_spec import get_frids_range
|
|
390
10
|
|
|
391
11
|
|
|
392
12
|
def get_render_range(render_range, plain_source):
|
|
393
13
|
render_range = render_range.split(",")
|
|
394
14
|
range_end = render_range[1] if len(render_range) == 2 else render_range[0]
|
|
395
15
|
|
|
396
|
-
return
|
|
16
|
+
return get_frids_range(plain_source, render_range[0], range_end)
|
|
397
17
|
|
|
398
18
|
|
|
399
19
|
def get_render_range_from(start, plain_source):
|
|
400
|
-
return
|
|
20
|
+
return get_frids_range(plain_source, start)
|
|
401
21
|
|
|
402
22
|
|
|
403
23
|
def compute_render_range(args, plain_source_tree):
|
|
@@ -415,27 +35,3 @@ def compute_render_range(args, plain_source_tree):
|
|
|
415
35
|
elif args.render_from:
|
|
416
36
|
return get_render_range_from(args.render_from, plain_source_tree)
|
|
417
37
|
return None
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
def _get_frids_range(plain_source, start, end=None):
|
|
421
|
-
frids = list(get_frids(plain_source))
|
|
422
|
-
|
|
423
|
-
start = str(start)
|
|
424
|
-
|
|
425
|
-
if start not in frids:
|
|
426
|
-
raise InvalidFridArgument(f"Invalid start functionality ID: {start}. Valid IDs are: {frids}.")
|
|
427
|
-
|
|
428
|
-
if end is not None:
|
|
429
|
-
end = str(end)
|
|
430
|
-
if end not in frids:
|
|
431
|
-
raise InvalidFridArgument(f"Invalid end functionality ID: {end}. Valid IDs are: {frids}.")
|
|
432
|
-
|
|
433
|
-
end_idx = frids.index(end) + 1
|
|
434
|
-
else:
|
|
435
|
-
end_idx = len(frids)
|
|
436
|
-
|
|
437
|
-
start_idx = frids.index(start)
|
|
438
|
-
if start_idx >= end_idx:
|
|
439
|
-
raise InvalidFridArgument(f"Start functionality ID: {start} must be before end functionality ID: {end}.")
|
|
440
|
-
|
|
441
|
-
return frids[start_idx:end_idx]
|
tests/test_file_utils.py
CHANGED
|
@@ -5,9 +5,7 @@ import pytest
|
|
|
5
5
|
|
|
6
6
|
import file_utils
|
|
7
7
|
from diff_utils import get_code_diff
|
|
8
|
-
from file_utils import get_existing_files_content,
|
|
9
|
-
from plain2code_exceptions import UnsupportedBase64Content, UnsupportedResourceType
|
|
10
|
-
from plain2code_utils import MAX_BASE64_BLOB_LENGTH
|
|
8
|
+
from file_utils import get_existing_files_content, store_response_files
|
|
11
9
|
|
|
12
10
|
|
|
13
11
|
@pytest.fixture
|
|
@@ -16,73 +14,6 @@ def template_dir():
|
|
|
16
14
|
yield d
|
|
17
15
|
|
|
18
16
|
|
|
19
|
-
def test_load_linked_resources_text_file(template_dir):
|
|
20
|
-
file_path = os.path.join(template_dir, "notes.md")
|
|
21
|
-
with open(file_path, "w") as f:
|
|
22
|
-
f.write("# hello")
|
|
23
|
-
|
|
24
|
-
result = load_linked_resources([template_dir], [{"text": "Notes", "target": "notes.md"}], "my_thing")
|
|
25
|
-
|
|
26
|
-
assert result == {"notes.md": "# hello"}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def test_load_linked_resources_binary_file_raises_unsupported_resource_type(template_dir):
|
|
30
|
-
file_path = os.path.join(template_dir, "icon.png")
|
|
31
|
-
with open(file_path, "wb") as f:
|
|
32
|
-
f.write(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\xff\xfe\xfd")
|
|
33
|
-
|
|
34
|
-
with pytest.raises(UnsupportedResourceType) as exc_info:
|
|
35
|
-
load_linked_resources([template_dir], [{"text": "Icon", "target": "icon.png"}], "my_thing")
|
|
36
|
-
|
|
37
|
-
assert "icon.png" in str(exc_info.value)
|
|
38
|
-
assert "binary file" in str(exc_info.value)
|
|
39
|
-
assert "my_thing" in str(exc_info.value)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def test_load_linked_resources_missing_file_raises_file_not_found(template_dir):
|
|
43
|
-
with pytest.raises(FileNotFoundError):
|
|
44
|
-
load_linked_resources([template_dir], [{"text": "Missing", "target": "missing.md"}], "my_thing")
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def test_load_linked_resources_base64_blob_raises(template_dir):
|
|
48
|
-
file_path = os.path.join(template_dir, "request.txt")
|
|
49
|
-
with open(file_path, "w") as f:
|
|
50
|
-
f.write("curl -d 'selfie_image=" + "A" * (MAX_BASE64_BLOB_LENGTH + 100) + "'")
|
|
51
|
-
|
|
52
|
-
with pytest.raises(UnsupportedBase64Content) as exc_info:
|
|
53
|
-
load_linked_resources([template_dir], [{"text": "Request", "target": "request.txt"}], "my_thing")
|
|
54
|
-
|
|
55
|
-
assert "request.txt" in str(exc_info.value)
|
|
56
|
-
assert "my_thing" in str(exc_info.value)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def test_load_linked_resources_small_base64_allowed(template_dir):
|
|
60
|
-
file_path = os.path.join(template_dir, "token.txt")
|
|
61
|
-
with open(file_path, "w") as f:
|
|
62
|
-
f.write("token=" + "A" * (MAX_BASE64_BLOB_LENGTH - 100))
|
|
63
|
-
|
|
64
|
-
result = load_linked_resources([template_dir], [{"text": "Token", "target": "token.txt"}], "my_thing")
|
|
65
|
-
|
|
66
|
-
assert "token.txt" in result
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def test_load_linked_resources_real_base64_image_raises(template_dir):
|
|
70
|
-
# Real base64-encoded JPEG that made Gemini 3 fail, inlined into a curl example resource.
|
|
71
|
-
sample_path = os.path.join(os.path.dirname(__file__), "data", "sample_base64_image.txt")
|
|
72
|
-
with open(sample_path) as f:
|
|
73
|
-
blob = f.read()
|
|
74
|
-
|
|
75
|
-
file_path = os.path.join(template_dir, "face_match_request.txt")
|
|
76
|
-
with open(file_path, "w") as f:
|
|
77
|
-
f.write(f"curl -d 'selfie_image={blob}' https://example.com/face-match")
|
|
78
|
-
|
|
79
|
-
with pytest.raises(UnsupportedBase64Content) as exc_info:
|
|
80
|
-
load_linked_resources([template_dir], [{"text": "Request", "target": "face_match_request.txt"}], "face_match")
|
|
81
|
-
|
|
82
|
-
assert "face_match_request.txt" in str(exc_info.value)
|
|
83
|
-
assert str(len(blob)) in str(exc_info.value)
|
|
84
|
-
|
|
85
|
-
|
|
86
17
|
def test_store_response_files_writes_unicode_as_utf8(template_dir):
|
|
87
18
|
"""A non-cp1252 character (📍 U+1F4CD) is written as UTF-8, whatever the platform's default encoding."""
|
|
88
19
|
content = "Location 📍 marker\n"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from types import SimpleNamespace
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
import plain_spec
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _four_functionalities():
|
|
9
|
+
return {plain_spec.FUNCTIONAL_REQUIREMENTS: [{"markdown": f"- Functionality {i}."} for i in range(1, 5)]}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_get_render_range_parses_start_and_end():
|
|
13
|
+
assert plain_spec.get_render_range("2,3", _four_functionalities()) == ["2", "3"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_get_render_range_single_value_renders_one_frid():
|
|
17
|
+
assert plain_spec.get_render_range("2", _four_functionalities()) == ["2"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_get_render_range_from_runs_to_last_frid():
|
|
21
|
+
assert plain_spec.get_render_range_from("3", _four_functionalities()) == ["3", "4"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_compute_render_range_prefers_render_range():
|
|
25
|
+
args = SimpleNamespace(render_range="1,2", render_from="3")
|
|
26
|
+
assert plain_spec.compute_render_range(args, _four_functionalities()) == ["1", "2"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_compute_render_range_falls_back_to_render_from():
|
|
30
|
+
args = SimpleNamespace(render_range=None, render_from="3")
|
|
31
|
+
assert plain_spec.compute_render_range(args, _four_functionalities()) == ["3", "4"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_compute_render_range_none_when_no_arguments():
|
|
35
|
+
args = SimpleNamespace(render_range=None, render_from=None)
|
|
36
|
+
assert plain_spec.compute_render_range(args, _four_functionalities()) is None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_compute_render_range_invalid_frid_raises():
|
|
40
|
+
args = SimpleNamespace(render_range="9", render_from=None)
|
|
41
|
+
with pytest.raises(plain_spec.InvalidFridArgument):
|
|
42
|
+
plain_spec.compute_render_range(args, _four_functionalities())
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from liquid2.exceptions import TemplateNotFoundError
|
|
2
|
+
|
|
3
|
+
from plain2code import user_error_message
|
|
4
|
+
|
|
5
|
+
SEARCH_ORDER_STEPS = [
|
|
6
|
+
"1. The directory containing your .plain file",
|
|
7
|
+
"2. The directory specified by --template-dir (if provided)",
|
|
8
|
+
"3. The built-in 'standard_template_library' directory",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_template_not_found_keeps_parser_message_and_adds_search_order():
|
|
13
|
+
message = user_error_message(TemplateNotFoundError("Template not found: 'nope.plain'"))
|
|
14
|
+
|
|
15
|
+
# liquid2 repr-quotes a bare message when no token is attached; the CLI attaches one.
|
|
16
|
+
assert "Template not found: 'nope.plain'" in message
|
|
17
|
+
for step in SEARCH_ORDER_STEPS:
|
|
18
|
+
assert step in message
|
|
19
|
+
assert "missing template exists" in message
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_resource_not_found_keeps_parser_message_and_adds_search_order():
|
|
23
|
+
message = user_error_message(FileNotFoundError("Resource file 'x.md' referenced in module 'm' not found."))
|
|
24
|
+
|
|
25
|
+
assert message.startswith("Resource file 'x.md' referenced in module 'm' not found.")
|
|
26
|
+
for step in SEARCH_ORDER_STEPS:
|
|
27
|
+
assert step in message
|
|
28
|
+
assert "resource exists" in message
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_other_errors_are_unchanged():
|
|
32
|
+
assert user_error_message(ValueError("boom")) == "boom"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_empty_message_falls_back_to_repr():
|
|
36
|
+
assert user_error_message(RuntimeError()) == "RuntimeError()"
|
tests/test_imports.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import pytest
|
|
2
|
-
|
|
3
|
-
import plain_file
|
|
4
|
-
from plain2code_exceptions import ImportedModuleWithFunctionalitiesError
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def test_imported_module_with_functionalities(load_test_data, get_test_data_path):
|
|
8
|
-
plain_source_text = load_test_data("data/imports/imports_module_with_functionalities.plain")
|
|
9
|
-
template_dirs = [get_test_data_path("data/imports")]
|
|
10
|
-
with pytest.raises(
|
|
11
|
-
ImportedModuleWithFunctionalitiesError,
|
|
12
|
-
match="use 'requires' instead of 'import'",
|
|
13
|
-
):
|
|
14
|
-
plain_file.parse_plain_source(plain_source_text, {}, template_dirs, [], [])
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def test_non_existent_import(load_test_data, get_test_data_path):
|
|
18
|
-
plain_source_text = load_test_data("data/imports/non_existent_import.plain")
|
|
19
|
-
with pytest.raises(Exception, match="Module does not exist"):
|
|
20
|
-
plain_file.parse_plain_source(plain_source_text, {}, [], [], [])
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def test_circular_imports(load_test_data, get_test_data_path):
|
|
24
|
-
plain_source_text = load_test_data("data/imports/circular_imports_main.plain")
|
|
25
|
-
template_dirs = [get_test_data_path("data/imports")]
|
|
26
|
-
with pytest.raises(Exception, match="Circular import detected"):
|
|
27
|
-
plain_file.parse_plain_source(plain_source_text, {}, template_dirs, [], [])
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def test_diamond_imports(load_test_data, get_test_data_path):
|
|
31
|
-
plain_source_text = load_test_data("data/imports/diamond_imports_main.plain")
|
|
32
|
-
template_dirs = [get_test_data_path("data/imports")]
|
|
33
|
-
plain_file_parse_result = plain_file.parse_plain_source(plain_source_text, {}, template_dirs, [], [])
|
|
34
|
-
|
|
35
|
-
assert plain_file.marshall_plain_source(plain_file_parse_result.plain_source) == {
|
|
36
|
-
"definitions": [
|
|
37
|
-
{"markdown": "- :CommonImportDef: is a definition in diamond_import_common."},
|
|
38
|
-
{"markdown": "- :Import1Def: is a definition in diamond_import_1."},
|
|
39
|
-
{"markdown": "- :Import2Def: is a definition in diamond_import_2."},
|
|
40
|
-
],
|
|
41
|
-
"implementation reqs": [
|
|
42
|
-
{"markdown": "- :CommonImportDef: is used in diamond_import_common."},
|
|
43
|
-
{"markdown": "- :Import1Def: is used in diamond_import_1."},
|
|
44
|
-
{"markdown": "- :Import2Def: is used in diamond_import_2."},
|
|
45
|
-
{"markdown": '- :MainExecutableFile: of :App: should be called "hello_world.py".'},
|
|
46
|
-
],
|
|
47
|
-
"test reqs": [
|
|
48
|
-
{"markdown": "- :CommonImportDef: is tested in diamond_import_common."},
|
|
49
|
-
{"markdown": "- :Import1Def: is tested in diamond_import_1."},
|
|
50
|
-
{"markdown": "- :Import2Def: is tested in diamond_import_2."},
|
|
51
|
-
],
|
|
52
|
-
"functional specs": [{"markdown": '- Display "hello, world"'}],
|
|
53
|
-
}
|