Search in sources :

Example 41 with InvocationPolicyEnforcer

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();
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) Test(org.junit.Test)

Example 42 with InvocationPolicyEnforcer

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);
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) Test(org.junit.Test)

Example 43 with InvocationPolicyEnforcer

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();
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) Test(org.junit.Test)

Example 44 with InvocationPolicyEnforcer

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");
    }
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) Test(org.junit.Test)

Example 45 with InvocationPolicyEnforcer

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");
    }
}
Also used : InvocationPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) Test(org.junit.Test)

Aggregations

InvocationPolicyEnforcer (com.google.devtools.build.lib.flags.InvocationPolicyEnforcer)49 InvocationPolicy (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy)41 Test (org.junit.Test)41 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)15 OptionsParser (com.google.devtools.common.options.OptionsParser)6 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)4 FragmentOptions (com.google.devtools.build.lib.analysis.config.FragmentOptions)2 ExecutionOptions (com.google.devtools.build.lib.exec.ExecutionOptions)2 OptionsBase (com.google.devtools.common.options.OptionsBase)2 ArrayList (java.util.ArrayList)2 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 BuildConfigurationCollection (com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection)1 BuildRequestOptions (com.google.devtools.build.lib.buildtool.BuildRequest.BuildRequestOptions)1 EventHandler (com.google.devtools.build.lib.events.EventHandler)1 Reporter (com.google.devtools.build.lib.events.Reporter)1 LoadingOptions (com.google.devtools.build.lib.pkgcache.LoadingOptions)1 PackageCacheOptions (com.google.devtools.build.lib.pkgcache.PackageCacheOptions)1 BlazeCommand (com.google.devtools.build.lib.runtime.BlazeCommand)1 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)1 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)1