Skip to content

Commit

Permalink
Add env_inherit to cc_test.
Browse files Browse the repository at this point in the history
RELNOTES: None
PiperOrigin-RevId: 442748692
  • Loading branch information
trybka authored and copybara-github committed Apr 19, 2022
1 parent 1a47d99 commit 4ed40ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ _cc_test_attrs.update(
"@" + paths.join(semantics.get_platforms_root(), "os:windows"),
],
),
# Starlark tests don't get `env_inherit` by default.
env_inherit = attr.string_list(),
stamp = attr.int(values = [-1, 0, 1], default = 0),
linkstatic = attr.bool(default = False),
)
Expand All @@ -67,7 +69,10 @@ def _cc_test_impl(ctx):
runfiles = runfiles.merge_all(runfiles_list)

test_env.update(coverage_env)
providers.append(testing.TestEnvironment(test_env))
providers.append(testing.TestEnvironment(
environment = test_env,
inherited_environment = ctx.attr.env_inherit,
))
providers.append(DefaultInfo(
files = binary_info.files,
runfiles = runfiles,
Expand Down
26 changes: 26 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1455,4 +1455,30 @@ EOF
expect_log "fatal error: '\?dep.h'\?"
}

function test_env_inherit_cc_test() {
mkdir pkg
cat > pkg/BUILD <<EOF
cc_test(
name = 'foo_test',
srcs = ['foo_test.cc'],
env_inherit = ['FOO'],
)
EOF

cat > pkg/foo_test.cc <<EOF
#include <stdlib.h>
int main() {
auto foo = getenv("FOO");
if (foo == nullptr) {
return 1;
}
return 0;
}
EOF

bazel test //pkg:foo_test &> "$TEST_log" && fail "Did not fail as expected. ENV leak?" || true
FOO=1 bazel test //pkg:foo_test &> "$TEST_log" || fail "Should have inherited FOO env."
}

run_suite "cc_integration_test"

0 comments on commit 4ed40ee

Please sign in to comment.