Skip to content

Commit

Permalink
add test for incompatible flag
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Nov 23, 2023
1 parent f69d263 commit 66cc0df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion proto/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proto_common = native_proto_common
ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo

def _incompatible_toolchains_enabled():
getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)
return getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)

def _find_toolchain(ctx, legacy_attr, toolchain_type):
if _incompatible_toolchains_enabled():
Expand Down
14 changes: 14 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("//proto:defs.bzl", "proto_library")
load("//proto:proto_common.bzl", "proto_common")

config_setting(
name = "incompatible_enable_proto_toolchain_resolution",
flag_values = {
":incompatible_enable_proto_toolchain_resolution_flag": "true",
},
)

bool_flag(
name = "incompatible_enable_proto_toolchain_resolution_flag",
build_setting_default = getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False),
)

proto_library(
name = "empty_proto",
Expand Down
36 changes: 32 additions & 4 deletions tests/proto_common/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
load("@bazel_skylib//lib:partial.bzl", "partial")
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//proto:proto_common.bzl", "toolchains")

def _test_toolchains(ctx):
def _test_toolchains_without_incompatible_flag(ctx):
env = unittest.begin(ctx)

asserts.equals(env, {}, toolchains.if_legacy_toolchain({}))
asserts.equals(env, None, toolchains.if_legacy_toolchain(None))
asserts.equals(env, False, toolchains.if_legacy_toolchain(False))
asserts.equals(env, False, toolchains.if_legacy_toolchain(False))

return unittest.end(env)

toolchains_test = unittest.make(_test_toolchains)
toolchains_without_incompatible_flags_test = unittest.make(_test_toolchains_without_incompatible_flag)

def _test_toolchains_with_incompatible_flag(ctx):
env = unittest.begin(ctx)

asserts.equals(env, {}, toolchains.if_legacy_toolchain({}))
asserts.equals(env, {}, toolchains.if_legacy_toolchain(None))
asserts.equals(env, {}, toolchains.if_legacy_toolchain(False))
toolchain = toolchains.use_toolchain("//nonexistent:toolchain_type")
asserts.equals(env, 1, len(toolchain))
asserts.equals(env, False, toolchain[0].mandatory)
asserts.equals(env, str(Label("//nonexistent:toolchain_type")), str(toolchain[0].toolchain_type))

return unittest.end(env)

toolchains_with_incompatible_flag_test = unittest.make(_test_toolchains_with_incompatible_flag)

# buildifier: disable=unnamed-macro
def unittest_toolchains():
unittest.suite(
"test_toolchains",
toolchains_test
partial.make(
toolchains_without_incompatible_flags_test,
target_compatible_with = select({
"//tests:incompatible_enable_proto_toolchain_resolution": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
),
partial.make(
toolchains_with_incompatible_flag_test,
target_compatible_with = select({
"//tests:incompatible_enable_proto_toolchain_resolution": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)
)

0 comments on commit 66cc0df

Please sign in to comment.