use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testDisallowValuesDisallowsFlagDefaultButNoPolicyDefault.
@Test
public void testDisallowValuesDisallowsFlagDefaultButNoPolicyDefault() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getDisallowValuesBuilder().addDisallowedValues(STRING_FLAG_DEFAULT);
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
// Option should be the default since the use didn't specify a value.
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 testSetValueOverridesDefault.
/**
* Tests that policy overrides a value when the user doesn't specify the value (i.e., the value
* is from the flag's default from its definition).
*/
@Test
public void testSetValueOverridesDefault() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getSetValueBuilder().addFlagValue("policy value");
// No user value.
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
// All the flags should be their default value.
TestOptions testOptions = getTestOptions();
assertEquals(STRING_FLAG_DEFAULT, testOptions.testString);
enforcer.enforce(parser, "build");
// Get the options again after policy enforcement.
testOptions = getTestOptions();
assertEquals("policy value", testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testAllowValuesSetsNewDefaultWhenFlagDefaultIsDisallowed.
/**
* Tests that AllowValues sets its default value when the user doesn't provide a value and the
* flag's default value is disallowed.
*/
@Test
public void testAllowValuesSetsNewDefaultWhenFlagDefaultIsDisallowed() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getAllowValuesBuilder().addAllowedValues("foo").addAllowedValues("bar").setNewValue("new default");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
// Option should be its default
TestOptions testOptions = getTestOptions();
assertEquals(STRING_FLAG_DEFAULT, testOptions.testString);
enforcer.enforce(parser, "build");
// Flag's value should be the default value from the policy.
testOptions = getTestOptions();
assertEquals("new default", testOptions.testString);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testSetValueWithExpandedFlags.
@Test
public void testSetValueWithExpandedFlags() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_expansion_c").getSetValueBuilder().addFlagValue("64");
InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
parser.parse("--test_expansion");
// --test_expansion should set the values from its 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, test_expansion_c should be set to 64 from the policy, but the
// flags should remain the same from the expansion of --test_expansion.
testOptions = getTestOptions();
assertFalse(testOptions.testExpansionA);
assertFalse(testOptions.testExpansionB);
assertEquals(64, testOptions.testExpansionC);
assertEquals("bar", testOptions.testExpansionD);
}
use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.
the class InvocationPolicyEnforcerTest method testSetValueOverridesUser.
/*************************************************************************************************
* Tests for SetValue
************************************************************************************************/
/**
* Tests that policy overrides a value when that value is from the user.
*/
@Test
public void testSetValueOverridesUser() throws Exception {
InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").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, "build");
// Get the options again after policy enforcement.
testOptions = getTestOptions();
assertEquals("policy value", testOptions.testString);
}
Aggregations