1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import unittest
19import os
20from update_versions_for_release import *
21from shutil import rmtree
22
23# Import functions from the parent directory
24sys.path.append("..")
25from update_tracing_perfetto import single
26from update_tracing_perfetto import sed
27
28class TestVersionUpdates(unittest.TestCase):
29
30    def test_increment_version(self):
31        new_version = increment_version("1.0.0-alpha01")
32        self.assertEqual("1.0.0-alpha02", new_version)
33
34        new_version = increment_version("1.1.0-alpha01")
35        self.assertEqual("1.1.0-alpha02", new_version)
36
37        new_version = increment_version("1.0.0-alpha19")
38        self.assertEqual("1.0.0-alpha20", new_version)
39
40        new_version = increment_version("1.0.0-rc01")
41        self.assertEqual("1.1.0-alpha01", new_version)
42
43        new_version = increment_version("1.3.0-beta02")
44        self.assertEqual("1.3.0-beta03", new_version)
45
46        new_version = increment_version("1.0.1")
47        self.assertEqual("1.1.0-alpha01", new_version)
48
49    def test_increment_version_within_minor_version(self):
50        new_version = increment_version_within_minor_version("1.0.0-alpha01")
51        self.assertEqual("1.0.0-alpha02", new_version)
52
53        new_version = increment_version_within_minor_version("1.1.0-alpha01")
54        self.assertEqual("1.1.0-alpha02", new_version)
55
56        new_version = increment_version_within_minor_version("1.0.0-alpha19")
57        self.assertEqual("1.0.0-alpha20", new_version)
58
59        new_version = increment_version_within_minor_version("1.0.0-rc01")
60        self.assertEqual("1.0.0-rc02", new_version)
61
62        new_version = increment_version_within_minor_version("1.3.0-beta02")
63        self.assertEqual("1.3.0-beta03", new_version)
64
65        new_version = increment_version_within_minor_version("1.0.1")
66        self.assertEqual("1.0.2", new_version)
67
68    def test_get_higher_version(self):
69        higher_version = get_higher_version("1.0.0-alpha01", "1.0.0-alpha02")
70        self.assertEqual("1.0.0-alpha02", higher_version)
71
72        higher_version = get_higher_version("1.0.0-alpha02", "1.0.0-alpha01")
73        self.assertEqual("1.0.0-alpha02", higher_version)
74
75        higher_version = get_higher_version("1.0.0-alpha02", "1.0.0-alpha02")
76        self.assertEqual("1.0.0-alpha02", higher_version)
77
78        higher_version = get_higher_version("1.1.0-alpha01", "1.0.0-alpha02")
79        self.assertEqual("1.1.0-alpha01", higher_version)
80
81        higher_version = get_higher_version("1.0.0-rc05", "1.2.0-beta02")
82        self.assertEqual("1.2.0-beta02", higher_version)
83
84        higher_version = get_higher_version("1.3.0-beta01", "1.5.0-beta01")
85        self.assertEqual("1.5.0-beta01", higher_version)
86
87        higher_version = get_higher_version("3.0.0-alpha01", "1.0.0-alpha02")
88        self.assertEqual("3.0.0-alpha01", higher_version)
89
90        higher_version = get_higher_version("1.0.0-beta01", "1.0.0-rc01")
91        self.assertEqual("1.0.0-rc01", higher_version)
92
93        higher_version = get_higher_version("1.4.0-beta01", "1.0.2")
94        self.assertEqual("1.4.0-beta01", higher_version)
95
96        higher_version = get_higher_version("1.4.0-beta01", "1.4.2")
97        self.assertEqual("1.4.2", higher_version)
98
99        higher_version = get_higher_version("1.4.0", "1.4.2")
100        self.assertEqual("1.4.2", higher_version)
101
102    def test_should_update_group_version_in_library_versions_toml(self):
103        self.assertTrue(should_update_group_version_in_library_versions_toml(
104            "1.1.0-alpha01", "1.1.0-alpha02", "androidx.core"))
105        self.assertTrue(should_update_group_version_in_library_versions_toml(
106            "1.1.0-alpha01", "1.3.0-alpha01", "androidx.appcompat"))
107        self.assertFalse(should_update_group_version_in_library_versions_toml(
108            "1.1.0-alpha01", "1.0.0-alpha01", "androidx.work"))
109
110        self.assertTrue(should_update_group_version_in_library_versions_toml(
111            "1.0.0-beta04", "1.1.0-alpha02", "androidx.wear"))
112        self.assertTrue(should_update_group_version_in_library_versions_toml(
113            "1.0.0-beta04", "1.3.0-alpha01", "androidx.viewpager"))
114        self.assertFalse(should_update_group_version_in_library_versions_toml(
115            "1.0.0-beta04", "1.0.0-alpha01", "androidx.compose.foundation"))
116
117        self.assertTrue(should_update_group_version_in_library_versions_toml(
118            "1.0.0-beta04", "1.1.0-alpha02", "androidx.tracing"))
119        self.assertTrue(should_update_group_version_in_library_versions_toml(
120            "1.0.0-beta04", "1.3.0-alpha01", "androidx.tracing"))
121        self.assertTrue(should_update_group_version_in_library_versions_toml(
122            "1.2.0", "1.3.0-alpha01", "androidx.tracing"))
123
124        self.assertFalse(should_update_group_version_in_library_versions_toml(
125            "1.0.0-beta04", "1.1.0-alpha02", "androidx.car"))
126        self.assertFalse(should_update_group_version_in_library_versions_toml(
127            "1.0.0-beta04", "1.3.0-alpha01", "androidx.car"))
128        self.assertFalse(should_update_group_version_in_library_versions_toml(
129            "1.0.0-beta04", "1.0.0-alpha01", "androidx.car"))
130
131        self.assertFalse(should_update_group_version_in_library_versions_toml(
132            "1.0.0-beta04", "1.1.0-alpha02", "androidx.compose.compiler"))
133        self.assertFalse(should_update_group_version_in_library_versions_toml(
134            "1.0.0-beta04", "1.3.0-alpha01", "androidx.compose.compiler"))
135        self.assertFalse(should_update_group_version_in_library_versions_toml(
136            "1.2.0", "1.3.0-alpha01", "androidx.compose.compiler"))
137
138    def test_should_update_artifact_version_in_library_versions_toml(self):
139        self.assertTrue(should_update_artifact_version_in_library_versions_toml(
140            "1.1.0-alpha01", "1.1.0-alpha02", "core"))
141        self.assertTrue(should_update_artifact_version_in_library_versions_toml(
142            "1.1.0-alpha01", "1.3.0-alpha01", "appcompat"))
143        self.assertFalse(should_update_artifact_version_in_library_versions_toml(
144            "1.1.0-alpha01", "1.0.0-alpha01", "work-runtime"))
145
146        self.assertTrue(should_update_artifact_version_in_library_versions_toml(
147            "1.0.0-beta04", "1.1.0-alpha02", "tracing-perfetto"))
148        self.assertTrue(should_update_artifact_version_in_library_versions_toml(
149            "1.0.0-beta04", "1.3.0-alpha01", "tracing-perfetto"))
150        self.assertFalse(should_update_artifact_version_in_library_versions_toml(
151            "1.0.0-beta04", "1.0.0-alpha01", "tracing-perfetto"))
152        self.assertTrue(should_update_artifact_version_in_library_versions_toml(
153            "1.1.0-alpha02", "1.1.0-alpha03", "tracing-perfetto"))
154        self.assertTrue(should_update_artifact_version_in_library_versions_toml(
155            "1.1.0-alpha02", "1.1.0-alpha03", "tracing-perfetto-binary"))
156        self.assertTrue(should_update_artifact_version_in_library_versions_toml(
157            "1.1.0-alpha02", "1.1.0-alpha03", "tracing-perfetto-common"))
158
159    def test_get_library_constants_in_library_versions_toml(self):
160        self.assertEqual(
161            get_library_constants_in_library_versions_toml("androidx.foo", "foo"),
162            ("FOO", "FOO"))
163        self.assertEqual(
164            get_library_constants_in_library_versions_toml("androidx.foo.bar", "bar-qux"),
165            ("FOO_BAR", "BAR_QUX"))
166        self.assertEqual(
167            get_library_constants_in_library_versions_toml("androidx.foo", "foo-ktx"),
168            ("FOO", "FOO_KTX"))
169        self.assertEqual(
170            get_library_constants_in_library_versions_toml("androidx.compose.runtime", "runtime"),
171            ("COMPOSE", "RUNTIME"))
172        self.assertEqual(
173            get_library_constants_in_library_versions_toml("androidx.compose.runtime", "runtime-tracing"),
174            ("COMPOSE_RUNTIME_TRACING", "COMPOSE_RUNTIME_TRACING"))
175        self.assertEqual(
176            get_library_constants_in_library_versions_toml("androidx.compose.material3", "material3"),
177            ("COMPOSE_MATERIAL3", "MATERIAL3"))
178
179
180class TestFileParsing(unittest.TestCase):
181
182    def test_get_compose_to_runtime_version_map(self):
183        compose_to_runtime_version_map = {}
184        get_compose_to_runtime_version_map(compose_to_runtime_version_map)
185        self.assertTrue("1.0.0" in compose_to_runtime_version_map.keys())
186        self.assertTrue("1.0.5" in compose_to_runtime_version_map.keys())
187        self.assertTrue("1.1.0-alpha05" in compose_to_runtime_version_map.keys())
188        self.assertEqual(3300, compose_to_runtime_version_map["1.0.0"]["runtime_version"])
189        self.assertEqual(3305, compose_to_runtime_version_map["1.0.5"]["runtime_version"])
190        self.assertEqual(4400, compose_to_runtime_version_map["1.1.0-alpha05"]["runtime_version"])
191
192
193class TestReplacements(unittest.TestCase):
194    def test_sed(self):
195        # given
196        out_dir = "./out"
197        test_file = out_dir + "/temp.txt"
198        if not os.path.exists(out_dir):
199            os.makedirs(out_dir)
200        with open(test_file, "w") as f:
201            f.write("ababaa\nfxfx\nbcbcbb")
202
203        # when
204        sed("[ac]", "d", test_file)
205
206        # then
207        with open(test_file) as f:
208            file_contents = f.read()
209        self.assertEqual("dbdbdd\nfxfx\nbdbdbb", file_contents)
210
211        # clean-up
212        if os.path.isdir(out_dir):
213            rmtree(out_dir)
214        elif os.path.exists(out_dir):
215            os.remove(out_dir)
216
217
218class TestLists(unittest.TestCase):
219    def test_single_no_items(self):
220        items = []
221        with self.assertRaisesRegex(ValueError, '^Expected a list of size 1. Found: \\[]$'):
222            single(items)
223
224    def test_single_one_item(self):
225        items = ['a']
226        item = single(items)
227        self.assertEqual(item, items[0])
228
229    def test_single_multiple_items(self):
230        items = ['a', 'b']
231        with self.assertRaisesRegex(ValueError, '^Expected a list of size 1.'
232                                                ' Found: \\[\'a\', \'b\']$'):
233            single(items)
234
235
236if __name__ == '__main__':
237    unittest.main()
238