1#
2# Copyright (C) 2024 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import unittest
17
18import updater_utils
19
20
21class GetLatestVersionTest(unittest.TestCase):
22    """Tests for updater_utils.get_latest_stable_release_tag.
23
24    We don't care which branch a tag belongs to because we look for the latest
25    tag in a list of all references of a remote repository.
26    """
27    def test_float_sort(self) -> None:
28        """ Tests if updater_utils.get_latest_stable_release_tag return the latest tag.
29
30        This is the most common case where tags are in lexicographical order.
31        """
32        self.assertEqual(
33            updater_utils.get_latest_stable_release_tag("v1.0.0", ["v1.0.0", "v2.0.0"]), "v2.0.0")
34        self.assertEqual(
35            updater_utils.get_latest_stable_release_tag("1.10", ["1.10", "1.2"]), "1.10")
36
37    def test_mixed_tag(self) -> None:
38        self.assertEqual(
39            updater_utils.get_latest_stable_release_tag("1.0", ["1.0", "foobar"]), "1.0")
40        self.assertEqual(
41            updater_utils.get_latest_stable_release_tag("1.0", ["1.0", "v1.1"]), "1.0")
42        self.assertEqual(
43            updater_utils.get_latest_stable_release_tag("v1.0", ["v1.0", "1.1"]), "v1.0")
44        self.assertEqual(
45            updater_utils.get_latest_stable_release_tag("1.0", ["1.0", "v1.0"]), "1.0")
46        self.assertEqual(
47            updater_utils.get_latest_stable_release_tag("v3.11.4", ["v3.11.4", "v3.12.2", "v3.13.0a4"]), "v3.12.2")
48
49    def test_non_release_prefix(self) -> None:
50        self.assertEqual(
51            updater_utils.get_latest_stable_release_tag("v32.1.3", ["v32.1.3", "v33.0.0", "failureaccess-v1.0.2"]), "v33.0.0")
52        self.assertEqual(
53            updater_utils.get_latest_stable_release_tag("1.0", ["1.0", "test-1.1 "]), "1.0")
54
55    def test_reject_rc_tags(self) -> None:
56        self.assertEqual(
57            updater_utils.get_latest_stable_release_tag("v3.27.0", ["v3.27.0", "v3.28.0-rc1"]), "v3.27.0")
58
59    def test_ndk_scheme(self) -> None:
60        self.assertEqual(
61            updater_utils.get_latest_stable_release_tag("r26", ["r26", "r27"]), "r27")
62        self.assertEqual(
63            updater_utils.get_latest_stable_release_tag("r26", ["r26", "r26-beta1"]), "r26")
64        self.assertEqual(
65            updater_utils.get_latest_stable_release_tag("r26", ["r26", "r27-beta1"]), "r26")
66
67    @unittest.expectedFailure
68    def test_ndk_scheme_fail(self) -> None:
69        # The actual latest tag is r26b but since r26b doesn't match the pattern
70        # of current tag, get_latest_stable_release_tag returns r26. Although
71        # get_latest_stable_release_tag doesn't return the answer we are looking
72        # for, we're going to keep this test case anyway.
73        self.assertEqual(
74            updater_utils.get_latest_stable_release_tag("r26", ["r26", "r26b"]), "r26b")
75
76    def test_no_tags(self) -> None:
77        """Tests that an error is raised when there are no tags."""
78        with self.assertRaises(ValueError):
79            updater_utils.get_latest_stable_release_tag("v1.0.0", [])
80
81
82if __name__ == "__main__":
83    unittest.main(verbosity=2)
84