use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesAllowsValue.
/*************************************************************************************************
* Tests for DisallowValues
************************************************************************************************/
@Test
public void testDisallowValuesAllowsValue() 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=baz");
// Option should be "baz" as specified by the user.
TestOptions testOptions = getTestOptions();
assertEquals("baz", testOptions.testString);
enforcer.enforce(parser, "build");
// Still "baz" since "baz" is allowed by the policy.
testOptions = getTestOptions();
assertEquals("baz", testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesSetsDefaultValueForRepeatableFlag.
@Test
public void testDisallowValuesSetsDefaultValueForRepeatableFlag() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_multiple_string").getDisallowValuesBuilder().addDisallowedValues("user value").getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_multiple_string=user value");
TestOptions testOptions = getTestOptions();
assertThat(testOptions.testMultipleString).containsExactly("user value");
enforcer.enforce(parser, "build");
testOptions = getTestOptions();
// Default 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 testAllowValuesAllowsValue.
/*************************************************************************************************
* Tests for AllowValues
************************************************************************************************/
/**
* Tests that AllowValues works in the normal case where the value the user specified is allowed
* by the policy.
*/
@Test
public void testAllowValuesAllowsValue() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getAllowValuesBuilder().addAllowedValues(STRING_FLAG_DEFAULT).addAllowedValues("foo").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);
enforcer.enforce(parser, "build");
// Still "foo" since "foo" is allowed by the policy.
testOptions = getTestOptions();
assertEquals("foo", testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testUseDefaultWhenFlagWasntSet.
/**
* Tests UseDefault when the user never actually specified the flag.
*/
@Test
public void testUseDefaultWhenFlagWasntSet() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
// Options should be the default since the user never specified it.
TestOptions testOptions = getTestOptions();
assertEquals(STRING_FLAG_DEFAULT, testOptions.testString);
enforcer.enforce(parser, "build");
// Still the default.
testOptions = getTestOptions();
assertEquals(STRING_FLAG_DEFAULT, testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testUseDefaultWithExpansionFlags.
@Test
public void testUseDefaultWithExpansionFlags() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_expansion").getUseDefaultBuilder();
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_expansion");
TestOptions testOptions = getTestOptions();
assertFalse(testOptions.testExpansionA);
assertFalse(testOptions.testExpansionB);
assertEquals(42, testOptions.testExpansionC);
assertEquals("bar", testOptions.testExpansionD);
enforcer.enforce(parser, "build");
// After policy enforcement, all the flags that --test_expansion expanded into should be back
// to their default values.
testOptions = getTestOptions();
assertTrue(testOptions.testExpansionA);
assertTrue(testOptions.testExpansionB);
assertEquals(12, testOptions.testExpansionC);
assertEquals("foo", testOptions.testExpansionD);
}
Aggregations