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 os 17import shutil 18import tempfile 19import unittest 20from pathlib import Path 21 22from python.pip_install.tools.wheel_installer import wheel_installer 23 24 25class TestRequirementExtrasParsing(unittest.TestCase): 26 def test_parses_requirement_for_extra(self) -> None: 27 cases = [ 28 ("name[foo]", ("name", frozenset(["foo"]))), 29 ("name[ Foo123 ]", ("name", frozenset(["Foo123"]))), 30 (" name1[ foo ] ", ("name1", frozenset(["foo"]))), 31 ("Name[foo]", ("name", frozenset(["foo"]))), 32 ("name_foo[bar]", ("name-foo", frozenset(["bar"]))), 33 ( 34 "name [fred,bar] @ http://foo.com ; python_version=='2.7'", 35 ("name", frozenset(["fred", "bar"])), 36 ), 37 ( 38 "name[quux, strange];python_version<'2.7' and platform_version=='2'", 39 ("name", frozenset(["quux", "strange"])), 40 ), 41 ( 42 "name; (os_name=='a' or os_name=='b') and os_name=='c'", 43 (None, None), 44 ), 45 ( 46 "name@http://foo.com", 47 (None, None), 48 ), 49 ] 50 51 for case, expected in cases: 52 with self.subTest(): 53 self.assertTupleEqual( 54 wheel_installer._parse_requirement_for_extra(case), expected 55 ) 56 57 58# TODO @aignas 2023-07-21: migrate to starlark 59# class BazelTestCase(unittest.TestCase): 60# def test_generate_entry_point_contents(self): 61# got = wheel_installer._generate_entry_point_contents("sphinx.cmd.build", "main") 62# want = """#!/usr/bin/env python3 63# import sys 64# from sphinx.cmd.build import main 65# if __name__ == "__main__": 66# sys.exit(main()) 67# """ 68# self.assertEqual(got, want) 69# 70# def test_generate_entry_point_contents_with_shebang(self): 71# got = wheel_installer._generate_entry_point_contents( 72# "sphinx.cmd.build", "main", shebang="#!/usr/bin/python" 73# ) 74# want = """#!/usr/bin/python 75# import sys 76# from sphinx.cmd.build import main 77# if __name__ == "__main__": 78# sys.exit(main()) 79# """ 80# self.assertEqual(got, want) 81 82 83class TestWhlFilegroup(unittest.TestCase): 84 def setUp(self) -> None: 85 self.wheel_name = "example_minimal_package-0.0.1-py3-none-any.whl" 86 self.wheel_dir = tempfile.mkdtemp() 87 self.wheel_path = os.path.join(self.wheel_dir, self.wheel_name) 88 shutil.copy(os.path.join("examples", "wheel", self.wheel_name), self.wheel_dir) 89 90 def tearDown(self): 91 shutil.rmtree(self.wheel_dir) 92 93 def test_wheel_exists(self) -> None: 94 wheel_installer._extract_wheel( 95 self.wheel_path, 96 installation_dir=Path(self.wheel_dir), 97 extras={}, 98 enable_implicit_namespace_pkgs=False, 99 ) 100 101 want_files = [ 102 "metadata.json", 103 "site-packages", 104 self.wheel_name, 105 ] 106 self.assertEqual( 107 sorted(want_files), 108 sorted( 109 [ 110 str(p.relative_to(self.wheel_dir)) 111 for p in Path(self.wheel_dir).glob("*") 112 ] 113 ), 114 ) 115 with open("{}/metadata.json".format(self.wheel_dir)) as metadata_file: 116 metadata_file_content = json.load(metadata_file) 117 118 want = dict( 119 version="0.0.1", 120 name="example-minimal-package", 121 deps=[], 122 entry_points=[], 123 ) 124 self.assertEqual(want, metadata_file_content) 125 126 127if __name__ == "__main__": 128 unittest.main() 129