diff --git a/tests/c/defs.bzl b/tests/c/defs.bzl index 76288280..1a38f550 100644 --- a/tests/c/defs.bzl +++ b/tests/c/defs.bzl @@ -1,3 +1,4 @@ +load("@prelude//utils:utils.bzl", "value_or") load("//tools:conformance.bzl", "wasi_test") _DEFAULT_FIXTURE_DIRS = { @@ -12,9 +13,7 @@ def _config_for(name, conf): return path if glob([path]) else None def _fixture_dirs_for(config, dirs): - if dirs != None: - return dirs - return _DEFAULT_FIXTURE_DIRS if config else {} + return value_or(dirs, _DEFAULT_FIXTURE_DIRS if config else {}) def _c_artifact(name): native.cxx_binary( diff --git a/tests/rust/wasm32-wasip1/defs.bzl b/tests/rust/wasm32-wasip1/defs.bzl index 49f15844..aba213de 100644 --- a/tests/rust/wasm32-wasip1/defs.bzl +++ b/tests/rust/wasm32-wasip1/defs.bzl @@ -1,3 +1,4 @@ +load("@prelude//utils:utils.bzl", "value_or") load("//platforms:defs.bzl", "transition_alias") load("//tools:conformance.bzl", "wasi_test") @@ -13,9 +14,7 @@ def _config_for(name, conf): return path if glob([path]) else None def _fixture_dirs_for(config, dirs): - if dirs != None: - return dirs - return _DEFAULT_FIXTURE_DIRS if config else {} + return value_or(dirs, _DEFAULT_FIXTURE_DIRS if config else {}) def _rust_artifact(name, deps): native.rust_binary( diff --git a/tests/rust/wasm32-wasip3/defs.bzl b/tests/rust/wasm32-wasip3/defs.bzl index 693f65a7..43c782f3 100644 --- a/tests/rust/wasm32-wasip3/defs.bzl +++ b/tests/rust/wasm32-wasip3/defs.bzl @@ -1,3 +1,4 @@ +load("@prelude//utils:utils.bzl", "value_or") load("@wasmono//:defs.bzl", "wasm_component") load("//tools:conformance.bzl", "wasi_test") @@ -13,9 +14,10 @@ def _config_for(name, conf): return path if glob([path]) else None def _fixture_dirs_for(config, dirs): - if dirs != None: - return dirs - return _DEFAULT_FIXTURE_DIRS if config and config.startswith("src/bin/filesystem-") else {} + return value_or( + dirs, + _DEFAULT_FIXTURE_DIRS if config and config.startswith("src/bin/filesystem-") else {}, + ) def _rust_p3_artifact(name, deps, wit_srcs): module_name = "{}_module".format(name) diff --git a/toolchains/runtimes/runtimes.bzl b/toolchains/runtimes/runtimes.bzl index e7ab030a..b499a4e2 100644 --- a/toolchains/runtimes/runtimes.bzl +++ b/toolchains/runtimes/runtimes.bzl @@ -1,5 +1,6 @@ """Download rules for WASI runtime binaries used by the test suite.""" +load("@prelude//utils:expect.bzl", "expect") load("@wasmono//toolchains/wasm:node.bzl", "NodeInfo") load("@wasmono//:defs.bzl", "host_arch", "host_os") load(":releases.bzl", "WAMR_RELEASES", "WASMEDGE_RELEASES", "WASMTIME_RELEASES", "WAZERO_RELEASES") @@ -16,24 +17,26 @@ def _runtime_platform() -> str: def _runtime_release(releases, runtime_name: str, version: str, target_compatible_with = []): platform = _runtime_platform() - if version not in releases: - fail("Unknown {} version '{}'. Available: {}".format( - runtime_name, - version, - ", ".join(releases.keys()), - )) + expect( + version in releases, + "Unknown {} version '{}'. Available: {}", + runtime_name, + version, + ", ".join(releases.keys()), + ) release = releases[version] if platform not in release and target_compatible_with: for fallback in release.values(): return fallback - if platform not in release: - fail("No {} release for platform '{}'. Available: {}".format( - runtime_name, - platform, - ", ".join(release.keys()), - )) + expect( + platform in release, + "No {} release for platform '{}'. Available: {}", + runtime_name, + platform, + ", ".join(release.keys()), + ) return release[platform] def _runtime_distribution_impl(ctx: AnalysisContext) -> list[Provider]: diff --git a/toolchains/rust/defs.bzl b/toolchains/rust/defs.bzl index af3aae08..81115602 100644 --- a/toolchains/rust/defs.bzl +++ b/toolchains/rust/defs.bzl @@ -3,6 +3,7 @@ Adapted from https://github.com/rue-language/rue/blob/trunk/toolchains/rust/defs """ load("@prelude//rust:rust_toolchain.bzl", "PanicRuntime", "RustToolchainInfo") +load("@prelude//utils:expect.bzl", "expect") load("@wasmono//:defs.bzl", "host_arch", "host_os") load( ":releases.bzl", @@ -26,17 +27,21 @@ _HOST_TRIPLES = { def host_rust_triple() -> str: """Return the Rust host triple for the machine running Buck.""" key = "{}-{}".format(host_arch(), host_os()) - if key not in _HOST_TRIPLES: - fail("Unsupported host platform for the hermetic Rust toolchain: '{}'".format(key)) + expect( + key in _HOST_TRIPLES, + "Unsupported host platform for the hermetic Rust toolchain: '{}'", + key, + ) return _HOST_TRIPLES[key] def download_rust_host(name: str, triple: str): """Download the combined ``rust`` package for ``triple``.""" - if triple not in RUST_HOST_RELEASES: - fail("No pinned Rust host release for '{}'. Available: {}".format( - triple, - ", ".join(sorted(RUST_HOST_RELEASES.keys())), - )) + expect( + triple in RUST_HOST_RELEASES, + "No pinned Rust host release for '{}'. Available: {}", + triple, + ", ".join(sorted(RUST_HOST_RELEASES.keys())), + ) native.http_archive( name = name, urls = [rust_host_url(triple)], @@ -47,11 +52,12 @@ def download_rust_host(name: str, triple: str): def download_rust_std(name: str, target: str): """Download the ``rust-std`` package for a wasm ``target``.""" - if target not in RUST_STD_RELEASES: - fail("No pinned Rust std release for '{}'. Available: {}".format( - target, - ", ".join(sorted(RUST_STD_RELEASES.keys())), - )) + expect( + target in RUST_STD_RELEASES, + "No pinned Rust std release for '{}'. Available: {}", + target, + ", ".join(sorted(RUST_STD_RELEASES.keys())), + ) native.http_archive( name = name, urls = [rust_std_url(target)], diff --git a/tools/conformance.bzl b/tools/conformance.bzl index 2a74feec..5c6b1560 100644 --- a/tools/conformance.bzl +++ b/tools/conformance.bzl @@ -13,6 +13,9 @@ The build target that produces a `.wasm` file should stay language-specific load("@prelude//decls:common.bzl", "buck") load("@prelude//test:inject_test_run_info.bzl", "inject_test_run_info") +load("@prelude//utils:expect.bzl", "expect") +load("@prelude//utils:type_defs.bzl", "type_utils") +load("@prelude//utils:utils.bzl", "value_or") load(":runtime.bzl", "WasiRuntimeInfo") WasiTestInfo = provider( @@ -55,23 +58,32 @@ def wasi_manifest( ) def _infer_test_name(wasm): - if type(wasm) != type(""): - fail("wasi_test: test_name is required when wasm is not a label string") - if ":" not in wasm: - fail("wasi_test: cannot infer test_name from '{}'; pass test_name explicitly".format(wasm)) + expect( + type_utils.is_string(wasm), + "wasi_test: test_name is required when wasm is not a label string", + ) + expect( + ":" in wasm, + "wasi_test: cannot infer test_name from '{}'; pass test_name explicitly", + wasm, + ) return wasm.rsplit(":", 1)[1] def _single_output(dep, attr_name): outputs = dep[DefaultInfo].default_outputs - if len(outputs) != 1: - fail("{} must provide exactly one output, got {}".format(attr_name, len(outputs))) + expect( + len(outputs) == 1, + "{} must provide exactly one output, got {}", + attr_name, + len(outputs), + ) return outputs[0] def _wasi_test_impl(ctx: AnalysisContext) -> list[Provider]: wasm = _single_output(ctx.attrs.wasm, "wasm") runtime_info = ctx.attrs.runtime[WasiRuntimeInfo] runtime = runtime_info.runtime - expectations = ctx.attrs.expectations or runtime_info.expectations + expectations = value_or(ctx.attrs.expectations, runtime_info.expectations) cmd = cmd_args(ctx.attrs._runner[RunInfo]) cmd.add("--wasm", wasm) @@ -203,12 +215,15 @@ def _wasi_test_suite_impl(ctx: AnalysisContext) -> list[Provider]: other_outputs.extend(default_info.other_outputs) # Collect test metadata, flattening nested wasi_suite targets. + expect( + WasiTestInfo in test or WasiTestSuiteInfo in test, + "{} does not provide WasiTestInfo or WasiTestSuiteInfo", + test.label, + ) if WasiTestInfo in test: tests.append(test[WasiTestInfo]) - elif WasiTestSuiteInfo in test: - tests.extend(test[WasiTestSuiteInfo].tests) else: - fail("{} does not provide WasiTestInfo or WasiTestSuiteInfo".format(test.label)) + tests.extend(test[WasiTestSuiteInfo].tests) return [ DefaultInfo( diff --git a/tools/dist.bzl b/tools/dist.bzl index ebb7d446..4b960dd7 100644 --- a/tools/dist.bzl +++ b/tools/dist.bzl @@ -1,11 +1,18 @@ """Rules for packaging WASI tests into a distribution archive.""" +load("@prelude//utils:dicts.bzl", "update_x") +load("@prelude//utils:expect.bzl", "expect") +load("@prelude//utils:utils.bzl", "map_idx") load("//tools:conformance.bzl", "WasiTestSuiteInfo") def _single_output(dep, attr_name): outputs = dep[DefaultInfo].default_outputs - if len(outputs) != 1: - fail("{} must provide exactly one output, got {}".format(attr_name, len(outputs))) + expect( + len(outputs) == 1, + "{} must provide exactly one output, got {}", + attr_name, + len(outputs), + ) return outputs[0] def _add_item(items, path, src): @@ -23,8 +30,7 @@ def _wasi_dist_impl(ctx: AnalysisContext) -> list[Provider]: for suite in ctx.attrs.suites: for test in suite[WasiTestSuiteInfo].tests: - if test.dist_dir == None: - fail("{} does not set dist_dir in wasi_test".format(test.test_name)) + expect(test.dist_dir != None, "{} does not set dist_dir in wasi_test", test.test_name) manifest_path = "{}/manifest.json".format(test.dist_dir) manifest = { @@ -32,10 +38,12 @@ def _wasi_dist_impl(ctx: AnalysisContext) -> list[Provider]: "version": test.wasi_version, } - if manifest_path in manifests and manifests[manifest_path] != manifest: - fail("conflicting manifest metadata for {}".format(manifest_path)) - - manifests[manifest_path] = manifest + update_x( + manifests, + manifest_path, + manifest, + fmt = "conflicting manifest metadata for {}: {} != {}", + ) _add_item(item_map, "{}/{}.wasm".format(test.dist_dir, test.test_name), test.wasm) _add_item(item_map, "{}/{}.json".format(test.dist_dir, test.test_name), test.config) @@ -73,7 +81,7 @@ def _wasi_dist_impl(ctx: AnalysisContext) -> list[Provider]: spec, "--output", output.as_output(), - hidden = [item["src"] for item in items], + hidden = map_idx("src", items), ), category = "wasi_dist", )