plain-parser 0.1.5.dev18__py3-none-any.whl → 0.2.0__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 CHANGED
@@ -1,6 +1,6 @@
1
1
  """Parser for ***plain specification files."""
2
2
 
3
- from plain_parser import concept_utils, exceptions, loaders, plain_file, plain_spec
3
+ from plain_parser import concept_utils, exceptions, loaders, plain_file, plain_spec, project, test_plain_file
4
4
  from plain_parser.plain_file import (
5
5
  PlainFileParseResult,
6
6
  get_filename_from_module_name,
@@ -26,4 +26,6 @@ __all__ = [
26
26
  "plain_file",
27
27
  "plain_file_parser",
28
28
  "plain_spec",
29
+ "project",
30
+ "test_plain_file",
29
31
  ]
plain_parser/cli.py CHANGED
@@ -1,18 +1,21 @@
1
- """Command-line entry point: ``plain-parser {check,parse} <file.plain> [--template-dir DIR] [--config-name NAME]``."""
1
+ """Command-line entry point: ``plain-parser {check,parse} <file.plain> [--template-dir DIR] [--tests-folder DIR]...
2
+ [--config-name NAME]``."""
2
3
 
3
4
  import argparse
4
5
  import json
5
6
  import os
6
7
  import sys
7
- from dataclasses import dataclass
8
+ from dataclasses import dataclass, field
8
9
 
9
10
  import yaml
10
11
 
11
- from plain_parser import loaders, plain_file, plain_spec
12
+ from plain_parser import loaders, plain_spec, project
12
13
 
13
14
  DEFAULT_CONFIG_NAME = "config.yaml"
14
15
  TEMPLATE_DIR_CONFIG_KEYS = ("template_dir", "template-dir")
15
- PARSE_FORMAT_VERSION = 1
16
+ TESTS_FOLDER_CONFIG_KEYS = ("tests_folder", "tests-folder")
17
+ CONFORMANCE_TESTS_SCRIPT_CONFIG_KEYS = ("conformance_tests_script", "conformance-tests-script")
18
+ PARSE_FORMAT_VERSION = 2
16
19
 
17
20
 
18
21
  class AmbiguousConfigFileError(Exception):
@@ -25,13 +28,13 @@ def build_parser() -> argparse.ArgumentParser:
25
28
 
26
29
  check_parser = subparsers.add_parser(
27
30
  "check",
28
- help="Validate a .plain module and its requires chain, and read every linked file: "
31
+ help="Validate a .plain module, its requires chain and its test plain files, and read every linked file: "
29
32
  "binary files and large base64 blobs are rejected.",
30
33
  )
31
34
  parse_parser = subparsers.add_parser(
32
35
  "parse",
33
- help="Print the parse result of a .plain module and its requires chain as JSON. Linked files are "
34
- "located but never read.",
36
+ help="Print the parse result of a .plain module, its requires chain and its test plain files as JSON. "
37
+ "Linked files are located but never read.",
35
38
  )
36
39
  for subparser in (check_parser, parse_parser):
37
40
  subparser.add_argument("plain_file", help="Path to the .plain module.")
@@ -40,11 +43,18 @@ def build_parser() -> argparse.ArgumentParser:
40
43
  help="Directory searched for modules and templates after the .plain file's own directory. "
41
44
  "Overrides the template-dir value of the config file.",
42
45
  )
46
+ subparser.add_argument(
47
+ "--tests-folder",
48
+ action="append",
49
+ metavar="DIR",
50
+ help="Directory scanned for test plain files; repeat the option for more than one. Overrides the "
51
+ "tests-folder value of the config file. Without both, no test plain files are read.",
52
+ )
43
53
  subparser.add_argument(
44
54
  "--config-name",
45
55
  default=DEFAULT_CONFIG_NAME,
46
- help="Name of the config file to read template-dir from. Looked up in the .plain file's directory "
47
- "and the current working directory. Defaults to %(default)s.",
56
+ help="Name of the config file to read template-dir, tests-folder and conformance-tests-script from. "
57
+ "Looked up in the .plain file's directory and the current working directory. Defaults to %(default)s.",
48
58
  )
49
59
  return parser
50
60
 
@@ -71,17 +81,39 @@ def resolve_config_file(config_name: str, plain_file_path: str) -> str | None:
71
81
  return None
72
82
 
73
83
 
74
- def template_dir_from_config(config_file: str) -> str | None:
75
- """The template-dir value of the config file, resolved against the file's directory. Other keys are ignored."""
84
+ @dataclass
85
+ class Config:
86
+ template_dir: str | None = None
87
+ tests_folders: list[str] | None = None # None when the config does not set tests-folder.
88
+ conformance_tests_enabled: bool = False # Whether the config names a conformance-tests-script.
89
+
90
+
91
+ def _config_value(config: dict, keys: tuple[str, ...]):
92
+ """The first value set under one of the key spellings, or None."""
93
+ return next((config[key] for key in keys if config.get(key)), None)
94
+
95
+
96
+ def read_config(config_file: str) -> Config:
97
+ """The values plain-parser reads from the config file, paths resolved against the file's directory. Other keys
98
+ are ignored."""
76
99
  with open(config_file) as f:
