Fix: release now skips missing pyproject.toml without failing

- Updated update_pyproject_version() to gracefully skip missing or unreadable pyproject.toml
- Added corresponding unit test ensuring missing file triggers no exception and no file creation
- Updated test wording for spec changelog section
- Ref: adjustments discussed in ChatGPT conversation (2025-12-09) - https://chatgpt.com/share/69388024-93e4-800f-a09f-bf78a6b9a53f
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-09 21:02:01 +01:00
parent 640b1042c2
commit 4285bf4a54
2 changed files with 33 additions and 7 deletions

View File

@@ -85,7 +85,6 @@ def _open_editor_for_changelog(initial_message: Optional[str] = None) -> str:
# File update helpers (pyproject + extra packaging + changelog) # File update helpers (pyproject + extra packaging + changelog)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def update_pyproject_version( def update_pyproject_version(
pyproject_path: str, pyproject_path: str,
new_version: str, new_version: str,
@@ -99,13 +98,25 @@ def update_pyproject_version(
version = "X.Y.Z" version = "X.Y.Z"
and replaces the version part with the given new_version string. and replaces the version part with the given new_version string.
If the file does not exist, it is skipped without failing the release.
""" """
if not os.path.exists(pyproject_path):
print(
f"[INFO] pyproject.toml not found at: {pyproject_path}, "
"skipping version update."
)
return
try: try:
with open(pyproject_path, "r", encoding="utf-8") as f: with open(pyproject_path, "r", encoding="utf-8") as f:
content = f.read() content = f.read()
except FileNotFoundError: except OSError as exc:
print(f"[ERROR] pyproject.toml not found at: {pyproject_path}") print(
sys.exit(1) f"[WARN] Could not read pyproject.toml at {pyproject_path}: {exc}. "
"Skipping version update."
)
return
pattern = r'^(version\s*=\s*")([^"]+)(")' pattern = r'^(version\s*=\s*")([^"]+)(")'
new_content, count = re.subn( new_content, count = re.subn(

View File

@@ -80,6 +80,21 @@ class TestUpdatePyprojectVersion(unittest.TestCase):
self.assertNotEqual(cm.exception.code, 0) self.assertNotEqual(cm.exception.code, 0)
def test_update_pyproject_version_missing_file_is_skipped(self) -> None:
"""
If pyproject.toml does not exist, the function must not raise
and must not create the file. It should simply be skipped.
"""
with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, "pyproject.toml")
self.assertFalse(os.path.exists(path))
# Must not raise an exception
update_pyproject_version(path, "1.2.3", preview=False)
# Still no file created
self.assertFalse(os.path.exists(path))
class TestUpdateFlakeVersion(unittest.TestCase): class TestUpdateFlakeVersion(unittest.TestCase):
def test_update_flake_version_normal(self) -> None: def test_update_flake_version_normal(self) -> None:
@@ -352,11 +367,11 @@ class TestUpdateSpecChangelog(unittest.TestCase):
with open(path, "r", encoding="utf-8") as f: with open(path, "r", encoding="utf-8") as f:
content = f.read() content = f.read()
# Neue Stanza muss nach %changelog stehen # New stanza must appear after the %changelog marker
self.assertIn("%changelog", content) self.assertIn("%changelog", content)
self.assertIn("Fedora changelog entry", content) self.assertIn("Fedora changelog entry", content)
self.assertIn("Test Maintainer <test@example.com>", content) self.assertIn("Test Maintainer <test@example.com>", content)
# Alte Einträge müssen erhalten bleiben # Old entries must still be present
self.assertIn("Old Maintainer <old@example.com>", content) self.assertIn("Old Maintainer <old@example.com>", content)
def test_update_spec_changelog_preview_does_not_write(self) -> None: def test_update_spec_changelog_preview_does_not_write(self) -> None:
@@ -396,7 +411,7 @@ class TestUpdateSpecChangelog(unittest.TestCase):
with open(path, "r", encoding="utf-8") as f: with open(path, "r", encoding="utf-8") as f:
content = f.read() content = f.read()
# Im Preview-Modus darf nichts verändert werden # In preview mode, the spec file must not change
self.assertEqual(content, original) self.assertEqual(content, original)