Add a test verifying that --action_env triggers a rerun of tests

Issue #3265 went unnoticed for a long time, till it was fixed as
a sideeffect of d1c5329ba622b29afd3ab9f670fa17064d493bc0. To avoid
that happing in the future,  add a test. Closes #3265.

Change-Id: Ie22a1d4a2f09fd3dcadcbd900795b3e12b7dc461
PiperOrigin-RevId: 160502516
diff --git a/src/test/shell/integration/action_env_test.sh b/src/test/shell/integration/action_env_test.sh
index 2564a5a..98e56de 100755
--- a/src/test/shell/integration/action_env_test.sh
+++ b/src/test/shell/integration/action_env_test.sh
@@ -39,6 +39,11 @@
 
 environ(name = "no_default_env", env = 0)
 environ(name = "with_default_env", env = 1)
+
+sh_test(
+    name = "test_env_foo",
+    srcs = ["test_env_foo.sh"],
+)
 EOF
   cat > pkg/build.bzl <<EOF
 def _impl(ctx):
@@ -55,6 +60,15 @@
     outputs={"out": "%{name}.env"},
 )
 EOF
+  cat > pkg/test_env_foo.sh <<'EOF'
+#!/bin/sh
+
+echo "FOO is >${FOO}<"
+
+{ echo "${FOO}" | grep foo; } || { echo "expected FOO to contain foo"; exit 1; }
+
+EOF
+  chmod u+x pkg/test_env_foo.sh
 }
 
 #### TESTS #############################################################
@@ -184,4 +198,24 @@
          && fail "dynamic action_env used, even though requested not to") || true
 }
 
+function test_action_env_changes_honored {
+    # Verify that changes to the explicitly specified action_env in honored in
+    # tests. Regression test for #3265.
+
+    # start with a fresh bazel, to have a reproducible starting point
+    bazel clean --expunge
+    bazel test --test_output=all --action_env=FOO=foo //pkg:test_env_foo \
+        || fail "expected to pass with correct value for FOO"
+    # While the test is cached, changing the environment should rerun it and
+    # detect the failure in the new environemnt.
+    (bazel test --test_output=all --action_env=FOO=bar //pkg:test_env_foo \
+         && fail "expected to fail with incorrect value for FOO") || true
+    # Redo the same FOO being taken from the environment
+    env FOO=foo bazel test --test_output=all --action_env=FOO //pkg:test_env_foo \
+        || fail "expected to pass with correct value for FOO from the environment"
+    (env FOO=bar bazel test --test_output=all --action_env=FOO=bar //pkg:test_env_foo \
+         && fail "expected to fail with incorrect value for FOO from the environment") || true
+
+}
+
 run_suite "Tests for bazel's handling of environment variables in actions"