|
| 1 | +"""Tests for check-release-version.py, the gate that protects every release. |
| 2 | +
|
| 3 | +Run with: uv run --python 3.12 scripts/ci/check_release_version_test.py |
| 4 | +
|
| 5 | +Standard library only, so it needs nothing but an interpreter. The GitHub API |
| 6 | +is stubbed by replacing the module's `gh` helper, which keeps the test offline |
| 7 | +and lets it assert the one behaviour that matters: a versionCode that does not |
| 8 | +move forward must fail the build, because Android will refuse the update |
| 9 | +anyway. |
| 10 | +""" |
| 11 | + |
| 12 | +import importlib.util |
| 13 | +import json |
| 14 | +import os |
| 15 | +import tempfile |
| 16 | +import unittest |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +MODULE = Path(__file__).with_name('check-release-version.py') |
| 20 | + |
| 21 | + |
| 22 | +def load_module(): |
| 23 | + spec = importlib.util.spec_from_file_location('check_release_version', MODULE) |
| 24 | + module = importlib.util.module_from_spec(spec) |
| 25 | + spec.loader.exec_module(module) |
| 26 | + return module |
| 27 | + |
| 28 | + |
| 29 | +def stub_gh(published_code): |
| 30 | + """Fake `gh api`. |
| 31 | +
|
| 32 | + `published_code=None` means a repository with no releases at all, which is |
| 33 | + what a first release actually faces. Any integer means one published |
| 34 | + release whose metadata asset reports that versionCode. |
| 35 | + """ |
| 36 | + |
| 37 | + def gh(*args): |
| 38 | + if 'releases/assets/' in args[0]: |
| 39 | + return json.dumps({ |
| 40 | + 'versionName': 'x', |
| 41 | + 'versionCode': published_code, |
| 42 | + 'sha256': 'y', |
| 43 | + 'sizeBytes': 1, |
| 44 | + }) |
| 45 | + if published_code is None: |
| 46 | + return json.dumps([[]]) |
| 47 | + return json.dumps([[{ |
| 48 | + 'assets': [{'name': 'ilink-release.json', 'size': 120, 'id': 42}], |
| 49 | + }]]) |
| 50 | + |
| 51 | + return gh |
| 52 | + |
| 53 | + |
| 54 | +class CheckReleaseVersion(unittest.TestCase): |
| 55 | + def setUp(self): |
| 56 | + self._cwd = os.getcwd() |
| 57 | + self._tmp = tempfile.TemporaryDirectory() |
| 58 | + os.chdir(self._tmp.name) |
| 59 | + os.environ['GITHUB_REPOSITORY'] = 'i99dev/ilink' |
| 60 | + self.addCleanup(self._restore) |
| 61 | + |
| 62 | + def _restore(self): |
| 63 | + os.chdir(self._cwd) |
| 64 | + self._tmp.cleanup() |
| 65 | + |
| 66 | + def run_guard(self, version, published_code=None): |
| 67 | + Path('pubspec.yaml').write_text( |
| 68 | + f'name: ilink\nversion: {version}\n', encoding='utf-8' |
| 69 | + ) |
| 70 | + module = load_module() |
| 71 | + module.gh = stub_gh(published_code) |
| 72 | + module.main() |
| 73 | + |
| 74 | + def assert_rejected(self, version, published_code=None, contains=''): |
| 75 | + with self.assertRaises(SystemExit) as caught: |
| 76 | + self.run_guard(version, published_code) |
| 77 | + if contains: |
| 78 | + self.assertIn(contains, str(caught.exception)) |
| 79 | + |
| 80 | + def test_accepts_a_code_above_the_published_one(self): |
| 81 | + self.run_guard('3.23.0-b+3023000', published_code=3022000) |
| 82 | + |
| 83 | + def test_rejects_an_unchanged_code(self): |
| 84 | + # The exact release-#2 failure: release-please bumps the versionName |
| 85 | + # but not the build metadata, so without tool/set_version_code.dart the |
| 86 | + # second release would carry the first one's versionCode. |
| 87 | + self.assert_rejected( |
| 88 | + '3.22.0-b+3022000', |
| 89 | + published_code=3022000, |
| 90 | + contains='must exceed previous release', |
| 91 | + ) |
| 92 | + |
| 93 | + def test_rejects_a_downgrade(self): |
| 94 | + self.assert_rejected('3.22.0-b+3021999', published_code=3022000) |
| 95 | + |
| 96 | + def test_requires_explicit_build_metadata(self): |
| 97 | + self.assert_rejected('3.22.0-b', contains='explicit positive integer') |
| 98 | + |
| 99 | + def test_rejects_zero(self): |
| 100 | + self.assert_rejected('3.22.0-b+0', contains='explicit positive integer') |
| 101 | + |
| 102 | + def test_accepts_a_small_code_on_a_fresh_repository(self): |
| 103 | + # iLINK has its own applicationId and inherits no continuity floor, so |
| 104 | + # a 1.0.0 first release must be allowed. |
| 105 | + self.run_guard('1.0.0+1000000') |
| 106 | + |
| 107 | + def test_rejects_a_repository_that_is_not_owner_slash_name(self): |
| 108 | + os.environ['GITHUB_REPOSITORY'] = 'i99dev/ilink; rm -rf /' |
| 109 | + self.assert_rejected('3.22.0-b+3022000', contains='Invalid repository') |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + unittest.main(verbosity=2) |
0 commit comments