Skip to content

Commit

Permalink
refactor(pkg_aliases): create a macro for creating whl aliases (bazel…
Browse files Browse the repository at this point in the history
…build#2391)

This just cleans up the code and moves more logic from the
repository_rule
(i.e. generation of `BUILD.bazel` files) to loading time (macro
evaluation).
This makes the unit testing easier and I plan to also move the code that
is
generating config setting names from filenames to this new macro, but
wanted to
submit this PR to reduce the review chunks.

Summary:
- Add a new `pkg_aliases` macro.
- Move logic and tests for creating WORKSPACE aliases.
- Move logic and tests bzlmod aliases.
- Move logic and tests bzlmod aliases with groups.
- Add a test for extra alias creation.
- Use `whl_alias` in `pypi` extension integration tests.
- Improve the serialization of `whl_alias` for passing to the pypi hub
repo.

Related to bazelbuild#260, bazelbuild#2386, bazelbuild#2337, bazelbuild#2319 - hopefully cleaning the code up
will make
it easier to address those feature requests later.

---------

Co-authored-by: Richard Levasseur <[email protected]>
aignas and rickeylev authored Nov 13, 2024
1 parent 0759322 commit 7b95ea6
Showing 9 changed files with 501 additions and 443 deletions.
168 changes: 84 additions & 84 deletions examples/bzlmod/MODULE.bazel.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -256,7 +256,6 @@ bzl_library(
srcs = ["render_pkg_aliases.bzl"],
deps = [
":generate_group_library_build_bazel_bzl",
":labels_bzl",
":parse_whl_name_bzl",
":whl_target_platforms_bzl",
"//python/private:normalize_name_bzl",
18 changes: 16 additions & 2 deletions python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
@@ -571,6 +571,20 @@ You cannot use both the additive_build_content and additive_build_content_file a
is_reproducible = is_reproducible,
)

def _alias_dict(a):
ret = {
"repo": a.repo,
}
if a.config_setting:
ret["config_setting"] = a.config_setting
if a.filename:
ret["filename"] = a.filename
if a.target_platforms:
ret["target_platforms"] = a.target_platforms
if a.version:
ret["version"] = a.version
return ret

def _pip_impl(module_ctx):
"""Implementation of a class tag that creates the pip hub and corresponding pip spoke whl repositories.
@@ -651,8 +665,8 @@ def _pip_impl(module_ctx):
repo_name = hub_name,
extra_hub_aliases = mods.extra_aliases.get(hub_name, {}),
whl_map = {
key: json.encode(value)
for key, value in whl_map.items()
key: json.encode([_alias_dict(a) for a in aliases])
for key, aliases in whl_map.items()
},
packages = mods.exposed_packages.get(hub_name, []),
groups = mods.hub_group_map.get(hub_name),
134 changes: 134 additions & 0 deletions python/private/pypi/pkg_aliases.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Copyright 2024 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.

"""pkg_aliases is a macro to generate aliases for selecting the right wheel for the right target platform.
This is used in bzlmod and non-bzlmod setups."""

load("//python/private:text_util.bzl", "render")
load(
":labels.bzl",
"DATA_LABEL",
"DIST_INFO_LABEL",
"PY_LIBRARY_IMPL_LABEL",
"PY_LIBRARY_PUBLIC_LABEL",
"WHEEL_FILE_IMPL_LABEL",
"WHEEL_FILE_PUBLIC_LABEL",
)

_NO_MATCH_ERROR_TEMPLATE = """\
No matching wheel for current configuration's Python version.
The current build configuration's Python version doesn't match any of the Python
wheels available for this wheel. This wheel supports the following Python
configuration settings:
{config_settings}
To determine the current configuration's Python version, run:
`bazel config <config id>` (shown further below)
and look for
{rules_python}//python/config_settings:python_version
If the value is missing, then the "default" Python version is being used,
which has a "null" version value and will not match version constraints.
"""

def _no_match_error(actual):
if type(actual) != type({}):
return None

if "//conditions:default" in actual:
return None

return _NO_MATCH_ERROR_TEMPLATE.format(
config_settings = render.indent(
"\n".join(sorted(actual.keys())),
).lstrip(),
rules_python = "rules_python",
)

def pkg_aliases(
*,
name,
actual,
group_name = None,
extra_aliases = None,
native = native,
select = select):
"""Create aliases for an actual package.
Args:
name: {type}`str` The name of the package.
actual: {type}`dict[Label, str] | str` The name of the repo the aliases point to, or a dict of select conditions to repo names for the aliases to point to
mapping to repositories.
group_name: {type}`str` The group name that the pkg belongs to.
extra_aliases: {type}`list[str]` The extra aliases to be created.
native: {type}`struct` used in unit tests.
select: {type}`select` used in unit tests.
"""
native.alias(
name = name,
actual = ":" + PY_LIBRARY_PUBLIC_LABEL,
)

target_names = {
PY_LIBRARY_PUBLIC_LABEL: PY_LIBRARY_IMPL_LABEL if group_name else PY_LIBRARY_PUBLIC_LABEL,
WHEEL_FILE_PUBLIC_LABEL: WHEEL_FILE_IMPL_LABEL if group_name else WHEEL_FILE_PUBLIC_LABEL,
DATA_LABEL: DATA_LABEL,
DIST_INFO_LABEL: DIST_INFO_LABEL,
} | {
x: x
for x in extra_aliases or []
}
no_match_error = _no_match_error(actual)

for name, target_name in target_names.items():
if type(actual) == type(""):
_actual = "@{repo}//:{target_name}".format(
repo = actual,
target_name = name,
)
elif type(actual) == type({}):
_actual = select(
{
config_setting: "@{repo}//:{target_name}".format(
repo = repo,
target_name = name,
)
for config_setting, repo in actual.items()
},
no_match_error = no_match_error,
)
else:
fail("The `actual` arg must be a dictionary or a string")

kwargs = {}
if target_name.startswith("_"):
kwargs["visibility"] = ["//_groups:__subpackages__"]

native.alias(
name = target_name,
actual = _actual,
**kwargs
)

if group_name:
native.alias(
name = PY_LIBRARY_PUBLIC_LABEL,
actual = "//_groups:{}_pkg".format(group_name),
)
native.alias(
name = WHEEL_FILE_PUBLIC_LABEL,
actual = "//_groups:{}_whl".format(group_name),
)
144 changes: 23 additions & 121 deletions python/private/pypi/render_pkg_aliases.bzl
Original file line number Diff line number Diff line change
@@ -22,15 +22,6 @@ load(
":generate_group_library_build_bazel.bzl",
"generate_group_library_build_bazel",
) # buildifier: disable=bzl-visibility
load(
":labels.bzl",
"DATA_LABEL",
"DIST_INFO_LABEL",
"PY_LIBRARY_IMPL_LABEL",
"PY_LIBRARY_PUBLIC_LABEL",
"WHEEL_FILE_IMPL_LABEL",
"WHEEL_FILE_PUBLIC_LABEL",
)
load(":parse_whl_name.bzl", "parse_whl_name")
load(":whl_target_platforms.bzl", "whl_target_platforms")

@@ -70,117 +61,32 @@ If the value is missing, then the "default" Python version is being used,
which has a "null" version value and will not match version constraints.
"""

def _render_whl_library_alias(
*,
name,
aliases,
target_name,
**kwargs):
"""Render an alias for common targets."""
if len(aliases) == 1 and not aliases[0].version:
alias = aliases[0]
return render.alias(
name = name,
actual = repr("@{repo}//:{name}".format(
repo = alias.repo,
name = target_name,
)),
**kwargs
)

# Create the alias repositories which contains different select
# statements These select statements point to the different pip
# whls that are based on a specific version of Python.
selects = {}
no_match_error = "_NO_MATCH_ERROR"
for alias in sorted(aliases, key = lambda x: x.version):
actual = "@{repo}//:{name}".format(repo = alias.repo, name = target_name)
selects.setdefault(actual, []).append(alias.config_setting)
def _repr_actual(aliases):
if len(aliases) == 1 and not aliases[0].version and not aliases[0].config_setting:
return repr(aliases[0].repo)

return render.alias(
name = name,
actual = render.select(
{
tuple(sorted(
conditions,
# Group `is_python` and other conditions for easier reading
# when looking at the generated files.
key = lambda condition: ("is_python" not in condition, condition),
)): target
for target, conditions in sorted(selects.items())
},
no_match_error = no_match_error,
# This key_repr is used to render selects.with_or keys
key_repr = lambda x: repr(x[0]) if len(x) == 1 else render.tuple(x),
name = "selects.with_or",
),
**kwargs
)
actual = {}
for alias in aliases:
actual[alias.config_setting or ("//_config:is_python_" + alias.version)] = alias.repo
return render.indent(render.dict(actual)).lstrip()

def _render_common_aliases(*, name, aliases, extra_aliases = [], group_name = None):
lines = [
"""load("@bazel_skylib//lib:selects.bzl", "selects")""",
"""package(default_visibility = ["//visibility:public"])""",
]

config_settings = None
if aliases:
config_settings = sorted([v.config_setting for v in aliases if v.config_setting])

if config_settings:
error_msg = NO_MATCH_ERROR_MESSAGE_TEMPLATE_V2.format(
config_settings = render.indent(
"\n".join(config_settings),
).lstrip(),
rules_python = "rules_python",
)
return """\
load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")
lines.append("_NO_MATCH_ERROR = \"\"\"\\\n{error_msg}\"\"\"".format(
error_msg = error_msg,
))
package(default_visibility = ["//visibility:public"])
lines.append(
render.alias(
name = name,
actual = repr(":pkg"),
),
)
lines.extend(
[
_render_whl_library_alias(
name = name,
aliases = aliases,
target_name = target_name,
visibility = ["//_groups:__subpackages__"] if name.startswith("_") else None,
)
for target_name, name in (
{
PY_LIBRARY_PUBLIC_LABEL: PY_LIBRARY_IMPL_LABEL if group_name else PY_LIBRARY_PUBLIC_LABEL,
WHEEL_FILE_PUBLIC_LABEL: WHEEL_FILE_IMPL_LABEL if group_name else WHEEL_FILE_PUBLIC_LABEL,
DATA_LABEL: DATA_LABEL,
DIST_INFO_LABEL: DIST_INFO_LABEL,
} | {
x: x
for x in extra_aliases
}
).items()
],
pkg_aliases(
name = "{name}",
actual = {actual},
group_name = {group_name},
extra_aliases = {extra_aliases},
)""".format(
name = name,
actual = _repr_actual(aliases),
group_name = repr(group_name),
extra_aliases = repr(extra_aliases),
)
if group_name:
lines.extend(
[
render.alias(
name = "pkg",
actual = repr("//_groups:{}_pkg".format(group_name)),
),
render.alias(
name = "whl",
actual = repr("//_groups:{}_whl".format(group_name)),
),
],
)

return "\n\n".join(lines)

def render_pkg_aliases(*, aliases, requirement_cycles = None, extra_hub_aliases = {}):
"""Create alias declarations for each PyPI package.
@@ -222,7 +128,7 @@ def render_pkg_aliases(*, aliases, requirement_cycles = None, extra_hub_aliases
"{}/BUILD.bazel".format(normalize_name(name)): _render_common_aliases(
name = normalize_name(name),
aliases = pkg_aliases,
extra_aliases = extra_hub_aliases.get(name, []),
extra_aliases = extra_hub_aliases.get(normalize_name(name), []),
group_name = whl_group_mapping.get(normalize_name(name)),
).strip()
for name, pkg_aliases in aliases.items()
@@ -256,21 +162,17 @@ def whl_alias(*, repo, version = None, config_setting = None, filename = None, t
if not repo:
fail("'repo' must be specified")

if version:
config_setting = config_setting or ("//_config:is_python_" + version)
config_setting = str(config_setting)

if target_platforms:
for p in target_platforms:
if not p.startswith("cp"):
fail("target_platform should start with 'cp' denoting the python version, got: " + p)

return struct(
repo = repo,
version = version,
config_setting = config_setting,
filename = filename,
repo = repo,
target_platforms = target_platforms,
version = version,
)

def render_multiplatform_pkg_aliases(*, aliases, **kwargs):
40 changes: 10 additions & 30 deletions tests/pypi/extension/extension_tests.bzl
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("@rules_testing//lib:truth.bzl", "subjects")
load("//python/private/pypi:extension.bzl", "parse_modules") # buildifier: disable=bzl-visibility
load("//python/private/pypi:render_pkg_aliases.bzl", "whl_alias") # buildifier: disable=bzl-visibility

_tests = []

@@ -158,11 +159,8 @@ def _test_simple(env):
pypi.hub_group_map().contains_exactly({"pypi": {}})
pypi.hub_whl_map().contains_exactly({"pypi": {
"simple": [
struct(
config_setting = "//_config:is_python_3.15",
filename = None,
whl_alias(
repo = "pypi_315_simple",
target_platforms = None,
version = "3.15",
),
],
@@ -209,18 +207,14 @@ def _test_simple_multiple_requirements(env):
pypi.hub_group_map().contains_exactly({"pypi": {}})
pypi.hub_whl_map().contains_exactly({"pypi": {
"simple": [
struct(
config_setting = "//_config:is_python_3.15",
filename = None,
whl_alias(
repo = "pypi_315_simple_windows_x86_64",
target_platforms = [
"cp315_windows_x86_64",
],
version = "3.15",
),
struct(
config_setting = "//_config:is_python_3.15",
filename = None,
whl_alias(
repo = "pypi_315_simple_osx_aarch64_osx_x86_64",
target_platforms = [
"cp315_osx_aarch64",
@@ -296,25 +290,18 @@ simple==0.0.3 --hash=sha256:deadbaaf
pypi.hub_group_map().contains_exactly({"pypi": {}})
pypi.hub_whl_map().contains_exactly({"pypi": {
"extra": [
struct(
config_setting = "//_config:is_python_3.15",
filename = None,
whl_alias(
repo = "pypi_315_extra",
target_platforms = None,
version = "3.15",
),
],
"simple": [
struct(
config_setting = "//_config:is_python_3.15",
filename = None,
whl_alias(
repo = "pypi_315_simple_linux_x86_64",
target_platforms = ["cp315_linux_x86_64"],
version = "3.15",
),
struct(
config_setting = "//_config:is_python_3.15",
filename = None,
whl_alias(
repo = "pypi_315_simple_osx_aarch64",
target_platforms = ["cp315_osx_aarch64"],
version = "3.15",
@@ -418,27 +405,20 @@ some_pkg==0.0.1
pypi.hub_whl_map().contains_exactly({
"pypi": {
"simple": [
struct(
config_setting = "//_config:is_python_3.15",
whl_alias(
filename = "simple-0.0.1-py3-none-any.whl",
repo = "pypi_315_simple_py3_none_any_deadb00f",
target_platforms = None,
version = "3.15",
),
struct(
config_setting = "//_config:is_python_3.15",
whl_alias(
filename = "simple-0.0.1.tar.gz",
repo = "pypi_315_simple_sdist_deadbeef",
target_platforms = None,
version = "3.15",
),
],
"some_pkg": [
struct(
config_setting = "//_config:is_python_3.15",
filename = None,
whl_alias(
repo = "pypi_315_some_pkg",
target_platforms = None,
version = "3.15",
),
],
3 changes: 3 additions & 0 deletions tests/pypi/pkg_aliases/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load(":pkg_aliases_test.bzl", "pkg_aliases_test_suite")

pkg_aliases_test_suite(name = "pkg_aliases_tests")
217 changes: 217 additions & 0 deletions tests/pypi/pkg_aliases/pkg_aliases_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# Copyright 2024 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.

"""pkg_aliases tests"""

load("@rules_testing//lib:test_suite.bzl", "test_suite")
load(
"//python/private/pypi:pkg_aliases.bzl",
"pkg_aliases",
) # buildifier: disable=bzl-visibility

_tests = []

def _test_legacy_aliases(env):
actual = []
pkg_aliases(
name = "foo",
actual = "repo",
native = struct(
alias = lambda **kwargs: actual.append(kwargs),
),
extra_aliases = ["my_special"],
)

# buildifier: disable=unsorted-dict-items
want = [
{
"name": "foo",
"actual": ":pkg",
},
{
"name": "pkg",
"actual": "@repo//:pkg",
},
{
"name": "whl",
"actual": "@repo//:whl",
},
{
"name": "data",
"actual": "@repo//:data",
},
{
"name": "dist_info",
"actual": "@repo//:dist_info",
},
{
"name": "my_special",
"actual": "@repo//:my_special",
},
]

env.expect.that_collection(actual).contains_exactly(want)

_tests.append(_test_legacy_aliases)

def _test_config_setting_aliases(env):
# Use this function as it is used in pip_repository
actual = []
actual_no_match_error = []

def mock_select(value, no_match_error = None):
actual_no_match_error.append(no_match_error)
env.expect.that_str(no_match_error).equals("""\
No matching wheel for current configuration's Python version.
The current build configuration's Python version doesn't match any of the Python
wheels available for this wheel. This wheel supports the following Python
configuration settings:
//:my_config_setting
To determine the current configuration's Python version, run:
`bazel config <config id>` (shown further below)
and look for
rules_python//python/config_settings:python_version
If the value is missing, then the "default" Python version is being used,
which has a "null" version value and will not match version constraints.
""")
return struct(value = value, no_match_error = no_match_error != None)

pkg_aliases(
name = "bar_baz",
actual = {
"//:my_config_setting": "bar_baz_repo",
},
extra_aliases = ["my_special"],
native = struct(
alias = lambda **kwargs: actual.append(kwargs),
),
select = mock_select,
)

# buildifier: disable=unsorted-dict-items
want = [
{
"name": "bar_baz",
"actual": ":pkg",
},
{
"name": "pkg",
"actual": struct(
value = {
"//:my_config_setting": "@bar_baz_repo//:pkg",
},
no_match_error = True,
),
},
{
"name": "whl",
"actual": struct(
value = {
"//:my_config_setting": "@bar_baz_repo//:whl",
},
no_match_error = True,
),
},
{
"name": "data",
"actual": struct(
value = {
"//:my_config_setting": "@bar_baz_repo//:data",
},
no_match_error = True,
),
},
{
"name": "dist_info",
"actual": struct(
value = {
"//:my_config_setting": "@bar_baz_repo//:dist_info",
},
no_match_error = True,
),
},
{
"name": "my_special",
"actual": struct(
value = {
"//:my_config_setting": "@bar_baz_repo//:my_special",
},
no_match_error = True,
),
},
]
env.expect.that_collection(actual).contains_exactly(want)

_tests.append(_test_config_setting_aliases)

def _test_group_aliases(env):
# Use this function as it is used in pip_repository
actual = []

pkg_aliases(
name = "foo",
actual = "repo",
group_name = "my_group",
native = struct(
alias = lambda **kwargs: actual.append(kwargs),
),
)

# buildifier: disable=unsorted-dict-items
want = [
{
"name": "foo",
"actual": ":pkg",
},
{
"name": "_pkg",
"actual": "@repo//:pkg",
"visibility": ["//_groups:__subpackages__"],
},
{
"name": "_whl",
"actual": "@repo//:whl",
"visibility": ["//_groups:__subpackages__"],
},
{
"name": "data",
"actual": "@repo//:data",
},
{
"name": "dist_info",
"actual": "@repo//:dist_info",
},
{
"name": "pkg",
"actual": "//_groups:my_group_pkg",
},
{
"name": "whl",
"actual": "//_groups:my_group_whl",
},
]
env.expect.that_collection(actual).contains_exactly(want)

_tests.append(_test_group_aliases)

def pkg_aliases_test_suite(name):
"""Create the test suite.
Args:
name: the name of the test suite
"""
test_suite(name = name, basic_tests = _tests)
219 changes: 14 additions & 205 deletions tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
"""render_pkg_aliases tests"""

load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
load("//python/private/pypi:config_settings.bzl", "config_settings") # buildifier: disable=bzl-visibility
load(
"//python/private/pypi:render_pkg_aliases.bzl",
@@ -27,29 +26,6 @@ load(
"whl_alias",
) # buildifier: disable=bzl-visibility

def _normalize_label_strings(want):
"""normalize expected strings.
This function ensures that the desired `render_pkg_aliases` outputs are
normalized from `bzlmod` to `WORKSPACE` values so that we don't have to
have to sets of expected strings. The main difference is that under
`bzlmod` the `str(Label("//my_label"))` results in `"@@//my_label"` whereas
under `non-bzlmod` we have `"@//my_label"`. This function does
`string.replace("@@", "@")` to normalize the strings.
NOTE, in tests, we should only use keep `@@` usage in expectation values
for the test cases where the whl_alias has the `config_setting` constructed
from a `Label` instance.
"""
if "@@" not in want:
fail("The expected string does not have '@@' labels, consider not using the function")

if BZLMOD_ENABLED:
# our expectations are already with double @
return want

return want.replace("@@", "@")

_tests = []

def _test_empty(env):
@@ -74,33 +50,15 @@ def _test_legacy_aliases(env):

want_key = "foo/BUILD.bazel"
want_content = """\
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")
package(default_visibility = ["//visibility:public"])
alias(
pkg_aliases(
name = "foo",
actual = ":pkg",
)
alias(
name = "pkg",
actual = "@pypi_foo//:pkg",
)
alias(
name = "whl",
actual = "@pypi_foo//:whl",
)
alias(
name = "data",
actual = "@pypi_foo//:data",
)
alias(
name = "dist_info",
actual = "@pypi_foo//:dist_info",
actual = "pypi_foo",
group_name = None,
extra_aliases = [],
)"""

env.expect.that_dict(actual).contains_exactly({want_key: want_content})
@@ -119,70 +77,17 @@ def _test_bzlmod_aliases(env):

want_key = "bar_baz/BUILD.bazel"
want_content = """\
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")
package(default_visibility = ["//visibility:public"])
_NO_MATCH_ERROR = \"\"\"\\
No matching wheel for current configuration's Python version.
The current build configuration's Python version doesn't match any of the Python
wheels available for this wheel. This wheel supports the following Python
configuration settings:
//:my_config_setting
To determine the current configuration's Python version, run:
`bazel config <config id>` (shown further below)
and look for
rules_python//python/config_settings:python_version
If the value is missing, then the "default" Python version is being used,
which has a "null" version value and will not match version constraints.
\"\"\"
alias(
pkg_aliases(
name = "bar_baz",
actual = ":pkg",
)
alias(
name = "pkg",
actual = selects.with_or(
{
"//:my_config_setting": "@pypi_32_bar_baz//:pkg",
},
no_match_error = _NO_MATCH_ERROR,
),
)
alias(
name = "whl",
actual = selects.with_or(
{
"//:my_config_setting": "@pypi_32_bar_baz//:whl",
},
no_match_error = _NO_MATCH_ERROR,
),
)
alias(
name = "data",
actual = selects.with_or(
{
"//:my_config_setting": "@pypi_32_bar_baz//:data",
},
no_match_error = _NO_MATCH_ERROR,
),
)
alias(
name = "dist_info",
actual = selects.with_or(
{
"//:my_config_setting": "@pypi_32_bar_baz//:dist_info",
},
no_match_error = _NO_MATCH_ERROR,
),
actual = {
"//:my_config_setting": "pypi_32_bar_baz",
},
group_name = None,
extra_aliases = [],
)"""

env.expect.that_str(actual.pop("_config/BUILD.bazel")).equals(
@@ -204,100 +109,6 @@ config_settings(

_tests.append(_test_bzlmod_aliases)

def _test_bzlmod_aliases_with_no_default_version(env):
actual = render_multiplatform_pkg_aliases(
aliases = {
"bar-baz": [
whl_alias(
version = "3.2",
repo = "pypi_32_bar_baz",
# pass the label to ensure that it gets converted to string
config_setting = Label("//python/config_settings:is_python_3.2"),
),
whl_alias(version = "3.1", repo = "pypi_31_bar_baz"),
],
},
)

want_key = "bar_baz/BUILD.bazel"
want_content = """\
load("@bazel_skylib//lib:selects.bzl", "selects")
package(default_visibility = ["//visibility:public"])
_NO_MATCH_ERROR = \"\"\"\\
No matching wheel for current configuration's Python version.
The current build configuration's Python version doesn't match any of the Python
wheels available for this wheel. This wheel supports the following Python
configuration settings:
//_config:is_python_3.1
@@//python/config_settings:is_python_3.2
To determine the current configuration's Python version, run:
`bazel config <config id>` (shown further below)
and look for
rules_python//python/config_settings:python_version
If the value is missing, then the "default" Python version is being used,
which has a "null" version value and will not match version constraints.
\"\"\"
alias(
name = "bar_baz",
actual = ":pkg",
)
alias(
name = "pkg",
actual = selects.with_or(
{
"//_config:is_python_3.1": "@pypi_31_bar_baz//:pkg",
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:pkg",
},
no_match_error = _NO_MATCH_ERROR,
),
)
alias(
name = "whl",
actual = selects.with_or(
{
"//_config:is_python_3.1": "@pypi_31_bar_baz//:whl",
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:whl",
},
no_match_error = _NO_MATCH_ERROR,
),
)
alias(
name = "data",
actual = selects.with_or(
{
"//_config:is_python_3.1": "@pypi_31_bar_baz//:data",
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:data",
},
no_match_error = _NO_MATCH_ERROR,
),
)
alias(
name = "dist_info",
actual = selects.with_or(
{
"//_config:is_python_3.1": "@pypi_31_bar_baz//:dist_info",
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:dist_info",
},
no_match_error = _NO_MATCH_ERROR,
),
)"""

actual.pop("_config/BUILD.bazel")
env.expect.that_collection(actual.keys()).contains_exactly([want_key])
env.expect.that_str(actual[want_key]).equals(_normalize_label_strings(want_content))

_tests.append(_test_bzlmod_aliases_with_no_default_version)

def _test_aliases_are_created_for_all_wheels(env):
actual = render_pkg_aliases(
aliases = {
@@ -357,10 +168,8 @@ def _test_aliases_with_groups(env):

want_key = "bar/BUILD.bazel"

# Just check that it contains a private whl
env.expect.that_str(actual[want_key]).contains("name = \"_whl\"")
env.expect.that_str(actual[want_key]).contains("name = \"whl\"")
env.expect.that_str(actual[want_key]).contains("\"//_groups:group_whl\"")
# Just check that we pass the group name
env.expect.that_str(actual[want_key]).contains("group_name = \"group\"")

_tests.append(_test_aliases_with_groups)

0 comments on commit 7b95ea6

Please sign in to comment.