aboutsummaryrefslogtreecommitdiff
path: root/python/pip_install/tools/lib/arguments_test.py
blob: dfa96a890e0a779ed957e1b106c6ebe3967372cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import json
import unittest

from python.pip_install.tools.lib import arguments


class ArgumentsTestCase(unittest.TestCase):
    def test_arguments(self) -> None:
        parser = argparse.ArgumentParser()
        parser = arguments.parse_common_args(parser)
        repo_name = "foo"
        repo_prefix = "pypi_"
        index_url = "--index_url=pypi.org/simple"
        extra_pip_args = [index_url]
        args_dict = vars(
            parser.parse_args(
                args=[
                    "--repo",
                    repo_name,
                    f"--extra_pip_args={json.dumps({'arg': extra_pip_args})}",
                    "--repo-prefix",
                    repo_prefix,
                ]
            )
        )
        args_dict = arguments.deserialize_structured_args(args_dict)
        self.assertIn("repo", args_dict)
        self.assertIn("extra_pip_args", args_dict)
        self.assertEqual(args_dict["pip_data_exclude"], [])
        self.assertEqual(args_dict["enable_implicit_namespace_pkgs"], False)
        self.assertEqual(args_dict["repo"], repo_name)
        self.assertEqual(args_dict["repo_prefix"], repo_prefix)
        self.assertEqual(args_dict["extra_pip_args"], extra_pip_args)

    def test_deserialize_structured_args(self) -> None:
        serialized_args = {
            "pip_data_exclude": json.dumps({"arg": ["**.foo"]}),
            "environment": json.dumps({"arg": {"PIP_DO_SOMETHING": "True"}}),
        }
        args = arguments.deserialize_structured_args(serialized_args)
        self.assertEqual(args["pip_data_exclude"], ["**.foo"])
        self.assertEqual(args["environment"], {"PIP_DO_SOMETHING": "True"})
        self.assertEqual(args["extra_pip_args"], [])


if __name__ == "__main__":
    unittest.main()