assert-no-pytest-plugin-declarations 20260902062339__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.
- assert_no_pytest_plugin_declarations/__init__.py +1 -0
- assert_no_pytest_plugin_declarations/__main__.py +5 -0
- assert_no_pytest_plugin_declarations/cli.py +340 -0
- assert_no_pytest_plugin_declarations/py.typed +0 -0
- assert_no_pytest_plugin_declarations/scanner.py +138 -0
- assert_no_pytest_plugin_declarations-20260902062339.dist-info/METADATA +138 -0
- assert_no_pytest_plugin_declarations-20260902062339.dist-info/RECORD +11 -0
- assert_no_pytest_plugin_declarations-20260902062339.dist-info/WHEEL +5 -0
- assert_no_pytest_plugin_declarations-20260902062339.dist-info/entry_points.txt +2 -0
- assert_no_pytest_plugin_declarations-20260902062339.dist-info/licenses/LICENSE.txt +190 -0
- assert_no_pytest_plugin_declarations-20260902062339.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Assert that no file declares pytest plugins by assigning pytest_plugins."""
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"""Command-line interface for assert-no-pytest-plugin-declarations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import fnmatch
|
|
7
|
+
import glob
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from .scanner import Finding, declarations
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from collections.abc import Sequence
|
|
17
|
+
|
|
18
|
+
EXIT_SUCCESS = 0
|
|
19
|
+
EXIT_FINDINGS = 1
|
|
20
|
+
EXIT_ERROR = 2
|
|
21
|
+
|
|
22
|
+
GLOB_CHARACTERS = ("*", "?", "[")
|
|
23
|
+
|
|
24
|
+
SKIPPED_DIRECTORIES = (".git", "__pycache__", "node_modules")
|
|
25
|
+
|
|
26
|
+
SUFFIX = ".py"
|
|
27
|
+
|
|
28
|
+
MESSAGE = (
|
|
29
|
+
"declares a plugin out of band; a fixture reaches a suite through the "
|
|
30
|
+
"conftest tree above it or through an import written where it is used, "
|
|
31
|
+
"and this is a second mechanism neither of those can be read through"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class ScanResult:
|
|
37
|
+
"""The outcome of one run over a set of trees."""
|
|
38
|
+
|
|
39
|
+
findings: list[Finding] = field(default_factory=list)
|
|
40
|
+
files_scanned: int = 0
|
|
41
|
+
had_error: bool = False
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def create_parser() -> argparse.ArgumentParser:
|
|
45
|
+
"""Create the argument parser for the CLI."""
|
|
46
|
+
parser = argparse.ArgumentParser(
|
|
47
|
+
prog="assert-no-pytest-plugin-declarations",
|
|
48
|
+
description=(
|
|
49
|
+
"Assert that no Python file in the given trees assigns pytest_plugins. A "
|
|
50
|
+
"fixture reaches a suite through the conftest tree above it or through an "
|
|
51
|
+
"import written where it is used, and either one can be followed from the "
|
|
52
|
+
"file asking for the fixture. An assignment to pytest_plugins loads "
|
|
53
|
+
"somebody else's fixtures from somewhere else entirely, and nothing "
|
|
54
|
+
"reading the suite can see through it."
|
|
55
|
+
),
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
parser.add_argument(
|
|
59
|
+
"trees",
|
|
60
|
+
nargs="+",
|
|
61
|
+
metavar="TREE",
|
|
62
|
+
help=(
|
|
63
|
+
"One or more file paths, directory paths, or glob patterns to read. "
|
|
64
|
+
"Directories are read recursively for *.py files."
|
|
65
|
+
),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
parser.add_argument(
|
|
69
|
+
"--exclude",
|
|
70
|
+
metavar="PATTERNS",
|
|
71
|
+
help=(
|
|
72
|
+
"Comma-separated glob patterns to leave out, such as 'test/vendor/*' for "
|
|
73
|
+
"tests somebody else wrote."
|
|
74
|
+
),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
parser.add_argument(
|
|
78
|
+
"--annotate",
|
|
79
|
+
action="store_true",
|
|
80
|
+
help=(
|
|
81
|
+
"Print each finding as a GitHub Actions ::error annotation, so it lands on "
|
|
82
|
+
"the line it names in the diff."
|
|
83
|
+
),
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
output_group = parser.add_mutually_exclusive_group()
|
|
87
|
+
output_group.add_argument(
|
|
88
|
+
"--quiet",
|
|
89
|
+
action="store_true",
|
|
90
|
+
help="Suppress all output. Exit code indicates success (0) or findings (1).",
|
|
91
|
+
)
|
|
92
|
+
output_group.add_argument(
|
|
93
|
+
"--count",
|
|
94
|
+
action="store_true",
|
|
95
|
+
help="Output only the count of findings.",
|
|
96
|
+
)
|
|
97
|
+
output_group.add_argument(
|
|
98
|
+
"--verbose",
|
|
99
|
+
action="store_true",
|
|
100
|
+
help="Show the files read, the findings and a summary.",
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
behavior_group = parser.add_mutually_exclusive_group()
|
|
104
|
+
behavior_group.add_argument(
|
|
105
|
+
"--fail-fast",
|
|
106
|
+
action="store_true",
|
|
107
|
+
help="Exit immediately after finding the first declaration.",
|
|
108
|
+
)
|
|
109
|
+
behavior_group.add_argument(
|
|
110
|
+
"--warn-only",
|
|
111
|
+
action="store_true",
|
|
112
|
+
help="Always exit with code 0, even if findings exist.",
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
return parser
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def parse_patterns(patterns_str: str | None) -> list[str]:
|
|
119
|
+
"""Parse a comma-separated patterns string.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
patterns_str: Comma-separated patterns, or None.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
The patterns, empty when the input is None or blank.
|
|
126
|
+
"""
|
|
127
|
+
if not patterns_str:
|
|
128
|
+
return []
|
|
129
|
+
return [pattern.strip() for pattern in patterns_str.split(",") if pattern.strip()]
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _is_glob_pattern(path: str) -> bool:
|
|
133
|
+
"""Check whether a path holds glob wildcard characters."""
|
|
134
|
+
return any(character in path for character in GLOB_CHARACTERS)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _walk_python_files(directory: str) -> list[str]:
|
|
138
|
+
"""Find every Python file under a directory."""
|
|
139
|
+
found: list[str] = []
|
|
140
|
+
for root, directories, filenames in os.walk(directory):
|
|
141
|
+
directories[:] = [name for name in directories if name not in SKIPPED_DIRECTORIES]
|
|
142
|
+
found.extend(
|
|
143
|
+
os.path.join(root, filename)
|
|
144
|
+
for filename in filenames
|
|
145
|
+
if filename.endswith(SUFFIX)
|
|
146
|
+
)
|
|
147
|
+
return found
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _expand(path: str) -> tuple[list[str], bool]:
|
|
151
|
+
"""Expand one path, directory or glob into the files it names.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
path: A file path, directory path or glob pattern.
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
The files it names, and whether it named anything at all.
|
|
158
|
+
"""
|
|
159
|
+
if _is_glob_pattern(path):
|
|
160
|
+
matched = glob.glob(path, recursive=True, include_hidden=True)
|
|
161
|
+
found: list[str] = []
|
|
162
|
+
for entry in matched:
|
|
163
|
+
if os.path.isfile(entry):
|
|
164
|
+
found.append(entry)
|
|
165
|
+
elif os.path.isdir(entry):
|
|
166
|
+
found.extend(_walk_python_files(entry))
|
|
167
|
+
return (found, bool(matched))
|
|
168
|
+
if os.path.isfile(path):
|
|
169
|
+
return ([path], True)
|
|
170
|
+
if os.path.isdir(path):
|
|
171
|
+
return (_walk_python_files(path), True)
|
|
172
|
+
return ([], False)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def should_skip(path: str, exclude_patterns: list[str]) -> bool:
|
|
176
|
+
"""Check whether a file matches any exclude pattern.
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
path: The file path to check.
|
|
180
|
+
exclude_patterns: Glob patterns to exclude.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
True when the path or its basename matches a pattern.
|
|
184
|
+
"""
|
|
185
|
+
return any(
|
|
186
|
+
fnmatch.fnmatch(path, pattern) or fnmatch.fnmatch(os.path.basename(path), pattern)
|
|
187
|
+
for pattern in exclude_patterns
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def collect(paths: Sequence[str], exclude_patterns: list[str]) -> tuple[list[str], list[str]]:
|
|
192
|
+
"""Expand paths into the files to read.
|
|
193
|
+
|
|
194
|
+
Args:
|
|
195
|
+
paths: File paths, directory paths or glob patterns.
|
|
196
|
+
exclude_patterns: Glob patterns to exclude.
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
The files to read, sorted and without repeats, and the paths that
|
|
200
|
+
named nothing.
|
|
201
|
+
"""
|
|
202
|
+
found: set[str] = set()
|
|
203
|
+
missing: list[str] = []
|
|
204
|
+
for path in paths:
|
|
205
|
+
expanded, matched = _expand(path)
|
|
206
|
+
if not matched:
|
|
207
|
+
missing.append(path)
|
|
208
|
+
continue
|
|
209
|
+
for entry in expanded:
|
|
210
|
+
normalised = os.path.normpath(entry)
|
|
211
|
+
if normalised.endswith(SUFFIX) and not should_skip(normalised, exclude_patterns):
|
|
212
|
+
found.add(normalised)
|
|
213
|
+
return (sorted(found), missing)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _read(path: str, result: ScanResult) -> str | None:
|
|
217
|
+
"""Read one file, recording an error on the result if it cannot be read."""
|
|
218
|
+
try:
|
|
219
|
+
with open(path, encoding="utf-8") as handle:
|
|
220
|
+
return handle.read()
|
|
221
|
+
except (OSError, UnicodeDecodeError) as error:
|
|
222
|
+
print(f"Error reading {path}: {error}", file=sys.stderr)
|
|
223
|
+
result.had_error = True
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def read_declarations(
|
|
228
|
+
paths: Sequence[str], result: ScanResult, verbose: bool = False
|
|
229
|
+
) -> list[Finding]:
|
|
230
|
+
"""Read every file and collect the plugin declarations in it.
|
|
231
|
+
|
|
232
|
+
Args:
|
|
233
|
+
paths: The files to read.
|
|
234
|
+
result: The result to record read errors and counts on.
|
|
235
|
+
verbose: Whether to name each file read.
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
Every finding, in path order.
|
|
239
|
+
"""
|
|
240
|
+
findings: list[Finding] = []
|
|
241
|
+
for path in paths:
|
|
242
|
+
content = _read(path, result)
|
|
243
|
+
if content is None:
|
|
244
|
+
continue
|
|
245
|
+
if verbose:
|
|
246
|
+
print(f"Reading: {path}")
|
|
247
|
+
try:
|
|
248
|
+
findings.extend(declarations(path, content))
|
|
249
|
+
except SyntaxError as error:
|
|
250
|
+
print(f"Could not parse {path}: {error}", file=sys.stderr)
|
|
251
|
+
result.had_error = True
|
|
252
|
+
continue
|
|
253
|
+
result.files_scanned += 1
|
|
254
|
+
return findings
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def output_findings(
|
|
258
|
+
findings: list[Finding], count_mode: bool = False, annotate: bool = False
|
|
259
|
+
) -> None:
|
|
260
|
+
"""Print findings in the requested format.
|
|
261
|
+
|
|
262
|
+
Args:
|
|
263
|
+
findings: The findings to print.
|
|
264
|
+
count_mode: Print only how many there are.
|
|
265
|
+
annotate: Print each as a GitHub Actions ::error annotation.
|
|
266
|
+
"""
|
|
267
|
+
if count_mode:
|
|
268
|
+
print(len(findings))
|
|
269
|
+
return
|
|
270
|
+
for finding in findings:
|
|
271
|
+
if annotate:
|
|
272
|
+
print(
|
|
273
|
+
f"::error file={finding.path},line={finding.line_number}::"
|
|
274
|
+
f"{finding} {MESSAGE}"
|
|
275
|
+
)
|
|
276
|
+
else:
|
|
277
|
+
print(f"{finding}: {MESSAGE}")
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def determine_exit_code(result: ScanResult, warn_only: bool = False) -> int:
|
|
281
|
+
"""Choose the exit code for a result.
|
|
282
|
+
|
|
283
|
+
Args:
|
|
284
|
+
result: The scan result.
|
|
285
|
+
warn_only: Always succeed when true.
|
|
286
|
+
|
|
287
|
+
Returns:
|
|
288
|
+
0 for success, 1 for findings, 2 for errors.
|
|
289
|
+
"""
|
|
290
|
+
if warn_only:
|
|
291
|
+
return EXIT_SUCCESS
|
|
292
|
+
if result.findings:
|
|
293
|
+
return EXIT_FINDINGS
|
|
294
|
+
if result.had_error:
|
|
295
|
+
return EXIT_ERROR
|
|
296
|
+
return EXIT_SUCCESS
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def _report(result: ScanResult, args: argparse.Namespace) -> None:
|
|
300
|
+
"""Print whatever the chosen output mode asks for."""
|
|
301
|
+
if args.quiet:
|
|
302
|
+
return
|
|
303
|
+
output_findings(result.findings, args.count, args.annotate)
|
|
304
|
+
if args.verbose:
|
|
305
|
+
print()
|
|
306
|
+
print(f"Files scanned: {result.files_scanned}")
|
|
307
|
+
print(f"Findings: {len(result.findings)}")
|
|
308
|
+
if result.had_error:
|
|
309
|
+
print("Errors occurred during scanning.")
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
def main(argv: Sequence[str] | None = None) -> None:
|
|
313
|
+
"""Run the CLI.
|
|
314
|
+
|
|
315
|
+
Args:
|
|
316
|
+
argv: Command-line arguments, defaulting to sys.argv[1:].
|
|
317
|
+
"""
|
|
318
|
+
args = create_parser().parse_args(argv)
|
|
319
|
+
exclude_patterns = parse_patterns(args.exclude)
|
|
320
|
+
result = ScanResult()
|
|
321
|
+
|
|
322
|
+
paths, missing = collect(args.trees, exclude_patterns)
|
|
323
|
+
for path in missing:
|
|
324
|
+
print(f"Error: Path not found: {path}", file=sys.stderr)
|
|
325
|
+
if not paths and missing:
|
|
326
|
+
sys.exit(EXIT_ERROR)
|
|
327
|
+
if missing:
|
|
328
|
+
result.had_error = True
|
|
329
|
+
|
|
330
|
+
if args.verbose:
|
|
331
|
+
print(f"Reading {len(paths)} file(s)...")
|
|
332
|
+
if exclude_patterns:
|
|
333
|
+
print(f"Excluding patterns: {', '.join(exclude_patterns)}")
|
|
334
|
+
print()
|
|
335
|
+
|
|
336
|
+
findings = read_declarations(paths, result, args.verbose)
|
|
337
|
+
result.findings = findings[:1] if (args.fail_fast and findings) else findings
|
|
338
|
+
|
|
339
|
+
_report(result, args)
|
|
340
|
+
sys.exit(determine_exit_code(result, args.warn_only))
|
|
File without changes
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""Core logic for finding the assignments that declare pytest plugins."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from collections.abc import Iterator
|
|
11
|
+
|
|
12
|
+
PLUGINS = "pytest_plugins"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class Finding:
|
|
17
|
+
"""An assignment binding the name pytest reads a plugin list from."""
|
|
18
|
+
|
|
19
|
+
path: str
|
|
20
|
+
line_number: int
|
|
21
|
+
declaration: str
|
|
22
|
+
|
|
23
|
+
def __str__(self) -> str:
|
|
24
|
+
"""Format as path:line:declaration."""
|
|
25
|
+
return f"{self.path}:{self.line_number}:{self.declaration}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def target_names(target: ast.expr) -> list[str]:
|
|
29
|
+
"""Name every identifier a binding target binds.
|
|
30
|
+
|
|
31
|
+
A tuple or a list unpacks into the names beneath it, and a star takes
|
|
32
|
+
whatever is left over. An attribute or a subscript reaches into something
|
|
33
|
+
that already exists and so binds no name of its own, which is why
|
|
34
|
+
assigning through one is not a declaration.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
target: The target expression to read.
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
The names bound, in the order they appear.
|
|
41
|
+
"""
|
|
42
|
+
if isinstance(target, ast.Name):
|
|
43
|
+
return [target.id]
|
|
44
|
+
if isinstance(target, ast.Starred):
|
|
45
|
+
return target_names(target.value)
|
|
46
|
+
if isinstance(target, (ast.List, ast.Tuple)):
|
|
47
|
+
return [name for element in target.elts for name in target_names(element)]
|
|
48
|
+
return []
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def targets_of(node: ast.AnnAssign | ast.Assign | ast.AugAssign) -> list[ast.expr]:
|
|
52
|
+
"""Name the targets one assignment binds through.
|
|
53
|
+
|
|
54
|
+
A plain assignment carries a target for each equals sign, where an
|
|
55
|
+
annotated or an augmented one carries exactly one.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
node: The assignment to read.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
Every target expression the assignment binds through.
|
|
62
|
+
"""
|
|
63
|
+
if isinstance(node, ast.Assign):
|
|
64
|
+
return node.targets
|
|
65
|
+
return [node.target]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def declares_plugins(node: ast.AnnAssign | ast.Assign | ast.AugAssign) -> bool:
|
|
69
|
+
"""Check whether an assignment binds the plugin list name.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
node: The assignment to read.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
True when any target of the assignment binds that name.
|
|
76
|
+
"""
|
|
77
|
+
return any(PLUGINS in target_names(target) for target in targets_of(node))
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def declaration_text(node: ast.AnnAssign | ast.Assign | ast.AugAssign) -> str:
|
|
81
|
+
"""Read back the source of what an assignment assigns.
|
|
82
|
+
|
|
83
|
+
The text is written out from the syntax rather than sliced out of the
|
|
84
|
+
file, so a list spread over several lines reads as one line here.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
node: The assignment to read.
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
The source of the assigned value, empty where an annotation assigns
|
|
91
|
+
nothing at all.
|
|
92
|
+
"""
|
|
93
|
+
if node.value is None:
|
|
94
|
+
return ""
|
|
95
|
+
return ast.unparse(node.value)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def assignments(tree: ast.Module) -> Iterator[ast.AnnAssign | ast.Assign | ast.AugAssign]:
|
|
99
|
+
"""Walk every assignment in a module, at whatever depth it is written.
|
|
100
|
+
|
|
101
|
+
A declaration made inside a condition, a context manager, a function or a
|
|
102
|
+
class body reaches pytest exactly as one written at the top of the file,
|
|
103
|
+
so the whole tree is read rather than its top level.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
tree: The parsed module.
|
|
107
|
+
|
|
108
|
+
Yields:
|
|
109
|
+
Each plain, annotated or augmented assignment in the module.
|
|
110
|
+
"""
|
|
111
|
+
for node in ast.walk(tree):
|
|
112
|
+
if isinstance(node, (ast.AnnAssign, ast.Assign, ast.AugAssign)):
|
|
113
|
+
yield node
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def declarations(path: str, content: str) -> list[Finding]:
|
|
117
|
+
"""Find every declaration of a plugin list in one file.
|
|
118
|
+
|
|
119
|
+
The rule is a property of the single file, so nothing outside it is read:
|
|
120
|
+
a name bound here is a declaration whether or not another file makes the
|
|
121
|
+
same one.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
path: The file path, used for reporting.
|
|
125
|
+
content: The file content to parse.
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
A finding for each assignment binding the plugin list name.
|
|
129
|
+
|
|
130
|
+
Raises:
|
|
131
|
+
SyntaxError: If the content cannot be parsed as Python.
|
|
132
|
+
"""
|
|
133
|
+
tree = ast.parse(content, filename=path)
|
|
134
|
+
return [
|
|
135
|
+
Finding(path, node.lineno, declaration_text(node))
|
|
136
|
+
for node in assignments(tree)
|
|
137
|
+
if declares_plugins(node)
|
|
138
|
+
]
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: assert-no-pytest-plugin-declarations
|
|
3
|
+
Version: 20260902062339
|
|
4
|
+
Summary: CLI tool to assert that no conftest declares a pytest plugin
|
|
5
|
+
Author-email: 10U Labs <dev@10ulabs.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/10U-Labs/assert-no-pytest-plugin-declarations
|
|
8
|
+
Project-URL: Repository, https://github.com/10U-Labs/assert-no-pytest-plugin-declarations
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
19
|
+
Classifier: Topic :: Software Development :: Testing
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE.txt
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# assert-no-pytest-plugin-declarations
|
|
26
|
+
|
|
27
|
+
Assert that no conftest declares a pytest plugin.
|
|
28
|
+
|
|
29
|
+
## Why
|
|
30
|
+
|
|
31
|
+
pytest loads a plugin once. Whether a given `pytest_plugins` line is
|
|
32
|
+
doing anything is therefore decided by files other than the one it is
|
|
33
|
+
written in — by a module in another directory, or by a conftest further
|
|
34
|
+
up the path that has already asked for the same module. Reading the
|
|
35
|
+
line tells you nothing about whether it is load-bearing, and no amount
|
|
36
|
+
of reading the file around it will either.
|
|
37
|
+
|
|
38
|
+
A fixture has two ways to reach a suite that do not have that problem.
|
|
39
|
+
It can be inherited, by sitting in a conftest above the suite that wants
|
|
40
|
+
it. Or it can be imported, by a `from ... import ...` written in the
|
|
41
|
+
conftest that needs it. Both are ordinary Python that ordinary tools
|
|
42
|
+
follow: pylint reports the import nothing uses, mypy resolves it, and
|
|
43
|
+
the sibling tool `assert-pytest-fixture-name-is-needed` reads the name
|
|
44
|
+
it binds. A `pytest_plugins` line is a second mechanism for the same
|
|
45
|
+
job that none of the three can see through.
|
|
46
|
+
|
|
47
|
+
It also holds package dependencies up, silently. A declaration that
|
|
48
|
+
registers no fixture at all still imports its module, and so keeps that
|
|
49
|
+
module's own imports load-bearing with nothing anywhere saying so. The
|
|
50
|
+
line cannot be deleted without a reader first working out, across the
|
|
51
|
+
whole tree, what would stop being imported if it went.
|
|
52
|
+
|
|
53
|
+
So the rule is the whole of it. Not a redeclaration, not a duplicate of
|
|
54
|
+
one further up: any declaration at all.
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install assert-no-pytest-plugin-declarations
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Every test tree this repository publishes
|
|
66
|
+
assert-no-pytest-plugin-declarations test
|
|
67
|
+
|
|
68
|
+
# The same, leaving out tests somebody else wrote
|
|
69
|
+
assert-no-pytest-plugin-declarations test \
|
|
70
|
+
--exclude 'test/vendor/*'
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Options
|
|
74
|
+
|
|
75
|
+
| Option | Effect |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| `--exclude PATTERNS` | Comma-separated globs to leave out. |
|
|
78
|
+
| `--annotate` | Print each finding as a GitHub Actions `::error` annotation. |
|
|
79
|
+
| `--quiet` | Print nothing; report through the exit code. |
|
|
80
|
+
| `--count` | Print only how many findings there were. |
|
|
81
|
+
| `--verbose` | Print the files read, the findings and a summary. |
|
|
82
|
+
| `--fail-fast` | Stop at the first finding. |
|
|
83
|
+
| `--warn-only` | Always exit 0. |
|
|
84
|
+
|
|
85
|
+
### Exit codes
|
|
86
|
+
|
|
87
|
+
| Code | Meaning |
|
|
88
|
+
| --- | --- |
|
|
89
|
+
| 0 | No file declares a pytest plugin |
|
|
90
|
+
| 1 | A file declares a pytest plugin |
|
|
91
|
+
| 2 | A tree was missing, unreadable, or would not parse |
|
|
92
|
+
|
|
93
|
+
## What counts as a declaration
|
|
94
|
+
|
|
95
|
+
Any assignment to the name `pytest_plugins`, whatever it is assigned:
|
|
96
|
+
|
|
97
|
+
| Written as | Example |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| A list | `pytest_plugins = ["fixtures.aws"]` |
|
|
100
|
+
| A string | `pytest_plugins = "fixtures.aws"` |
|
|
101
|
+
| An annotation | `pytest_plugins: list[str] = []` |
|
|
102
|
+
| An addition | `pytest_plugins += ["fixtures.aws"]` |
|
|
103
|
+
| One target of several | `pytest_plugins = names = []` |
|
|
104
|
+
|
|
105
|
+
The value is never inspected, because the declaration this tool most
|
|
106
|
+
wants to refuse is the one whose value cannot be worked out by reading:
|
|
107
|
+
a name built elsewhere, or a list a loop appends to. An empty list is a
|
|
108
|
+
finding like any other. It registers nothing today and is an invitation
|
|
109
|
+
to the next person to add an entry rather than a conftest.
|
|
110
|
+
|
|
111
|
+
## What is walked
|
|
112
|
+
|
|
113
|
+
A directory argument is read recursively for `*.py` files. `.git`,
|
|
114
|
+
`__pycache__` and `node_modules` are skipped, and everything else you
|
|
115
|
+
want left out goes in `--exclude`. Each file is read on its own and
|
|
116
|
+
needs nothing from any other, so a single file is as valid an argument
|
|
117
|
+
as a tree.
|
|
118
|
+
|
|
119
|
+
Every Python file is read, not only files named `conftest.py`. pytest
|
|
120
|
+
honours the name in a conftest and in a test module, and a line that
|
|
121
|
+
sits anywhere else is one that starts working the day the file is
|
|
122
|
+
renamed or its contents are moved.
|
|
123
|
+
|
|
124
|
+
## GitHub Actions
|
|
125
|
+
|
|
126
|
+
```yaml
|
|
127
|
+
- name: Assert no conftest declares a pytest plugin
|
|
128
|
+
uses: 10U-Labs/assert-no-pytest-plugin-declarations@latest
|
|
129
|
+
with:
|
|
130
|
+
trees: test
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`annotate` defaults to true there, so each finding lands on the line it
|
|
134
|
+
names in the diff.
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
Apache-2.0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
assert_no_pytest_plugin_declarations/__init__.py,sha256=1JZh5KsRBURI7hDT6RhDYxXYwU_Ec0PM5oV7sOPmDKA,79
|
|
2
|
+
assert_no_pytest_plugin_declarations/__main__.py,sha256=m-bkd5DfW4SWS2grULbG9UdiHZRhIi8WEOVWhS_3-F8,112
|
|
3
|
+
assert_no_pytest_plugin_declarations/cli.py,sha256=65QLpforLtufmYluqlcvsookh_1ZFnHjhCJBFco4Vb4,10246
|
|
4
|
+
assert_no_pytest_plugin_declarations/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
assert_no_pytest_plugin_declarations/scanner.py,sha256=GQZQZebIKVBl0f9FgZ5CBS9fTB1BdXb_BFmtjdakMYI,4180
|
|
6
|
+
assert_no_pytest_plugin_declarations-20260902062339.dist-info/licenses/LICENSE.txt,sha256=7C7FkuzwSQFE_PL6st5x2YyMN1aiyjZ9ti1s5X45no8,10761
|
|
7
|
+
assert_no_pytest_plugin_declarations-20260902062339.dist-info/METADATA,sha256=k3PNJ3XiAftkn5b3fpJMhgVG0vU6LMBgPHEQvEDpqtU,5086
|
|
8
|
+
assert_no_pytest_plugin_declarations-20260902062339.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
9
|
+
assert_no_pytest_plugin_declarations-20260902062339.dist-info/entry_points.txt,sha256=gchD4lf1n-19OIGocVsk7RG0WpQ6ilypkhZfl2L8Wjc,103
|
|
10
|
+
assert_no_pytest_plugin_declarations-20260902062339.dist-info/top_level.txt,sha256=_PzkQ4kaQOfoqOpkZiFi82vUjTM5eQEHX1NBL5CPOPw,37
|
|
11
|
+
assert_no_pytest_plugin_declarations-20260902062339.dist-info/RECORD,,
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2024 10U Labs LLC
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
assert_no_pytest_plugin_declarations
|