Search in sources :

Example 6 with InvocationPolicyEnforcer

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.
    }
}
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 7 with InvocationPolicyEnforcer

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

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

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

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