Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass --host_action_env to host options hostActionEnvironment attribute #12694

Closed
wants to merge 12 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ public FragmentOptions getHost() {

// Pass host action environment variables
host.actionEnvironment = hostActionEnvironment;
host.hostActionEnvironment = hostActionEnvironment;

return host;
}
Expand Down
221 changes: 221 additions & 0 deletions src/test/shell/integration/action_env_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ sh_test(
name = "test_env_foo",
srcs = ["test_env_foo.sh"],
)

genrule(
name = "show_host_env_using_tools",
tools = ["env.txt"],
outs = ["tools_env.txt"],
cmd = "cp \$(location env.txt) \"\$@\""
)

genrule(
name = "show_host_env_using_exec_tools",
exec_tools = ["env.txt"],
outs = ["exec_tools_env.txt"],
cmd = "cp \$(location env.txt) \"\$@\""
)

genrule(
name = "show_host_env_using_successive_exec_tools",
exec_tools = ["exec_tools_env.txt"],
outs = ["successive_exec_tools_env.txt"],
cmd = "cp \$(location exec_tools_env.txt) \"\$@\""
)
EOF
cat > pkg/build.bzl <<EOF
def _impl(ctx):
Expand Down Expand Up @@ -218,4 +239,204 @@ function test_action_env_changes_honored {

}

function test_host_env_using_tools_simple() {
export FOO=baz

# If FOO is passed using --host_action_env, it should be listed in host env vars
bazel build --host_action_env=FOO=bar pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=bar"

# But if FOO is passed using --action_env, it should not be listed in host env vars
bazel build --action_env=FOO=bar pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_not_log "FOO=bar"
}

function test_host_env_using_tools_latest_wins() {
export FOO=environmentfoo
export BAR=environmentbar
bazel build --host_action_env=FOO=foo \
--host_action_env=BAR=willbeoverridden --host_action_env=BAR=bar pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=bar"
}

function test_client_env_using_tools() {
export FOO=startup_foo
bazel clean --expunge
bazel help build > /dev/null || fail "${PRODUCT_NAME} help failed"
export FOO=client_foo
bazel build --host_action_env=FOO pkg:show_host_env_using_tools || \
fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=client_foo"
}

function test_redo_host_env_using_tools() {
export FOO=initial_foo
export UNRELATED=some_value
bazel build --host_action_env=FOO pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=initial_foo"

# If an unrelated value changes, we expect the action not to be executed again
export UNRELATED=some_other_value
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'

# However, if a used variable changes, we expect the change to be propagated
export FOO=changed_foo
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
expect_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=changed_foo"

# But repeating the build with no further changes, no action should happen
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'
}

function test_latest_wins_arg_using_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO --host_action_env=FOO=foo \
pkg:show_host_env_using_tools || fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=baz"
expect_not_log "FOO=bar"
}

function test_latest_wins_env_using_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO=foo --host_action_env=FOO \
pkg:show_host_env_using_tools || fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=bar"
expect_log "BAR=baz"
expect_not_log "FOO=foo"
}

function test_host_env_using_exec_tools_simple() {
export FOO=baz

# If FOO is passed using --host_action_env, it should be listed in host env vars
bazel build --host_action_env=FOO=bar pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=bar"

# But if FOO is passed using --action_env, it should not be listed in host env vars
bazel build --action_env=FOO=bar pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_not_log "FOO=bar"
}

function test_host_env_using_exec_tools_latest_wins() {
export FOO=environmentfoo
export BAR=environmentbar
bazel build --host_action_env=FOO=foo \
--host_action_env=BAR=willbeoverridden --host_action_env=BAR=bar pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=bar"
}

function test_client_env_using_exec_tools() {
export FOO=startup_foo
bazel clean --expunge
bazel help build > /dev/null || fail "${PRODUCT_NAME} help failed"
export FOO=client_foo
bazel build --host_action_env=FOO pkg:show_host_env_using_exec_tools || \
fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=client_foo"
}

function test_redo_host_env_using_exec_tools() {
export FOO=initial_foo
export UNRELATED=some_value
bazel build --host_action_env=FOO pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=initial_foo"

# If an unrelated value changes, we expect the action not to be executed again
export UNRELATED=some_other_value
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'

# However, if a used variable changes, we expect the change to be propagated
export FOO=changed_foo
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
expect_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=changed_foo"

# But repeating the build with no further changes, no action should happen
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'
}

function test_latest_wins_arg_using_exec_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO --host_action_env=FOO=foo \
pkg:show_host_env_using_exec_tools || fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=baz"
expect_not_log "FOO=bar"
}

function test_latest_wins_env_using_exec_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO=foo --host_action_env=FOO \
pkg:show_host_env_using_exec_tools || fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=bar"
expect_log "BAR=baz"
expect_not_log "FOO=foo"
}

function test_host_env_using_successive_exec_tools_simple() {
export FOO=baz

bazel build --host_action_env=FOO=bar pkg:show_host_env_using_successive_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_successive_exec_tool failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/successive_exec_tools_env.txt > $TEST_log
expect_log "FOO=bar"
}

run_suite "Tests for bazel's handling of environment variables in actions"