Skip to content

Commit 878331c

Browse files
committed
Add unit test
1 parent 6e498ce commit 878331c

File tree

11 files changed

+101
-7
lines changed

11 files changed

+101
-7
lines changed

gazelle/modules_mapping/def.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def _modules_mapping_impl(ctx):
3232
)
3333
args.add("--output_file", modules_mapping.path)
3434
args.add_all("--exclude_patterns", ctx.attr.exclude_patterns)
35+
args.add_all("--include_stub_packages", ctx.attr.include_stub_packages)
3536
args.add_all("--wheels", [whl.path for whl in all_wheels.to_list()])
3637
ctx.actions.run(
3738
inputs = all_wheels.to_list(),
@@ -55,6 +56,11 @@ modules_mapping = rule(
5556
doc = "The name for the output JSON file.",
5657
mandatory = False,
5758
),
59+
"include_stub_packages": attr.bool(
60+
default = False,
61+
doc = "Whether to include stub packages in the mapping.",
62+
mandatory = False,
63+
),
5864
"wheels": attr.label_list(
5965
allow_files = True,
6066
doc = "The list of wheels, usually the 'all_whl_requirements' from @<pip_repository>//:requirements.bzl",

gazelle/modules_mapping/generator.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ class Generator:
2525
stderr = None
2626
output_file = None
2727
excluded_patterns = None
28-
mapping = {}
2928

30-
def __init__(self, stderr, output_file, excluded_patterns):
29+
def __init__(self, stderr, output_file, excluded_patterns, include_stub_packages):
3130
self.stderr = stderr
3231
self.output_file = output_file
3332
self.excluded_patterns = [re.compile(pattern) for pattern in excluded_patterns]
33+
self.include_stub_packages = include_stub_packages
34+
self.mapping = {}
3435

3536
# dig_wheel analyses the wheel .whl file determining the modules it provides
3637
# by looking at the directory structure.
3738
def dig_wheel(self, whl):
3839
# Skip stubs and types wheels.
3940
wheel_name = get_wheel_name(whl)
40-
if wheel_name.endswith(("_stubs", "_types")):
41+
if wheel_name.endswith(("_stubs", "_types")) and self.include_stub_packages:
4142
self.mapping[wheel_name.lower()] = wheel_name.lower()
4243
return
4344
with zipfile.ZipFile(whl, "r") as zip_file:
@@ -150,8 +151,11 @@ def data_has_purelib_or_platlib(path):
150151
description="Generates the modules mapping used by the Gazelle manifest.",
151152
)
152153
parser.add_argument("--output_file", type=str)
154+
parser.add_argument("--include_stub_packages", type=bool)
153155
parser.add_argument("--exclude_patterns", nargs="+", default=[])
154156
parser.add_argument("--wheels", nargs="+", default=[])
155157
args = parser.parse_args()
156-
generator = Generator(sys.stderr, args.output_file, args.exclude_patterns)
158+
generator = Generator(
159+
sys.stderr, args.output_file, args.exclude_patterns, args.include_stub_packages
160+
)
157161
exit(generator.run(args.wheels))

gazelle/modules_mapping/test_generator.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_generator(self):
99
whl = pathlib.Path(
1010
pathlib.Path(__file__).parent, "testdata", "pytest-7.1.1-py3-none-any.whl"
1111
)
12-
gen = Generator(None, None, {})
12+
gen = Generator(None, None, {}, False)
1313
gen.dig_wheel(whl)
1414
self.assertLessEqual(
1515
{
@@ -27,7 +27,7 @@ def test_stub_generator(self):
2727
"testdata",
2828
"django_types-0.15.0-py3-none-any.whl",
2929
)
30-
gen = Generator(None, None, {})
30+
gen = Generator(None, None, {}, True)
3131
gen.dig_wheel(whl)
3232
self.assertLessEqual(
3333
{
@@ -36,6 +36,19 @@ def test_stub_generator(self):
3636
gen.mapping.items(),
3737
)
3838

39+
def test_stub_excluded(self):
40+
whl = pathlib.Path(
41+
pathlib.Path(__file__).parent
42+
/ "testdata"
43+
/ "django_types-0.15.0-py3-none-any.whl"
44+
)
45+
gen = Generator(None, None, {}, False)
46+
gen.dig_wheel(whl)
47+
self.assertEqual(
48+
{}.items(),
49+
gen.mapping.items(),
50+
)
51+
3952

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

gazelle/python/resolve.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ func (py *Resolver) Resolve(
195195
typeModule := fmt.Sprintf("%s_types", strings.ToLower(distributionName))
196196
if dep, _, ok := cfg.FindThirdPartyDependency(typeModule); ok {
197197
deps.Add(dep)
198-
199198
}
200199
stubModule := fmt.Sprintf("%s_stubs", strings.ToLower(distributionName))
201200
if dep, _, ok := cfg.FindThirdPartyDependency(stubModule); ok {

gazelle/python/testdata/add_type_stub_packages/BUILD.in

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
3+
py_binary(
4+
name = "add_type_stub_packages_bin",
5+
srcs = ["__main__.py"],
6+
main = "__main__.py",
7+
visibility = ["//:__subpackages__"],
8+
deps = [
9+
"@gazelle_python_test//boto3",
10+
"@gazelle_python_test//boto3_stubs",
11+
"@gazelle_python_test//django",
12+
"@gazelle_python_test//django_types",
13+
],
14+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Add stubs to `deps` of `py_library` target
2+
3+
This test case asserts that
4+
* if a package has the corresponding stub available, it is added to the `deps` of the `py_library` target.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
15+
import boto3
16+
import django
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
15+
manifest:
16+
modules_mapping:
17+
boto3: boto3
18+
boto3_stubs: boto3_stubs
19+
django_types: django_types
20+
django: Django
21+
22+
pip_deps_repository_name: gazelle_python_test

0 commit comments

Comments
 (0)