• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 argparse
16import json
17import unittest
18
19from python.pip_install.tools.wheel_installer import arguments
20
21
22class ArgumentsTestCase(unittest.TestCase):
23    def test_arguments(self) -> None:
24        parser = arguments.parser()
25        repo_name = "foo"
26        repo_prefix = "pypi_"
27        index_url = "--index_url=pypi.org/simple"
28        extra_pip_args = [index_url]
29        requirement = "foo==1.0.0 --hash=sha256:deadbeef"
30        args_dict = vars(
31            parser.parse_args(
32                args=[
33                    f'--requirement="{requirement}"',
34                    f"--extra_pip_args={json.dumps({'arg': extra_pip_args})}",
35                ]
36            )
37        )
38        args_dict = arguments.deserialize_structured_args(args_dict)
39        self.assertIn("requirement", args_dict)
40        self.assertIn("extra_pip_args", args_dict)
41        self.assertEqual(args_dict["pip_data_exclude"], [])
42        self.assertEqual(args_dict["enable_implicit_namespace_pkgs"], False)
43        self.assertEqual(args_dict["extra_pip_args"], extra_pip_args)
44
45    def test_deserialize_structured_args(self) -> None:
46        serialized_args = {
47            "pip_data_exclude": json.dumps({"arg": ["**.foo"]}),
48            "environment": json.dumps({"arg": {"PIP_DO_SOMETHING": "True"}}),
49        }
50        args = arguments.deserialize_structured_args(serialized_args)
51        self.assertEqual(args["pip_data_exclude"], ["**.foo"])
52        self.assertEqual(args["environment"], {"PIP_DO_SOMETHING": "True"})
53        self.assertEqual(args["extra_pip_args"], [])
54
55
56if __name__ == "__main__":
57    unittest.main()
58