use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testSetValueWithMultipleValuesOverridesDefault.
/**
* Tests that policy overrides the default value when the flag allows multiple values and the user
* doesn't provide a value.
*/
@Test
public void testSetValueWithMultipleValuesOverridesDefault() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getSetValueBuilder().addFlagValue("policy value 1").addFlagValue("policy value 2");
// No user value.
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
// Repeatable flags always default to the empty list.
TestOptions testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).isEmpty();
enforcer.enforce(parser, "build");
// Options should now be the values from the policy.
testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).containsExactly("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 testUseDefaultWithFlagWithRecursiveImplicitRequirements.
@Test
public void testUseDefaultWithFlagWithRecursiveImplicitRequirements() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_recursive_implicit_requirement").getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_recursive_implicit_requirement=user value");
// test_recursive_implicit_requirement gets its value from the command line,
// test_implicit_requirement gets its value from test_recursive_implicit_requirement, and
// an_implicit_requirement gets its value from test_implicit_requirement.
TestOptions testOptions = getTestOptions();
assertEquals("user value", testOptions.testRecursiveImplicitRequirement);
assertEquals("bar", testOptions.testImplicitRequirement);
assertEquals("foo", testOptions.anImplicitRequirement);
enforcer.enforce(parser, "build");
// Policy enforcement should set everything back to its default value.
testOptions = getTestOptions();
assertEquals("test recursive implicit requirement default", testOptions.testRecursiveImplicitRequirement);
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 testAllowValuesSetsDefaultValueForRepeatableFlag.
@Test
public void testAllowValuesSetsDefaultValueForRepeatableFlag() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getAllowValuesBuilder().addAllowedValues("foo").addAllowedValues("bar").getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_multiple_string=foo", "--test_multiple_string=baz");
TestOptions testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).containsExactly("foo", "baz").inOrder();
enforcer.enforce(parser, "build");
testOptions = getTestOptions();
// Default value for repeatable flags is always empty.
assertThat(testOptions.testMultipleString).isEmpty();
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesDisallowsListConverterFlag.
@Test
public void testDisallowValuesDisallowsListConverterFlag() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_list_converters").getDisallowValuesBuilder().addDisallowedValues("a");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_list_converters=a,b,c");
TestOptions testOptions = getTestOptions();
assertEquals(Arrays.asList("a", "b", "c"), testOptions.testListConverters);
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
assertThat(e.getMessage()).contains("Flag value 'a' for flag 'test_list_converters' is not allowed by invocation policy");
}
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testAllowValuesDisallowsListConverterFlagValues.
@Test
public void testAllowValuesDisallowsListConverterFlagValues() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_list_converters").getAllowValuesBuilder().addAllowedValues("a");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_list_converters=a,b,c");
TestOptions testOptions = getTestOptions();
assertEquals(Arrays.asList("a", "b", "c"), testOptions.testListConverters);
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
assertThat(e.getMessage()).contains("Flag value 'b' for flag 'test_list_converters' is not allowed by invocation policy");
}
}
Aggregations