blob: b1970b5b296de4f78f4ebedf96a0921e6e7aa0ba [file] [log] [blame]
jcatera4d93ca2019-12-02 07:30:47 -08001// 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.
14package com.google.devtools.build.lib.runtime;
15
16import static com.google.common.truth.Truth.assertThat;
17
18import com.google.devtools.build.lib.bazel.BazelStartupOptionsModule.Options;
jcatera1033c62019-12-02 13:15:38 -080019import com.google.devtools.build.lib.runtime.CommandLineEvent.CanonicalCommandLineEvent;
jcatera4d93ca2019-12-02 07:30:47 -080020import com.google.devtools.build.lib.runtime.CommandLineEvent.OriginalCommandLineEvent;
21import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.CommandLine;
laurentlbc67c5702020-07-30 09:25:45 -070022import com.google.devtools.build.lib.starlark.util.StarlarkOptionsTestCase;
jcatera4d93ca2019-12-02 07:30:47 -080023import com.google.devtools.common.options.OptionsParser;
24import java.util.Optional;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import 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)
34public class StarlarkOptionCommandLineEventTest extends StarlarkOptionsTestCase {
35
36 @Test
jcatera1033c62019-12-02 13:15:38 -080037 public void testStarlarkOptions_original() throws Exception {
jcatera4d93ca2019-12-02 07:30:47 -080038 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
jcatera1033c62019-12-02 13:15:38 -080064 @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 }
jcatera4d93ca2019-12-02 07:30:47 -0800116}