Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
ccalvarin | f39dc6f | 2017-06-09 11:51:45 -0400 | [diff] [blame] | 14 | package com.google.devtools.common.options; |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
Chloe Calvarin | 7a8bd74 | 2017-03-21 16:11:22 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; |
| 19 | import org.junit.Test; |
| 20 | import org.junit.runner.RunWith; |
| 21 | import org.junit.runners.JUnit4; |
| 22 | |
| 23 | /** Test InvocationPolicies on cases where we expect it to fail gracefully. */ |
| 24 | @RunWith(JUnit4.class) |
| 25 | public class InvocationPolicyBreakingConditionsTest extends InvocationPolicyEnforcerTestBase { |
| 26 | |
| 27 | // Useful constants |
| 28 | public static final String TEST_STRING_USER_VALUE = "user value"; |
| 29 | public static final String TEST_STRING_POLICY_VALUE = "policy value"; |
| 30 | public static final String TEST_STRING_POLICY_VALUE_2 = "policy value 2"; |
| 31 | |
| 32 | @Test |
| 33 | public void testFlagPolicyDoesNotApply() throws Exception { |
| 34 | InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder(); |
| 35 | invocationPolicyBuilder |
| 36 | .addFlagPoliciesBuilder() |
| 37 | .setFlagName("test_string") |
| 38 | .addCommands("build") |
| 39 | .getSetValueBuilder() |
| 40 | .addFlagValue(TEST_STRING_POLICY_VALUE); |
| 41 | |
| 42 | InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder); |
| 43 | parser.parse("--test_string=" + TEST_STRING_USER_VALUE); |
| 44 | |
| 45 | TestOptions testOptions = getTestOptions(); |
| 46 | assertThat(testOptions.testString).isEqualTo(TEST_STRING_USER_VALUE); |
| 47 | |
| 48 | enforcer.enforce(parser, "test"); |
| 49 | |
| 50 | // Still user value. |
| 51 | testOptions = getTestOptions(); |
| 52 | assertThat(testOptions.testString).isEqualTo(TEST_STRING_USER_VALUE); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testNonExistantFlagFromPolicy() throws Exception { |
| 57 | InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder(); |
| 58 | invocationPolicyBuilder |
| 59 | .addFlagPoliciesBuilder() |
| 60 | .setFlagName("i_do_not_exist") |
| 61 | .getSetValueBuilder() |
| 62 | .addFlagValue(TEST_STRING_POLICY_VALUE); |
| 63 | invocationPolicyBuilder |
| 64 | .addFlagPoliciesBuilder() |
| 65 | .setFlagName("test_string") |
| 66 | .getSetValueBuilder() |
| 67 | .addFlagValue(TEST_STRING_POLICY_VALUE_2); |
| 68 | |
| 69 | InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder); |
| 70 | parser.parse("--test_string=" + TEST_STRING_USER_VALUE); |
| 71 | |
| 72 | TestOptions testOptions = getTestOptions(); |
| 73 | assertThat(testOptions.testString).isEqualTo(TEST_STRING_USER_VALUE); |
| 74 | |
| 75 | enforcer.enforce(parser, "test"); |
| 76 | |
| 77 | // Still user value. |
| 78 | testOptions = getTestOptions(); |
| 79 | assertThat(testOptions.testString).isEqualTo(TEST_STRING_POLICY_VALUE_2); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testOperationNotSet() throws Exception { |
| 84 | InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder(); |
| 85 | invocationPolicyBuilder.addFlagPoliciesBuilder(); |
| 86 | // No operations added to the flag policy |
| 87 | |
| 88 | InvocationPolicyEnforcer enforcer = createOptionsPolicyEnforcer(invocationPolicyBuilder); |
| 89 | parser.parse("--test_string=" + TEST_STRING_USER_VALUE); |
| 90 | |
| 91 | TestOptions testOptions = getTestOptions(); |
| 92 | assertThat(testOptions.testString).isEqualTo(TEST_STRING_USER_VALUE); |
| 93 | |
| 94 | // Shouldn't throw. |
| 95 | enforcer.enforce(parser, "test"); |
| 96 | |
| 97 | // Still user value. |
| 98 | testOptions = getTestOptions(); |
| 99 | assertThat(testOptions.testString).isEqualTo(TEST_STRING_USER_VALUE); |
| 100 | } |
| 101 | } |