• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:2@12.2.mpacbti-rel1.1'],
42                        'subdir': 'arm',
43                    },
44                    {
45                        'path': 'infra/3pp/tools/cpython3/${platform}',
46                        'tags': ['version:2@3.8.10.chromium.24'],
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:2@3.11.3.chromium.29'],
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:2@3.11.4.chromium.29'],
61                        'subdir': 'python',
62                    },
63                ],
64                [
65                    {
66                        'path': 'fuchsia/third_party/armgcc/${platform}',
67                        'tags': ['version:2@12.2.mpacbti-rel1.1'],
68                        'subdir': 'arm',
69                    },
70                    {
71                        'path': 'infra/3pp/tools/cpython3/${platform}',
72                        'tags': ['version:2@3.8.10.chromium.24'],
73                        'subdir': 'arm/python',
74                        'original_subdir': 'python',
75                    },
76                    {
77                        'path': 'infra/3pp/tools/cpython3/${platform}',
78                        'tags': ['version:2@3.11.4.chromium.29'],
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:2@12.2.mpacbti-rel1.1'],
89                        'subdir': 'arm',
90                    },
91                    {
92                        'path': 'fuchsia/third_party/armgcc/${platform}',
93                        'tags': ['version:2@10.3-2021.10.1'],
94                        'subdir': 'another_arm',
95                    },
96                ],
97                [
98                    {
99                        'path': 'fuchsia/third_party/armgcc/${platform}',
100                        'tags': ['version:2@10.3-2021.10.1'],
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:2@12.2.mpacbti-rel1.1'],
111                        'subdir': 'arm',
112                    },
113                    {
114                        'path': 'fuchsia/third_party/armgcc/${platform}',
115                        'tags': ['version:2@10.3-2021.10.1'],
116                        'subdir': 'arm',
117                    },
118                ],
119                [
120                    # The second older version takes precedence
121                    {
122                        'path': 'fuchsia/third_party/armgcc/${platform}',
123                        'tags': ['version:2@10.3-2021.10.1'],
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('flatc.json'),
165            Path('go.json'),
166            Path('host_tools.json'),
167            Path('kythe.json'),
168            Path('luci.json'),
169            Path('msrv_python.json'),
170            Path('python310.json'),
171            Path('rbe.json'),
172            Path('ruff.json'),
173            Path('testing.json'),
174            Path('web.json'),
175        ]
176
177        with importlib.resources.path(
178            'pw_env_setup.cipd_setup', 'upstream.json'
179        ) as upstream_json:
180            all_files = all_package_files(None, [upstream_json])
181            all_files_relative = [
182                Path(f).relative_to(upstream_json.parent) for f in all_files
183            ]
184            self.assertEqual(upstream_load_order, all_files_relative)
185
186
187if __name__ == '__main__':
188    unittest.main()
189