jarbin-c-coding-style 2.0.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.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import xml.etree.ElementTree as ElementTree
|
|
3
|
+
from itertools import groupby
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from sources.constants import PROGRAM_NAME
|
|
7
|
+
from sources.outputs.junit.renderer import INVALID_XML_CHARACTERS
|
|
8
|
+
from sources.subcommands.test.runner import STATUS_ERROR, STATUS_FAILED, STATUS_PASSED, TestCase, TestResult
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Version of the `test --format json` output.
|
|
12
|
+
TEST_REPORT_VERSION: int = 1
|
|
13
|
+
|
|
14
|
+
# Label of each status in the text report.
|
|
15
|
+
STATUS_LABELS: dict[str, str] = {STATUS_PASSED: "PASS", STATUS_FAILED: "FAIL", STATUS_ERROR: "ERROR"}
|
|
16
|
+
|
|
17
|
+
# Style method of each status in the text report.
|
|
18
|
+
STATUS_STYLES: dict[str, str] = {STATUS_PASSED: "success", STATUS_FAILED: "failure", STATUS_ERROR: "error"}
|
|
19
|
+
|
|
20
|
+
# Longest captured output kept per failing test in the text report, in characters.
|
|
21
|
+
TEXT_OUTPUT_LIMIT: int = 2000
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _xml(text: str) -> str:
|
|
25
|
+
"""
|
|
26
|
+
Make text safe for XML 1.0.
|
|
27
|
+
|
|
28
|
+
Parameters
|
|
29
|
+
----------
|
|
30
|
+
text : str
|
|
31
|
+
Raw text.
|
|
32
|
+
|
|
33
|
+
Returns
|
|
34
|
+
----------
|
|
35
|
+
str
|
|
36
|
+
The text with forbidden characters replaced.
|
|
37
|
+
"""
|
|
38
|
+
return INVALID_XML_CHARACTERS.sub("�", text)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def counts(results: list[TestResult]) -> dict[str, int]:
|
|
42
|
+
"""
|
|
43
|
+
Number of tests per status.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
results : list[TestResult]
|
|
48
|
+
The results.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
----------
|
|
52
|
+
dict[str, int]
|
|
53
|
+
total, passed, failed, errors.
|
|
54
|
+
"""
|
|
55
|
+
return {
|
|
56
|
+
"total": len(results),
|
|
57
|
+
"passed": sum(1 for result in results if result.status == STATUS_PASSED),
|
|
58
|
+
"failed": sum(1 for result in results if result.status == STATUS_FAILED),
|
|
59
|
+
"errors": sum(1 for result in results if result.status == STATUS_ERROR),
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def summary_line(results: list[TestResult], seed: int | None) -> str:
|
|
64
|
+
"""
|
|
65
|
+
One-line summary of a run.
|
|
66
|
+
|
|
67
|
+
Parameters
|
|
68
|
+
----------
|
|
69
|
+
results : list[TestResult]
|
|
70
|
+
The results.
|
|
71
|
+
seed : int | None
|
|
72
|
+
Ordering seed.
|
|
73
|
+
|
|
74
|
+
Returns
|
|
75
|
+
----------
|
|
76
|
+
str
|
|
77
|
+
e.g. "Tests: 380 passed, 0 failed, 0 errors (380 total) in 12.345 s".
|
|
78
|
+
"""
|
|
79
|
+
total: dict[str, int] = counts(results)
|
|
80
|
+
seconds: float = sum(result.duration for result in results)
|
|
81
|
+
seeded: str = f", seed {seed}" if seed is not None else ""
|
|
82
|
+
return f"Tests: {total['passed']} passed, {total['failed']} failed, {total['errors']} errors ({total['total']} total) in {seconds:.3f} s{seeded}"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def render_text(results: list[TestResult], seed: int | None, paint: Any) -> str:
|
|
86
|
+
"""
|
|
87
|
+
Text report: one line per test, then the details of every test that did not pass, then the summary.
|
|
88
|
+
|
|
89
|
+
Parameters
|
|
90
|
+
----------
|
|
91
|
+
results : list[TestResult]
|
|
92
|
+
The results, in execution order.
|
|
93
|
+
seed : int | None
|
|
94
|
+
Ordering seed.
|
|
95
|
+
paint : Any
|
|
96
|
+
Callable (style name, text) -> text.
|
|
97
|
+
|
|
98
|
+
Returns
|
|
99
|
+
----------
|
|
100
|
+
str
|
|
101
|
+
The report.
|
|
102
|
+
"""
|
|
103
|
+
lines: list[str] = [
|
|
104
|
+
f"{paint(STATUS_STYLES[result.status], STATUS_LABELS[result.status].ljust(5))} {result.case.id} ({result.duration * 1000:.1f} ms)"
|
|
105
|
+
for result in results
|
|
106
|
+
]
|
|
107
|
+
problems: list[TestResult] = [result for result in results if result.status != STATUS_PASSED]
|
|
108
|
+
if problems:
|
|
109
|
+
lines.append("")
|
|
110
|
+
for result in problems:
|
|
111
|
+
lines.append(paint(STATUS_STYLES[result.status], f"{STATUS_LABELS[result.status]} {result.case.id}"))
|
|
112
|
+
lines.extend(f" {line}" for line in result.message.splitlines() or [""])
|
|
113
|
+
output: str = result.output.strip()
|
|
114
|
+
if output:
|
|
115
|
+
if len(output) > TEXT_OUTPUT_LIMIT:
|
|
116
|
+
output = "..." + output[-TEXT_OUTPUT_LIMIT:]
|
|
117
|
+
lines.append(" output:")
|
|
118
|
+
lines.extend(f" {line}" for line in output.splitlines())
|
|
119
|
+
lines.append("")
|
|
120
|
+
lines.append(paint("success" if not problems else "failure", summary_line(results, seed)))
|
|
121
|
+
return "\n".join(lines) + "\n"
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def render_json(results: list[TestResult], seed: int | None) -> str:
|
|
125
|
+
"""
|
|
126
|
+
JSON report.
|
|
127
|
+
|
|
128
|
+
Parameters
|
|
129
|
+
----------
|
|
130
|
+
results : list[TestResult]
|
|
131
|
+
The results, in execution order.
|
|
132
|
+
seed : int | None
|
|
133
|
+
Ordering seed.
|
|
134
|
+
|
|
135
|
+
Returns
|
|
136
|
+
----------
|
|
137
|
+
str
|
|
138
|
+
The document, with a final newline.
|
|
139
|
+
"""
|
|
140
|
+
document: dict[str, Any] = {
|
|
141
|
+
"schema_version": TEST_REPORT_VERSION,
|
|
142
|
+
"seed": seed,
|
|
143
|
+
"summary": {**counts(results), "duration_ms": round(sum(result.duration for result in results) * 1000, 3)},
|
|
144
|
+
"tests": [
|
|
145
|
+
{
|
|
146
|
+
"id": result.case.id,
|
|
147
|
+
"suite": result.case.suite,
|
|
148
|
+
"status": result.status,
|
|
149
|
+
"duration_ms": round(result.duration * 1000, 3),
|
|
150
|
+
"message": result.message,
|
|
151
|
+
"output": result.output,
|
|
152
|
+
}
|
|
153
|
+
for result in results
|
|
154
|
+
],
|
|
155
|
+
}
|
|
156
|
+
return json.dumps(document, indent=2, ensure_ascii=False) + "\n"
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def render_junit(results: list[TestResult]) -> str:
|
|
160
|
+
"""
|
|
161
|
+
JUnit XML report: one test suite per group (ID without its last part), one test case per test.
|
|
162
|
+
|
|
163
|
+
Parameters
|
|
164
|
+
----------
|
|
165
|
+
results : list[TestResult]
|
|
166
|
+
The results.
|
|
167
|
+
|
|
168
|
+
Returns
|
|
169
|
+
----------
|
|
170
|
+
str
|
|
171
|
+
The XML document, with a final newline.
|
|
172
|
+
"""
|
|
173
|
+
root: ElementTree.Element = ElementTree.Element("testsuites", {"name": PROGRAM_NAME})
|
|
174
|
+
ordered: list[TestResult] = sorted(results, key=lambda result: result.case.id)
|
|
175
|
+
for group, items in groupby(ordered, key=lambda result: result.case.group):
|
|
176
|
+
members: list[TestResult] = list(items)
|
|
177
|
+
total: dict[str, int] = counts(members)
|
|
178
|
+
suite: ElementTree.Element = ElementTree.SubElement(root, "testsuite", {
|
|
179
|
+
"name": _xml(group), "tests": str(total["total"]), "failures": str(total["failed"]), "errors": str(total["errors"]),
|
|
180
|
+
"skipped": "0", "time": f"{sum(member.duration for member in members):.6f}",
|
|
181
|
+
})
|
|
182
|
+
for result in members:
|
|
183
|
+
case: ElementTree.Element = ElementTree.SubElement(suite, "testcase", {
|
|
184
|
+
"name": _xml(result.case.name), "classname": _xml(group), "time": f"{result.duration:.6f}",
|
|
185
|
+
})
|
|
186
|
+
if result.status != STATUS_PASSED:
|
|
187
|
+
tag: str = "failure" if result.status == STATUS_FAILED else "error"
|
|
188
|
+
ElementTree.SubElement(case, tag, {"message": _xml(result.message.splitlines()[0] if result.message else tag)}).text = _xml(result.message)
|
|
189
|
+
if result.output:
|
|
190
|
+
ElementTree.SubElement(case, "system-out").text = _xml(result.output)
|
|
191
|
+
total = counts(results)
|
|
192
|
+
root.set("tests", str(total["total"]))
|
|
193
|
+
root.set("failures", str(total["failed"]))
|
|
194
|
+
root.set("errors", str(total["errors"]))
|
|
195
|
+
ElementTree.indent(root, space=" ")
|
|
196
|
+
return ElementTree.tostring(root, encoding="unicode", xml_declaration=True) + "\n"
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def render_list(cases: list[TestCase], as_json: bool, unavailable: dict[str, str]) -> str:
|
|
200
|
+
"""
|
|
201
|
+
`--list`: the IDs of the selected tests.
|
|
202
|
+
|
|
203
|
+
Parameters
|
|
204
|
+
----------
|
|
205
|
+
cases : list[TestCase]
|
|
206
|
+
Selected tests, in declaration order.
|
|
207
|
+
as_json : bool
|
|
208
|
+
JSON output.
|
|
209
|
+
unavailable : dict[str, str]
|
|
210
|
+
Unavailable suites, with the reason.
|
|
211
|
+
|
|
212
|
+
Returns
|
|
213
|
+
----------
|
|
214
|
+
str
|
|
215
|
+
One ID per line, or a JSON document.
|
|
216
|
+
"""
|
|
217
|
+
if as_json:
|
|
218
|
+
return json.dumps({"schema_version": TEST_REPORT_VERSION, "tests": [case.id for case in cases], "unavailable": unavailable}, indent=2) + "\n"
|
|
219
|
+
return "".join(f"{case.id}\n" for case in cases)
|
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
import fnmatch
|
|
3
|
+
import importlib
|
|
4
|
+
import io
|
|
5
|
+
import os
|
|
6
|
+
import random
|
|
7
|
+
import signal
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
from collections.abc import Callable, Iterator
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from types import FrameType
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from sources.utils.build_info import INSTALL_ROOT
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Suite of the self-test shipped with the program (plan 11, section 8).
|
|
20
|
+
SELFTEST_SUITE: str = "selftest"
|
|
21
|
+
|
|
22
|
+
# Suite of the development JarTest suite (tests/, checkout only).
|
|
23
|
+
JARTEST_SUITE: str = "jartest"
|
|
24
|
+
|
|
25
|
+
# Test statuses.
|
|
26
|
+
STATUS_PASSED: str = "passed"
|
|
27
|
+
STATUS_FAILED: str = "failed"
|
|
28
|
+
STATUS_ERROR: str = "error"
|
|
29
|
+
|
|
30
|
+
# Prefix of the JarTest group names, removed from test IDs.
|
|
31
|
+
JARTEST_ROOT_NAME: str = "JTT_JCCS"
|
|
32
|
+
|
|
33
|
+
# Prefix of the JarTest test function names, removed from test IDs.
|
|
34
|
+
JARTEST_TEST_PREFIX: str = "JT_"
|
|
35
|
+
|
|
36
|
+
# Characters that make a --filter pattern a glob.
|
|
37
|
+
GLOB_CHARACTERS: frozenset[str] = frozenset("*?[")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TestTimeout(BaseException):
|
|
41
|
+
"""
|
|
42
|
+
Raised in a test that exceeds `--timeout` (a BaseException so that test code catching Exception, and
|
|
43
|
+
JarTest's own benchmark, let it through).
|
|
44
|
+
(BaseException)
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True, slots=True)
|
|
49
|
+
class TestCase:
|
|
50
|
+
"""
|
|
51
|
+
One runnable test.
|
|
52
|
+
(dataclass)
|
|
53
|
+
|
|
54
|
+
Attributes
|
|
55
|
+
----------
|
|
56
|
+
id : str
|
|
57
|
+
`<suite>/<group>/.../<name>`.
|
|
58
|
+
suite : str
|
|
59
|
+
"selftest" or "jartest".
|
|
60
|
+
execute : Callable[[], tuple[str, str]]
|
|
61
|
+
Runs the test and returns (status, message).
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
id: str
|
|
65
|
+
suite: str
|
|
66
|
+
execute: Callable[[], tuple[str, str]]
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def group(self) -> str:
|
|
70
|
+
"""
|
|
71
|
+
Group of the test (its ID without the last part).
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
----------
|
|
75
|
+
str
|
|
76
|
+
e.g. "jartest/CLI/Parser".
|
|
77
|
+
"""
|
|
78
|
+
return self.id.rpartition("/")[0] or self.suite
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def name(self) -> str:
|
|
82
|
+
"""
|
|
83
|
+
Name of the test (last part of its ID).
|
|
84
|
+
|
|
85
|
+
Returns
|
|
86
|
+
----------
|
|
87
|
+
str
|
|
88
|
+
The name.
|
|
89
|
+
"""
|
|
90
|
+
return self.id.rpartition("/")[2]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass(frozen=True, slots=True)
|
|
94
|
+
class TestResult:
|
|
95
|
+
"""
|
|
96
|
+
Outcome of one test.
|
|
97
|
+
(dataclass)
|
|
98
|
+
|
|
99
|
+
Attributes
|
|
100
|
+
----------
|
|
101
|
+
case : TestCase
|
|
102
|
+
The test.
|
|
103
|
+
status : str
|
|
104
|
+
passed, failed or error.
|
|
105
|
+
message : str
|
|
106
|
+
Why it did not pass ("" when it passed).
|
|
107
|
+
duration : float
|
|
108
|
+
Duration, in seconds.
|
|
109
|
+
output : str
|
|
110
|
+
What the test printed (stdout and stderr).
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
case: TestCase
|
|
114
|
+
status: str
|
|
115
|
+
message: str
|
|
116
|
+
duration: float
|
|
117
|
+
output: str
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def plain_call(function: Callable[[], None]) -> Callable[[], tuple[str, str]]:
|
|
121
|
+
"""
|
|
122
|
+
Wrap a self-test function: an AssertionError is a failure, any other exception an error.
|
|
123
|
+
|
|
124
|
+
Parameters
|
|
125
|
+
----------
|
|
126
|
+
function : Callable[[], None]
|
|
127
|
+
The test.
|
|
128
|
+
|
|
129
|
+
Returns
|
|
130
|
+
----------
|
|
131
|
+
Callable[[], tuple[str, str]]
|
|
132
|
+
The wrapped test.
|
|
133
|
+
"""
|
|
134
|
+
def execute() -> tuple[str, str]:
|
|
135
|
+
try:
|
|
136
|
+
function()
|
|
137
|
+
except AssertionError as error:
|
|
138
|
+
return STATUS_FAILED, str(error) or "assertion failed"
|
|
139
|
+
except Exception as error: # noqa: BLE001 - every unexpected exception is a test error
|
|
140
|
+
return STATUS_ERROR, f"{type(error).__name__}: {error}"
|
|
141
|
+
return STATUS_PASSED, ""
|
|
142
|
+
|
|
143
|
+
return execute
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _assertion_message(assertion: Any) -> str:
|
|
147
|
+
"""
|
|
148
|
+
Text of a failed JarTest assertion.
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
assertion : Any
|
|
153
|
+
A jarbin_toolkit_jartest AssertionResult.
|
|
154
|
+
|
|
155
|
+
Returns
|
|
156
|
+
----------
|
|
157
|
+
str
|
|
158
|
+
The message, with the compared values when there are some.
|
|
159
|
+
"""
|
|
160
|
+
message: str = assertion.message or assertion.name
|
|
161
|
+
values: tuple[Any, ...] = tuple(assertion.values or ())
|
|
162
|
+
if len(values) == 2:
|
|
163
|
+
message += f" | {values[0]!r} {assertion.meta.get('operator', '?')} {values[1]!r}"
|
|
164
|
+
elif len(values) == 1:
|
|
165
|
+
message += f" | {values[0]!r}"
|
|
166
|
+
return message
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def jartest_call(benchmark: Any) -> Callable[[], tuple[str, str]]:
|
|
170
|
+
"""
|
|
171
|
+
Wrap a JarTest test: it runs in a fresh Benchmark (so repeated runs never mix results), with the test's
|
|
172
|
+
own context setup and teardown.
|
|
173
|
+
|
|
174
|
+
Parameters
|
|
175
|
+
----------
|
|
176
|
+
benchmark : Any
|
|
177
|
+
The jarbin_toolkit_jartest Benchmark of the test.
|
|
178
|
+
|
|
179
|
+
Returns
|
|
180
|
+
----------
|
|
181
|
+
Callable[[], tuple[str, str]]
|
|
182
|
+
The wrapped test.
|
|
183
|
+
"""
|
|
184
|
+
def execute() -> tuple[str, str]:
|
|
185
|
+
from jarbin_toolkit_jartest.benchmark import Benchmark
|
|
186
|
+
|
|
187
|
+
fresh: Any = Benchmark(benchmark.test)
|
|
188
|
+
fresh.context = benchmark.context
|
|
189
|
+
if not fresh.context.setup():
|
|
190
|
+
return STATUS_ERROR, "test setup failed"
|
|
191
|
+
try:
|
|
192
|
+
fresh(1)
|
|
193
|
+
finally:
|
|
194
|
+
fresh.context.teardown()
|
|
195
|
+
if fresh.error is not None:
|
|
196
|
+
return STATUS_ERROR, f"{type(fresh.error).__name__}: {fresh.error}"
|
|
197
|
+
failed: list[Any] = [item for item in (fresh.assertion or []) if not item.passed]
|
|
198
|
+
if failed:
|
|
199
|
+
return STATUS_FAILED, _assertion_message(failed[0])
|
|
200
|
+
return STATUS_PASSED, ""
|
|
201
|
+
|
|
202
|
+
return execute
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _segment(name: str, parent: str) -> str:
|
|
206
|
+
"""
|
|
207
|
+
ID segment of a JarTest group: its name without the parent's name.
|
|
208
|
+
|
|
209
|
+
Parameters
|
|
210
|
+
----------
|
|
211
|
+
name : str
|
|
212
|
+
Group name (e.g. "JTT_JCCS_CLI_Parser").
|
|
213
|
+
parent : str
|
|
214
|
+
Parent group name (e.g. "JTT_JCCS_CLI").
|
|
215
|
+
|
|
216
|
+
Returns
|
|
217
|
+
----------
|
|
218
|
+
str
|
|
219
|
+
e.g. "Parser".
|
|
220
|
+
"""
|
|
221
|
+
return name.removeprefix(parent + "_") or name
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _walk(group: Any, raw: str, path: str, cases: list[TestCase], visited: set[int]) -> None:
|
|
225
|
+
"""
|
|
226
|
+
Collect the tests of a JarTest group and its sub-groups, in declaration order.
|
|
227
|
+
|
|
228
|
+
Parameters
|
|
229
|
+
----------
|
|
230
|
+
group : Any
|
|
231
|
+
A JarTest instance.
|
|
232
|
+
raw : str
|
|
233
|
+
Its name.
|
|
234
|
+
path : str
|
|
235
|
+
Its ID prefix.
|
|
236
|
+
cases : list[TestCase]
|
|
237
|
+
Where the tests go.
|
|
238
|
+
visited : set[int]
|
|
239
|
+
Groups already walked.
|
|
240
|
+
|
|
241
|
+
Returns
|
|
242
|
+
----------
|
|
243
|
+
None
|
|
244
|
+
Nothing.
|
|
245
|
+
"""
|
|
246
|
+
if id(group) in visited:
|
|
247
|
+
return
|
|
248
|
+
visited.add(id(group))
|
|
249
|
+
for name, benchmark in group.tests.items():
|
|
250
|
+
cases.append(TestCase(f"{path}/{name.removeprefix(JARTEST_TEST_PREFIX)}", JARTEST_SUITE, jartest_call(benchmark)))
|
|
251
|
+
for name, child in group.jartests.items():
|
|
252
|
+
_walk(child, name, f"{path}/{_segment(name, raw)}", cases, visited)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def jartest_cases(root: Path = INSTALL_ROOT) -> tuple[list[TestCase], str | None]:
|
|
256
|
+
"""
|
|
257
|
+
Tests of the development JarTest suite (tests/), when this installation has it.
|
|
258
|
+
|
|
259
|
+
Parameters
|
|
260
|
+
----------
|
|
261
|
+
root : Path
|
|
262
|
+
JCCS installation root.
|
|
263
|
+
|
|
264
|
+
Returns
|
|
265
|
+
----------
|
|
266
|
+
tuple[list[TestCase], str | None]
|
|
267
|
+
The tests, and why the suite is unavailable (None when it is available).
|
|
268
|
+
"""
|
|
269
|
+
if not (root / "tests" / "__main__.py").is_file():
|
|
270
|
+
return [], "tests/ is not part of this installation"
|
|
271
|
+
try:
|
|
272
|
+
importlib.import_module("jarbin_toolkit_jartest")
|
|
273
|
+
except ImportError:
|
|
274
|
+
return [], "jarbin-toolkit-jartest is not installed (development dependency)"
|
|
275
|
+
try:
|
|
276
|
+
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
|
|
277
|
+
module: Any = importlib.import_module("tests.__main__")
|
|
278
|
+
except Exception as error: # noqa: BLE001 - any import failure makes the suite unavailable
|
|
279
|
+
return [], f"cannot load tests/: {type(error).__name__}: {error}"
|
|
280
|
+
cases: list[TestCase] = []
|
|
281
|
+
_walk(module.JTT_JCCS, JARTEST_ROOT_NAME, JARTEST_SUITE, cases, set())
|
|
282
|
+
return cases, None
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def matches(case: TestCase, pattern: str) -> bool:
|
|
286
|
+
"""
|
|
287
|
+
Whether a test matches a --filter pattern: a glob on the test ID, or, without glob characters, the ID
|
|
288
|
+
itself or any group above it (`jartest/CLI` selects every CLI test).
|
|
289
|
+
|
|
290
|
+
Parameters
|
|
291
|
+
----------
|
|
292
|
+
case : TestCase
|
|
293
|
+
The test.
|
|
294
|
+
pattern : str
|
|
295
|
+
The pattern.
|
|
296
|
+
|
|
297
|
+
Returns
|
|
298
|
+
----------
|
|
299
|
+
bool
|
|
300
|
+
True when selected.
|
|
301
|
+
"""
|
|
302
|
+
if GLOB_CHARACTERS & set(pattern):
|
|
303
|
+
return fnmatch.fnmatchcase(case.id, pattern)
|
|
304
|
+
prefix: str = pattern.rstrip("/")
|
|
305
|
+
return case.id == prefix or case.id.startswith(prefix + "/")
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def timeout_supported() -> bool:
|
|
309
|
+
"""
|
|
310
|
+
Whether per-test timeouts can be enforced (POSIX interval timer, main thread).
|
|
311
|
+
|
|
312
|
+
Returns
|
|
313
|
+
----------
|
|
314
|
+
bool
|
|
315
|
+
True when supported.
|
|
316
|
+
"""
|
|
317
|
+
return hasattr(signal, "setitimer") and threading.current_thread() is threading.main_thread()
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
@contextlib.contextmanager
|
|
321
|
+
def _time_budget(seconds: float | None) -> Iterator[None]:
|
|
322
|
+
"""
|
|
323
|
+
Raise TestTimeout in the running test after `seconds`.
|
|
324
|
+
|
|
325
|
+
Parameters
|
|
326
|
+
----------
|
|
327
|
+
seconds : float | None
|
|
328
|
+
Budget, or None for none.
|
|
329
|
+
|
|
330
|
+
Returns
|
|
331
|
+
----------
|
|
332
|
+
Iterator[None]
|
|
333
|
+
Context manager.
|
|
334
|
+
"""
|
|
335
|
+
if seconds is None:
|
|
336
|
+
yield
|
|
337
|
+
return
|
|
338
|
+
|
|
339
|
+
def expire(signum: int, frame: FrameType | None) -> None:
|
|
340
|
+
raise TestTimeout()
|
|
341
|
+
|
|
342
|
+
previous: Any = signal.signal(signal.SIGALRM, expire)
|
|
343
|
+
outer, _ = signal.setitimer(signal.ITIMER_REAL, seconds)
|
|
344
|
+
start: float = time.monotonic()
|
|
345
|
+
try:
|
|
346
|
+
yield
|
|
347
|
+
finally:
|
|
348
|
+
signal.setitimer(signal.ITIMER_REAL, 0)
|
|
349
|
+
signal.signal(signal.SIGALRM, previous)
|
|
350
|
+
if outer > 0:
|
|
351
|
+
signal.setitimer(signal.ITIMER_REAL, max(outer - (time.monotonic() - start), 0.001))
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def run_case(case: TestCase, timeout: float | None = None) -> TestResult:
|
|
355
|
+
"""
|
|
356
|
+
Run one test with its output captured and its time budget enforced.
|
|
357
|
+
|
|
358
|
+
Parameters
|
|
359
|
+
----------
|
|
360
|
+
case : TestCase
|
|
361
|
+
The test.
|
|
362
|
+
timeout : float | None
|
|
363
|
+
Time budget in seconds, or None.
|
|
364
|
+
|
|
365
|
+
Returns
|
|
366
|
+
----------
|
|
367
|
+
TestResult
|
|
368
|
+
The outcome.
|
|
369
|
+
"""
|
|
370
|
+
captured: io.StringIO = io.StringIO()
|
|
371
|
+
start: float = time.perf_counter()
|
|
372
|
+
try:
|
|
373
|
+
with contextlib.redirect_stdout(captured), contextlib.redirect_stderr(captured), _time_budget(timeout):
|
|
374
|
+
status, message = case.execute()
|
|
375
|
+
except TestTimeout:
|
|
376
|
+
status, message = STATUS_FAILED, f"exceeded the time budget of {timeout:g} s"
|
|
377
|
+
except KeyboardInterrupt:
|
|
378
|
+
raise
|
|
379
|
+
except BaseException as error: # noqa: BLE001 - SystemExit and friends from a test are test errors
|
|
380
|
+
status, message = STATUS_ERROR, f"{type(error).__name__}: {error}"
|
|
381
|
+
return TestResult(case, status, message, time.perf_counter() - start, captured.getvalue())
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def order_cases(cases: list[TestCase], seed: int | None) -> list[TestCase]:
|
|
385
|
+
"""
|
|
386
|
+
Order of execution: declaration order, or a reproducible shuffle with a seed.
|
|
387
|
+
|
|
388
|
+
Parameters
|
|
389
|
+
----------
|
|
390
|
+
cases : list[TestCase]
|
|
391
|
+
Selected tests.
|
|
392
|
+
seed : int | None
|
|
393
|
+
Shuffle seed, or None.
|
|
394
|
+
|
|
395
|
+
Returns
|
|
396
|
+
----------
|
|
397
|
+
list[TestCase]
|
|
398
|
+
The tests in execution order.
|
|
399
|
+
"""
|
|
400
|
+
ordered: list[TestCase] = list(cases)
|
|
401
|
+
if seed is not None:
|
|
402
|
+
random.Random(seed).shuffle(ordered)
|
|
403
|
+
return ordered
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def run_cases(cases: list[TestCase], *, timeout: float | None = None, working_directory: Path = INSTALL_ROOT) -> list[TestResult]:
|
|
407
|
+
"""
|
|
408
|
+
Run tests one after the other, from the JCCS installation root (the JarTest suite relies on it); the
|
|
409
|
+
previous working directory is restored afterwards.
|
|
410
|
+
|
|
411
|
+
Parameters
|
|
412
|
+
----------
|
|
413
|
+
cases : list[TestCase]
|
|
414
|
+
Tests, in execution order.
|
|
415
|
+
timeout : float | None
|
|
416
|
+
Per-test time budget, or None.
|
|
417
|
+
working_directory : Path
|
|
418
|
+
Directory the tests run in.
|
|
419
|
+
|
|
420
|
+
Returns
|
|
421
|
+
----------
|
|
422
|
+
list[TestResult]
|
|
423
|
+
One result per test, in execution order.
|
|
424
|
+
"""
|
|
425
|
+
previous: str = os.getcwd()
|
|
426
|
+
os.chdir(working_directory)
|
|
427
|
+
try:
|
|
428
|
+
return [run_case(case, timeout) for case in cases]
|
|
429
|
+
finally:
|
|
430
|
+
os.chdir(previous)
|
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from sources.cli.arguments import CommandSpec
|
|
2
|
+
from sources.cli.context import ParsedArguments
|
|
3
|
+
from sources.constants import UPDATE_COMMAND
|
|
4
|
+
from sources.enums import ExitCode
|
|
5
|
+
from sources.services import Services
|
|
6
|
+
from sources.subcommands.base import Command
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class UpdateCommand(Command):
|
|
10
|
+
"""
|
|
11
|
+
`jccs update`: update JCCS itself (plan 02 section 7, plan 12).
|
|
12
|
+
(ABC -> Command)
|
|
13
|
+
|
|
14
|
+
Attributes
|
|
15
|
+
----------
|
|
16
|
+
spec : CommandSpec
|
|
17
|
+
UPDATE_COMMAND.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
spec: CommandSpec = UPDATE_COMMAND
|
|
21
|
+
|
|
22
|
+
def run(self, arguments: ParsedArguments, services: Services) -> ExitCode:
|
|
23
|
+
"""
|
|
24
|
+
Run the command (see sources.subcommands.update.handler).
|
|
25
|
+
|
|
26
|
+
Parameters
|
|
27
|
+
----------
|
|
28
|
+
arguments : ParsedArguments
|
|
29
|
+
Parsed command line.
|
|
30
|
+
services : Services
|
|
31
|
+
Process seams.
|
|
32
|
+
|
|
33
|
+
Returns
|
|
34
|
+
----------
|
|
35
|
+
ExitCode
|
|
36
|
+
The exit code.
|
|
37
|
+
|
|
38
|
+
Raises
|
|
39
|
+
----------
|
|
40
|
+
JccsError
|
|
41
|
+
Any error to report to the user.
|
|
42
|
+
"""
|
|
43
|
+
from sources.subcommands.update.handler import run_update
|
|
44
|
+
|
|
45
|
+
return run_update(arguments, services)
|