use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesDisallowsValue.
@Test
public void testDisallowValuesDisallowsValue() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getDisallowValuesBuilder().addDisallowedValues("foo").addDisallowedValues("bar");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_string=foo");
// Option should be "foo" as specified by the user.
TestOptions testOptions = getTestOptions();
assertEquals("foo", testOptions.testString);
try {
enforcer.enforce(parser, "build");
fail();
} catch (OptionsParsingException e) {
// expected, since foo is disallowed.
}
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testSetValueWithMultipleValuesOverridesUser.
/**
* Tests that SetValue overrides the user's value when the flag allows multiple values.
*/
@Test
public void testSetValueWithMultipleValuesOverridesUser() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getSetValueBuilder().addFlagValue("policy value 1").addFlagValue("policy value 2");
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("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 testFlagPolicyDoesNotApply.
/*************************************************************************************************
* Other tests
************************************************************************************************/
@Test
public void testFlagPolicyDoesNotApply() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").addCommands("build").getSetValueBuilder().addFlagValue("policy value");
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("user value", testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testAllowValuesDisallowsValue.
@Test
public void testAllowValuesDisallowsValue() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getAllowValuesBuilder().addAllowedValues(STRING_FLAG_DEFAULT).addAllowedValues("bar");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_string=foo");
// Option should be "foo" as specified by the user.
TestOptions testOptions = getTestOptions();
assertEquals("foo", testOptions.testString);
try {
// Should throw because "foo" is not allowed.
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 testAllowValuesSetsDefaultValue.
@Test
public void testAllowValuesSetsDefaultValue() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getAllowValuesBuilder().addAllowedValues("foo").addAllowedValues(STRING_FLAG_DEFAULT).getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_string=bar");
TestOptions testOptions = getTestOptions();
assertEquals("bar", testOptions.testString);
enforcer.enforce(parser, "build");
testOptions = getTestOptions();
assertEquals(STRING_FLAG_DEFAULT, testOptions.testString);
}
Aggregations