2025-12-08 20:15:13 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import tempfile
|
|
|
|
|
import textwrap
|
|
|
|
|
import unittest
|
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
2025-12-09 15:21:06 +01:00
|
|
|
from pkgmgr.actions.release.files import (
|
2025-12-08 20:15:13 +01:00
|
|
|
update_changelog,
|
|
|
|
|
update_debian_changelog,
|
2025-12-19 14:06:33 +01:00
|
|
|
update_flake_version,
|
|
|
|
|
update_pkgbuild_version,
|
|
|
|
|
update_pyproject_version,
|
2025-12-09 15:47:37 +01:00
|
|
|
update_spec_changelog,
|
2025-12-19 14:06:33 +01:00
|
|
|
update_spec_version,
|
2025-12-08 20:15:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdatePyprojectVersion(unittest.TestCase):
|
|
|
|
|
def test_update_pyproject_version_replaces_version_line(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
[project]
|
|
|
|
|
name = "example"
|
|
|
|
|
version = "0.1.0"
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "pyproject.toml")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_pyproject_version(path, "1.2.3", preview=False)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn('version = "1.2.3"', content)
|
|
|
|
|
self.assertNotIn('version = "0.1.0"', content)
|
|
|
|
|
|
|
|
|
|
def test_update_pyproject_version_preview_does_not_write(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
[project]
|
|
|
|
|
name = "example"
|
|
|
|
|
version = "0.1.0"
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "pyproject.toml")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_pyproject_version(path, "1.2.3", preview=True)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
2025-12-19 14:06:33 +01:00
|
|
|
def test_update_pyproject_version_raises_when_no_version_line_found(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
[project]
|
|
|
|
|
name = "example"
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "pyproject.toml")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
2025-12-19 14:06:33 +01:00
|
|
|
with self.assertRaises(RuntimeError) as cm:
|
2025-12-08 20:15:13 +01:00
|
|
|
update_pyproject_version(path, "1.2.3", preview=False)
|
|
|
|
|
|
2025-12-19 14:06:33 +01:00
|
|
|
self.assertIn("Missing version key", str(cm.exception))
|
|
|
|
|
|
2026-10-31 18:42:41 +01:00
|
|
|
def test_update_pyproject_version_skips_tool_only_pyproject(self) -> None:
|
2025-12-19 14:06:33 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2026-10-31 18:42:41 +01:00
|
|
|
[dependency-groups]
|
|
|
|
|
lint = ["ruff==0.17.3"]
|
|
|
|
|
|
|
|
|
|
[tool.ruff]
|
|
|
|
|
line-length = 88
|
2025-12-19 14:06:33 +01:00
|
|
|
"""
|
|
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "pyproject.toml")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
2026-10-31 18:42:41 +01:00
|
|
|
update_pyproject_version(path, "1.2.3", preview=False)
|
2025-12-19 14:06:33 +01:00
|
|
|
|
2026-10-31 18:42:41 +01:00
|
|
|
with open(path, encoding="utf-8") as f:
|
|
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
|
|
|
|
def test_update_pyproject_version_skips_dynamic_version(self) -> None:
|
|
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
|
|
|
|
[project]
|
|
|
|
|
name = "example"
|
|
|
|
|
dynamic = [
|
|
|
|
|
"version",
|
|
|
|
|
]
|
|
|
|
|
"""
|
|
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "pyproject.toml")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_pyproject_version(path, "1.2.3", preview=False)
|
|
|
|
|
|
|
|
|
|
with open(path, encoding="utf-8") as f:
|
|
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
2026-11-25 16:23:54 +00:00
|
|
|
def write_dynamic_attr_project(self, tmpdir: str, module: str) -> str:
|
|
|
|
|
with open(os.path.join(tmpdir, "pyproject.toml"), "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
|
|
|
|
[project]
|
|
|
|
|
name = "quality"
|
|
|
|
|
dynamic = ["version"]
|
|
|
|
|
|
|
|
|
|
[tool.setuptools]
|
|
|
|
|
package-dir = {"" = "src"}
|
|
|
|
|
|
|
|
|
|
[tool.setuptools.dynamic]
|
|
|
|
|
version = {attr = "quality.__version__"}
|
|
|
|
|
"""
|
|
|
|
|
).lstrip()
|
|
|
|
|
)
|
|
|
|
|
os.makedirs(os.path.join(tmpdir, "src", "quality"))
|
|
|
|
|
path = os.path.join(tmpdir, "src", "quality", "__init__.py")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(module)
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
def test_a_dynamic_attr_version_is_bumped_in_its_module_and_read_back(self) -> None:
|
|
|
|
|
from pkgmgr.core.version.source import read_pyproject_version
|
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
module = self.write_dynamic_attr_project(
|
|
|
|
|
tmpdir, '"""Quality."""\n\n__version__ = "0.1.0"\n'
|
|
|
|
|
)
|
|
|
|
|
with open(os.path.join(tmpdir, "pyproject.toml"), encoding="utf-8") as f:
|
|
|
|
|
before = f.read()
|
|
|
|
|
self.assertEqual(read_pyproject_version(tmpdir), "0.1.0")
|
|
|
|
|
|
|
|
|
|
update_pyproject_version(
|
|
|
|
|
os.path.join(tmpdir, "pyproject.toml"), "0.2.0", preview=False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with open(module, encoding="utf-8") as f:
|
|
|
|
|
self.assertEqual(f.read(), '"""Quality."""\n\n__version__ = "0.2.0"\n')
|
|
|
|
|
with open(os.path.join(tmpdir, "pyproject.toml"), encoding="utf-8") as f:
|
|
|
|
|
self.assertEqual(f.read(), before)
|
|
|
|
|
self.assertEqual(read_pyproject_version(tmpdir), "0.2.0")
|
|
|
|
|
|
|
|
|
|
def test_a_dynamic_attr_version_preview_writes_nothing(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
module = self.write_dynamic_attr_project(tmpdir, "__version__ = '0.1.0'\n")
|
|
|
|
|
|
|
|
|
|
update_pyproject_version(
|
|
|
|
|
os.path.join(tmpdir, "pyproject.toml"), "0.2.0", preview=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with open(module, encoding="utf-8") as f:
|
|
|
|
|
self.assertEqual(f.read(), "__version__ = '0.1.0'\n")
|
|
|
|
|
|
|
|
|
|
def test_a_dynamic_attr_module_without_the_literal_raises(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
self.write_dynamic_attr_project(
|
|
|
|
|
tmpdir,
|
|
|
|
|
"from importlib.metadata import version\n__version__ = version('quality')\n",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError) as cm:
|
|
|
|
|
update_pyproject_version(
|
|
|
|
|
os.path.join(tmpdir, "pyproject.toml"), "0.2.0", preview=False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertIn("Missing __version__ literal", str(cm.exception))
|
|
|
|
|
|
2025-12-09 21:02:01 +01:00
|
|
|
def test_update_pyproject_version_missing_file_is_skipped(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "pyproject.toml")
|
|
|
|
|
self.assertFalse(os.path.exists(path))
|
|
|
|
|
|
|
|
|
|
update_pyproject_version(path, "1.2.3", preview=False)
|
|
|
|
|
|
|
|
|
|
self.assertFalse(os.path.exists(path))
|
|
|
|
|
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
class TestUpdateFlakeVersion(unittest.TestCase):
|
|
|
|
|
def test_update_flake_version_normal(self) -> None:
|
|
|
|
|
original = 'version = "0.1.0";\n'
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "flake.nix")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_flake_version(path, "1.2.3", preview=False)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn('version = "1.2.3";', content)
|
|
|
|
|
self.assertNotIn('version = "0.1.0";', content)
|
|
|
|
|
|
|
|
|
|
def test_update_flake_version_preview(self) -> None:
|
|
|
|
|
original = 'version = "0.1.0";\n'
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "flake.nix")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_flake_version(path, "1.2.3", preview=True)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdatePkgbuildVersion(unittest.TestCase):
|
|
|
|
|
def test_update_pkgbuild_version_normal(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
pkgname=example
|
|
|
|
|
pkgver=0.1.0
|
|
|
|
|
pkgrel=5
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "PKGBUILD")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_pkgbuild_version(path, "1.2.3", preview=False)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn("pkgver=1.2.3", content)
|
|
|
|
|
self.assertIn("pkgrel=1", content)
|
|
|
|
|
self.assertNotIn("pkgver=0.1.0", content)
|
|
|
|
|
|
|
|
|
|
def test_update_pkgbuild_version_preview(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
pkgname=example
|
|
|
|
|
pkgver=0.1.0
|
|
|
|
|
pkgrel=5
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "PKGBUILD")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_pkgbuild_version(path, "1.2.3", preview=True)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateSpecVersion(unittest.TestCase):
|
|
|
|
|
def test_update_spec_version_normal(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
Name: package-manager
|
|
|
|
|
Version: 0.1.0
|
|
|
|
|
Release: 5%{?dist}
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "package-manager.spec")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_spec_version(path, "1.2.3", preview=False)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn("Version: 1.2.3", content)
|
|
|
|
|
self.assertIn("Release: 1%{?dist}", content)
|
|
|
|
|
self.assertNotIn("Version: 0.1.0", content)
|
|
|
|
|
self.assertNotIn("Release: 5%{?dist}", content)
|
|
|
|
|
|
|
|
|
|
def test_update_spec_version_preview(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
Name: package-manager
|
|
|
|
|
Version: 0.1.0
|
|
|
|
|
Release: 5%{?dist}
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-08 20:15:13 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "package-manager.spec")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_spec_version(path, "1.2.3", preview=True)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateChangelog(unittest.TestCase):
|
2026-05-28 08:17:35 +02:00
|
|
|
def test_update_changelog_creates_file_with_h1_if_missing(self) -> None:
|
2025-12-08 20:15:13 +01:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "CHANGELOG.md")
|
|
|
|
|
self.assertFalse(os.path.exists(path))
|
|
|
|
|
|
|
|
|
|
update_changelog(path, "1.2.3", message="First release", preview=False)
|
|
|
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(path))
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
2026-05-28 08:17:35 +02:00
|
|
|
# New file must lead with an H1 so markdownlint MD041 is happy.
|
|
|
|
|
self.assertTrue(content.startswith("# Changelog\n\n## [1.2.3]"))
|
2025-12-08 20:15:13 +01:00
|
|
|
self.assertIn("First release", content)
|
|
|
|
|
|
2026-05-28 08:17:35 +02:00
|
|
|
def test_update_changelog_inserts_below_existing_h1(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "CHANGELOG.md")
|
|
|
|
|
existing = "# Changelog\n\n## [0.1.0] - 2024-01-01\n\n* Initial content\n"
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(existing)
|
|
|
|
|
|
|
|
|
|
update_changelog(path, "1.0.0", message="Second release", preview=False)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2026-05-28 08:17:35 +02:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
# H1 still on top, new entry above the existing one.
|
|
|
|
|
self.assertTrue(content.startswith("# Changelog\n\n## [1.0.0]"))
|
|
|
|
|
# Exactly one H1.
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
content.count("\n# Changelog\n") + content.startswith("# Changelog\n"), 1
|
|
|
|
|
)
|
|
|
|
|
# Old entry still present, after the new one.
|
|
|
|
|
self.assertLess(
|
|
|
|
|
content.index("## [1.0.0]"),
|
|
|
|
|
content.index("## [0.1.0]"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_update_changelog_legacy_headerless_gets_h1_synthesized(self) -> None:
|
2025-12-08 20:15:13 +01:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "CHANGELOG.md")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write("## [0.1.0] - 2024-01-01\n\n* Initial content\n")
|
|
|
|
|
|
|
|
|
|
update_changelog(path, "1.0.0", message=None, preview=False)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
2026-05-28 08:17:35 +02:00
|
|
|
# An H1 is added so MD041 is satisfied even for legacy files.
|
|
|
|
|
self.assertTrue(content.startswith("# Changelog\n\n## [1.0.0]"))
|
2025-12-08 20:15:13 +01:00
|
|
|
self.assertIn("## [0.1.0] - 2024-01-01", content)
|
|
|
|
|
|
2026-06-28 00:51:37 +02:00
|
|
|
def test_update_changelog_does_not_inject_a_leading_bullet(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "CHANGELOG.md")
|
2026-10-18 23:30:58 +02:00
|
|
|
update_changelog(path, "1.2.3", message="- Provided bullet", preview=False)
|
2026-06-28 00:51:37 +02:00
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2026-06-28 00:51:37 +02:00
|
|
|
content = f.read()
|
|
|
|
|
|
2026-10-18 23:30:58 +02:00
|
|
|
self.assertIn("\n\n- Provided bullet\n", content)
|
|
|
|
|
self.assertNotIn("- - Provided bullet", content)
|
2026-06-28 00:51:37 +02:00
|
|
|
|
2026-06-28 02:32:53 +02:00
|
|
|
def test_update_changelog_transforms_heading_and_inline_code(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "CHANGELOG.md")
|
|
|
|
|
update_changelog(
|
|
|
|
|
path,
|
|
|
|
|
"1.2.3",
|
2026-10-18 23:30:58 +02:00
|
|
|
message="# Summary\n\n- uses `foo` tool",
|
2026-06-28 02:32:53 +02:00
|
|
|
preview=False,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2026-06-28 02:32:53 +02:00
|
|
|
content = f.read()
|
|
|
|
|
|
2026-10-01 17:41:59 +02:00
|
|
|
self.assertIn("### Summary", content)
|
2026-06-28 02:32:53 +02:00
|
|
|
self.assertIn("*foo*", content)
|
2026-10-01 17:41:59 +02:00
|
|
|
self.assertNotIn("\n# Summary", content)
|
|
|
|
|
self.assertNotIn("\n## Summary", content)
|
2026-06-28 02:32:53 +02:00
|
|
|
self.assertNotIn("`foo`", content)
|
|
|
|
|
|
2025-12-08 20:15:13 +01:00
|
|
|
def test_update_changelog_preview_does_not_write(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "CHANGELOG.md")
|
|
|
|
|
original = "## [0.1.0] - 2024-01-01\n\n* Initial content\n"
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
update_changelog(path, "1.0.0", message="Preview only", preview=True)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateDebianChangelog(unittest.TestCase):
|
|
|
|
|
def test_update_debian_changelog_creates_new_stanza(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "changelog")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write("existing content\n")
|
|
|
|
|
|
|
|
|
|
with patch.dict(
|
|
|
|
|
os.environ,
|
2025-12-19 14:06:33 +01:00
|
|
|
{"DEBFULLNAME": "Test Maintainer", "DEBEMAIL": "test@example.com"},
|
2025-12-08 20:15:13 +01:00
|
|
|
clear=False,
|
|
|
|
|
):
|
|
|
|
|
update_debian_changelog(
|
|
|
|
|
debian_changelog_path=path,
|
|
|
|
|
package_name="package-manager",
|
|
|
|
|
new_version="1.2.3",
|
|
|
|
|
message="Test debian entry",
|
|
|
|
|
preview=False,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn("package-manager (1.2.3-1) unstable; urgency=medium", content)
|
2026-06-28 00:51:37 +02:00
|
|
|
self.assertIn(" Test debian entry", content)
|
2025-12-08 20:15:13 +01:00
|
|
|
self.assertIn("Test Maintainer <test@example.com>", content)
|
|
|
|
|
self.assertIn("existing content", content)
|
|
|
|
|
|
|
|
|
|
def test_update_debian_changelog_preview_does_not_write(self) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "changelog")
|
|
|
|
|
original = "existing content\n"
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
with patch.dict(
|
|
|
|
|
os.environ,
|
2025-12-19 14:06:33 +01:00
|
|
|
{"DEBFULLNAME": "Test Maintainer", "DEBEMAIL": "test@example.com"},
|
2025-12-08 20:15:13 +01:00
|
|
|
clear=False,
|
|
|
|
|
):
|
|
|
|
|
update_debian_changelog(
|
|
|
|
|
debian_changelog_path=path,
|
|
|
|
|
package_name="package-manager",
|
|
|
|
|
new_version="1.2.3",
|
|
|
|
|
message="Test debian entry",
|
|
|
|
|
preview=True,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-08 20:15:13 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
2026-06-28 00:51:37 +02:00
|
|
|
def test_update_debian_changelog_indents_every_line_of_multiline_message(
|
|
|
|
|
self,
|
|
|
|
|
) -> None:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "changelog")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write("existing content\n")
|
|
|
|
|
|
|
|
|
|
with patch.dict(
|
|
|
|
|
os.environ,
|
|
|
|
|
{"DEBFULLNAME": "Test Maintainer", "DEBEMAIL": "test@example.com"},
|
|
|
|
|
clear=False,
|
|
|
|
|
):
|
|
|
|
|
update_debian_changelog(
|
|
|
|
|
debian_changelog_path=path,
|
|
|
|
|
package_name="package-manager",
|
|
|
|
|
new_version="1.2.3",
|
|
|
|
|
message="* First bullet\n\n* Second bullet",
|
|
|
|
|
preview=False,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2026-06-28 00:51:37 +02:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn(" * First bullet", content)
|
|
|
|
|
self.assertIn(" * Second bullet", content)
|
|
|
|
|
stanza = content.split(" -- ", 1)[0]
|
|
|
|
|
body_lines = [
|
|
|
|
|
line
|
|
|
|
|
for line in stanza.splitlines()
|
|
|
|
|
if line.strip() and not line.startswith("package-manager (")
|
|
|
|
|
]
|
|
|
|
|
for line in body_lines:
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
line.startswith(" "),
|
|
|
|
|
msg=f"un-indented debian change line: {line!r}",
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-08 20:15:13 +01:00
|
|
|
|
2025-12-09 15:47:37 +01:00
|
|
|
class TestUpdateSpecChangelog(unittest.TestCase):
|
|
|
|
|
def test_update_spec_changelog_inserts_stanza_after_changelog_marker(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
Name: package-manager
|
|
|
|
|
Version: 0.1.0
|
|
|
|
|
Release: 5%{?dist}
|
2025-12-09 15:47:37 +01:00
|
|
|
|
2025-12-19 14:06:33 +01:00
|
|
|
%description
|
|
|
|
|
Some description.
|
2025-12-09 15:47:37 +01:00
|
|
|
|
2025-12-19 14:06:33 +01:00
|
|
|
%changelog
|
|
|
|
|
* Mon Jan 01 2024 Old Maintainer <old@example.com> - 0.1.0-1
|
|
|
|
|
- Old entry
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-09 15:47:37 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "package-manager.spec")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
with patch.dict(
|
|
|
|
|
os.environ,
|
2025-12-19 14:06:33 +01:00
|
|
|
{"DEBFULLNAME": "Test Maintainer", "DEBEMAIL": "test@example.com"},
|
2025-12-09 15:47:37 +01:00
|
|
|
clear=False,
|
|
|
|
|
):
|
|
|
|
|
update_spec_changelog(
|
|
|
|
|
spec_path=path,
|
|
|
|
|
package_name="package-manager",
|
|
|
|
|
new_version="1.2.3",
|
|
|
|
|
message="Fedora changelog entry",
|
|
|
|
|
preview=False,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-09 15:47:37 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn("%changelog", content)
|
|
|
|
|
self.assertIn("Fedora changelog entry", content)
|
|
|
|
|
self.assertIn("Test Maintainer <test@example.com>", content)
|
|
|
|
|
self.assertIn("Old Maintainer <old@example.com>", content)
|
|
|
|
|
|
|
|
|
|
def test_update_spec_changelog_preview_does_not_write(self) -> None:
|
2025-12-18 14:04:44 +01:00
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
2025-12-19 14:06:33 +01:00
|
|
|
Name: package-manager
|
|
|
|
|
Version: 0.1.0
|
|
|
|
|
Release: 5%{?dist}
|
2025-12-09 15:47:37 +01:00
|
|
|
|
2025-12-19 14:06:33 +01:00
|
|
|
%changelog
|
|
|
|
|
* Mon Jan 01 2024 Old Maintainer <old@example.com> - 0.1.0-1
|
|
|
|
|
- Old entry
|
|
|
|
|
"""
|
2025-12-18 14:04:44 +01:00
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
2025-12-09 15:47:37 +01:00
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "package-manager.spec")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
with patch.dict(
|
|
|
|
|
os.environ,
|
2025-12-19 14:06:33 +01:00
|
|
|
{"DEBFULLNAME": "Test Maintainer", "DEBEMAIL": "test@example.com"},
|
2025-12-09 15:47:37 +01:00
|
|
|
clear=False,
|
|
|
|
|
):
|
|
|
|
|
update_spec_changelog(
|
|
|
|
|
spec_path=path,
|
|
|
|
|
package_name="package-manager",
|
|
|
|
|
new_version="1.2.3",
|
|
|
|
|
message="Fedora changelog entry",
|
|
|
|
|
preview=True,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2025-12-09 15:47:37 +01:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(content, original)
|
|
|
|
|
|
2026-06-28 00:51:37 +02:00
|
|
|
def test_update_spec_changelog_dashes_every_line_of_multiline_message(
|
|
|
|
|
self,
|
|
|
|
|
) -> None:
|
|
|
|
|
original = (
|
|
|
|
|
textwrap.dedent(
|
|
|
|
|
"""
|
|
|
|
|
Name: package-manager
|
|
|
|
|
Version: 0.1.0
|
|
|
|
|
Release: 5%{?dist}
|
|
|
|
|
|
|
|
|
|
%changelog
|
|
|
|
|
* Mon Jan 01 2024 Old Maintainer <old@example.com> - 0.1.0-1
|
|
|
|
|
- Old entry
|
|
|
|
|
"""
|
|
|
|
|
).strip()
|
|
|
|
|
+ "\n"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
path = os.path.join(tmpdir, "package-manager.spec")
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(original)
|
|
|
|
|
|
|
|
|
|
with patch.dict(
|
|
|
|
|
os.environ,
|
|
|
|
|
{"DEBFULLNAME": "Test Maintainer", "DEBEMAIL": "test@example.com"},
|
|
|
|
|
clear=False,
|
|
|
|
|
):
|
|
|
|
|
update_spec_changelog(
|
|
|
|
|
spec_path=path,
|
|
|
|
|
package_name="package-manager",
|
|
|
|
|
new_version="1.2.3",
|
|
|
|
|
message="* First bullet\n\n* Second bullet",
|
|
|
|
|
preview=False,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-27 16:47:13 +02:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2026-06-28 00:51:37 +02:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
self.assertIn("- * First bullet", content)
|
|
|
|
|
self.assertIn("- * Second bullet", content)
|
|
|
|
|
new_section = content.split("- Old entry", 1)[0].split("%changelog", 1)[1]
|
|
|
|
|
for line in new_section.splitlines():
|
|
|
|
|
if not line.strip() or line.startswith("* "):
|
|
|
|
|
continue
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
line.startswith("- "),
|
|
|
|
|
msg=f"rpm body line would be read as a new entry: {line!r}",
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-09 15:47:37 +01:00
|
|
|
|
2025-12-08 20:15:13 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|