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,138 @@
|
|
|
1
|
+
# Skeleton written by `jccs rule --create <category> <name>` (plan 03, section 5.2). Every field of schema 2 is
|
|
2
|
+
# present; the values make a valid `preview` rule (inactive until enabled) whose example check forbids "TODO".
|
|
3
|
+
RULE_SKELETON: str = """\
|
|
4
|
+
# JCCS rule definition (schema 2)
|
|
5
|
+
# Generated by `jccs rule --create {category} {name}`.
|
|
6
|
+
# An empty value means "not set": the documented default applies.
|
|
7
|
+
# Code lines start with "|"; escapes: \\t \\r \\s (space) \\\\ .
|
|
8
|
+
# The rule is in preview: enable it with `jccs rule --enable {category} {name}` once it is ready.
|
|
9
|
+
|
|
10
|
+
[rule]
|
|
11
|
+
# Schema version of this file; required.
|
|
12
|
+
schema = 2
|
|
13
|
+
# Must equal <category>/<name> and the file location.
|
|
14
|
+
id = {category}/{name}
|
|
15
|
+
# Code shown in reports; default: [epitech] ref, else id.
|
|
16
|
+
code =
|
|
17
|
+
title =
|
|
18
|
+
# One-paragraph paraphrase of the rule (never a verbatim copy of an official text).
|
|
19
|
+
summary =
|
|
20
|
+
rationale =
|
|
21
|
+
# Comma-separated file kinds: c, h, make, other | any.
|
|
22
|
+
applies_to = c, h
|
|
23
|
+
# info | minor | major | fatal
|
|
24
|
+
severity = minor
|
|
25
|
+
# automatic | heuristic | manual (default for checks that do not override it)
|
|
26
|
+
detection = automatic
|
|
27
|
+
# preview | stable | deprecated
|
|
28
|
+
status = preview
|
|
29
|
+
since =
|
|
30
|
+
deprecated_by =
|
|
31
|
+
aliases =
|
|
32
|
+
tags =
|
|
33
|
+
docs_url =
|
|
34
|
+
# Can inline suppression and baselines hide this rule? Forced to false for fatal rules.
|
|
35
|
+
suppressible = true
|
|
36
|
+
# JCCS version constraint, e.g. >=2.1
|
|
37
|
+
requires_jccs =
|
|
38
|
+
supersedes =
|
|
39
|
+
conflicts_with =
|
|
40
|
+
|
|
41
|
+
[epitech]
|
|
42
|
+
# Official reference, e.g. C-L3; empty for non-Epitech rules.
|
|
43
|
+
ref =
|
|
44
|
+
# Coding-style document version this rule implements.
|
|
45
|
+
version =
|
|
46
|
+
# full | partial | none (Banana support level as stated by the document)
|
|
47
|
+
banana =
|
|
48
|
+
# Short description of what remains to be checked by a human.
|
|
49
|
+
manual_part =
|
|
50
|
+
|
|
51
|
+
[argument:forbidden_word]
|
|
52
|
+
# int | float | bool | str | enum | regex | list | glob | path | template | word_set
|
|
53
|
+
type = regex
|
|
54
|
+
default = TODO
|
|
55
|
+
min =
|
|
56
|
+
max =
|
|
57
|
+
choices =
|
|
58
|
+
# For list: type of each item.
|
|
59
|
+
item_type =
|
|
60
|
+
description = Text that must not appear in the code.
|
|
61
|
+
|
|
62
|
+
[check:forbidden_word]
|
|
63
|
+
# Primitive from the catalogue (plan 03, section 6.3).
|
|
64
|
+
kind = text_regex
|
|
65
|
+
# Python entry point; only for kind = python or plugin (not supported yet).
|
|
66
|
+
entry =
|
|
67
|
+
description = Lines must not contain the forbidden word.
|
|
68
|
+
# Overrides of [rule] values for this check only.
|
|
69
|
+
severity =
|
|
70
|
+
detection =
|
|
71
|
+
applies_to =
|
|
72
|
+
# Selection (plan 03, section 6.2); every primitive lists the where.* keys it accepts.
|
|
73
|
+
target =
|
|
74
|
+
where.file_kind =
|
|
75
|
+
# Primitive-specific parameters follow, one key each.
|
|
76
|
+
# A value "@<argument>" reads the effective value of that argument.
|
|
77
|
+
on = line
|
|
78
|
+
pattern = @forbidden_word
|
|
79
|
+
mode = forbid
|
|
80
|
+
# each | once_per_line | once_per_node | once_per_file
|
|
81
|
+
report = each
|
|
82
|
+
# Placeholders: {{code}} {{check}} {{path}} {{line}} {{column}} {{name}} {{value}} {{limit}} + any argument.
|
|
83
|
+
message = {{code}}: '{{value}}' must not appear in the code
|
|
84
|
+
# none | safe | unsafe
|
|
85
|
+
fix = none
|
|
86
|
+
# Fix primitive (plan 07), e.g. strip_trailing_whitespace.
|
|
87
|
+
fix.kind =
|
|
88
|
+
|
|
89
|
+
[example:good_1]
|
|
90
|
+
# good | bad
|
|
91
|
+
kind = good
|
|
92
|
+
description = Code without the forbidden word.
|
|
93
|
+
# For bad examples: one "<path>:<line>:<column>:<check_id>" per line.
|
|
94
|
+
expect =
|
|
95
|
+
|
|
96
|
+
[example:good_1:file:good.c]
|
|
97
|
+
# Inline content, one "|" line per source line.
|
|
98
|
+
code =
|
|
99
|
+
|int get_answer(void)
|
|
100
|
+
|{{
|
|
101
|
+
| return 42;
|
|
102
|
+
|}}
|
|
103
|
+
# Or: path of a byte-exact fixture, relative to this .ini file.
|
|
104
|
+
fixture =
|
|
105
|
+
# Whether a final "\\n" is appended to inline code.
|
|
106
|
+
final_newline = true
|
|
107
|
+
|
|
108
|
+
[example:bad_1]
|
|
109
|
+
kind = bad
|
|
110
|
+
description = The forbidden word in a comment.
|
|
111
|
+
expect =
|
|
112
|
+
1:4:forbidden_word
|
|
113
|
+
|
|
114
|
+
[example:bad_1:file:bad.c]
|
|
115
|
+
code =
|
|
116
|
+
|/* TODO: write the code */
|
|
117
|
+
fixture =
|
|
118
|
+
final_newline = true
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def render_rule_skeleton(category: str, name: str) -> str:
|
|
123
|
+
"""
|
|
124
|
+
Text of a new custom rule file.
|
|
125
|
+
|
|
126
|
+
Parameters
|
|
127
|
+
----------
|
|
128
|
+
category : str
|
|
129
|
+
Category of the rule.
|
|
130
|
+
name : str
|
|
131
|
+
Name of the rule.
|
|
132
|
+
|
|
133
|
+
Returns
|
|
134
|
+
----------
|
|
135
|
+
str
|
|
136
|
+
The rule file content.
|
|
137
|
+
"""
|
|
138
|
+
return RULE_SKELETON.format(category=category, name=name)
|
|
File without changes
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import json
|
|
3
|
+
import tempfile
|
|
4
|
+
import xml.etree.ElementTree as ElementTree
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from html.parser import HTMLParser
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from sources.services import Services
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Header every sample C file starts with (C-G1).
|
|
14
|
+
SAMPLE_HEADER: str = "/*\n** EPITECH PROJECT, 2026\n** selftest\n** File description:\n** Sample file.\n*/\n\n"
|
|
15
|
+
|
|
16
|
+
# Sample project of the golden run: one clean file, one file with known findings.
|
|
17
|
+
SAMPLE_PROJECT: dict[str, str] = {
|
|
18
|
+
"src/clean.c": SAMPLE_HEADER + "int add(int a, int b)\n{\n return a + b;\n}\n",
|
|
19
|
+
"src/dirty.c": SAMPLE_HEADER + "int main(void)\n{\n int a = 1+2;\n\n goto end;\nend:\n return a; \n}\n",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
# Findings the sample project must produce ("path:line:code"), non-review only.
|
|
23
|
+
SAMPLE_EXPECTED: frozenset[str] = frozenset({
|
|
24
|
+
"src/dirty.c:10:C-L3",
|
|
25
|
+
"src/dirty.c:12:C-C3",
|
|
26
|
+
"src/dirty.c:14:C-G7",
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
# Text the lexer must reproduce byte for byte (comments, strings, directives, splices, CRLF).
|
|
30
|
+
LEXER_SAMPLE: str = (
|
|
31
|
+
"#include <stdio.h>\r\n#define TWICE(x) ((x) * 2) \\\n /* body */\n"
|
|
32
|
+
"/* block\n comment */ static const char *s = \"a\\\"b\"; // line\n"
|
|
33
|
+
"int f(char c) { return c == '\\'' ? 1 : TWICE(3); }\n"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _sample_root(directory: str) -> Path:
|
|
38
|
+
"""
|
|
39
|
+
Write the sample project in a directory (with a git root marker).
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
directory : str
|
|
44
|
+
Empty directory.
|
|
45
|
+
|
|
46
|
+
Returns
|
|
47
|
+
----------
|
|
48
|
+
Path
|
|
49
|
+
The project root.
|
|
50
|
+
"""
|
|
51
|
+
root: Path = Path(directory).resolve()
|
|
52
|
+
(root / ".git").mkdir()
|
|
53
|
+
for path, content in SAMPLE_PROJECT.items():
|
|
54
|
+
(root / path).parent.mkdir(parents=True, exist_ok=True)
|
|
55
|
+
(root / path).write_text(content, encoding="utf-8")
|
|
56
|
+
return root
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@functools.cache
|
|
60
|
+
def _environment(cwd: str, path_variable: str) -> tuple[Any, ...]:
|
|
61
|
+
"""
|
|
62
|
+
Environment checks of `info --check-env`, computed once.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
cwd : str
|
|
67
|
+
Working directory.
|
|
68
|
+
path_variable : str
|
|
69
|
+
PATH used to find git.
|
|
70
|
+
|
|
71
|
+
Returns
|
|
72
|
+
----------
|
|
73
|
+
tuple[Any, ...]
|
|
74
|
+
The checks.
|
|
75
|
+
"""
|
|
76
|
+
import io
|
|
77
|
+
|
|
78
|
+
from sources.subcommands.info.handler import environment_checks
|
|
79
|
+
|
|
80
|
+
services: Services = Services(io.StringIO(), io.StringIO(), io.StringIO(), {"PATH": path_variable}, Path(cwd))
|
|
81
|
+
return tuple(environment_checks(services))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _environment_case(services: Services, name: str) -> Callable[[], None]:
|
|
85
|
+
"""
|
|
86
|
+
Self-test of one environment check (warnings pass).
|
|
87
|
+
|
|
88
|
+
Parameters
|
|
89
|
+
----------
|
|
90
|
+
services : Services
|
|
91
|
+
Process seams.
|
|
92
|
+
name : str
|
|
93
|
+
Check name.
|
|
94
|
+
|
|
95
|
+
Returns
|
|
96
|
+
----------
|
|
97
|
+
Callable[[], None]
|
|
98
|
+
The test.
|
|
99
|
+
"""
|
|
100
|
+
def test() -> None:
|
|
101
|
+
for check in _environment(str(services.cwd), services.env.get("PATH", "")):
|
|
102
|
+
if check.name == name:
|
|
103
|
+
assert check.status != "fail", check.detail
|
|
104
|
+
return
|
|
105
|
+
raise AssertionError(f"no environment check named {name}")
|
|
106
|
+
|
|
107
|
+
return test
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _example_case(rule_id: str, example_id: str) -> Callable[[], None]:
|
|
111
|
+
"""
|
|
112
|
+
Self-test of one rule example (findings and fixes, plan 03 section 5.5).
|
|
113
|
+
|
|
114
|
+
Parameters
|
|
115
|
+
----------
|
|
116
|
+
rule_id : str
|
|
117
|
+
Rule ID.
|
|
118
|
+
example_id : str
|
|
119
|
+
Example ID.
|
|
120
|
+
|
|
121
|
+
Returns
|
|
122
|
+
----------
|
|
123
|
+
Callable[[], None]
|
|
124
|
+
The test.
|
|
125
|
+
"""
|
|
126
|
+
def test() -> None:
|
|
127
|
+
from sources.engine.examples import run_example
|
|
128
|
+
from sources.rules.catalog import RuleCatalog
|
|
129
|
+
|
|
130
|
+
rule = RuleCatalog.load().rules[rule_id]
|
|
131
|
+
example = next(item for item in rule.examples if item.id == example_id)
|
|
132
|
+
result = run_example(rule, example)
|
|
133
|
+
assert result.passed, "; ".join(result.problems)
|
|
134
|
+
|
|
135
|
+
return test
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def lexer_lossless() -> None:
|
|
139
|
+
"""
|
|
140
|
+
The lexer reproduces a file byte for byte and the parser does not crash on it.
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
----------
|
|
144
|
+
None
|
|
145
|
+
Nothing.
|
|
146
|
+
"""
|
|
147
|
+
from sources.analysis.source import build_source_file
|
|
148
|
+
from sources.analysis.unit import build_unit, lex_unit
|
|
149
|
+
|
|
150
|
+
source = build_source_file("sample.c", LEXER_SAMPLE.encode("utf-8"))
|
|
151
|
+
assert "".join(token.text for token in lex_unit(source)) == source.text, "tokens do not rebuild the text"
|
|
152
|
+
unit = build_unit(source)
|
|
153
|
+
assert [function.name for function in unit.functions] == ["f"], f"functions: {[function.name for function in unit.functions]}"
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def config_template() -> None:
|
|
157
|
+
"""
|
|
158
|
+
The `.jccs.ini` template written for new projects is a valid configuration.
|
|
159
|
+
|
|
160
|
+
Returns
|
|
161
|
+
----------
|
|
162
|
+
None
|
|
163
|
+
Nothing.
|
|
164
|
+
"""
|
|
165
|
+
from sources.configs.project import CONFIG_FILE_NAME, load_project_config_file, render_config_template
|
|
166
|
+
from sources.rules.catalog import RuleCatalog
|
|
167
|
+
|
|
168
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
169
|
+
path: Path = Path(directory) / CONFIG_FILE_NAME
|
|
170
|
+
path.write_text(render_config_template(RuleCatalog.load().rules.values()), encoding="utf-8")
|
|
171
|
+
load_project_config_file(path)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _sample_report() -> Any:
|
|
175
|
+
"""
|
|
176
|
+
Analyse the sample project from disk (full pipeline, nothing written into it).
|
|
177
|
+
|
|
178
|
+
Returns
|
|
179
|
+
----------
|
|
180
|
+
Any
|
|
181
|
+
The CheckReport.
|
|
182
|
+
"""
|
|
183
|
+
from sources.engine.api import CheckOptions, check_project
|
|
184
|
+
|
|
185
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
186
|
+
return check_project(_sample_root(directory), CheckOptions(create_project_directory=False))
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def project_run() -> None:
|
|
190
|
+
"""
|
|
191
|
+
The sample project gives exactly the expected findings and exit code.
|
|
192
|
+
|
|
193
|
+
Returns
|
|
194
|
+
----------
|
|
195
|
+
None
|
|
196
|
+
Nothing.
|
|
197
|
+
"""
|
|
198
|
+
report = _sample_report()
|
|
199
|
+
found: set[str] = {f"{item.location.path}:{item.location.line}:{item.code}" for item in report.result.diagnostics if not item.review}
|
|
200
|
+
assert found == SAMPLE_EXPECTED, f"missing {sorted(SAMPLE_EXPECTED - found)}, unexpected {sorted(found - SAMPLE_EXPECTED)}"
|
|
201
|
+
assert int(report.exit_code) == 84, f"exit code {int(report.exit_code)}"
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def report_formats() -> None:
|
|
205
|
+
"""
|
|
206
|
+
Every report format renders the sample project: JSON valid against the shipped schema, JUnit and SARIF
|
|
207
|
+
parseable, HTML well formed.
|
|
208
|
+
|
|
209
|
+
Returns
|
|
210
|
+
----------
|
|
211
|
+
None
|
|
212
|
+
Nothing.
|
|
213
|
+
"""
|
|
214
|
+
from sources.enums import ReportFormat
|
|
215
|
+
from sources.outputs.registry import RenderOptions, render_report
|
|
216
|
+
from sources.outputs.report import ReportView
|
|
217
|
+
from sources.subcommands.info.handler import REPORT_SCHEMA_FILE
|
|
218
|
+
from sources.utils.schema import schema_errors
|
|
219
|
+
|
|
220
|
+
view = ReportView.build(_sample_report())
|
|
221
|
+
options = RenderOptions(include_timing=False, summary_only=False)
|
|
222
|
+
schema: Any = json.loads(REPORT_SCHEMA_FILE.read_text(encoding="utf-8"))
|
|
223
|
+
errors: list[str] = schema_errors(schema, json.loads(render_report(view, ReportFormat.JSON, options)))
|
|
224
|
+
assert not errors, f"JSON report: {errors[:3]}"
|
|
225
|
+
ElementTree.fromstring(render_report(view, ReportFormat.JUNIT, options).split("\n", 1)[1])
|
|
226
|
+
assert json.loads(render_report(view, ReportFormat.SARIF, options))["version"] == "2.1.0", "SARIF version"
|
|
227
|
+
HTMLParser().feed(render_report(view, ReportFormat.HTML, options))
|
|
228
|
+
assert render_report(view, ReportFormat.GITHUB, options).startswith("::"), "no GitHub annotation"
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def fix_roundtrip() -> None:
|
|
232
|
+
"""
|
|
233
|
+
Fixing the sample project reaches a fixpoint without rejected fixes, and fixing again changes nothing.
|
|
234
|
+
|
|
235
|
+
Returns
|
|
236
|
+
----------
|
|
237
|
+
None
|
|
238
|
+
Nothing.
|
|
239
|
+
"""
|
|
240
|
+
from sources.configs.project import ProjectConfig
|
|
241
|
+
from sources.engine.plan import build_plan
|
|
242
|
+
from sources.fixers.engine import fix_project
|
|
243
|
+
from sources.rules.catalog import RuleCatalog
|
|
244
|
+
|
|
245
|
+
plan = build_plan(RuleCatalog.load(), ProjectConfig())
|
|
246
|
+
contents: dict[str, bytes] = {path: text.encode("utf-8") for path, text in SAMPLE_PROJECT.items()}
|
|
247
|
+
first = fix_project(plan, contents)
|
|
248
|
+
assert first.converged and any(file.changed for file in first.files), "nothing fixed"
|
|
249
|
+
assert not [item.reason for file in first.files for item in file.rejected], "a fix was rejected"
|
|
250
|
+
second = fix_project(plan, dict(first.contents))
|
|
251
|
+
assert not any(file.changed for file in second.files), "fixing twice changed a file"
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
# Golden self-tests, by name.
|
|
255
|
+
GOLDEN_CASES: dict[str, Callable[[], None]] = {
|
|
256
|
+
"lexer_lossless": lexer_lossless,
|
|
257
|
+
"config_template": config_template,
|
|
258
|
+
"project_run": project_run,
|
|
259
|
+
"report_formats": report_formats,
|
|
260
|
+
"fix_roundtrip": fix_roundtrip,
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def selftest_cases(services: Services) -> list[tuple[str, Callable[[], None]]]:
|
|
265
|
+
"""
|
|
266
|
+
Tests of the self-test shipped with the program (plan 11, section 8): environment checks, every rule
|
|
267
|
+
example, and golden runs. IDs are relative to the `selftest/` suite.
|
|
268
|
+
|
|
269
|
+
Parameters
|
|
270
|
+
----------
|
|
271
|
+
services : Services
|
|
272
|
+
Process seams.
|
|
273
|
+
|
|
274
|
+
Returns
|
|
275
|
+
----------
|
|
276
|
+
list[tuple[str, Callable[[], None]]]
|
|
277
|
+
(id, test) pairs, in a fixed order.
|
|
278
|
+
"""
|
|
279
|
+
from sources.constants import RUNTIME_DEPENDENCIES
|
|
280
|
+
from sources.rules.catalog import RuleCatalog
|
|
281
|
+
|
|
282
|
+
cases: list[tuple[str, Callable[[], None]]] = []
|
|
283
|
+
names: list[str] = ["python", *RUNTIME_DEPENDENCIES, "builtin_rules"]
|
|
284
|
+
cases.extend((f"environment/{name}", _environment_case(services, name)) for name in names)
|
|
285
|
+
try:
|
|
286
|
+
rules = sorted(RuleCatalog.load().rules.values(), key=lambda rule: rule.id)
|
|
287
|
+
except Exception: # noqa: BLE001 - the environment/builtin_rules test reports the problem
|
|
288
|
+
rules = []
|
|
289
|
+
for rule in rules:
|
|
290
|
+
cases.extend((f"rules/{rule.id}/{example.id}", _example_case(rule.id, example.id)) for example in rule.examples)
|
|
291
|
+
cases.extend((f"golden/{name}", test) for name, test in GOLDEN_CASES.items())
|
|
292
|
+
return cases
|
sources/services.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from types import MappingProxyType
|
|
7
|
+
from typing import TextIO
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True, slots=True)
|
|
11
|
+
class Services:
|
|
12
|
+
"""
|
|
13
|
+
Injected seams of the process (plan 01, P9): standard streams, environment and working directory.
|
|
14
|
+
Commands never touch `sys` or `os.environ` directly, so tests can run them with fakes.
|
|
15
|
+
(dataclass)
|
|
16
|
+
|
|
17
|
+
Attributes
|
|
18
|
+
----------
|
|
19
|
+
stdin : TextIO
|
|
20
|
+
Input stream.
|
|
21
|
+
stdout : TextIO
|
|
22
|
+
Primary payload stream (reports, values, scripts).
|
|
23
|
+
stderr : TextIO
|
|
24
|
+
Console chatter (errors, warnings, progress, verbose output).
|
|
25
|
+
env : Mapping[str, str]
|
|
26
|
+
Environment variables.
|
|
27
|
+
cwd : Path
|
|
28
|
+
Working directory.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
stdin: TextIO
|
|
32
|
+
stdout: TextIO
|
|
33
|
+
stderr: TextIO
|
|
34
|
+
env: Mapping[str, str] = field(default_factory=lambda: MappingProxyType({}))
|
|
35
|
+
cwd: Path = field(default_factory=Path.cwd)
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def from_process(cls) -> "Services":
|
|
39
|
+
"""
|
|
40
|
+
Services backed by the real process.
|
|
41
|
+
|
|
42
|
+
Returns
|
|
43
|
+
----------
|
|
44
|
+
Services
|
|
45
|
+
The services.
|
|
46
|
+
"""
|
|
47
|
+
return cls(sys.stdin, sys.stdout, sys.stderr, MappingProxyType(dict(os.environ)), Path.cwd())
|
|
48
|
+
|
|
49
|
+
def is_tty(self, stream: TextIO) -> bool:
|
|
50
|
+
"""
|
|
51
|
+
Whether a stream is an interactive terminal.
|
|
52
|
+
|
|
53
|
+
Parameters
|
|
54
|
+
----------
|
|
55
|
+
stream : TextIO
|
|
56
|
+
The stream.
|
|
57
|
+
|
|
58
|
+
Returns
|
|
59
|
+
----------
|
|
60
|
+
bool
|
|
61
|
+
True for a terminal.
|
|
62
|
+
"""
|
|
63
|
+
isatty = getattr(stream, "isatty", None)
|
|
64
|
+
try:
|
|
65
|
+
return bool(isatty()) if callable(isatty) else False
|
|
66
|
+
except (OSError, ValueError):
|
|
67
|
+
return False
|
|
68
|
+
|
|
69
|
+
def color_enabled(self, stream: TextIO, *, disabled: bool = False) -> bool:
|
|
70
|
+
"""
|
|
71
|
+
Whether ANSI colours may be written to a stream (plan 08, OUT-04): off when disabled by a flag or
|
|
72
|
+
`NO_COLOR` / `JCCS_NO_COLOR`, forced on by `FORCE_COLOR`, else on for terminals only.
|
|
73
|
+
|
|
74
|
+
Parameters
|
|
75
|
+
----------
|
|
76
|
+
stream : TextIO
|
|
77
|
+
The stream.
|
|
78
|
+
disabled : bool
|
|
79
|
+
Whether a flag (`--no-color`, `--ci`) turned colours off.
|
|
80
|
+
|
|
81
|
+
Returns
|
|
82
|
+
----------
|
|
83
|
+
bool
|
|
84
|
+
True when colours are allowed.
|
|
85
|
+
"""
|
|
86
|
+
if disabled or "NO_COLOR" in self.env or "JCCS_NO_COLOR" in self.env:
|
|
87
|
+
return False
|
|
88
|
+
if self.env.get("FORCE_COLOR"):
|
|
89
|
+
return True
|
|
90
|
+
return self.is_tty(stream) and self.env.get("TERM") != "dumb"
|
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from typing import ClassVar
|
|
3
|
+
|
|
4
|
+
from sources.cli.arguments import CommandSpec
|
|
5
|
+
from sources.cli.context import ParsedArguments
|
|
6
|
+
from sources.enums import ExitCode
|
|
7
|
+
from sources.services import Services
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Command(ABC):
|
|
11
|
+
"""
|
|
12
|
+
A subcommand: its specification and its handler. Handlers return an exit code or raise a JccsError;
|
|
13
|
+
they never print errors themselves and never exit (plan 01, section 7.1).
|
|
14
|
+
(ABC)
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
spec : CommandSpec
|
|
19
|
+
Specification of the subcommand (from sources.constants).
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
spec: ClassVar[CommandSpec]
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def run(self, arguments: ParsedArguments, services: Services) -> ExitCode:
|
|
26
|
+
"""
|
|
27
|
+
Run the subcommand.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
arguments : ParsedArguments
|
|
32
|
+
Parsed command line (global flags and subcommand flags).
|
|
33
|
+
services : Services
|
|
34
|
+
Process seams (streams, environment, working directory).
|
|
35
|
+
|
|
36
|
+
Returns
|
|
37
|
+
----------
|
|
38
|
+
ExitCode
|
|
39
|
+
The exit code.
|
|
40
|
+
|
|
41
|
+
Raises
|
|
42
|
+
----------
|
|
43
|
+
JccsError
|
|
44
|
+
Any error to report to the user.
|
|
45
|
+
"""
|
|
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 CHECK_COMMAND
|
|
4
|
+
from sources.enums import ExitCode
|
|
5
|
+
from sources.services import Services
|
|
6
|
+
from sources.subcommands.base import Command
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class CheckCommand(Command):
|
|
10
|
+
"""
|
|
11
|
+
`jccs check`: scan the project against the active rules.
|
|
12
|
+
(ABC -> Command)
|
|
13
|
+
|
|
14
|
+
Attributes
|
|
15
|
+
----------
|
|
16
|
+
spec : CommandSpec
|
|
17
|
+
CHECK_COMMAND.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
spec: CommandSpec = CHECK_COMMAND
|
|
21
|
+
|
|
22
|
+
def run(self, arguments: ParsedArguments, services: Services) -> ExitCode:
|
|
23
|
+
"""
|
|
24
|
+
Run the check (see sources.subcommands.check.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.check.handler import run_check
|
|
44
|
+
|
|
45
|
+
return run_check(arguments, services)
|