|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +ROOT = Path(__file__).resolve().parents[1] |
| 9 | +SCRIPTS_DIR = ROOT / "scripts" |
| 10 | +if str(SCRIPTS_DIR) not in sys.path: |
| 11 | + sys.path.insert(0, str(SCRIPTS_DIR)) |
| 12 | + |
| 13 | +try: |
| 14 | + import requests # type: ignore |
| 15 | +except ModuleNotFoundError: |
| 16 | + import types |
| 17 | + |
| 18 | + class _DummyResponse: |
| 19 | + status_code = 0 |
| 20 | + |
| 21 | + def json(self): |
| 22 | + return {} |
| 23 | + |
| 24 | + requests = types.SimpleNamespace(get=lambda *args, **kwargs: _DummyResponse()) |
| 25 | + sys.modules["requests"] = requests |
| 26 | + |
| 27 | +try: |
| 28 | + import tomlkit # type: ignore |
| 29 | +except ModuleNotFoundError: |
| 30 | + import types |
| 31 | + |
| 32 | + def _noop_parse(content: str): |
| 33 | + return {} |
| 34 | + |
| 35 | + def _noop_dumps(data): |
| 36 | + return "" |
| 37 | + |
| 38 | + tomlkit = types.SimpleNamespace(parse=_noop_parse, dumps=_noop_dumps) |
| 39 | + sys.modules["tomlkit"] = tomlkit |
| 40 | + |
| 41 | +try: |
| 42 | + from ruamel.yaml import YAML # type: ignore |
| 43 | + from ruamel.yaml.scalarstring import SingleQuotedScalarString # type: ignore |
| 44 | +except ModuleNotFoundError: |
| 45 | + import types |
| 46 | + |
| 47 | + class _DummyYAML: |
| 48 | + preserve_quotes = False |
| 49 | + |
| 50 | + def load(self, stream): |
| 51 | + return {} |
| 52 | + |
| 53 | + def dump(self, data, stream=None): |
| 54 | + if stream and hasattr(stream, "write"): |
| 55 | + stream.write("") |
| 56 | + return "" |
| 57 | + |
| 58 | + class _DummyScalarString(str): |
| 59 | + pass |
| 60 | + |
| 61 | + ruamel_module = types.ModuleType("ruamel") |
| 62 | + ruamel_yaml_module = types.ModuleType("ruamel.yaml") |
| 63 | + ruamel_yaml_scalar_module = types.ModuleType("ruamel.yaml.scalarstring") |
| 64 | + ruamel_yaml_module.YAML = _DummyYAML |
| 65 | + ruamel_yaml_scalar_module.SingleQuotedScalarString = _DummyScalarString |
| 66 | + ruamel_module.yaml = ruamel_yaml_module |
| 67 | + sys.modules["ruamel"] = ruamel_module |
| 68 | + sys.modules["ruamel.yaml"] = ruamel_yaml_module |
| 69 | + sys.modules["ruamel.yaml.scalarstring"] = ruamel_yaml_scalar_module |
| 70 | + YAML = _DummyYAML |
| 71 | + SingleQuotedScalarString = _DummyScalarString |
| 72 | + |
| 73 | +import release_modules |
| 74 | + |
| 75 | + |
| 76 | +class UpdateCitationReleaseInfoTests(unittest.TestCase): |
| 77 | + def setUp(self) -> None: |
| 78 | + self.original_repo_root = release_modules.REPO_ROOT |
| 79 | + self.original_datetime = release_modules.datetime |
| 80 | + |
| 81 | + def tearDown(self) -> None: |
| 82 | + release_modules.REPO_ROOT = self.original_repo_root |
| 83 | + release_modules.datetime = self.original_datetime |
| 84 | + |
| 85 | + def _patch_repo_root_with_citation(self, content: str) -> Path: |
| 86 | + tmp_dir = tempfile.TemporaryDirectory() |
| 87 | + self.addCleanup(tmp_dir.cleanup) |
| 88 | + repo_root = Path(tmp_dir.name) |
| 89 | + (repo_root / "CITATION.cff").write_text(content, encoding="utf-8") |
| 90 | + release_modules.REPO_ROOT = repo_root |
| 91 | + return repo_root |
| 92 | + |
| 93 | + def test_updates_version_and_date_for_blox_tailwind(self) -> None: |
| 94 | + repo_root = self._patch_repo_root_with_citation( |
| 95 | + 'version: "0.10.0"\ndate-released: 2024-01-01\n', |
| 96 | + ) |
| 97 | + |
| 98 | + original_datetime = release_modules.datetime |
| 99 | + |
| 100 | + class FixedDatetime(original_datetime): |
| 101 | + @classmethod |
| 102 | + def now(cls, tz=None): |
| 103 | + return cls(2024, 2, 3, tzinfo=tz) |
| 104 | + |
| 105 | + release_modules.datetime = FixedDatetime |
| 106 | + |
| 107 | + module = release_modules.Module( |
| 108 | + name="blox-tailwind", |
| 109 | + rel_dir=Path("modules/blox-tailwind"), |
| 110 | + module_path="modules/blox-tailwind", |
| 111 | + major=0, |
| 112 | + ) |
| 113 | + |
| 114 | + updated_paths = release_modules.update_citation_release_info(module, "1.2.3") |
| 115 | + self.assertEqual(updated_paths, [repo_root / "CITATION.cff"]) |
| 116 | + |
| 117 | + contents = (repo_root / "CITATION.cff").read_text(encoding="utf-8") |
| 118 | + self.assertIn('version: "1.2.3"', contents) |
| 119 | + self.assertIn("date-released: 2024-02-03", contents) |
| 120 | + |
| 121 | + def test_noop_for_non_blox_tailwind_module(self) -> None: |
| 122 | + repo_root = self._patch_repo_root_with_citation('version: "0.10.0"\n') |
| 123 | + |
| 124 | + module = release_modules.Module( |
| 125 | + name="other-module", |
| 126 | + rel_dir=Path("modules/other-module"), |
| 127 | + module_path="modules/other-module", |
| 128 | + major=0, |
| 129 | + ) |
| 130 | + |
| 131 | + result = release_modules.update_citation_release_info(module, "9.9.9") |
| 132 | + self.assertEqual(result, []) |
| 133 | + |
| 134 | + contents = (repo_root / "CITATION.cff").read_text(encoding="utf-8") |
| 135 | + self.assertEqual(contents, 'version: "0.10.0"\n') |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + unittest.main() |
0 commit comments