use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testAllowValuesSetsNewValue.
@Test
public void testAllowValuesSetsNewValue() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getAllowValuesBuilder().addAllowedValues("foo").addAllowedValues("bar").setNewValue("foo");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_string=baz");
TestOptions testOptions = getTestOptions();
assertEquals("baz", testOptions.testString);
enforcer.enforce(parser, "build");
testOptions = getTestOptions();
assertEquals("foo", testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testUseDefaultWithFlagWithImplicitRequirements.
@Test
public void testUseDefaultWithFlagWithImplicitRequirements() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_implicit_requirement").getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_implicit_requirement=user value");
// test_implicit_requirement sets an_implicit_requirement to "foo", which ignores the user's
// value because the parser processes implicit values last.
TestOptions testOptions = getTestOptions();
assertEquals("user value", testOptions.testImplicitRequirement);
assertEquals("foo", testOptions.anImplicitRequirement);
// Then policy puts test_implicit_requirement and its implicit requirements back to its default.
enforcer.enforce(parser, "build");
testOptions = getTestOptions();
assertEquals("test implicit requirement default", testOptions.testImplicitRequirement);
assertEquals("implicit default", testOptions.anImplicitRequirement);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testNonExistantFlagFromPolicy.
@Test
public void testNonExistantFlagFromPolicy() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("i_do_not_exist").getSetValueBuilder().addFlagValue("policy value 1");
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getSetValueBuilder().addFlagValue("policy value 2");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_string=user value");
TestOptions testOptions = getTestOptions();
assertEquals("user value", testOptions.testString);
enforcer.enforce(parser, "test");
// Still user value.
testOptions = getTestOptions();
assertEquals("policy value 2", testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testSetValueAppendsToMultipleValuedFlag.
@Test
public void testSetValueAppendsToMultipleValuedFlag() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getSetValueBuilder().addFlagValue("policy value 1").addFlagValue("policy value 2").setAppend(true);
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_multiple_string=user value 1", "--test_multiple_string=user value 2");
// Options should not be modified by running the parser through OptionsPolicyEnforcer.create().
TestOptions testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).containsExactly("user value 1", "user value 2").inOrder();
enforcer.enforce(parser, "build");
// Get the options again after policy enforcement.
testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).containsExactly("user value 1", "user value 2", "policy value 1", "policy value 2").inOrder();
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testUseDefaultWithRecursiveExpansionFlags.
@Test
public void testUseDefaultWithRecursiveExpansionFlags() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_expansion").getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_recursive_expansion_top_level");
TestOptions testOptions = getTestOptions();
assertFalse(testOptions.testExpansionA);
assertFalse(testOptions.testExpansionB);
assertEquals(56, testOptions.testExpansionC);
assertEquals("baz", testOptions.testExpansionD);
enforcer.enforce(parser, "build");
// After policy enforcement, all the flags that --test_recursive_expansion_top_level and its
// recursive expansions set should be back to their default values.
testOptions = getTestOptions();
assertTrue(testOptions.testExpansionA);
assertTrue(testOptions.testExpansionB);
assertEquals(12, testOptions.testExpansionC);
assertEquals("foo", testOptions.testExpansionD);
}
Aggregations