aboutsummaryrefslogtreecommitdiff
path: root/python/pip_install/tools/wheel_installer/wheel_installer_test.py
blob: 6eacd1fda5e592bbab6750f71861faaa745d6b39 (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, 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
                )


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(
            Path(self.wheel_path),
            installation_dir=Path(self.wheel_dir),
            extras={},
            enable_implicit_namespace_pkgs=False,
            platforms=[],
        )

        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=[],
            deps_by_platform={},
            entry_points=[],
        )
        self.assertEqual(want, metadata_file_content)


class TestWheelPlatform(unittest.TestCase):
    def test_wheel_os_alias(self):
        self.assertEqual("OS.osx", str(wheel.OS.osx))
        self.assertEqual(str(wheel.OS.darwin), str(wheel.OS.osx))

    def test_wheel_arch_alias(self):
        self.assertEqual("Arch.x86_64", str(wheel.Arch.x86_64))
        self.assertEqual(str(wheel.Arch.amd64), str(wheel.Arch.x86_64))

    def test_wheel_platform_alias(self):
        give = wheel.Platform(
            os=wheel.OS.darwin,
            arch=wheel.Arch.amd64,
        )
        alias = wheel.Platform(
            os=wheel.OS.osx,
            arch=wheel.Arch.x86_64,
        )

        self.assertEqual("osx_x86_64", str(give))
        self.assertEqual(str(alias), str(give))


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