xref: /aosp_15_r20/external/pigweed/pw_env_setup/py/cipd_setup_update_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Tests for cipd_setup.update logic."""
15
16import importlib.resources
17import operator
18from pathlib import Path
19import unittest
20
21from parameterized import parameterized  # type: ignore
22
23from pw_env_setup.cipd_setup.update import (
24    all_package_files,
25    deduplicate_packages,
26)
27
28
29class TestCipdSetupUpdate(unittest.TestCase):
30    """Tests for cipd_setup.update logic."""
31
32    maxDiff = None
33
34    @parameterized.expand(
35        [
36            (
37                'overriden Python',
38                [
39                    {
40                        'path': 'fuchsia/third_party/armgcc/${platform}',
41                        'tags': ['version:[email protected]'],
42                        'subdir': 'arm',
43                    },
44                    {
45                        'path': 'infra/3pp/tools/cpython3/${platform}',
46                        'tags': ['version:[email protected]'],
47                        'subdir': 'arm/python',
48                        'original_subdir': 'python',
49                    },
50                    # Python 3.11.3
51                    {
52                        'path': 'infra/3pp/tools/cpython3/${platform}',
53                        'tags': ['version:[email protected]'],
54                        'subdir': 'python',
55                    },
56                    # Duplicate Python, different version 3.11.4
57                    # This should take precedence.
58                    {
59                        'path': 'infra/3pp/tools/cpython3/${platform}',
60                        'tags': ['version:[email protected]'],
61                        'subdir': 'python',
62                    },
63                ],
64                [
65                    {
66                        'path': 'fuchsia/third_party/armgcc/${platform}',
67                        'tags': ['version:[email protected]'],
68                        'subdir': 'arm',
69                    },
70                    {
71                        'path': 'infra/3pp/tools/cpython3/${platform}',
72                        'tags': ['version:[email protected]'],
73                        'subdir': 'arm/python',
74                        'original_subdir': 'python',
75                    },
76                    {
77                        'path': 'infra/3pp/tools/cpython3/${platform}',
78                        'tags': ['version:[email protected]'],
79                        'subdir': 'python',
80                    },
81                ],
82            ),
83            (
84                'duplicate package in a different subdir',
85                [
86                    {
87                        'path': 'fuchsia/third_party/armgcc/${platform}',
88                        'tags': ['version:[email protected]'],
89                        'subdir': 'arm',
90                    },
91                    {
92                        'path': 'fuchsia/third_party/armgcc/${platform}',
93                        'tags': ['version:[email protected]'],
94                        'subdir': 'another_arm',
95                    },
96                ],
97                [
98                    {
99                        'path': 'fuchsia/third_party/armgcc/${platform}',
100                        'tags': ['version:[email protected]'],
101                        'subdir': 'another_arm',
102                    },
103                ],
104            ),
105            (
106                'duplicate package in the same subdir',
107                [
108                    {
109                        'path': 'fuchsia/third_party/armgcc/${platform}',
110                        'tags': ['version:[email protected]'],
111                        'subdir': 'arm',
112                    },
113                    {
114                        'path': 'fuchsia/third_party/armgcc/${platform}',
115                        'tags': ['version:[email protected]'],
116                        'subdir': 'arm',
117                    },
118                ],
119                [
120                    # The second older version takes precedence
121                    {
122                        'path': 'fuchsia/third_party/armgcc/${platform}',
123                        'tags': ['version:[email protected]'],
124                        'subdir': 'arm',
125                    },
126                ],
127            ),
128        ]
129    )
130    def test_deduplicate_packages(
131        self,
132        _name,
133        packages,
134        expected_packages,
135    ) -> None:
136        """Test package deduplication logic."""
137        pkgs = sorted(
138            deduplicate_packages(packages),
139            key=operator.itemgetter('path'),
140        )
141        expected_pkgs = sorted(
142            expected_packages,
143            key=operator.itemgetter('path'),
144        )
145        self.assertSequenceEqual(expected_pkgs, pkgs)
146
147    def test_all_package_files(self) -> None:
148        """Test that CIPD files are loaded in the correct order."""
149
150        upstream_load_order = [
151            Path('upstream.json'),
152            Path('bazelisk.json'),
153            Path('buildifier.json'),
154            Path('openjdk.json'),
155            Path('cmake.json'),
156            Path('coverage.json'),
157            Path('default.json'),
158            Path('arm.json'),
159            Path('pigweed.json'),
160            Path('clang.json'),
161            Path('python.json'),
162            Path('python311.json'),
163            Path('doxygen.json'),
164            Path('go.json'),
165            Path('host_tools.json'),
166            Path('kythe.json'),
167            Path('luci.json'),
168            Path('msrv_python.json'),
169            Path('python310.json'),
170            Path('rbe.json'),
171            Path('ruff.json'),
172            Path('testing.json'),
173            Path('web.json'),
174        ]
175
176        with importlib.resources.path(
177            'pw_env_setup.cipd_setup', 'upstream.json'
178        ) as upstream_json:
179            all_files = all_package_files(None, [upstream_json])
180            all_files_relative = [
181                Path(f).relative_to(upstream_json.parent) for f in all_files
182            ]
183            self.assertEqual(upstream_load_order, all_files_relative)
184
185
186if __name__ == '__main__':
187    unittest.main()
188