aboutsummaryrefslogtreecommitdiff
path: root/tests/pip_hub_repository/render_pkg_aliases/render_pkg_aliases_test.bzl
blob: dff7cd0fbc859c76a5df55030b37455b5f8481e8 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# 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.

"""render_pkg_aliases tests"""

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

_tests = []

def _test_legacy_aliases(env):
    actual = render_pkg_aliases(
        bzl_packages = ["foo"],
        repo_name = "pypi",
    )

    want = {
        "foo/BUILD.bazel": """\
package(default_visibility = ["//visibility:public"])

alias(
    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",
)""",
    }

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

_tests.append(_test_legacy_aliases)

def _test_all_legacy_aliases_are_created(env):
    actual = render_pkg_aliases(
        bzl_packages = ["foo", "bar"],
        repo_name = "pypi",
    )

    want_files = ["bar/BUILD.bazel", "foo/BUILD.bazel"]

    env.expect.that_dict(actual).keys().contains_exactly(want_files)

_tests.append(_test_all_legacy_aliases_are_created)

def _test_bzlmod_aliases(env):
    actual = render_pkg_aliases(
        default_version = "3.2.3",
        repo_name = "pypi",
        rules_python = "rules_python",
        whl_map = {
            "bar-baz": ["3.2.3"],
        },
    )

    want = {
        "bar_baz/BUILD.bazel": """\
package(default_visibility = ["//visibility:public"])

alias(
    name = "bar_baz",
    actual = ":pkg",
)

alias(
    name = "pkg",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:pkg",
            "//conditions:default": "@pypi_32_bar_baz//:pkg",
        },
    ),
)

alias(
    name = "whl",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:whl",
            "//conditions:default": "@pypi_32_bar_baz//:whl",
        },
    ),
)

alias(
    name = "data",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:data",
            "//conditions:default": "@pypi_32_bar_baz//:data",
        },
    ),
)

alias(
    name = "dist_info",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:dist_info",
            "//conditions:default": "@pypi_32_bar_baz//:dist_info",
        },
    ),
)""",
    }

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

_tests.append(_test_bzlmod_aliases)

def _test_bzlmod_aliases_with_no_default_version(env):
    actual = render_pkg_aliases(
        default_version = None,
        repo_name = "pypi",
        rules_python = "rules_python",
        whl_map = {
            "bar-baz": ["3.2.3", "3.1.3"],
        },
    )

    want_key = "bar_baz/BUILD.bazel"
    want_content = """\
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
versions available for this wheel. This wheel supports the following Python versions:
    3.1.3, 3.2.3

As matched by the `@rules_python//python/config_settings:is_python_<version>`
configuration 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.
\"\"\"

alias(
    name = "bar_baz",
    actual = ":pkg",
)

alias(
    name = "pkg",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:pkg",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:pkg",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)

alias(
    name = "whl",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:whl",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:whl",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)

alias(
    name = "data",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:data",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:data",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)

alias(
    name = "dist_info",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:dist_info",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:dist_info",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)"""

    env.expect.that_collection(actual.keys()).contains_exactly([want_key])
    env.expect.that_str(actual[want_key]).equals(want_content)

_tests.append(_test_bzlmod_aliases_with_no_default_version)

def _test_bzlmod_aliases_for_non_root_modules(env):
    actual = render_pkg_aliases(
        default_version = "3.2.4",
        repo_name = "pypi",
        rules_python = "rules_python",
        whl_map = {
            "bar-baz": ["3.2.3", "3.1.3"],
        },
    )

    want_key = "bar_baz/BUILD.bazel"
    want_content = """\
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
versions available for this wheel. This wheel supports the following Python versions:
    3.1.3, 3.2.3

As matched by the `@rules_python//python/config_settings:is_python_<version>`
configuration 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.
\"\"\"

alias(
    name = "bar_baz",
    actual = ":pkg",
)

alias(
    name = "pkg",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:pkg",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:pkg",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)

alias(
    name = "whl",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:whl",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:whl",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)

alias(
    name = "data",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:data",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:data",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)

alias(
    name = "dist_info",
    actual = select(
        {
            "@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:dist_info",
            "@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:dist_info",
        },
        no_match_error = _NO_MATCH_ERROR,
    ),
)"""

    env.expect.that_collection(actual.keys()).contains_exactly([want_key])
    env.expect.that_str(actual[want_key]).equals(want_content)

_tests.append(_test_bzlmod_aliases_for_non_root_modules)

def _test_bzlmod_aliases_are_created_for_all_wheels(env):
    actual = render_pkg_aliases(
        default_version = "3.2.3",
        repo_name = "pypi",
        rules_python = "rules_python",
        whl_map = {
            "bar": ["3.1.2", "3.2.3"],
            "foo": ["3.1.2", "3.2.3"],
        },
    )

    want_files = [
        "bar/BUILD.bazel",
        "foo/BUILD.bazel",
    ]

    env.expect.that_dict(actual).keys().contains_exactly(want_files)

_tests.append(_test_bzlmod_aliases_are_created_for_all_wheels)

def render_pkg_aliases_test_suite(name):
    """Create the test suite.

    Args:
        name: the name of the test suite
    """
    test_suite(name = name, basic_tests = _tests)