ansible-test - Initial support for black in core (#84741)

This commit is contained in:
Matt Clay
2025-02-24 08:39:59 +00:00
committed by GitHub
parent 2a3c93f593
commit 989e583356
7 changed files with 103 additions and 0 deletions
@@ -4,6 +4,7 @@ from __future__ import annotations
import argparse
from ...config import (
data_context,
SanityConfig,
)
@@ -102,4 +103,11 @@ def do_sanity(
help='prepare virtual environments without running tests',
)
if data_context().content.is_ansible:
sanity.add_argument(
'--fix',
action='store_true',
help='fix issues when possible instead of reporting them',
)
add_environments(parser, completer, ControllerMode.DELEGATED, TargetMode.SANITY) # sanity
@@ -992,6 +992,7 @@ class SanityScript(SanityTest, metaclass=abc.ABCMeta):
ANSIBLE_TEST_TARGET_PYTHON_VERSION=python.version,
ANSIBLE_TEST_CONTROLLER_PYTHON_VERSIONS=','.join(CONTROLLER_PYTHON_VERSIONS),
ANSIBLE_TEST_REMOTE_ONLY_PYTHON_VERSIONS=','.join(REMOTE_ONLY_PYTHON_VERSIONS),
ANSIBLE_TEST_FIX_MODE=str(int(args.fix)),
)
if self.min_max_python_only:
@@ -262,6 +262,7 @@ class SanityConfig(TestConfig):
self.allow_disabled: bool = args.allow_disabled
self.enable_optional_errors: bool = args.enable_optional_errors
self.prime_venvs: bool = args.prime_venvs
self.fix: bool = getattr(args, 'fix', False)
self.display_stderr = self.lint or self.list_tests
+10
View File
@@ -0,0 +1,10 @@
{
"prefixes": [
"test/sanity/code-smell/black."
],
"extensions": [
".py"
],
"error_code": "ansible-test",
"output": "path-message"
}
+75
View File
@@ -0,0 +1,75 @@
"""Sanity test which executes black."""
from __future__ import annotations
import itertools
import os
import re
import subprocess
import sys
def main() -> None:
"""Main program entry point."""
paths = sys.argv[1:] or sys.stdin.read().splitlines()
env = os.environ.copy()
controller_python_versions = env['ANSIBLE_TEST_CONTROLLER_PYTHON_VERSIONS'].split(',')
fix_mode = bool(int(env['ANSIBLE_TEST_FIX_MODE']))
version_options = [('-t', f'py{version.replace(".", "")}') for version in controller_python_versions]
options = {
'-m': 'black',
'--line-length': '160',
'--config': '/dev/null',
}
flags = [
'--skip-string-normalization',
]
if not fix_mode:
flags.append('--check')
cmd = [sys.executable]
cmd += itertools.chain.from_iterable(options.items())
cmd += itertools.chain.from_iterable(version_options)
cmd += flags
cmd.extend(paths)
try:
completed_process = subprocess.run(cmd, env=env, capture_output=True, check=True, text=True)
stdout, stderr = completed_process.stdout, completed_process.stderr
if stdout:
raise Exception(f'{stdout=} {stderr=}')
except subprocess.CalledProcessError as ex:
if ex.returncode != 1 or ex.stdout or not ex.stderr:
raise Exception(f'{ex.returncode=} {ex.stdout=} {ex.stderr=}') from None
stderr = ex.stderr
stderr = re.sub('(Oh no|All done).*$', '', stderr, flags=re.DOTALL).strip()
lines = stderr.splitlines()
check_prefix = 'would reformat '
fix_prefix = 'reformatted '
prefix = fix_prefix if fix_mode else check_prefix
for line in lines:
if not line.startswith(prefix):
raise Exception(f'{line=}')
if fix_mode:
continue
line = line.removeprefix(prefix)
print(f'{line}: Reformatting required. Run `ansible-test sanity --test black --fix` to update this file.')
if __name__ == '__main__':
main()
@@ -0,0 +1 @@
black
@@ -0,0 +1,7 @@
# edit "black.requirements.in" and generate with: hacking/update-sanity-requirements.py --test black
black==25.1.0
click==8.1.8
mypy-extensions==1.0.0
packaging==24.2
pathspec==0.12.1
platformdirs==4.3.6