Search in sources :

Example 31 with InvocationPolicyEnforcer

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);
}
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 32 with InvocationPolicyEnforcer

use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.

the class InvocationPolicyEnforcerTest method testDisallowValuesSetsDefaultValue.

@Test
public void testDisallowValuesSetsDefaultValue() throws Exception {
    InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
    invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getDisallowValuesBuilder().addDisallowedValues("user value").getUseDefaultBuilder();
    InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
    parser.parse("--test_string=user value");
    TestOptions testOptions = getTestOptions();
    assertEquals("user value", testOptions.testString);
    enforcer.enforce(parser, "build");
    testOptions = getTestOptions();
    assertEquals(STRING_FLAG_DEFAULT, testOptions.testString);
}
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 33 with InvocationPolicyEnforcer

use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.

the class InvocationPolicyEnforcerTest method testUseDefaultWithExpandedFlags.

@Test
public void testUseDefaultWithExpandedFlags() throws Exception {
    InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
    invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_expansion_b").getUseDefaultBuilder();
    InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
    parser.parse("--test_expansion");
    // --test_expansion should turn 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_b should be back to its default (true), but the
    // rest should remain the same.
    testOptions = getTestOptions();
    assertFalse(testOptions.testExpansionA);
    assertTrue(testOptions.testExpansionB);
    assertEquals(42, testOptions.testExpansionC);
    assertEquals("bar", testOptions.testExpansionD);
}
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 34 with InvocationPolicyEnforcer

use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.

the class InvocationPolicyEnforcerTest method testSetValueHasMultipleValuesButFlagIsNotMultiple.

@Test
public void testSetValueHasMultipleValuesButFlagIsNotMultiple() throws Exception {
    InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
    invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName(// Not repeatable flag.
    "test_string").getSetValueBuilder().addFlagValue(// Has multiple values.
    "policy value 1").addFlagValue("policy value 2");
    InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder);
    try {
        enforcer.enforce(parser, "build");
        fail();
    } catch (OptionsParsingException e) {
    // expected.
    }
}
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 35 with InvocationPolicyEnforcer

use of com.google.devtools.build.lib.flags.InvocationPolicyEnforcer in project bazel by bazelbuild.

the class AnalysisTestCase method useConfiguration.

/**
   * Sets host and target configuration using the specified options, falling back to the default
   * options for unspecified ones, and recreates the build view.
   */
protected final void useConfiguration(String... args) throws Exception {
    optionsParser = OptionsParser.newOptionsParser(Iterables.concat(Arrays.asList(ExecutionOptions.class, PackageCacheOptions.class, BuildRequestOptions.class, BuildView.Options.class), ruleClassProvider.getConfigurationOptions()));
    optionsParser.parse(new String[] { "--default_visibility=public" });
    optionsParser.parse(args);
    if (defaultFlags().contains(Flag.DYNAMIC_CONFIGURATIONS)) {
        optionsParser.parse("--experimental_dynamic_configs=on");
    } else if (defaultFlags().contains(Flag.DYNAMIC_CONFIGURATIONS_NOTRIM)) {
        optionsParser.parse("--experimental_dynamic_configs=notrim");
    }
    InvocationPolicyEnforcer optionsPolicyEnforcer = analysisMock.getInvocationPolicyEnforcer();
    optionsPolicyEnforcer.enforce(optionsParser);
}
Also used : BuildRequestOptions(com.google.devtools.build.lib.buildtool.BuildRequest.BuildRequestOptions) ExecutionOptions(com.google.devtools.build.lib.exec.ExecutionOptions) LoadingOptions(com.google.devtools.build.lib.pkgcache.LoadingOptions) BuildOptions(com.google.devtools.build.lib.analysis.config.BuildOptions) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) Options(com.google.devtools.common.options.Options) ExecutionOptions(com.google.devtools.build.lib.exec.ExecutionOptions) BuildRequestOptions(com.google.devtools.build.lib.buildtool.BuildRequest.BuildRequestOptions) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions)

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