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

Specify fixedEnv/inheritedEnv interaction in ActionEnvironment #15170

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
public final class ActionEnvironment {

/**
* A map of environment variables together with a list of variables to inherit from the shell
* environment.
* A map of environment variables and their values together with a list of variables whose values
* should be inherited from the client environment.
*/
public interface EnvironmentVariables {

Expand Down Expand Up @@ -77,7 +77,7 @@ default int size() {

/**
* An {@link EnvironmentVariables} that combines variables from two different environments without
* allocation a new map.
* allocating a new map.
*/
static class CompoundEnvironmentVariables implements EnvironmentVariables {

Expand Down Expand Up @@ -167,8 +167,6 @@ public ImmutableSet<String> getInheritedEnvironment() {
* given map. Returns these two parts as a new {@link ActionEnvironment} instance.
*/
public static ActionEnvironment split(Map<String, String> env) {
// Care needs to be taken that the two sets don't overlap - the order in which the two parts are
// combined later is undefined.
Map<String, String> fixedEnv = new TreeMap<>();
Set<String> inheritedEnv = new TreeSet<>();
for (Map.Entry<String, String> entry : env.entrySet()) {
Expand All @@ -189,9 +187,10 @@ private ActionEnvironment(EnvironmentVariables vars) {
}

/**
* Creates a new action environment. The order in which the environments are combined is
* undefined, so callers need to take care that the key set of the {@code fixedEnv} map and the
* set of {@code inheritedEnv} elements are disjoint.
* Creates a new action environment. If an environment variable is contained in both {@link
* EnvironmentVariables#getFixedEnvironment()} and {@link EnvironmentVariables#getInheritedEnvironment()},
* the result of {@link ActionEnvironment#resolve(Map, Map)} will contain the value inherited from
* the client environment.
*/
private static ActionEnvironment create(EnvironmentVariables vars) {
if (vars.isEmpty()) {
Expand All @@ -200,6 +199,12 @@ private static ActionEnvironment create(EnvironmentVariables vars) {
return new ActionEnvironment(vars);
}

/**
* Creates a new action environment. If an environment variable is contained both as a key in
* {@code fixedEnv} and in {@code inheritedEnv}, the result of {@link
* ActionEnvironment#resolve(Map, Map)} will contain the value inherited from the client
* environment.
*/
public static ActionEnvironment create(
Map<String, String> fixedEnv, ImmutableSet<String> inheritedEnv) {
return ActionEnvironment.create(SimpleEnvironmentVariables.create(fixedEnv, inheritedEnv));
Expand Down Expand Up @@ -227,7 +232,11 @@ public ActionEnvironment addVariables(Map<String, String> fixedVars, Set<String>
CompoundEnvironmentVariables.create(fixedVars, inheritedVars, vars));
}

/** Returns the combined size of the fixed and inherited environments. */
/**
* Returns an upper bound on the combined size of the fixed and inherited environments. A call to
* {@link ActionEnvironment#resolve(Map, Map)} may add less entries than this number if
* environment variables are contained in both the fixed and the inherited environment.
*/
public int size() {
return vars.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -40,4 +42,20 @@ public void compoundEnvOrdering() {
assertThat(env2.getFixedEnv()).containsExactly("FOO", "foo2", "BAR", "bar");
assertThat(env2.getInheritedEnv()).containsExactly("baz");
}

@Test
public void fixedInheritedInteraction() {
ActionEnvironment env = ActionEnvironment.create(
ImmutableMap.of("FIXED_ONLY", "fixed"),
ImmutableSet.of("INHERITED_ONLY"))
.addVariables(ImmutableMap.of("FIXED_AND_INHERITED", "fixed"),
ImmutableSet.of("FIXED_AND_INHERITED"));
Map<String, String> clientEnv = ImmutableMap.of("INHERITED_ONLY", "inherited",
"FIXED_AND_INHERITED", "inherited");
Map<String, String> result = new HashMap<>();
env.resolve(result, clientEnv);

assertThat(result).containsExactly("FIXED_ONLY", "fixed", "FIXED_AND_INHERITED", "inherited",
"INHERITED_ONLY", "inherited");
}
}