|
| 1 | +# Copyright 2025 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 | +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") |
| 16 | +load("@rules_testing//lib:test_suite.bzl", "test_suite") |
| 17 | +load("@rules_testing//lib:truth.bzl", "subjects") |
| 18 | +load("@rules_testing//lib:util.bzl", rt_util = "util") |
| 19 | +load("//python/private:attr_builders.bzl", "attrb") # buildifier: disable=bzl-visibility |
| 20 | +load("//python/private:rule_builders.bzl", "ruleb") # buildifier: disable=bzl-visibility |
| 21 | + |
| 22 | +BananaInfo = provider() |
| 23 | + |
| 24 | +def _banana_impl(ctx): |
| 25 | + return [BananaInfo( |
| 26 | + color = ctx.attr.color, |
| 27 | + flavors = ctx.attr.flavors, |
| 28 | + organic = ctx.attr.organic, |
| 29 | + size = ctx.attr.size, |
| 30 | + origin = ctx.attr.origin, |
| 31 | + fertilizers = ctx.attr.fertilizers, |
| 32 | + xx = mybool, |
| 33 | + )] |
| 34 | + |
| 35 | +banana = ruleb.Rule( |
| 36 | + implementation = _banana_impl, |
| 37 | + attrs = { |
| 38 | + "color": attrb.String(default = "yellow"), |
| 39 | + "flavors": attrb.StringList(), |
| 40 | + "organic": lambda: attrb.Bool(), |
| 41 | + "size": lambda: attrb.Int(default = 10), |
| 42 | + "origin": lambda: attrb.Label(), |
| 43 | + "fertilizers": attrb.LabelList( |
| 44 | + allow_files = True, |
| 45 | + ), |
| 46 | + }, |
| 47 | +).build() |
| 48 | + |
| 49 | +_tests = [] |
| 50 | + |
| 51 | +mybool = attrb.Bool() |
| 52 | + |
| 53 | +def _test_basic_rule(name): |
| 54 | + banana( |
| 55 | + name = name + "_subject", |
| 56 | + flavors = ["spicy", "sweet"], |
| 57 | + organic = True, |
| 58 | + size = 5, |
| 59 | + origin = "//python:none", |
| 60 | + fertilizers = [ |
| 61 | + "nitrogen.txt", |
| 62 | + "phosphorus.txt", |
| 63 | + ], |
| 64 | + ) |
| 65 | + |
| 66 | + analysis_test( |
| 67 | + name = name, |
| 68 | + target = name + "_subject", |
| 69 | + impl = _test_basic_rule_impl, |
| 70 | + ) |
| 71 | + |
| 72 | +def _test_basic_rule_impl(env, target): |
| 73 | + info = target[BananaInfo] |
| 74 | + env.expect.that_str(info.color).equals("yellow") |
| 75 | + env.expect.that_collection(info.flavors).contains_exactly(["spicy", "sweet"]) |
| 76 | + env.expect.that_bool(info.organic).equals(True) |
| 77 | + env.expect.that_int(info.size).equals(5) |
| 78 | + |
| 79 | + # //python:none is an alias to //python/private:sentinel; we see the |
| 80 | + # resolved value, not the intermediate alias |
| 81 | + env.expect.that_target(info.origin).label().equals(Label("//python/private:sentinel")) |
| 82 | + |
| 83 | + env.expect.that_collection(info.fertilizers).transform( |
| 84 | + desc = "target.label", |
| 85 | + map_each = lambda t: t.label, |
| 86 | + ).contains_exactly([ |
| 87 | + Label(":nitrogen.txt"), |
| 88 | + Label(":phosphorus.txt"), |
| 89 | + ]) |
| 90 | + |
| 91 | +_tests.append(_test_basic_rule) |
| 92 | + |
| 93 | +def rule_builders_test_suite(name): |
| 94 | + test_suite( |
| 95 | + name = name, |
| 96 | + tests = _tests, |
| 97 | + ) |
0 commit comments