Search in sources :

Example 21 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException 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 22 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.

the class InvocationPolicyEnforcerTest method testDisallowValuesDisallowsValue.

@Test
public void testDisallowValuesDisallowsValue() 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=foo");
    // Option should be "foo" as specified by the user.
    TestOptions testOptions = getTestOptions();
    assertEquals("foo", testOptions.testString);
    try {
        enforcer.enforce(parser, "build");
        fail();
    } catch (OptionsParsingException e) {
    // expected, since foo is disallowed.
    }
}
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 23 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException in project bazel by bazelbuild.

the class InvocationPolicyEnforcerTest method testAllowValuesDisallowsValue.

@Test
public void testAllowValuesDisallowsValue() throws Exception {
    InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder();
    invocationPolicyBuilder.addFlagPoliciesBuilder().setFlagName("test_string").getAllowValuesBuilder().addAllowedValues(STRING_FLAG_DEFAULT).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);
    try {
        // Should throw because "foo" is not allowed.
        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 24 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException 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 25 with OptionsParsingException

use of com.google.devtools.common.options.OptionsParsingException 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

OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)26 InvocationPolicyEnforcer (com.google.devtools.build.lib.flags.InvocationPolicyEnforcer)15 InvocationPolicy (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy)11 Test (org.junit.Test)11 OptionsParser (com.google.devtools.common.options.OptionsParser)6 Path (com.google.devtools.build.lib.vfs.Path)3 PackageCacheOptions (com.google.devtools.build.lib.pkgcache.PackageCacheOptions)2 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)2 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)2 OptionsBase (com.google.devtools.common.options.OptionsBase)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Artifact (com.google.devtools.build.lib.actions.Artifact)1 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)1 BuildOptions (com.google.devtools.build.lib.analysis.config.BuildOptions)1 FragmentOptions (com.google.devtools.build.lib.analysis.config.FragmentOptions)1 Label (com.google.devtools.build.lib.cmdline.Label)1