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:
1 parent
557f573df2
commit
db1c1e176f
12 files changed
+1520
-427
No files matched your search
@@ -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 shutil
|
||||
@@ -33,8 +34,18 @@ CI_DOCKERFILE = "FROM x AS b\nCOPY requirements-dev.txt .pre-commit-config.yaml
|
||||
|
||||
|
||||
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 PureTest(unittest.TestCase):
|
||||
@@ -82,12 +93,17 @@ class EntryAttributionTest(unittest.TestCase):
|
||||
self.assertEqual(out, SHUFFLED_YML.format(mol="0.1.1", ci="0.2.0"))
|
||||
|
||||
def test_entry_without_its_own_version_never_borrows_another(self):
|
||||
text = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0").replace(' version: "0.2.0"\n', "")
|
||||
text = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0").replace(
|
||||
' version: "0.2.0"\n', ""
|
||||
)
|
||||
with self.assertRaises(biv.civ.CheckError):
|
||||
biv.rewrite(text, {"mol": "0.1.1"})
|
||||
|
||||
def test_flow_style_entry_is_refused(self):
|
||||
text = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0") + " - {name: x, version: 1.0.0}\n"
|
||||
text = (
|
||||
SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0")
|
||||
+ " - {name: x, version: 1.0.0}\n"
|
||||
)
|
||||
with self.assertRaises(biv.civ.CheckError):
|
||||
biv.rewrite(text, {"ci": "0.2.1"})
|
||||
|
||||
@@ -100,7 +116,9 @@ class RepoTest(unittest.TestCase):
|
||||
run(self.repo, "git", "init", "-q", "-b", "main")
|
||||
run(self.repo, "git", "config", "user.email", "[email protected]")
|
||||
run(self.repo, "git", "config", "user.name", "t")
|
||||
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.0"))
|
||||
self.write(
|
||||
".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
|
||||
)
|
||||
self.write("images/ci/Dockerfile", CI_DOCKERFILE)
|
||||
self.write("images/mol/Dockerfile", "FROM x\n")
|
||||
self.write("requirements-dev.txt", "a==1\n")
|
||||
@@ -118,8 +136,13 @@ class RepoTest(unittest.TestCase):
|
||||
run(self.repo, "git", "commit", "-q", "-m", msg)
|
||||
|
||||
def script(self, name, *args):
|
||||
proc = subprocess.run([sys.executable, str(SCRIPTS / name), *args], cwd=self.repo,
|
||||
capture_output=True, text=True, check=False)
|
||||
proc = subprocess.run(
|
||||
[sys.executable, str(SCRIPTS / name), *args],
|
||||
cwd=self.repo,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
return proc.returncode, proc.stdout, proc.stderr
|
||||
|
||||
def bump(self, *args):
|
||||
@@ -161,7 +184,9 @@ class RepoTest(unittest.TestCase):
|
||||
|
||||
def test_higher_version_already_present_is_kept(self):
|
||||
self.write("images/mol/Dockerfile", "FROM y\n")
|
||||
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.3.0", ci="0.2.0"))
|
||||
self.write(
|
||||
".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.3.0", ci="0.2.0")
|
||||
)
|
||||
self.assertEqual(self.bump()[1], "image versions: nothing to bump\n")
|
||||
self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.3.0", ci="0.2.0"))
|
||||
|
||||
@@ -169,7 +194,9 @@ class RepoTest(unittest.TestCase):
|
||||
self.write("images/mol/new-file", "x\n") # untracked
|
||||
self.write("requirements-dev.txt", "a==2\n")
|
||||
out = self.bump()[1]
|
||||
self.assertEqual(out, "image versions: bumped ci 0.2.0 -> 0.2.1, mol 0.1.0 -> 0.1.1\n")
|
||||
self.assertEqual(
|
||||
out, "image versions: bumped ci 0.2.0 -> 0.2.1, mol 0.1.0 -> 0.1.1\n"
|
||||
)
|
||||
|
||||
def test_bad_base_ref_is_a_clean_failure(self):
|
||||
for ref in ("--output=x", "nope"):
|
||||
@@ -179,15 +206,22 @@ class RepoTest(unittest.TestCase):
|
||||
self.assertEqual(len(err.strip().splitlines()), 1, err)
|
||||
|
||||
def test_shuffled_key_order_bumps_the_right_entry_end_to_end(self):
|
||||
self.write(".gitea/workflows/image.yml", SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0"))
|
||||
self.write(
|
||||
".gitea/workflows/image.yml", SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0")
|
||||
)
|
||||
self.commit("shuffle")
|
||||
self.write("requirements-dev.txt", "a==2\n") # an input of ci only
|
||||
code, out, err = self.bump()
|
||||
self.assertEqual((code, out), (0, "image versions: bumped ci 0.2.0 -> 0.2.1\n"), err)
|
||||
self.assertEqual(
|
||||
(code, out), (0, "image versions: bumped ci 0.2.0 -> 0.2.1\n"), err
|
||||
)
|
||||
self.assertEqual(self.versions(), SHUFFLED_YML.format(mol="0.1.0", ci="0.2.1"))
|
||||
|
||||
def test_unparseable_entry_is_a_one_line_failure(self):
|
||||
flow = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0") + " - {name: x, version: 1.0.0}\n"
|
||||
flow = (
|
||||
SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0")
|
||||
+ " - {name: x, version: 1.0.0}\n"
|
||||
)
|
||||
self.write(".gitea/workflows/image.yml", flow)
|
||||
self.commit("flow")
|
||||
self.write("requirements-dev.txt", "a==2\n")
|
||||
@@ -197,7 +231,11 @@ class RepoTest(unittest.TestCase):
|
||||
self.assertEqual(self.versions(), flow)
|
||||
|
||||
def test_crlf_line_endings_are_preserved(self):
|
||||
crlf = IMAGE_YML.format(mol="0.1.0", ci="0.2.0").replace("\n", "\r\n").encode("utf-8")
|
||||
crlf = (
|
||||
IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
|
||||
.replace("\n", "\r\n")
|
||||
.encode("utf-8")
|
||||
)
|
||||
path = self.repo / ".gitea/workflows/image.yml"
|
||||
path.write_bytes(crlf)
|
||||
self.commit("crlf")
|
||||
@@ -220,11 +258,35 @@ class RepoTest(unittest.TestCase):
|
||||
self.commit("two")
|
||||
self.write("images/mol/Dockerfile", "FROM z\n")
|
||||
self.commit("three")
|
||||
run(self.repo.parent, "git", "clone", "-q", "--depth", "1", "--branch", "feature", f"file://{self.repo}",
|
||||
str(clone))
|
||||
run(clone, "git", "fetch", "-q", "--depth", "1", "origin", "main:refs/remotes/origin/main")
|
||||
proc = subprocess.run([sys.executable, str(SCRIPTS / "bump_image_versions.py"), "origin/main"], cwd=clone,
|
||||
capture_output=True, text=True, check=False)
|
||||
run(
|
||||
self.repo.parent,
|
||||
"git",
|
||||
"clone",
|
||||
"-q",
|
||||
"--depth",
|
||||
"1",
|
||||
"--branch",
|
||||
"feature",
|
||||
f"file://{self.repo}",
|
||||
str(clone),
|
||||
)
|
||||
run(
|
||||
clone,
|
||||
"git",
|
||||
"fetch",
|
||||
"-q",
|
||||
"--depth",
|
||||
"1",
|
||||
"origin",
|
||||
"main:refs/remotes/origin/main",
|
||||
)
|
||||
proc = subprocess.run(
|
||||
[sys.executable, str(SCRIPTS / "bump_image_versions.py"), "origin/main"],
|
||||
cwd=clone,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
self.assertEqual(proc.returncode, 1)
|
||||
self.assertEqual(len(proc.stderr.strip().splitlines()), 1, proc.stderr)
|
||||
self.assertIn("git fetch --unshallow", proc.stderr)
|
||||
|
||||
Reference in new issue
Block a user