CI / test (push) Successful in 38s
Track VERSION, compare it with Gitea tags via git ls-remote (API fallback), expose GET /api/update and a Services card with the current version.
119 lines
4.4 KiB
Python
119 lines
4.4 KiB
Python
"""Tests for the read-only Gitea tag update check."""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import urllib.error
|
|
from unittest import mock
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
|
|
|
import check_updates
|
|
|
|
|
|
class VersionTests(unittest.TestCase):
|
|
def test_parse_version(self):
|
|
self.assertEqual(check_updates.parse_version("v1.2.3"), (1, 2, 3))
|
|
self.assertEqual(check_updates.parse_version("1.2.3"), (1, 2, 3))
|
|
self.assertEqual(check_updates.parse_version("v1.2.3-beta"), (1, 2, 3))
|
|
self.assertIsNone(check_updates.parse_version("nightly"))
|
|
|
|
def test_latest_version(self):
|
|
name, parsed = check_updates.latest_version(
|
|
["v1.0.0", "v1.10.0", "v1.2.0", "bad"])
|
|
self.assertEqual(name, "v1.10.0")
|
|
self.assertEqual(parsed, (1, 10, 0))
|
|
|
|
def test_read_version(self):
|
|
with tempfile.NamedTemporaryFile("w", delete=False, encoding="utf-8") as handle:
|
|
handle.write("2.3.4\n")
|
|
path = handle.name
|
|
try:
|
|
self.assertEqual(check_updates.read_version(path), "2.3.4")
|
|
finally:
|
|
os.unlink(path)
|
|
self.assertEqual(check_updates.read_version("missing-file"), "0.0.0")
|
|
|
|
|
|
class FetchTests(unittest.TestCase):
|
|
def setUp(self):
|
|
check_updates.clear_cache()
|
|
self.addCleanup(check_updates.clear_cache)
|
|
|
|
def test_fetch_tags(self):
|
|
payload = [{"name": "v1.1.0"}, {"name": "v1.0.0"}]
|
|
|
|
class Response:
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *args):
|
|
pass
|
|
|
|
def read(self):
|
|
return json.dumps(payload).encode("utf-8")
|
|
|
|
with mock.patch("urllib.request.urlopen", return_value=Response()):
|
|
self.assertEqual(check_updates.fetch_tags_api("https://example/api"),
|
|
["v1.1.0", "v1.0.0"])
|
|
|
|
def test_fetch_error(self):
|
|
error = urllib.error.HTTPError("https://example", 500, "err", {}, None)
|
|
with mock.patch("urllib.request.urlopen", side_effect=error):
|
|
with self.assertRaises(RuntimeError):
|
|
check_updates.fetch_tags_api("https://example/api")
|
|
|
|
def test_fetch_tags_git(self):
|
|
stdout = (
|
|
"abc\trefs/tags/v1.0.0\n"
|
|
"def\trefs/tags/v1.2.0\n"
|
|
"def\trefs/tags/v1.2.0^{}\n"
|
|
"ghi\trefs/heads/main\n")
|
|
completed = mock.Mock(returncode=0, stdout=stdout, stderr="")
|
|
with mock.patch("subprocess.run", return_value=completed):
|
|
self.assertEqual(check_updates.fetch_tags_git("origin"),
|
|
["v1.0.0", "v1.2.0"])
|
|
|
|
def test_fetch_tags_falls_back_to_api(self):
|
|
with mock.patch.object(check_updates, "fetch_tags_git",
|
|
side_effect=RuntimeError("no git")), \
|
|
mock.patch.object(check_updates, "fetch_tags_api",
|
|
return_value=["v1.0.0"]) as api:
|
|
self.assertEqual(check_updates.fetch_tags(method="auto"), ["v1.0.0"])
|
|
self.assertEqual(api.call_count, 1)
|
|
|
|
def test_update_available(self):
|
|
with mock.patch.object(check_updates, "fetch_tags",
|
|
return_value=["v1.0.0", "v1.2.0"]):
|
|
result = check_updates.check(current="1.1.0")
|
|
self.assertTrue(result["update_available"])
|
|
self.assertEqual(result["latest"], "v1.2.0")
|
|
self.assertEqual(result["current"], "1.1.0")
|
|
self.assertTrue(result["url"].endswith("/src/tag/v1.2.0"))
|
|
|
|
def test_actual_version(self):
|
|
with mock.patch.object(check_updates, "fetch_tags",
|
|
return_value=["v1.0.0", "v1.2.0"]):
|
|
result = check_updates.check(current="1.2.0")
|
|
self.assertFalse(result["update_available"])
|
|
|
|
def test_no_tags(self):
|
|
with mock.patch.object(check_updates, "fetch_tags", return_value=[]):
|
|
result = check_updates.check(current="1.0.0")
|
|
self.assertIsNone(result["latest"])
|
|
self.assertFalse(result["update_available"])
|
|
|
|
def test_cache(self):
|
|
with mock.patch.object(check_updates, "fetch_tags",
|
|
return_value=["v1.0.0"]) as fetch:
|
|
check_updates.check(current="1.0.0")
|
|
check_updates.check(current="1.0.0")
|
|
self.assertEqual(fetch.call_count, 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|