77
100
  config = yaml.safe_load(f) or {}
78
101
 
79
- for key in TEMPLATE_DIR_CONFIG_KEYS:
80
- value = config.get(key)
81
- if value:
82
- value = os.path.expanduser(str(value))
83
- return value if os.path.isabs(value) else os.path.join(os.path.dirname(config_file), value)
84
- return None
102
+ def resolve(value) -> str:
103
+ value = os.path.expanduser(str(value))
104
+ return value if os.path.isabs(value) else os.path.join(os.path.dirname(config_file), value)
105
+
106
+ result = Config()
107
+ template_dir = _config_value(config, TEMPLATE_DIR_CONFIG_KEYS)
108
+ if template_dir:
109
+ result.template_dir = resolve(template_dir)
110
+ tests_folder = _config_value(config, TESTS_FOLDER_CONFIG_KEYS)
111
+ if tests_folder:
112
+ result.tests_folders = [
113
+ resolve(value) for value in (tests_folder if isinstance(tests_folder, list) else [tests_folder])
114
+ ]
115
+ result.conformance_tests_enabled = bool(_config_value(config, CONFORMANCE_TESTS_SCRIPT_CONFIG_KEYS))
116
+ return result
85
117
 
86
118
 
87
119
  @dataclass
@@ -92,20 +124,36 @@ class ParseResult:
92
124
  # Each link target in the chain, as written, to the path it resolves to, relative to the entry module's dir
93
125
  # with `/` as separator (loaders.relative_resource_path).
94
126
  resources: dict[str, str]
