xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/pypi/whl_installer/arguments_test.py (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import json
16import unittest
17
18from python.private.pypi.whl_installer import arguments, wheel
19
20
21class ArgumentsTestCase(unittest.TestCase):
22    def test_arguments(self) -> None:
23        parser = arguments.parser()
24        index_url = "--index_url=pypi.org/simple"
25        extra_pip_args = [index_url]
26        requirement = "foo==1.0.0 --hash=sha256:deadbeef"
27        args_dict = vars(
28            parser.parse_args(
29                args=[
30                    f'--requirement="{requirement}"',
31                    f"--extra_pip_args={json.dumps({'arg': extra_pip_args})}",
32                ]
33            )
34        )
35        args_dict = arguments.deserialize_structured_args(args_dict)
36        self.assertIn("requirement", args_dict)
37        self.assertIn("extra_pip_args", args_dict)
38        self.assertEqual(args_dict["pip_data_exclude"], [])
39        self.assertEqual(args_dict["enable_implicit_namespace_pkgs"], False)
40        self.assertEqual(args_dict["extra_pip_args"], extra_pip_args)
41
42    def test_deserialize_structured_args(self) -> None:
43        serialized_args = {
44            "pip_data_exclude": json.dumps({"arg": ["**.foo"]}),
45            "environment": json.dumps({"arg": {"PIP_DO_SOMETHING": "True"}}),
46        }
47        args = arguments.deserialize_structured_args(serialized_args)
48        self.assertEqual(args["pip_data_exclude"], ["**.foo"])
49        self.assertEqual(args["environment"], {"PIP_DO_SOMETHING": "True"})
50        self.assertEqual(args["extra_pip_args"], [])
51
52    def test_platform_aggregation(self) -> None:
53        parser = arguments.parser()
54        args = parser.parse_args(
55            args=[
56                "--platform=linux_*",
57                "--platform=osx_*",
58                "--platform=windows_*",
59                "--requirement=foo",
60            ]
61        )
62        self.assertEqual(set(wheel.Platform.all()), arguments.get_platforms(args))
63
64
65if __name__ == "__main__":
66    unittest.main()
67