plain-parser 0.2.1.dev23__py3-none-any.whl → 1.0.0.dev2__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.
- plain_parser/__init__.py +1 -1
- plain_parser/cli.py +189 -215
- plain_parser/concept_utils.py +158 -194
- plain_parser/exceptions.py +3 -11
- plain_parser/graph.py +146 -142
- plain_parser/liquid_nodes.py +30 -94
- plain_parser/loaders.py +112 -101
- plain_parser/plain_file.py +635 -796
- plain_parser/plain_spec.py +153 -212
- plain_parser/project.py +123 -114
- plain_parser/test_plain_file.py +187 -184
- plain_parser-1.0.0.dev2.dist-info/METADATA +28 -0
- plain_parser-1.0.0.dev2.dist-info/RECORD +16 -0
- {plain_parser-0.2.1.dev23.dist-info → plain_parser-1.0.0.dev2.dist-info}/licenses/LICENSE +1 -1
- plain_parser-0.2.1.dev23.dist-info/METADATA +0 -145
- plain_parser-0.2.1.dev23.dist-info/RECORD +0 -16
- {plain_parser-0.2.1.dev23.dist-info → plain_parser-1.0.0.dev2.dist-info}/WHEEL +0 -0
- {plain_parser-0.2.1.dev23.dist-info → plain_parser-1.0.0.dev2.dist-info}/entry_points.txt +0 -0
plain_parser/project.py
CHANGED
|
@@ -1,160 +1,169 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Parsing of a project: an entry module with its requires chain and the test plain files bound to its functionalities."""
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
4
|
from dataclasses import dataclass
|
|
5
|
+
from typing import Dict, List, Set, Tuple
|
|
5
6
|
|
|
6
7
|
import frontmatter
|
|
7
8
|
|
|
8
|
-
from plain_parser import
|
|
9
|
+
from plain_parser.concept_utils import extract_concepts_from_definition
|
|
9
10
|
from plain_parser.exceptions import MissingTestPlainFileError, PlainSyntaxError, UnsupportedBase64Content
|
|
11
|
+
from plain_parser.loaders import find_large_base64_blob, resolve_linked_resource
|
|
12
|
+
from plain_parser.plain_file import PLAIN_SOURCE_FILE_EXTENSION, normalize_line_endings, parse_module_chain
|
|
13
|
+
from plain_parser.plain_spec import ACCEPTANCE_TESTS, CONFORMANCE_TESTS, DEFINITIONS, FUNCTIONAL_REQUIREMENTS
|
|
14
|
+
from plain_parser.test_plain_file import (
|
|
15
|
+
MODULE_KEY,
|
|
16
|
+
TestPlainFile,
|
|
17
|
+
is_test_plain_file,
|
|
18
|
+
parse_test_plain_file_source,
|
|
19
|
+
validate_scenario_concepts,
|
|
20
|
+
)
|
|
10
21
|
|
|
11
22
|
|
|
12
23
|
@dataclass
|
|
13
24
|
class BoundTestPlainFile:
|
|
14
|
-
path: str
|
|
15
|
-
name: str
|
|
16
|
-
parsed:
|
|
25
|
+
path: str
|
|
26
|
+
name: str
|
|
27
|
+
parsed: TestPlainFile
|
|
17
28
|
|
|
18
29
|
|
|
19
30
|
@dataclass
|
|
20
31
|
class ProjectParseResult:
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
chain: List[Tuple[str, dict]]
|
|
33
|
+
test_plain_files: List[BoundTestPlainFile]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _walk(directory: str) -> List[str]:
|
|
37
|
+
entries = sorted(os.listdir(directory))
|
|
38
|
+
files = [
|
|
39
|
+
e for e in entries if e.endswith(PLAIN_SOURCE_FILE_EXTENSION) and os.path.isfile(os.path.join(directory, e))
|
|
40
|
+
]
|
|
41
|
+
directories = [e for e in entries if os.path.isdir(os.path.join(directory, e))]
|
|
42
|
+
found = [os.path.join(directory, name) for name in files]
|
|
43
|
+
for name in directories:
|
|
44
|
+
found.extend(_walk(os.path.join(directory, name)))
|
|
45
|
+
return found
|
|
24
46
|
|
|
25
47
|
|
|
26
|
-
def discover_test_plain_files(tests_folders:
|
|
27
|
-
"""
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if not os.path.isdir(
|
|
48
|
+
def discover_test_plain_files(tests_folders: List[str], entry_dir: str) -> List[Tuple[str, str]]:
|
|
49
|
+
"""List the `.plain` files under the tests folders as pairs of path and name, in discovery order."""
|
|
50
|
+
discovered: List[Tuple[str, str]] = []
|
|
51
|
+
for folder in tests_folders:
|
|
52
|
+
folder_path = folder if os.path.isabs(folder) else os.path.join(entry_dir, folder)
|
|
53
|
+
if not os.path.isdir(folder_path):
|
|
32
54
|
continue
|
|
33
|
-
for
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if file_name.endswith(plain_file.PLAIN_SOURCE_FILE_EXTENSION):
|
|
37
|
-
found.append(
|
|
38
|
-
(os.path.join(root, file_name), loaders.relative_resource_path(entry_dir, root, file_name))
|
|
39
|
-
)
|
|
40
|
-
return found
|
|
55
|
+
for path in _walk(folder_path):
|
|
56
|
+
discovered.append((path, os.path.relpath(path, entry_dir).replace(os.sep, "/")))
|
|
57
|
+
return discovered
|
|
41
58
|
|
|
42
59
|
|
|
43
|
-
def
|
|
44
|
-
with open(path, "rb") as f:
|
|
45
|
-
raw = f.read()
|
|
60
|
+
def _read_test_file(path: str, name: str) -> Tuple[str, dict]:
|
|
46
61
|
try:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
blob =
|
|
62
|
+
with open(path, "rb") as file:
|
|
63
|
+
text = file.read().decode("utf-8")
|
|
64
|
+
except UnicodeDecodeError as error:
|
|
65
|
+
raise PlainSyntaxError(f"Plain syntax error: File '{name}' under a tests folder is not a text file.") from error
|
|
66
|
+
blob = find_large_base64_blob(text)
|
|
52
67
|
if blob is not None:
|
|
53
68
|
raise UnsupportedBase64Content(
|
|
54
69
|
f"Test plain file '{name}' contains a base64-encoded blob ({len(blob)} characters) inlined in the "
|
|
55
70
|
"scenarios. This is not supported. Remove the base64 data from the file."
|
|
56
71
|
)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
unique across that merged set.
|
|
70
|
-
"""
|
|
71
|
-
trees = dict(chain)
|
|
72
|
-
bound = []
|
|
73
|
-
scenario_places: dict[tuple[str, str], dict[str, str]] = {} # (module, func-id) -> scenario-id -> place
|
|
74
|
-
for path, name in discover_test_plain_files(tests_folders, entry_dir):
|
|
75
|
-
text = read_test_plain_file_text(path, name)
|
|
76
|
-
try:
|
|
77
|
-
metadata = frontmatter.loads(text).metadata
|
|
78
|
-
except Exception as e:
|
|
79
|
-
raise PlainSyntaxError(
|
|
80
|
-
f"Plain syntax error: File '{name}' under a tests folder has invalid frontmatter: {e}"
|
|
81
|
-
)
|
|
82
|
-
if not test_plain_file.is_test_plain_file(metadata):
|
|
83
|
-
raise PlainSyntaxError(
|
|
84
|
-
f"Plain syntax error: File '{name}' is under a tests folder but is not a test plain file: its "
|
|
85
|
-
f"frontmatter needs the keys `module:` and `func_id:`."
|
|
86
|
-
)
|
|
72
|
+
try:
|
|
73
|
+
metadata = frontmatter.loads(normalize_line_endings(text)).metadata
|
|
74
|
+
except Exception as error:
|
|
75
|
+
raise PlainSyntaxError(
|
|
76
|
+
f"Plain syntax error: File '{name}' under a tests folder has invalid frontmatter: {error}"
|
|
77
|
+
) from error
|
|
78
|
+
if not is_test_plain_file(metadata):
|
|
79
|
+
raise PlainSyntaxError(
|
|
80
|
+
f"Plain syntax error: File '{name}' is under a tests folder but is not a test plain file: "
|
|
81
|
+
"its frontmatter needs the keys `module:` and `func_id:`."
|
|
82
|
+
)
|
|
83
|
+
return text, metadata
|
|
87
84
|
|
|
88
|
-
if metadata[test_plain_file.MODULE_KEY] not in trees:
|
|
89
|
-
# The file belongs to a module this render does not build. One tests folder may serve
|
|
90
|
-
# several self-standing specs, so such a file is passed over, not read. A module name
|
|
91
|
-
# that is a typo binds the file to nothing, and the functionality it was written for
|
|
92
|
-
# then has no test plain file, which check_test_plain_files reports.
|
|
93
|
-
continue
|
|
94
85
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
86
|
+
def bind_test_plain_files(chain, tests_folders, template_dirs, entry_dir) -> List[BoundTestPlainFile]:
|
|
87
|
+
"""Add the scenarios of every test plain file of the chain to the functionality it binds to."""
|
|
88
|
+
trees: Dict[str, dict] = dict(chain)
|
|
89
|
+
bound: List[BoundTestPlainFile] = []
|
|
90
|
+
for path, name in discover_test_plain_files(tests_folders, entry_dir):
|
|
91
|
+
text, metadata = _read_test_file(path, name)
|
|
92
|
+
module = metadata[MODULE_KEY]
|
|
93
|
+
if not isinstance(module, str) or module not in trees:
|
|
94
|
+
continue
|
|
95
|
+
parsed = parse_test_plain_file_source(text, name, template_dirs, {})
|
|
96
|
+
functionalities = {f["id"]: f for f in trees[parsed.module].get(FUNCTIONAL_REQUIREMENTS, [])}
|
|
97
|
+
if parsed.func_id not in functionalities:
|
|
100
98
|
raise PlainSyntaxError(
|
|
101
99
|
f"Plain syntax error: Test plain file '{name}' binds to functionality "
|
|
102
100
|
f"'{parsed.module}:{parsed.func_id}', which module '{parsed.module}' does not have."
|
|
103
101
|
)
|
|
102
|
+
functionality = functionalities[parsed.func_id]
|
|
103
|
+
functionality[CONFORMANCE_TESTS].extend(s.to_dict() for s in parsed.conformance_tests)
|
|
104
|
+
functionality[ACCEPTANCE_TESTS].extend(s.to_dict() for s in parsed.acceptance_tests)
|
|
105
|
+
bound.append(BoundTestPlainFile(path, name, parsed))
|
|
106
|
+
return bound
|
|
104
107
|
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
|
|
109
|
+
def _visible_concepts(tree: dict) -> Set[str]:
|
|
110
|
+
concepts: Set[str] = set()
|
|
111
|
+
for item in tree.get(DEFINITIONS, []):
|
|
112
|
+
concepts.update(extract_concepts_from_definition(item["markdown"])[0])
|
|
113
|
+
return concepts
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def check_test_plain_files(chain, bound) -> None:
|
|
117
|
+
"""Check the scenarios of the bound files: no id used twice for a functionality, no undefined concept."""
|
|
118
|
+
visible = {name: _visible_concepts(tree) for name, tree in chain}
|
|
119
|
+
first_use: Dict[Tuple[str, str, str], Tuple[int, str]] = {}
|
|
120
|
+
for file in bound:
|
|
121
|
+
parsed = file.parsed
|
|
122
|
+
for scenario in parsed.scenarios:
|
|
123
|
+
key = (parsed.module, parsed.func_id, scenario.id)
|
|
124
|
+
if key in first_use:
|
|
125
|
+
first_line, first_name = first_use[key]
|
|
126
|
+
raise PlainSyntaxError(
|
|
127
|
+
f"Plain syntax error: Syntax error at line {scenario.line_number} in test plain file "
|
|
128
|
+
f"'{file.name}': Duplicate scenario-id `{scenario.id}` for functionality "
|
|
129
|
+
f"'{parsed.module}:{parsed.func_id}'; it is first used at line {first_line} in test plain file "
|
|
130
|
+
f"'{first_name}'."
|
|
131
|
+
)
|
|
132
|
+
first_use[key] = (scenario.line_number, file.name)
|
|
133
|
+
errors = validate_scenario_concepts(parsed, visible[parsed.module])
|
|
107
134
|
if errors:
|
|
108
135
|
raise PlainSyntaxError(
|
|
109
|
-
f"Plain syntax error: Found {len(errors)} errors in test plain file '{name}':\n"
|
|
136
|
+
f"Plain syntax error: Found {len(errors)} errors in test plain file '{file.name}':\n"
|
|
137
|
+
+ "\n".join(errors)
|
|
110
138
|
)
|
|
111
139
|
|
|
112
|
-
places = scenario_places.setdefault((parsed.module, parsed.func_id), {})
|
|
113
|
-
for key, scenarios in (
|
|
114
|
-
(plain_spec.CONFORMANCE_TESTS, parsed.conformance_tests),
|
|
115
|
-
(plain_spec.ACCEPTANCE_TESTS, parsed.acceptance_tests),
|
|
116
|
-
):
|
|
117
|
-
for scenario in scenarios:
|
|
118
|
-
if scenario.id in places:
|
|
119
|
-
raise PlainSyntaxError(
|
|
120
|
-
f"Plain syntax error: Syntax error at line {scenario.line_number} in test plain file "
|
|
121
|
-
f"'{name}': Duplicate scenario-id `{scenario.id}` for functionality "
|
|
122
|
-
f"'{parsed.module}:{parsed.func_id}'; it is first used {places[scenario.id]}."
|
|
123
|
-
)
|
|
124
|
-
places[scenario.id] = f"at line {scenario.line_number} in test plain file '{name}'"
|
|
125
|
-
functionality[key].append(scenario.to_dict())
|
|
126
140
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
def check_test_plain_files(chain: list[tuple[str, dict]], bound: list[BoundTestPlainFile]) -> None:
|
|
133
|
-
"""Raise for the first functionality of the chain, in render order, that has no test plain file bound to it.
|
|
134
|
-
|
|
135
|
-
The sections of the file may be empty; the file itself is what the check asks for.
|
|
136
|
-
"""
|
|
137
|
-
bound_functionalities = {(item.parsed.module, item.parsed.func_id) for item in bound}
|
|
138
|
-
for module_name, tree in chain:
|
|
139
|
-
for functionality in tree.get(plain_spec.FUNCTIONAL_REQUIREMENTS) or []:
|
|
140
|
-
func_id = functionality["id"]
|
|
141
|
-
if (module_name, func_id) not in bound_functionalities:
|
|
141
|
+
def _require_test_plain_files(chain, bound) -> None:
|
|
142
|
+
bound_keys = {(file.parsed.module, file.parsed.func_id) for file in bound}
|
|
143
|
+
for module, tree in chain:
|
|
144
|
+
for functionality in tree.get(FUNCTIONAL_REQUIREMENTS, []):
|
|
145
|
+
if (module, functionality["id"]) not in bound_keys:
|
|
142
146
|
raise MissingTestPlainFileError(
|
|
143
|
-
f"Functionality {
|
|
144
|
-
f"tests/{
|
|
147
|
+
f"Functionality {module}:{functionality['id']} has no test plain file. Add one, by convention "
|
|
148
|
+
f"tests/{module}/{functionality['id']}.plain, with the add-test-scenarios skill."
|
|
145
149
|
)
|
|
146
150
|
|
|
147
151
|
|
|
152
|
+
def _entry_dir(plain_file_name: str, template_dirs: List[str]) -> str:
|
|
153
|
+
if os.path.isabs(plain_file_name):
|
|
154
|
+
return os.path.dirname(plain_file_name)
|
|
155
|
+
resolved = resolve_linked_resource(template_dirs, plain_file_name)
|
|
156
|
+
return os.path.dirname(resolved) if resolved else os.path.dirname(os.path.abspath(plain_file_name))
|
|
157
|
+
|
|
158
|
+
|
|
148
159
|
def parse_project(
|
|
149
|
-
plain_file_name: str, template_dirs:
|
|
160
|
+
plain_file_name: str, template_dirs: List[str], tests_folders: List[str], require_test_plain_files: bool
|
|
150
161
|
) -> ProjectParseResult:
|
|
151
|
-
"""Parse
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
chain = plain_file.parse_module_chain(plain_file_name, template_dirs)
|
|
157
|
-
bound = bind_test_plain_files(chain, tests_folders, template_dirs, template_dirs[0])
|
|
162
|
+
"""Parse an entry module with its requires chain and bind the test plain files to its functionalities."""
|
|
163
|
+
chain = parse_module_chain(plain_file_name, template_dirs)
|
|
164
|
+
entry_dir = _entry_dir(plain_file_name, template_dirs)
|
|
165
|
+
bound = bind_test_plain_files(chain, tests_folders, template_dirs, entry_dir)
|
|
166
|
+
check_test_plain_files(chain, bound)
|
|
158
167
|
if require_test_plain_files:
|
|
159
|
-
|
|
168
|
+
_require_test_plain_files(chain, bound)
|
|
160
169
|
return ProjectParseResult(chain, bound)
|