aboutsummaryrefslogtreecommitdiff
path: root/python/pip_install/tools/wheel_installer/wheel_installer_test.py
blob: b24e50053f6aa27f5476b372a5fc9ad4267f6c3f (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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 json
import os
import shutil
import tempfile
import unittest
from pathlib import Path

from python.pip_install.tools.wheel_installer import wheel_installer


class TestRequirementExtrasParsing(unittest.TestCase):
    def test_parses_requirement_for_extra(self) -> None:
        cases = [
            ("name[foo]", ("name", frozenset(["foo"]))),
            ("name[ Foo123 ]", ("name", frozenset(["Foo123"]))),
            (" name1[ foo ] ", ("name1", frozenset(["foo"]))),
            ("Name[foo]", ("name", frozenset(["foo"]))),
            ("name_foo[bar]", ("name-foo", frozenset(["bar"]))),
            (
                "name [fred,bar] @ http://foo.com ; python_version=='2.7'",
                ("name", frozenset(["fred", "bar"])),
            ),
            (
                "name[quux, strange];python_version<'2.7' and platform_version=='2'",
                ("name", frozenset(["quux", "strange"])),
            ),
            (
                "name; (os_name=='a' or os_name=='b') and os_name=='c'",
                (None, None),
            ),
            (
                "name@http://foo.com",
                (None, None),
            ),
        ]

        for case, expected in cases:
            with self.subTest():
                self.assertTupleEqual(
                    wheel_installer._parse_requirement_for_extra(case), expected
                )


# TODO @aignas 2023-07-21: migrate to starlark
# class BazelTestCase(unittest.TestCase):
#     def test_generate_entry_point_contents(self):
#         got = wheel_installer._generate_entry_point_contents("sphinx.cmd.build", "main")
#         want = """#!/usr/bin/env python3
# import sys
# from sphinx.cmd.build import main
# if __name__ == "__main__":
#     sys.exit(main())
# """
#         self.assertEqual(got, want)
#
#     def test_generate_entry_point_contents_with_shebang(self):
#         got = wheel_installer._generate_entry_point_contents(
#             "sphinx.cmd.build", "main", shebang="#!/usr/bin/python"
#         )
#         want = """#!/usr/bin/python
# import sys
# from sphinx.cmd.build import main
# if __name__ == "__main__":
#     sys.exit(main())
# """
#         self.assertEqual(got, want)


class TestWhlFilegroup(unittest.TestCase):
    def setUp(self) -> None:
        self.wheel_name = "example_minimal_package-0.0.1-py3-none-any.whl"
        self.wheel_dir = tempfile.mkdtemp()
        self.wheel_path = os.path.join(self.wheel_dir, self.wheel_name)
        shutil.copy(os.path.join("examples", "wheel", self.wheel_name), self.wheel_dir)

    def tearDown(self):
        shutil.rmtree(self.wheel_dir)

    def test_wheel_exists(self) -> None:
        wheel_installer._extract_wheel(
            self.wheel_path,
            installation_dir=Path(self.wheel_dir),
            extras={},
            enable_implicit_namespace_pkgs=False,
        )

        want_files = [
            "metadata.json",
            "site-packages",
            self.wheel_name,
        ]
        self.assertEqual(
            sorted(want_files),
            sorted(
                [
                    str(p.relative_to(self.wheel_dir))
                    for p in Path(self.wheel_dir).glob("*")
                ]
            ),
        )
        with open("{}/metadata.json".format(self.wheel_dir)) as metadata_file:
            metadata_file_content = json.load(metadata_file)

        want = dict(
            version="0.0.1",
            name="example-minimal-package",
            deps=[],
            entry_points=[],
        )
        self.assertEqual(want, metadata_file_content)


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