jcater | a4d93ca | 2019-12-02 07:30:47 -0800 | [diff] [blame] | 1 | // Copyright 2019 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. |
| 14 | package com.google.devtools.build.lib.runtime; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import com.google.devtools.build.lib.bazel.BazelStartupOptionsModule.Options; |
jcater | a1033c6 | 2019-12-02 13:15:38 -0800 | [diff] [blame] | 19 | import com.google.devtools.build.lib.runtime.CommandLineEvent.CanonicalCommandLineEvent; |
jcater | a4d93ca | 2019-12-02 07:30:47 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.runtime.CommandLineEvent.OriginalCommandLineEvent; |
| 21 | import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.CommandLine; |
laurentlb | c67c570 | 2020-07-30 09:25:45 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.starlark.util.StarlarkOptionsTestCase; |
jcater | a4d93ca | 2019-12-02 07:30:47 -0800 | [diff] [blame] | 23 | import com.google.devtools.common.options.OptionsParser; |
| 24 | import java.util.Optional; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.junit.runners.JUnit4; |
| 28 | |
| 29 | /** |
| 30 | * Tests for {@link CommandLineEvent}'s construction of the command lines which contain |
| 31 | * Starlark-style flags. |
| 32 | */ |
| 33 | @RunWith(JUnit4.class) |
| 34 | public class StarlarkOptionCommandLineEventTest extends StarlarkOptionsTestCase { |
| 35 | |
| 36 | @Test |
jcater | a1033c6 | 2019-12-02 13:15:38 -0800 | [diff] [blame] | 37 | public void testStarlarkOptions_original() throws Exception { |
jcater | a4d93ca | 2019-12-02 07:30:47 -0800 | [diff] [blame] | 38 | OptionsParser fakeStartupOptions = |
| 39 | OptionsParser.builder() |
| 40 | .optionsClasses(BlazeServerStartupOptions.class, Options.class) |
| 41 | .build(); |
| 42 | |
| 43 | writeBasicIntFlag(); |
| 44 | |
| 45 | parseStarlarkOptions("--//test:my_int_setting=666"); |
| 46 | |
| 47 | CommandLine line = |
| 48 | new OriginalCommandLineEvent( |
| 49 | "testblaze", fakeStartupOptions, "someCommandName", optionsParser, Optional.empty()) |
| 50 | .asStreamProto(null) |
| 51 | .getStructuredCommandLine(); |
| 52 | |
| 53 | // Command options should appear in section 3. See |
| 54 | // CommandLineEventTest#testOptionsAtVariousPriorities_OriginalCommandLine. |
| 55 | // Verify that the starlark flag was processed as expected. |
| 56 | assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(1); |
| 57 | assertThat(line.getSections(3).getOptionList().getOption(0).getCombinedForm()) |
| 58 | .isEqualTo("--//test:my_int_setting=666"); |
| 59 | assertThat(line.getSections(3).getOptionList().getOption(0).getOptionName()) |
| 60 | .isEqualTo("//test:my_int_setting"); |
| 61 | assertThat(line.getSections(3).getOptionList().getOption(0).getOptionValue()).isEqualTo("666"); |
| 62 | } |
| 63 | |
jcater | a1033c6 | 2019-12-02 13:15:38 -0800 | [diff] [blame] | 64 | @Test |
| 65 | public void testStarlarkOptions_canonical() throws Exception { |
| 66 | OptionsParser fakeStartupOptions = |
| 67 | OptionsParser.builder() |
| 68 | .optionsClasses(BlazeServerStartupOptions.class, Options.class) |
| 69 | .build(); |
| 70 | |
| 71 | writeBasicIntFlag(); |
| 72 | |
| 73 | parseStarlarkOptions("--//test:my_int_setting=666"); |
| 74 | |
| 75 | CommandLine line = |
| 76 | new CanonicalCommandLineEvent( |
| 77 | "testblaze", fakeStartupOptions, "someCommandName", optionsParser) |
| 78 | .asStreamProto(null) |
| 79 | .getStructuredCommandLine(); |
| 80 | assertThat(line.getCommandLineLabel()).isEqualTo("canonical"); |
| 81 | |
| 82 | // Command options should appear in section 3. See |
| 83 | // CommandLineEventTest#testOptionsAtVariousPriorities_OriginalCommandLine. |
| 84 | // Verify that the starlark flag was processed as expected. |
| 85 | assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(1); |
| 86 | assertThat(line.getSections(3).getOptionList().getOption(0).getCombinedForm()) |
| 87 | .isEqualTo("--//test:my_int_setting=666"); |
| 88 | assertThat(line.getSections(3).getOptionList().getOption(0).getOptionName()) |
| 89 | .isEqualTo("//test:my_int_setting"); |
| 90 | assertThat(line.getSections(3).getOptionList().getOption(0).getOptionValue()).isEqualTo("666"); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testStarlarkOptions_canonical_defaultValue() throws Exception { |
| 95 | OptionsParser fakeStartupOptions = |
| 96 | OptionsParser.builder() |
| 97 | .optionsClasses(BlazeServerStartupOptions.class, Options.class) |
| 98 | .build(); |
| 99 | |
| 100 | writeBasicIntFlag(); |
| 101 | |
| 102 | parseStarlarkOptions("--//test:my_int_setting=42"); |
| 103 | |
| 104 | CommandLine line = |
| 105 | new CanonicalCommandLineEvent( |
| 106 | "testblaze", fakeStartupOptions, "someCommandName", optionsParser) |
| 107 | .asStreamProto(null) |
| 108 | .getStructuredCommandLine(); |
| 109 | assertThat(line.getCommandLineLabel()).isEqualTo("canonical"); |
| 110 | |
| 111 | // Command options should appear in section 3. See |
| 112 | // CommandLineEventTest#testOptionsAtVariousPriorities_OriginalCommandLine. |
| 113 | // Verify that the starlark flag was processed as expected. |
| 114 | assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(0); |
| 115 | } |
jcater | a4d93ca | 2019-12-02 07:30:47 -0800 | [diff] [blame] | 116 | } |