Skip to content

Commit

Permalink
Support merging itest_service/task env with underlying binary
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Aug 24, 2024
1 parent 3b322be commit 9572fa9
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions private/itest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def _itest_binary_impl(ctx, extra_service_spec_kwargs, extra_exe_runfiles = []):
for (var, val) in ctx.attr.env.items()
}

if RunEnvironmentInfo in ctx.attr.exe:
for k, v in ctx.attr.exe[RunEnvironmentInfo].environment.items():
if k in env:
fail("Env key %s specified both in raw binary and itest wrapper rule" % k)
env[k] = v

if version_file:
extra_service_spec_kwargs["version_file"] = to_rlocation_path(ctx, version_file)

Expand Down
1 change: 1 addition & 0 deletions tests/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import ../.bazelrc
common --noenable_runfiles
common --@rules_go//go/config:pure
8 changes: 8 additions & 0 deletions tests/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ bazel_dep(name = "aspect_rules_js", version = "1.39.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "rules_go", version = "0.49.0")
bazel_dep(name = "gazelle", version = "0.37.0")
bazel_dep(name = "hermetic_cc_toolchain", version = "3.1.0")
toolchains = use_extension("@hermetic_cc_toolchain//toolchain:ext.bzl", "toolchains")
use_repo(toolchains, "zig_sdk")

register_toolchains(
"@zig_sdk//toolchain:darwin_amd64",
"@zig_sdk//toolchain:darwin_arm64",
)

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
Expand Down
2 changes: 2 additions & 0 deletions tests/dependencies/test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
set -eux

got="$(cat $TEST_TMPDIR/out.txt)"

if [ "$got" != "hello world" ]; then
Expand Down
26 changes: 25 additions & 1 deletion tests/test_env/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_go//go:def.bzl", "go_test")
load("@rules_itest//:itest.bzl", "service_test")
load("@rules_itest//:itest.bzl", "service_test", "itest_task")

env = {
"ITEST_ENV_VAR": "ITEST_ENV_VAR_VALUE",
Expand Down Expand Up @@ -28,3 +28,27 @@ service_test(
env = env,
test = ":_env_not_specified",
)

cc_binary(
name = "_task_cc",
srcs = ["task.cc"],
env = env,
)

itest_task(
name = "task_cc",
exe = ":_task_cc",
)

sh_binary(
name = "_task_sh",
srcs = ["task.sh"],
env = env,
)

itest_task(
name = "task_sh",
exe = ":_task_sh",
# sh_binary does not provide RunEnvironmentInfo.
tags = ["manual"],
)
14 changes: 14 additions & 0 deletions tests/test_env/task.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
const char *want = "ITEST_ENV_VAR_VALUE";
const char *got = getenv("ITEST_ENV_VAR");
if (got == NULL) {
fprintf(stderr, "ITEST_ENV_VAR_VALUE was not passed\n");
return 1;
}

return strcmp(want, got);
}
7 changes: 7 additions & 0 deletions tests/test_env/task.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -eux

if [ "$ITEST_ENV_VAR" != "ITEST_ENV_VAR_VALUE" ]; then
echo "Missing expected env var"
exit 1
fi;

0 comments on commit 9572fa9

Please sign in to comment.