use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testAllowValuesDisallowsFlagDefaultButNoPolicyDefault.
@Test
public void testAllowValuesDisallowsFlagDefaultButNoPolicyDefault() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getAllowValuesBuilder().addAllowedValues("foo").addAllowedValues("bar");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
// Option should be its default
TestOptions testOptions = getTestOptions();
assertEquals(STRING_FLAG_DEFAULT, testOptions.testString);
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
// expected.
}
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testAllowValuesDisallowsMultipleValues.
@Test
public void testAllowValuesDisallowsMultipleValues() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getAllowValuesBuilder().addAllowedValues("foo").addAllowedValues("bar");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_multiple_string=baz", "--test_multiple_string=bar");
// Option should be "baz" and "bar" as specified by the user.
TestOptions testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).containsExactly("baz", "bar").inOrder();
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
// expected, since baz is not allowed.
}
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesRaisesErrorIfDefaultIsDisallowedAndSetsUseDefault.
@Test
public void testDisallowValuesRaisesErrorIfDefaultIsDisallowedAndSetsUseDefault() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getDisallowValuesBuilder().addDisallowedValues(STRING_FLAG_DEFAULT).getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
assertThat(e.getMessage()).contains("but also specifies to use the default value");
}
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testSetValueWithImplicitlyRequiredFlags.
@Test
public void testSetValueWithImplicitlyRequiredFlags() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("an_implicit_requirement").getSetValueBuilder().addFlagValue("policy value");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_implicit_requirement=user value");
// test_implicit_requirement sets an_implicit_requirement to "foo"
TestOptions testOptions = getTestOptions();
assertEquals("user value", testOptions.testImplicitRequirement);
assertEquals("foo", testOptions.anImplicitRequirement);
enforcer.enforce(parser, "build");
testOptions = getTestOptions();
assertEquals("user value", testOptions.testImplicitRequirement);
assertEquals("policy value", testOptions.anImplicitRequirement);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesSetsNewValue.
@Test
public void testDisallowValuesSetsNewValue() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getDisallowValuesBuilder().addDisallowedValues("user value").setNewValue("baz");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_string=user value");
TestOptions testOptions = getTestOptions();
assertEquals("user value", testOptions.testString);
enforcer.enforce(parser, "build");
// Should now be "baz" because the policy forces disallowed values to "baz"
testOptions = getTestOptions();
assertEquals("baz", testOptions.testString);
}
Aggregations