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
|
File without changes
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from collections import Counter
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from sources.constants import PROGRAM_NAME, PROGRAM_VERSION
|
|
6
|
+
from sources.diagnostics.model import Diagnostic
|
|
7
|
+
from sources.enums import ExitCode
|
|
8
|
+
from sources.outputs.report import ReportView
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Version of the JSON report contract (plan 08, OUT-08).
|
|
12
|
+
JSON_SCHEMA_VERSION: int = 1
|
|
13
|
+
|
|
14
|
+
# `run.status` value of each exit code (plan 08, section 4).
|
|
15
|
+
STATUS_BY_EXIT_CODE: dict[ExitCode, str] = {
|
|
16
|
+
ExitCode.OK: "ok",
|
|
17
|
+
ExitCode.FINDINGS: "findings",
|
|
18
|
+
ExitCode.TIMEOUT: "timeout",
|
|
19
|
+
ExitCode.INTERRUPTED: "interrupted",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _diagnostic(view: ReportView, item: Diagnostic) -> dict[str, Any]:
|
|
24
|
+
"""
|
|
25
|
+
JSON object of one diagnostic.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
view : ReportView
|
|
30
|
+
The report.
|
|
31
|
+
item : Diagnostic
|
|
32
|
+
The diagnostic.
|
|
33
|
+
|
|
34
|
+
Returns
|
|
35
|
+
----------
|
|
36
|
+
dict[str, Any]
|
|
37
|
+
The object (fixed key order).
|
|
38
|
+
"""
|
|
39
|
+
location = item.location
|
|
40
|
+
return {
|
|
41
|
+
"rule": item.rule_id,
|
|
42
|
+
"check": item.check_id,
|
|
43
|
+
"code": view.code(item),
|
|
44
|
+
"severity": item.severity.value,
|
|
45
|
+
"detection": item.detection.value,
|
|
46
|
+
"review": item.review,
|
|
47
|
+
"message": view.message(item),
|
|
48
|
+
"path": location.path,
|
|
49
|
+
"line": location.line,
|
|
50
|
+
"column": location.column,
|
|
51
|
+
"end_line": location.end_line,
|
|
52
|
+
"end_column": location.end_column,
|
|
53
|
+
"fingerprint": item.fingerprint,
|
|
54
|
+
"data": json_data(item.data),
|
|
55
|
+
"suppressed": None,
|
|
56
|
+
"fix": None if item.fix.value == "none" else {"applicability": item.fix.value, "kind": item.fix_kind},
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def render_json(view: ReportView, *, include_timing: bool, summary_only: bool) -> str:
|
|
61
|
+
"""
|
|
62
|
+
Render the JSON report, contract v1 (plan 08, section 4).
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
view : ReportView
|
|
67
|
+
The report.
|
|
68
|
+
include_timing : bool
|
|
69
|
+
Whether `run.timing` is written (omitted under `--ci` for byte-identical reports).
|
|
70
|
+
summary_only : bool
|
|
71
|
+
Whether the `diagnostics` array is left empty.
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
----------
|
|
75
|
+
str
|
|
76
|
+
The JSON document, with a final newline.
|
|
77
|
+
"""
|
|
78
|
+
return json.dumps(json_document(view, include_timing=include_timing, summary_only=summary_only), indent=2, ensure_ascii=False) + "\n"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def json_data(data: Any) -> dict[str, Any]:
|
|
82
|
+
"""
|
|
83
|
+
JSON-safe copy of a diagnostic payload: scalars kept, anything else turned into text.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
data : Any
|
|
88
|
+
The `data` mapping of a diagnostic.
|
|
89
|
+
|
|
90
|
+
Returns
|
|
91
|
+
----------
|
|
92
|
+
dict[str, Any]
|
|
93
|
+
The payload.
|
|
94
|
+
"""
|
|
95
|
+
return {str(key): value if isinstance(value, (int, float, str, bool, type(None))) else str(value) for key, value in data.items()}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def json_document(view: ReportView, *, include_timing: bool, summary_only: bool) -> dict[str, Any]:
|
|
99
|
+
"""
|
|
100
|
+
JSON report as a Python object (also embedded in the HTML report).
|
|
101
|
+
|
|
102
|
+
Parameters
|
|
103
|
+
----------
|
|
104
|
+
view : ReportView
|
|
105
|
+
The report.
|
|
106
|
+
include_timing : bool
|
|
107
|
+
Whether `run.timing` is written.
|
|
108
|
+
summary_only : bool
|
|
109
|
+
Whether the `diagnostics` array is left empty.
|
|
110
|
+
|
|
111
|
+
Returns
|
|
112
|
+
----------
|
|
113
|
+
dict[str, Any]
|
|
114
|
+
The document (fixed key order).
|
|
115
|
+
"""
|
|
116
|
+
report = view.report
|
|
117
|
+
files = report.project.files
|
|
118
|
+
analysed: tuple[str, ...] = report.result.files_analyzed
|
|
119
|
+
run: dict[str, Any] = {
|
|
120
|
+
"root": str(report.root),
|
|
121
|
+
"config": report.config.source,
|
|
122
|
+
"status": STATUS_BY_EXIT_CODE.get(report.exit_code, "error"),
|
|
123
|
+
"exit_code": int(report.exit_code),
|
|
124
|
+
"exit_reason": report.outcome.reason,
|
|
125
|
+
"fail_on": report.fail_on.value,
|
|
126
|
+
"max_errors": report.max_errors,
|
|
127
|
+
}
|
|
128
|
+
if include_timing:
|
|
129
|
+
run["timing"] = {"total_ms": round(sum(report.timing.values()), 3), "phases_ms": dict(report.timing)}
|
|
130
|
+
by_rule: Counter[str] = Counter(item.rule_id for item in view.findings)
|
|
131
|
+
document: dict[str, Any] = {
|
|
132
|
+
"schema_version": JSON_SCHEMA_VERSION,
|
|
133
|
+
"tool": {"name": PROGRAM_NAME, "version": PROGRAM_VERSION},
|
|
134
|
+
"run": run,
|
|
135
|
+
"summary": {
|
|
136
|
+
"files": {
|
|
137
|
+
"scanned": len(analysed),
|
|
138
|
+
"skipped": sum(1 for path in analysed if files[path].skipped is not None),
|
|
139
|
+
"failed": 0,
|
|
140
|
+
},
|
|
141
|
+
"diagnostics": {"total": len(view.findings), **view.severity_totals(view.findings)},
|
|
142
|
+
"review": len(view.review_items),
|
|
143
|
+
"counted": report.outcome.counted,
|
|
144
|
+
"fixed": len(report.fixes),
|
|
145
|
+
"by_rule": dict(sorted(by_rule.items())),
|
|
146
|
+
},
|
|
147
|
+
"diagnostics": [] if summary_only else [_diagnostic(view, item) for item in report.result.diagnostics],
|
|
148
|
+
"fixes": [] if summary_only else [
|
|
149
|
+
{"path": record.path, "rule": record.rule_id, "check": record.check_id, "code": record.code, "kind": record.kind, "line": record.line}
|
|
150
|
+
for record in report.fixes
|
|
151
|
+
],
|
|
152
|
+
"errors": [],
|
|
153
|
+
}
|
|
154
|
+
return document
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://github.com/Jarjarbin06/Jarbin-C-Coding-Style/report.schema.json",
|
|
4
|
+
"title": "jccs check --format json report",
|
|
5
|
+
"description": "Report contract v1 (plan 08, section 4). Additive changes keep schema_version; consumers ignore unknown keys.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"required": ["schema_version", "tool", "run", "summary", "diagnostics", "fixes", "errors"],
|
|
8
|
+
"properties": {
|
|
9
|
+
"schema_version": {"const": 1},
|
|
10
|
+
"tool": {
|
|
11
|
+
"type": "object",
|
|
12
|
+
"required": ["name", "version"],
|
|
13
|
+
"properties": {
|
|
14
|
+
"name": {"type": "string"},
|
|
15
|
+
"version": {"type": "string"}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"run": {
|
|
19
|
+
"type": "object",
|
|
20
|
+
"required": ["root", "config", "status", "exit_code", "exit_reason", "fail_on", "max_errors"],
|
|
21
|
+
"properties": {
|
|
22
|
+
"root": {"type": "string"},
|
|
23
|
+
"config": {"type": ["string", "null"]},
|
|
24
|
+
"status": {"enum": ["ok", "findings", "error", "timeout", "interrupted"]},
|
|
25
|
+
"exit_code": {"type": "integer"},
|
|
26
|
+
"exit_reason": {"type": "string"},
|
|
27
|
+
"fail_on": {"enum": ["info", "minor", "major", "fatal"]},
|
|
28
|
+
"max_errors": {"type": "integer", "minimum": 0},
|
|
29
|
+
"timing": {
|
|
30
|
+
"type": "object",
|
|
31
|
+
"required": ["total_ms", "phases_ms"],
|
|
32
|
+
"properties": {
|
|
33
|
+
"total_ms": {"type": "number"},
|
|
34
|
+
"phases_ms": {"type": "object", "additionalProperties": {"type": "number"}}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"summary": {
|
|
40
|
+
"type": "object",
|
|
41
|
+
"required": ["files", "diagnostics", "review", "counted", "fixed", "by_rule"],
|
|
42
|
+
"properties": {
|
|
43
|
+
"files": {
|
|
44
|
+
"type": "object",
|
|
45
|
+
"required": ["scanned", "skipped", "failed"],
|
|
46
|
+
"properties": {
|
|
47
|
+
"scanned": {"type": "integer", "minimum": 0},
|
|
48
|
+
"skipped": {"type": "integer", "minimum": 0},
|
|
49
|
+
"failed": {"type": "integer", "minimum": 0}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"diagnostics": {
|
|
53
|
+
"type": "object",
|
|
54
|
+
"required": ["total", "fatal", "major", "minor", "info"],
|
|
55
|
+
"properties": {
|
|
56
|
+
"total": {"type": "integer", "minimum": 0},
|
|
57
|
+
"fatal": {"type": "integer", "minimum": 0},
|
|
58
|
+
"major": {"type": "integer", "minimum": 0},
|
|
59
|
+
"minor": {"type": "integer", "minimum": 0},
|
|
60
|
+
"info": {"type": "integer", "minimum": 0}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"review": {"type": "integer", "minimum": 0},
|
|
64
|
+
"counted": {"type": "integer", "minimum": 0},
|
|
65
|
+
"fixed": {"type": "integer", "minimum": 0},
|
|
66
|
+
"by_rule": {"type": "object", "additionalProperties": {"type": "integer", "minimum": 1}}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"diagnostics": {
|
|
70
|
+
"type": "array",
|
|
71
|
+
"items": {
|
|
72
|
+
"type": "object",
|
|
73
|
+
"required": [
|
|
74
|
+
"rule", "check", "code", "severity", "detection", "review", "message", "path", "line", "column",
|
|
75
|
+
"end_line", "end_column", "fingerprint", "data", "suppressed", "fix"
|
|
76
|
+
],
|
|
77
|
+
"properties": {
|
|
78
|
+
"rule": {"type": "string"},
|
|
79
|
+
"check": {"type": ["string", "null"]},
|
|
80
|
+
"code": {"type": "string"},
|
|
81
|
+
"severity": {"enum": ["info", "minor", "major", "fatal"]},
|
|
82
|
+
"detection": {"enum": ["automatic", "heuristic", "manual"]},
|
|
83
|
+
"review": {"type": "boolean"},
|
|
84
|
+
"message": {"type": "string"},
|
|
85
|
+
"path": {"type": "string"},
|
|
86
|
+
"line": {"type": "integer", "minimum": 1},
|
|
87
|
+
"column": {"type": "integer", "minimum": 1},
|
|
88
|
+
"end_line": {"type": ["integer", "null"], "minimum": 1},
|
|
89
|
+
"end_column": {"type": ["integer", "null"], "minimum": 1},
|
|
90
|
+
"fingerprint": {"type": "string"},
|
|
91
|
+
"data": {"type": "object"},
|
|
92
|
+
"suppressed": {"type": ["string", "null"]},
|
|
93
|
+
"fix": {
|
|
94
|
+
"type": ["object", "null"],
|
|
95
|
+
"required": ["applicability", "kind"],
|
|
96
|
+
"properties": {
|
|
97
|
+
"applicability": {"enum": ["safe", "unsafe"]},
|
|
98
|
+
"kind": {"type": ["string", "null"]}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
"fixes": {
|
|
105
|
+
"type": "array",
|
|
106
|
+
"items": {
|
|
107
|
+
"type": "object",
|
|
108
|
+
"required": ["path", "rule", "check", "code", "kind", "line"],
|
|
109
|
+
"properties": {
|
|
110
|
+
"path": {"type": "string"},
|
|
111
|
+
"rule": {"type": "string"},
|
|
112
|
+
"check": {"type": ["string", "null"]},
|
|
113
|
+
"code": {"type": "string"},
|
|
114
|
+
"kind": {"type": "string"},
|
|
115
|
+
"line": {"type": "integer", "minimum": 1}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
"errors": {"type": "array"}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import xml.etree.ElementTree as ElementTree
|
|
3
|
+
from itertools import groupby
|
|
4
|
+
|
|
5
|
+
from sources.constants import PROGRAM_NAME
|
|
6
|
+
from sources.diagnostics.model import Diagnostic
|
|
7
|
+
from sources.enums import SeverityLevel
|
|
8
|
+
from sources.outputs.report import ReportView
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# Characters XML 1.0 cannot contain, even escaped (replaced by U+FFFD).
|
|
12
|
+
INVALID_XML_CHARACTERS: re.Pattern[str] = re.compile("[^\t\n\r --�\U00010000-\U0010ffff]")
|
|
13
|
+
|
|
14
|
+
# Name of the passing test case of a file without counted findings (plan 08, OUT-07).
|
|
15
|
+
CLEAN_CASE: str = "clean"
|
|
16
|
+
|
|
17
|
+
# Name of the single test case of a file in a summary-only report.
|
|
18
|
+
SUMMARY_CASE: str = "summary"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _text(value: str) -> str:
|
|
22
|
+
"""
|
|
23
|
+
Make text safe for XML 1.0.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
value : str
|
|
28
|
+
Raw text.
|
|
29
|
+
|
|
30
|
+
Returns
|
|
31
|
+
----------
|
|
32
|
+
str
|
|
33
|
+
The text with forbidden characters replaced.
|
|
34
|
+
"""
|
|
35
|
+
return INVALID_XML_CHARACTERS.sub("�", value)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _line(view: ReportView, item: Diagnostic) -> str:
|
|
39
|
+
"""
|
|
40
|
+
Location line of a diagnostic, in the stable text form.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
view : ReportView
|
|
45
|
+
The report.
|
|
46
|
+
item : Diagnostic
|
|
47
|
+
The diagnostic.
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
----------
|
|
51
|
+
str
|
|
52
|
+
`<path>:<line>:<column>: <severity> [<code>] <message>`.
|
|
53
|
+
"""
|
|
54
|
+
location = item.location
|
|
55
|
+
suffix: str = " (to review)" if item.review else ""
|
|
56
|
+
return f"{location.path}:{location.line}:{location.column}: {item.severity.value} [{view.code(item)}] {view.message(item)}{suffix}"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _case(view: ReportView, suite: ElementTree.Element, item: Diagnostic) -> tuple[int, int]:
|
|
60
|
+
"""
|
|
61
|
+
Add the test case of one diagnostic: counted findings fail (fatal ones are errors), the others pass
|
|
62
|
+
with the finding recorded as system-out (plan 06, section 3).
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
view : ReportView
|
|
67
|
+
The report.
|
|
68
|
+
suite : ElementTree.Element
|
|
69
|
+
Test suite of the file.
|
|
70
|
+
item : Diagnostic
|
|
71
|
+
The diagnostic.
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
----------
|
|
75
|
+
tuple[int, int]
|
|
76
|
+
(failures, errors) added.
|
|
77
|
+
"""
|
|
78
|
+
location = item.location
|
|
79
|
+
case: ElementTree.Element = ElementTree.SubElement(suite, "testcase", {
|
|
80
|
+
"name": _text(f"{view.code(item)} {location.line}:{location.column}"),
|
|
81
|
+
"classname": _text(location.path),
|
|
82
|
+
})
|
|
83
|
+
line: str = _text(_line(view, item))
|
|
84
|
+
if not view.counted(item):
|
|
85
|
+
ElementTree.SubElement(case, "system-out").text = line
|
|
86
|
+
return 0, 0
|
|
87
|
+
tag: str = "error" if item.severity is SeverityLevel.FATAL else "failure"
|
|
88
|
+
ElementTree.SubElement(case, tag, {"type": _text(view.code(item)), "message": _text(view.message(item))}).text = line
|
|
89
|
+
return (0, 1) if tag == "error" else (1, 0)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def render_junit(view: ReportView, *, summary_only: bool) -> str:
|
|
93
|
+
"""
|
|
94
|
+
Render the JUnit XML report (plan 08, OUT-07): one test suite per analysed file, one test case per
|
|
95
|
+
diagnostic, and a passing "clean" case for files without any diagnostic.
|
|
96
|
+
|
|
97
|
+
Parameters
|
|
98
|
+
----------
|
|
99
|
+
view : ReportView
|
|
100
|
+
The report.
|
|
101
|
+
summary_only : bool
|
|
102
|
+
Whether each file gets one "summary" case instead of one case per diagnostic.
|
|
103
|
+
|
|
104
|
+
Returns
|
|
105
|
+
----------
|
|
106
|
+
str
|
|
107
|
+
The XML document, with a final newline.
|
|
108
|
+
"""
|
|
109
|
+
report = view.report
|
|
110
|
+
by_path: dict[str, list[Diagnostic]] = {
|
|
111
|
+
path: list(items) for path, items in groupby(report.result.diagnostics, key=lambda item: item.location.path)
|
|
112
|
+
}
|
|
113
|
+
paths: list[str] = sorted(set(report.result.files_analyzed) | set(by_path))
|
|
114
|
+
root: ElementTree.Element = ElementTree.Element("testsuites", {"name": PROGRAM_NAME})
|
|
115
|
+
totals: list[int] = [0, 0, 0]
|
|
116
|
+
for path in paths:
|
|
117
|
+
items: list[Diagnostic] = by_path.get(path, [])
|
|
118
|
+
suite: ElementTree.Element = ElementTree.SubElement(root, "testsuite", {"name": _text(path)})
|
|
119
|
+
tests: int
|
|
120
|
+
failures: int = 0
|
|
121
|
+
errors: int = 0
|
|
122
|
+
if summary_only:
|
|
123
|
+
tests = 1
|
|
124
|
+
case: ElementTree.Element = ElementTree.SubElement(suite, "testcase", {"name": SUMMARY_CASE, "classname": _text(path)})
|
|
125
|
+
counted: list[Diagnostic] = [item for item in items if view.counted(item)]
|
|
126
|
+
if counted:
|
|
127
|
+
fatal: bool = any(item.severity is SeverityLevel.FATAL for item in counted)
|
|
128
|
+
tag: str = "error" if fatal else "failure"
|
|
129
|
+
ElementTree.SubElement(case, tag, {"type": PROGRAM_NAME, "message": f"{len(counted)} finding(s)"})
|
|
130
|
+
failures, errors = (0, 1) if fatal else (1, 0)
|
|
131
|
+
elif not items:
|
|
132
|
+
tests = 1
|
|
133
|
+
ElementTree.SubElement(suite, "testcase", {"name": CLEAN_CASE, "classname": _text(path)})
|
|
134
|
+
else:
|
|
135
|
+
tests = len(items)
|
|
136
|
+
for item in items:
|
|
137
|
+
added: tuple[int, int] = _case(view, suite, item)
|
|
138
|
+
failures += added[0]
|
|
139
|
+
errors += added[1]
|
|
140
|
+
suite.set("tests", str(tests))
|
|
141
|
+
suite.set("failures", str(failures))
|
|
142
|
+
suite.set("errors", str(errors))
|
|
143
|
+
suite.set("skipped", "0")
|
|
144
|
+
totals[0] += tests
|
|
145
|
+
totals[1] += failures
|
|
146
|
+
totals[2] += errors
|
|
147
|
+
root.set("tests", str(totals[0]))
|
|
148
|
+
root.set("failures", str(totals[1]))
|
|
149
|
+
root.set("errors", str(totals[2]))
|
|
150
|
+
ElementTree.indent(root, space=" ")
|
|
151
|
+
return ElementTree.tostring(root, encoding="unicode", xml_declaration=True) + "\n"
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import PurePath
|
|
4
|
+
|
|
5
|
+
from sources.enums import ReportFormat
|
|
6
|
+
from sources.errors.exceptions import UsageError
|
|
7
|
+
from sources.outputs.github.renderer import render_github
|
|
8
|
+
from sources.outputs.html.renderer import render_html
|
|
9
|
+
from sources.outputs.json.renderer import render_json
|
|
10
|
+
from sources.outputs.junit.renderer import render_junit
|
|
11
|
+
from sources.outputs.report import ReportView
|
|
12
|
+
from sources.outputs.sarif.renderer import render_sarif
|
|
13
|
+
from sources.outputs.terminal.renderer import LEVEL_NORMAL, TextOptions, render_text
|
|
14
|
+
from sources.outputs.terminal.style import Style
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Report format inferred from the extension of a `--report` path (plan 02, section 4.1).
|
|
18
|
+
FORMAT_BY_EXTENSION: dict[str, ReportFormat] = {
|
|
19
|
+
".txt": ReportFormat.TEXT,
|
|
20
|
+
".json": ReportFormat.JSON,
|
|
21
|
+
".xml": ReportFormat.JUNIT,
|
|
22
|
+
".sarif": ReportFormat.SARIF,
|
|
23
|
+
".html": ReportFormat.HTML,
|
|
24
|
+
".htm": ReportFormat.HTML,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
# Separator between the format and the path in `--report [FORMAT:]PATH`.
|
|
28
|
+
FORMAT_SEPARATOR: str = ":"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, slots=True)
|
|
32
|
+
class RenderOptions:
|
|
33
|
+
"""
|
|
34
|
+
Options shared by every machine renderer (plan 08, section 10).
|
|
35
|
+
(dataclass)
|
|
36
|
+
|
|
37
|
+
Attributes
|
|
38
|
+
----------
|
|
39
|
+
include_timing : bool
|
|
40
|
+
Whether wall-clock data is written (False under `--ci`).
|
|
41
|
+
summary_only : bool
|
|
42
|
+
Totals only, no individual findings.
|
|
43
|
+
max_annotations : int | None
|
|
44
|
+
Largest number of GitHub annotations, or None for all.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
include_timing: bool
|
|
48
|
+
summary_only: bool
|
|
49
|
+
max_annotations: int | None = None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True, slots=True)
|
|
53
|
+
class ReportTarget:
|
|
54
|
+
"""
|
|
55
|
+
One `--report` artifact.
|
|
56
|
+
(dataclass)
|
|
57
|
+
|
|
58
|
+
Attributes
|
|
59
|
+
----------
|
|
60
|
+
format : ReportFormat
|
|
61
|
+
Format of the artifact.
|
|
62
|
+
path : str
|
|
63
|
+
Destination as typed ("-" for stdout).
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
format: ReportFormat
|
|
67
|
+
path: str
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# Renderer of each format (pure functions: view and options in, text out).
|
|
71
|
+
RENDERERS: dict[ReportFormat, Callable[[ReportView, RenderOptions], str]] = {
|
|
72
|
+
ReportFormat.TEXT: lambda view, options: render_text(view, TextOptions(LEVEL_NORMAL, False, options.summary_only, Style(False))),
|
|
73
|
+
ReportFormat.JSON: lambda view, options: render_json(view, include_timing=options.include_timing, summary_only=options.summary_only),
|
|
74
|
+
ReportFormat.JUNIT: lambda view, options: render_junit(view, summary_only=options.summary_only),
|
|
75
|
+
ReportFormat.SARIF: lambda view, options: render_sarif(view, summary_only=options.summary_only),
|
|
76
|
+
ReportFormat.GITHUB: lambda view, options: render_github(view, summary_only=options.summary_only, max_annotations=options.max_annotations),
|
|
77
|
+
ReportFormat.HTML: lambda view, options: render_html(view, include_timing=options.include_timing, summary_only=options.summary_only),
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def render_report(view: ReportView, report_format: ReportFormat, options: RenderOptions) -> str:
|
|
82
|
+
"""
|
|
83
|
+
Render a report in a machine format (the coloured, grouped terminal view stays in the check handler).
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
view : ReportView
|
|
88
|
+
The report.
|
|
89
|
+
report_format : ReportFormat
|
|
90
|
+
The format.
|
|
91
|
+
options : RenderOptions
|
|
92
|
+
Shared options.
|
|
93
|
+
|
|
94
|
+
Returns
|
|
95
|
+
----------
|
|
96
|
+
str
|
|
97
|
+
The rendered report.
|
|
98
|
+
"""
|
|
99
|
+
return RENDERERS[report_format](view, options)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def parse_report_target(raw: str) -> ReportTarget:
|
|
103
|
+
"""
|
|
104
|
+
Parse a `--report [FORMAT:]PATH` value. The prefix is a format only when it names one (so `a:b.json`
|
|
105
|
+
is a path); without a prefix the format comes from the extension.
|
|
106
|
+
|
|
107
|
+
Parameters
|
|
108
|
+
----------
|
|
109
|
+
raw : str
|
|
110
|
+
The value as typed.
|
|
111
|
+
|
|
112
|
+
Returns
|
|
113
|
+
----------
|
|
114
|
+
ReportTarget
|
|
115
|
+
Format and path.
|
|
116
|
+
|
|
117
|
+
Raises
|
|
118
|
+
----------
|
|
119
|
+
UsageError
|
|
120
|
+
Empty path, or a format that cannot be inferred.
|
|
121
|
+
"""
|
|
122
|
+
prefix, separator, rest = raw.partition(FORMAT_SEPARATOR)
|
|
123
|
+
if separator and prefix.lower() in tuple(item.value for item in ReportFormat):
|
|
124
|
+
if not rest:
|
|
125
|
+
raise UsageError(f"Missing path in --report '{raw}'.", f"Write it as --report {prefix}:PATH.")
|
|
126
|
+
return ReportTarget(ReportFormat(prefix.lower()), rest)
|
|
127
|
+
report_format: ReportFormat | None = FORMAT_BY_EXTENSION.get(PurePath(raw).suffix.lower())
|
|
128
|
+
if report_format is None:
|
|
129
|
+
known: str = ", ".join(FORMAT_BY_EXTENSION)
|
|
130
|
+
formats: str = ", ".join(item.value for item in ReportFormat)
|
|
131
|
+
raise UsageError(
|
|
132
|
+
f"Cannot infer the format of --report '{raw}' (known extensions: {known}).",
|
|
133
|
+
f"Prefix the path with a format ({formats}), e.g. --report json:{raw}.",
|
|
134
|
+
)
|
|
135
|
+
return ReportTarget(report_format, raw)
|