95
-
96
-
97
- def parse_spec(plain_file_path: str, template_dir: str | None, load_resources: bool) -> ParseResult:
98
- """Parse the module and its requires chain, resolve its links, and walk the entry's FRIDs; raise on the first error.
99
-
100
- With ``load_resources``, each module's linked files are also read and checked (text only, no large base64 blob) right
101
- after that module's links are collected, so a resource error is reported before a FRID-walk error.
127
+ # Each test plain file, by its path relative to the entry module's dir with `/`, to the functionality it binds to.
128
+ test_plain_files: dict[str, dict] = field(default_factory=dict)
129
+
130
+
131
+ def parse_spec(
132
+ plain_file_path: str,
133
+ template_dir: str | None,
134
+ load_resources: bool,
135
+ tests_folders: list[str] | None = None,
136
+ require_test_plain_files: bool = False,
137
+ ) -> ParseResult:
138
+ """Parse the module, its requires chain and its test plain files, resolve the links, and walk the entry's
139
+ FRIDs; raise on the first error.
140
+
141
+ Without ``tests_folders`` no test plain files are read. With ``require_test_plain_files``, every functionality needs a
142
+ test plain file bound to it. With ``load_resources``, each module's linked files are
143
+ also read and checked (text only, no large base64 blob) right after that module's links are collected, so a
144
+ resource error is reported before a FRID-walk error.
102
145
  """
103
146
  # Absolute, so the chain parse and the resource paths below search the same directories.
104
- template_dirs = [os.path.dirname(os.path.abspath(plain_file_path))]
147
+ entry_dir = os.path.dirname(os.path.abspath(plain_file_path))
148
+ template_dirs = [entry_dir]
105
149
  if template_dir:
106
150
  template_dirs.append(os.path.abspath(template_dir))
151
+ tests_folders = [os.path.abspath(tests_folder) for tests_folder in tests_folders or []]
107
152
 
108
- chain = plain_file.parse_module_chain(os.path.basename(plain_file_path), template_dirs)
153
+ parsed = project.parse_project(
154
+ os.path.basename(plain_file_path), template_dirs, tests_folders, require_test_plain_files
155
+ )
156
+ chain = parsed.chain
109
157
 
110
158
  # The target joined as written onto its template dir (`..` kept, symlinks not resolved), made relative to the
111
159
  # entry module's dir so the result does not depend on where the project sits on the machine.
@@ -124,12 +172,21 @@ def parse_spec(plain_file_path: str, template_dir: str | None, load_resources: b
124
172
  for frid in plain_spec.get_frids(top_plain_source_tree):
125
173
  plain_spec.get_specifications_for_frid(top_plain_source_tree, frid)
126
174
 
127
- return ParseResult(chain, resources)
175
+ test_plain_files = {
176
+ bound.name: {"module": bound.parsed.module, "func_id": bound.parsed.func_id}
177
+ for bound in parsed.test_plain_files
178
+ }
179
+ return ParseResult(chain, resources, test_plain_files)
128
180
 
129
181
 
130
- def check(plain_file_path: str, template_dir: str | None) -> None:
131
- """Raise if the module, its requires chain, or its linked resources are invalid."""
132
- parse_spec(plain_file_path, template_dir, load_resources=True)
182
+ def check(
183
+ plain_file_path: str,
184
+ template_dir: str | None,
185
+ tests_folders: list[str] | None = None,
186
+ require_test_plain_files: bool = False,
187
+ ) -> None:
188
+ """Raise if the module, its requires chain, its test plain files, or its linked resources are invalid."""
189
+ parse_spec(plain_file_path, template_dir, True, tests_folders, require_test_plain_files)
133
190
 
134
191
 
135
192
  def _error_output(type_name: str, message: str) -> dict:
@@ -146,15 +203,21 @@ def _encode_output(output: dict) -> bytes:
146
203
  return (json.dumps(output, indent=2, ensure_ascii=False) + "\n").encode("utf-8")
147
204
 
148
205
 
149
- def parse(plain_file_path: str, template_dir: str | None) -> int:
206
+ def parse(
207
+ plain_file_path: str,
208
+ template_dir: str | None,
209
+ tests_folders: list[str] | None = None,
210
+ require_test_plain_files: bool = False,
211
+ ) -> int:
150
212
  """Print the parse result, or the parser error, as one UTF-8 JSON document on stdout; return the exit code."""
151
213
  output: dict
152
214
  try:
153
- result = parse_spec(plain_file_path, template_dir, load_resources=False)
215
+ result = parse_spec(plain_file_path, template_dir, False, tests_folders, require_test_plain_files)
154
216
  output = {
155
217
  "format_version": PARSE_FORMAT_VERSION,
156
218
  "modules": [{"module": module_name, "tree": tree} for module_name, tree in result.modules],
157
219
  "resources": result.resources,
220
+ "test_plain_files": result.test_plain_files,
158
221
  }
159
222
  exit_code = 0
160
223
  except Exception as e:
@@ -209,10 +272,17 @@ def main(argv: list[str] | None = None) -> int:
209
272
 
210
273
  try:
211
274
  # The config file is resolved even when --template-dir is given, so an ambiguous config is always an error.
212
- config_template_dir = _template_dir_from_config(parser, args)
275
+ config = _read_config(parser, args)
276
+ template_dir = args.template_dir or config.template_dir
277
+ tests_folders = args.tests_folder or config.tests_folders
278
+ if config.conformance_tests_enabled and not tests_folders:
279
+ parser.error(
280
+ "conformance-tests-script is set in the config file, but no tests folder is given. "
281
+ "Pass --tests-folder or set tests-folder in the config file."
282
+ )
213
283
  if args.command == "parse":
214
- return parse(args.plain_file, args.template_dir or config_template_dir)
215
- check(args.plain_file, args.template_dir or config_template_dir)
284
+ return parse(args.plain_file, template_dir, tests_folders, config.conformance_tests_enabled)
285
+ check(args.plain_file, template_dir, tests_folders, config.conformance_tests_enabled)
216
286
  except KeyboardInterrupt:
217
287
  return 130
218
288
  except Exception as e:
@@ -223,10 +293,10 @@ def main(argv: list[str] | None = None) -> int:
223
293
  return 0
224
294
 
225
295
 
226
- def _template_dir_from_config(parser: argparse.ArgumentParser, args: argparse.Namespace) -> str | None:
296
+ def _read_config(parser: argparse.ArgumentParser, args: argparse.Namespace) -> Config:
227
297
  try:
228
298
  config_file = resolve_config_file(args.config_name, args.plain_file)
229
- return template_dir_from_config(config_file) if config_file is not None else None
299
+ return read_config(config_file) if config_file is not None else Config()
230
300
  except AmbiguousConfigFileError as e:
231
301
  parser.error(str(e))
232
302
  except Exception as e:
@@ -117,15 +117,6 @@ def validate_concepts(marshalled_plain_source) -> list[str]:
117
117
  ]:
118
118
  errors.extend(validate_concepts_in_spec_group(spec_group, marshalled_plain_source, tmp_tree_based_concepts))
119
119
 
120
- if plain_spec.FUNCTIONAL_REQUIREMENTS in marshalled_plain_source:
121
- for func_spec in marshalled_plain_source[plain_spec.FUNCTIONAL_REQUIREMENTS]:
122
- if "acceptance_tests" not in func_spec:
123
- continue
124
-
125
- acceptance_tests = func_spec["acceptance_tests"]
126
- for spec in acceptance_tests:
127
- errors.extend(validate_concepts_in_spec(spec, tmp_tree_based_concepts, plain_spec.ACCEPTANCE_TESTS))
128
-
129
120
  return errors
130
121
 
131
122
 
@@ -37,3 +37,9 @@ class ImportedModuleWithFunctionalitiesError(Exception):
37
37
  """
38
38
 
39
39
  pass
40
+
41
+
42
+ class MissingTestPlainFileError(Exception):
43
+ """Raised when conformance tests are enabled and a functionality has no test plain file bound to it."""
44
+
45
+ pass