|
| 1 | +load("@build_stack_rules_proto//rules:providers.bzl", "ProtoRepositoryInfo") |
| 2 | + |
| 3 | +def _proto_repository_info_test_impl(ctx): |
| 4 | + info = ctx.attr.info[ProtoRepositoryInfo] |
| 5 | + |
| 6 | + must_attr(info, ctx.attr, "source_host") |
| 7 | + must_attr(info, ctx.attr, "source_owner") |
| 8 | + must_attr(info, ctx.attr, "source_repo") |
| 9 | + must_attr(info, ctx.attr, "source_commit") |
| 10 | + must_attr(info, ctx.attr, "source_prefix") |
| 11 | + |
| 12 | + ctx.actions.write(ctx.outputs.json, info.to_json()) |
| 13 | + |
| 14 | + # we're checking attr values in the provider, so the script really does not |
| 15 | + # need to do anything |
| 16 | + ctx.actions.write(ctx.outputs.executable, "echo PASS") |
| 17 | + |
| 18 | + return [DefaultInfo( |
| 19 | + files = depset([ctx.outputs.json, ctx.outputs.executable]), |
| 20 | + )] |
| 21 | + |
| 22 | +proto_repository_info_test = rule( |
| 23 | + implementation = _proto_repository_info_test_impl, |
| 24 | + attrs = { |
| 25 | + "info": attr.label( |
| 26 | + providers = [ProtoRepositoryInfo], |
| 27 | + mandatory = True, |
| 28 | + ), |
| 29 | + "want_source_host": attr.string(), |
| 30 | + "want_source_owner": attr.string(), |
| 31 | + "want_source_repo": attr.string(), |
| 32 | + "want_source_commit": attr.string(), |
| 33 | + "want_source_prefix": attr.string(), |
| 34 | + }, |
| 35 | + outputs = { |
| 36 | + "json": "%{name}.json", |
| 37 | + }, |
| 38 | + test = True, |
| 39 | +) |
| 40 | + |
| 41 | +def must_attr(info, attr, name): |
| 42 | + got = getattr(info, name) |
| 43 | + want = getattr(attr, "want_" + name) |
| 44 | + if got != want: |
| 45 | + fail(".%s: want %s, got %s" % (name, want, got)) |
0 commit comments