fix(scripts): resolve the ruff findings in the scripts and tests
Fix what ruff 0.17.5's default rule set and the complexity rules report (#112): removeprefix/removesuffix instead of slicing, `_unused` for discarded tuple members, obsolete `noqa: E402` removed, a parenthesised string pair and an immutable class constant in the tests. Three functions stay over a complexity threshold and carry an inline `# noqa` with the rule code and a reason: matrix_of and copy_sources of check_image_versions.py (#96 may replace them) and path_problems of check_tarball.py. No rule is ignored globally and no threshold is tuned. ruff writes .ruff_cache/ into the checkout, which test_real_build_passes caught in the Collection archive: add it to galaxy.yml build_ignore. ruff.toml joins the molecule selector's IGNORED paths, since no scenario reads it. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
This commit is contained in:
1 parent
db1c1e176f
commit
697e4b6092
9 files changed
+30
-26
No files matched your search
@@ -35,6 +35,7 @@ build_ignore:
|
|||||||
- .gitignore
|
- .gitignore
|
||||||
- .pre-commit-config.yaml
|
- .pre-commit-config.yaml
|
||||||
- .reuse
|
- .reuse
|
||||||
|
- .ruff_cache
|
||||||
- .venv
|
- .venv
|
||||||
- .yamllint
|
- .yamllint
|
||||||
- docs
|
- docs
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ def _scalar(raw, where):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def matrix_of(text):
|
def matrix_of(text): # noqa: C901, PLR0912, PLR0915 hand-written block-YAML reader; #96 may replace it
|
||||||
"""Return {name: {version, subpath, dockerfile[, job_image]}} from the text of image.yml.
|
"""Return {name: {version, subpath, dockerfile[, job_image]}} from the text of image.yml.
|
||||||
|
|
||||||
A purpose-built block-style reader, not a YAML parser: the Renovate image has no PyYAML
|
A purpose-built block-style reader, not a YAML parser: the Renovate image has no PyYAML
|
||||||
@@ -294,7 +294,7 @@ def _mount_reads_context(line):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def copy_sources(dockerfile_text, name="Dockerfile"):
|
def copy_sources(dockerfile_text, name="Dockerfile"): # noqa: C901 one branch per unsupported COPY form; #96 may replace it
|
||||||
"""Context paths a Dockerfile COPY/ADD reads (not --from=, not URLs).
|
"""Context paths a Dockerfile COPY/ADD reads (not --from=, not URLs).
|
||||||
|
|
||||||
Fails closed (CheckError naming the file and line) on anything whose real path cannot
|
Fails closed (CheckError naming the file and line) on anything whose real path cannot
|
||||||
@@ -353,7 +353,7 @@ def _under(path, entry):
|
|||||||
entry = entry.strip()
|
entry = entry.strip()
|
||||||
if entry in ("", ".", "./", "/"):
|
if entry in ("", ".", "./", "/"):
|
||||||
return True
|
return True
|
||||||
entry = entry[2:] if entry.startswith("./") else entry
|
entry = entry.removeprefix("./")
|
||||||
return (
|
return (
|
||||||
path == entry.rstrip("/")
|
path == entry.rstrip("/")
|
||||||
or path.startswith(entry.rstrip("/") + "/")
|
or path.startswith(entry.rstrip("/") + "/")
|
||||||
@@ -432,7 +432,7 @@ def dockerfile_releases(dockerfile_text):
|
|||||||
f"line {lineno}: FROM {ref} holds a variable, so its release cannot be read"
|
f"line {lineno}: FROM {ref} holds a variable, so its release cannot be read"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
name, unused, tag = ref.split("@")[0].partition(":")
|
name, _unused, tag = ref.split("@")[0].partition(":")
|
||||||
if name in UBUNTU_NAMES:
|
if name in UBUNTU_NAMES:
|
||||||
releases.append(tag)
|
releases.append(tag)
|
||||||
else:
|
else:
|
||||||
@@ -570,7 +570,7 @@ def check_image_metadata(matrix, read_file, size_of):
|
|||||||
f"image '{name}': the title label is '{title}', expected the image name"
|
f"image '{name}': the title label is '{title}', expected the image name"
|
||||||
)
|
)
|
||||||
base = labels.get("org.opencontainers.image.base.name", "").strip()
|
base = labels.get("org.opencontainers.image.base.name", "").strip()
|
||||||
releases, unused = dockerfile_releases(text)
|
releases, _unused = dockerfile_releases(text)
|
||||||
if (
|
if (
|
||||||
base
|
base
|
||||||
and sorted(set(releases))
|
and sorted(set(releases))
|
||||||
|
|||||||
@@ -104,8 +104,8 @@ MAX_COMPONENT_BYTES = 255
|
|||||||
|
|
||||||
def normalize(name):
|
def normalize(name):
|
||||||
"""The member name without one leading './' and one trailing '/' (so 'a//' stays odd and is rejected)."""
|
"""The member name without one leading './' and one trailing '/' (so 'a//' stays odd and is rejected)."""
|
||||||
path = name[:-1] if name.endswith("/") else name
|
path = name.removesuffix("/")
|
||||||
return path[2:] if path.startswith("./") else path
|
return path.removeprefix("./")
|
||||||
|
|
||||||
|
|
||||||
def fold(text):
|
def fold(text):
|
||||||
@@ -130,7 +130,7 @@ def component_problems(path, part):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def path_problems(name):
|
def path_problems(name): # noqa: C901 one check per rule of the archive allowlist
|
||||||
"""Problems of one member path, as a list of messages (empty when the path is fine)."""
|
"""Problems of one member path, as a list of messages (empty when the path is fine)."""
|
||||||
if name.startswith("/"):
|
if name.startswith("/"):
|
||||||
return [f"{name!r}: absolute path"]
|
return [f"{name!r}: absolute path"]
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ IGNORED = (
|
|||||||
".ansible-lint",
|
".ansible-lint",
|
||||||
".pre-commit-config.yaml",
|
".pre-commit-config.yaml",
|
||||||
"requirements-dev.txt",
|
"requirements-dev.txt",
|
||||||
|
"ruff.toml",
|
||||||
"apm.yml",
|
"apm.yml",
|
||||||
"apm.lock.yaml",
|
"apm.lock.yaml",
|
||||||
"tests/sanity/*",
|
"tests/sanity/*",
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import unittest
|
|||||||
|
|
||||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||||
sys.path.insert(0, str(SCRIPTS))
|
sys.path.insert(0, str(SCRIPTS))
|
||||||
import bump_image_versions as biv # noqa: E402
|
import bump_image_versions as biv
|
||||||
|
|
||||||
IMAGE_YML = """\
|
IMAGE_YML = """\
|
||||||
# header comment
|
# header comment
|
||||||
@@ -152,7 +152,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
return (self.repo / ".gitea/workflows/image.yml").read_text()
|
return (self.repo / ".gitea/workflows/image.yml").read_text()
|
||||||
|
|
||||||
def test_nothing_changed(self):
|
def test_nothing_changed(self):
|
||||||
code, out, unused = self.bump()
|
code, out, _unused = self.bump()
|
||||||
self.assertEqual((code, out), (0, "image versions: nothing to bump\n"))
|
self.assertEqual((code, out), (0, "image versions: nothing to bump\n"))
|
||||||
self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.0", ci="0.2.0"))
|
self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.0", ci="0.2.0"))
|
||||||
|
|
||||||
@@ -200,7 +200,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_bad_base_ref_is_a_clean_failure(self):
|
def test_bad_base_ref_is_a_clean_failure(self):
|
||||||
for ref in ("--output=x", "nope"):
|
for ref in ("--output=x", "nope"):
|
||||||
code, unused, err = self.bump(ref)
|
code, _unused, err = self.bump(ref)
|
||||||
self.assertEqual(code, 1)
|
self.assertEqual(code, 1)
|
||||||
self.assertNotIn("Traceback", err)
|
self.assertNotIn("Traceback", err)
|
||||||
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
||||||
@@ -225,7 +225,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
self.write(".gitea/workflows/image.yml", flow)
|
self.write(".gitea/workflows/image.yml", flow)
|
||||||
self.commit("flow")
|
self.commit("flow")
|
||||||
self.write("requirements-dev.txt", "a==2\n")
|
self.write("requirements-dev.txt", "a==2\n")
|
||||||
code, unused, err = self.bump()
|
code, _unused, err = self.bump()
|
||||||
self.assertEqual(code, 1)
|
self.assertEqual(code, 1)
|
||||||
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
||||||
self.assertEqual(self.versions(), flow)
|
self.assertEqual(self.versions(), flow)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from unittest import mock
|
|||||||
|
|
||||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||||
sys.path.insert(0, str(SCRIPTS))
|
sys.path.insert(0, str(SCRIPTS))
|
||||||
import check_bot_pr_diff as cbd # noqa: E402
|
import check_bot_pr_diff as cbd
|
||||||
|
|
||||||
D1, D2 = "sha256:" + "a" * 64, "sha256:" + "b" * 64
|
D1, D2 = "sha256:" + "a" * 64, "sha256:" + "b" * 64
|
||||||
FILES = {
|
FILES = {
|
||||||
@@ -282,7 +282,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
"requirements-dev.txt", "# pins\nansible-core==2.19.14\nreuse==6.3.0\n\0"
|
"requirements-dev.txt", "# pins\nansible-core==2.19.14\nreuse==6.3.0\n\0"
|
||||||
)
|
)
|
||||||
self.commit("bot")
|
self.commit("bot")
|
||||||
code, unused, err = self.gate()
|
code, _unused, err = self.gate()
|
||||||
self.assertEqual(code, 1, err)
|
self.assertEqual(code, 1, err)
|
||||||
self.assertIn("requirements-dev.txt", err)
|
self.assertIn("requirements-dev.txt", err)
|
||||||
|
|
||||||
@@ -292,7 +292,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
(self.repo / "requirements-dev.txt").write_bytes(text.encode("utf-16"))
|
(self.repo / "requirements-dev.txt").write_bytes(text.encode("utf-16"))
|
||||||
self.commit("bot")
|
self.commit("bot")
|
||||||
code, unused, err = self.gate()
|
code, _unused, err = self.gate()
|
||||||
self.assertEqual(code, 1, err)
|
self.assertEqual(code, 1, err)
|
||||||
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
||||||
|
|
||||||
@@ -300,7 +300,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
self.write(".gitattributes", "requirements-dev.txt -diff\n")
|
self.write(".gitattributes", "requirements-dev.txt -diff\n")
|
||||||
self.edit("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0")
|
self.edit("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0")
|
||||||
self.commit("bot")
|
self.commit("bot")
|
||||||
code, unused, err = self.gate()
|
code, _unused, err = self.gate()
|
||||||
self.assertEqual(code, 1, err)
|
self.assertEqual(code, 1, err)
|
||||||
self.assertIn(".gitattributes", err)
|
self.assertIn(".gitattributes", err)
|
||||||
|
|
||||||
@@ -374,7 +374,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
def test_lowering_a_matrix_version_fails_and_raising_it_passes(self):
|
def test_lowering_a_matrix_version_fails_and_raising_it_passes(self):
|
||||||
self.edit(".gitea/workflows/image.yml", "version: 0.2.0", "version: 0.1.9")
|
self.edit(".gitea/workflows/image.yml", "version: 0.2.0", "version: 0.1.9")
|
||||||
self.commit("lower")
|
self.commit("lower")
|
||||||
code, unused, err = self.gate()
|
code, _unused, err = self.gate()
|
||||||
self.assertEqual(code, 1, err)
|
self.assertEqual(code, 1, err)
|
||||||
self.assertIn("version", err)
|
self.assertIn("version", err)
|
||||||
run(self.repo, "git", "checkout", "-q", "main")
|
run(self.repo, "git", "checkout", "-q", "main")
|
||||||
@@ -399,7 +399,7 @@ class RepoTest(unittest.TestCase):
|
|||||||
self.write("roles/x/main.yml", "a\n")
|
self.write("roles/x/main.yml", "a\n")
|
||||||
self.edit("renovate.json", "{}", '{"a":1}')
|
self.edit("renovate.json", "{}", '{"a":1}')
|
||||||
self.commit("bot")
|
self.commit("bot")
|
||||||
code, unused, err = self.gate()
|
code, _unused, err = self.gate()
|
||||||
self.assertEqual(code, 1)
|
self.assertEqual(code, 1)
|
||||||
lines = err.strip().splitlines()
|
lines = err.strip().splitlines()
|
||||||
self.assertEqual(len(lines), 3, err)
|
self.assertEqual(len(lines), 3, err)
|
||||||
@@ -418,12 +418,12 @@ class RepoTest(unittest.TestCase):
|
|||||||
def test_other_branches_are_skipped(self):
|
def test_other_branches_are_skipped(self):
|
||||||
self.write("roles/x/main.yml", "a\n")
|
self.write("roles/x/main.yml", "a\n")
|
||||||
self.commit("human")
|
self.commit("human")
|
||||||
code, out, unused = self.gate(head="feat/x")
|
code, out, _unused = self.gate(head="feat/x")
|
||||||
self.assertEqual(code, 0)
|
self.assertEqual(code, 0)
|
||||||
self.assertIn("skipped", out)
|
self.assertIn("skipped", out)
|
||||||
|
|
||||||
def test_bad_input(self):
|
def test_bad_input(self):
|
||||||
code, unused, err = self.gate(base="--output=x")
|
code, _unused, err = self.gate(base="--output=x")
|
||||||
self.assertEqual(code, 1)
|
self.assertEqual(code, 1)
|
||||||
self.assertNotIn("Traceback", err)
|
self.assertNotIn("Traceback", err)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import unittest
|
|||||||
|
|
||||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||||
sys.path.insert(0, str(SCRIPTS))
|
sys.path.insert(0, str(SCRIPTS))
|
||||||
import check_image_versions as civ # noqa: E402
|
import check_image_versions as civ
|
||||||
|
|
||||||
DIGEST = "sha256:" + "a" * 64
|
DIGEST = "sha256:" + "a" * 64
|
||||||
JOB = f"ubuntu:24.04@{DIGEST}"
|
JOB = f"ubuntu:24.04@{DIGEST}"
|
||||||
@@ -376,8 +376,10 @@ class PureTest(unittest.TestCase):
|
|||||||
matrix, read, text.replace("matrix.job_image", "matrix.other")
|
matrix, read, text.replace("matrix.job_image", "matrix.other")
|
||||||
),
|
),
|
||||||
[
|
[
|
||||||
".gitea/workflows/image.yml: the job container must be "
|
(
|
||||||
"`image: ${{ matrix.job_image }}`"
|
".gitea/workflows/image.yml: the job container must be "
|
||||||
|
"`image: ${{ matrix.job_image }}`"
|
||||||
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import yaml
|
|||||||
|
|
||||||
REPO = pathlib.Path(__file__).resolve().parents[2]
|
REPO = pathlib.Path(__file__).resolve().parents[2]
|
||||||
sys.path.insert(0, str(REPO / "scripts"))
|
sys.path.insert(0, str(REPO / "scripts"))
|
||||||
import check_tarball # noqa: E402
|
import check_tarball
|
||||||
|
|
||||||
|
|
||||||
class TarballTest(unittest.TestCase):
|
class TarballTest(unittest.TestCase):
|
||||||
@@ -403,7 +403,7 @@ class BuildWorkflowTest(unittest.TestCase):
|
|||||||
"""The symlink guard of build.yml prunes directories that the build itself skips."""
|
"""The symlink guard of build.yml prunes directories that the build itself skips."""
|
||||||
|
|
||||||
# ansible-galaxy excludes these without being told (ansible.galaxy.collection, the default ignore list).
|
# ansible-galaxy excludes these without being told (ansible.galaxy.collection, the default ignore list).
|
||||||
DEFAULT_EXCLUDED = {".git"}
|
DEFAULT_EXCLUDED = frozenset({".git"})
|
||||||
|
|
||||||
def test_pruned_directories_are_not_built(self):
|
def test_pruned_directories_are_not_built(self):
|
||||||
workflow = (REPO / ".gitea" / "workflows" / "build.yml").read_text()
|
workflow = (REPO / ".gitea" / "workflows" / "build.yml").read_text()
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import yaml
|
|||||||
|
|
||||||
REPO = pathlib.Path(__file__).resolve().parents[2]
|
REPO = pathlib.Path(__file__).resolve().parents[2]
|
||||||
sys.path.insert(0, str(REPO / "scripts"))
|
sys.path.insert(0, str(REPO / "scripts"))
|
||||||
import select_molecule_scenarios as sel # noqa: E402
|
import select_molecule_scenarios as sel
|
||||||
|
|
||||||
ALL = [
|
ALL = [
|
||||||
"extensions/molecule/image-smoke",
|
"extensions/molecule/image-smoke",
|
||||||
|
|||||||
Reference in new issue
Block a user