Search in sources :

Example 21 with InvocationPolicyEnforcer

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

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

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

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

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);
}
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