Skip to content

Commit

Permalink
Add Starlark flags to BEP output.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 283336349
  • Loading branch information
katre authored and copybara-github committed Dec 2, 2019
1 parent 452355f commit a4d93ca
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ private Option createOption(
return option.build();
}

Option createStarlarkOption(String starlarkFlag, @Nullable Object value) {
String combinedForm = String.format("--%s=%s", starlarkFlag, value);
Option.Builder option = Option.newBuilder();
option.setCombinedForm(combinedForm);
option.setOptionName(starlarkFlag);
if (value != null) {
option.setOptionValue(String.valueOf(value));
}
return option.build();
}

/**
* Returns the startup option section of the command line for the startup options as the server
* received them at its startup. Since not all client options get passed to the server as
Expand Down Expand Up @@ -251,11 +262,16 @@ private CommandLineSection getExplicitCommandOptions() {
parsedOptionDescription.getPriority().getPriorityCategory()
== OptionPriority.PriorityCategory.COMMAND_LINE)
.collect(Collectors.toList());
List<Option> starlarkOptions =
commandOptions.getStarlarkOptions().entrySet().stream()
.map(e -> createStarlarkOption(e.getKey(), e.getValue()))
.collect(Collectors.toList());
return CommandLineSection.newBuilder()
.setSectionLabel("command options")
.setOptionList(
OptionList.newBuilder()
.addAllOption(getOptionListFromParsedOptionDescriptions(explicitOptions)))
.addAllOption(getOptionListFromParsedOptionDescriptions(explicitOptions))
.addAllOption(starlarkOptions))
.build();
}

Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ java_test(
"//src/main/protobuf:command_line_java_proto",
"//src/main/protobuf:invocation_policy_java_proto",
"//src/main/protobuf:test_status_java_proto",
"//src/test/java/com/google/devtools/build/lib/skylark:testutil",
"//src/test/java/com/google/devtools/common/options:testutils",
"//third_party:apache_commons_lang",
"//third_party:mockito",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.runtime;

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.bazel.BazelStartupOptionsModule.Options;
import com.google.devtools.build.lib.runtime.CommandLineEvent.OriginalCommandLineEvent;
import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.CommandLine;
import com.google.devtools.build.lib.skylark.util.StarlarkOptionsTestCase;
import com.google.devtools.common.options.OptionsParser;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests for {@link CommandLineEvent}'s construction of the command lines which contain
* Starlark-style flags.
*/
@RunWith(JUnit4.class)
public class StarlarkOptionCommandLineEventTest extends StarlarkOptionsTestCase {

@Test
public void testStarlarkOptions() throws Exception {
OptionsParser fakeStartupOptions =
OptionsParser.builder()
.optionsClasses(BlazeServerStartupOptions.class, Options.class)
.build();

writeBasicIntFlag();

parseStarlarkOptions("--//test:my_int_setting=666");

CommandLine line =
new OriginalCommandLineEvent(
"testblaze", fakeStartupOptions, "someCommandName", optionsParser, Optional.empty())
.asStreamProto(null)
.getStructuredCommandLine();

// Command options should appear in section 3. See
// CommandLineEventTest#testOptionsAtVariousPriorities_OriginalCommandLine.
// Verify that the starlark flag was processed as expected.
assertThat(line.getSections(3).getOptionList().getOptionCount()).isEqualTo(1);
assertThat(line.getSections(3).getOptionList().getOption(0).getCombinedForm())
.isEqualTo("--//test:my_int_setting=666");
assertThat(line.getSections(3).getOptionList().getOption(0).getOptionName())
.isEqualTo("//test:my_int_setting");
assertThat(line.getSections(3).getOptionList().getOption(0).getOptionValue()).isEqualTo("666");
}

// TODO(katre): Test mix of regular and starlark flags.
}
28 changes: 26 additions & 2 deletions src/test/shell/integration/build_event_stream_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# An end-to-end test that Bazel's experimental UI produces reasonable output.

# Load the test setup defined in the parent directory
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Expand Down Expand Up @@ -944,4 +942,30 @@ function test_bep_report_only_important_artifacts() {
rm bep.txt
}

function test_starlark_flags() {
cat >> build_setting.bzl <<EOF
def _build_setting_impl(ctx):
return []
int_setting = rule(
implementation = _build_setting_impl,
build_setting = config.int(flag=True)
)
EOF
cat >> BUILD <<EOF
load('//:build_setting.bzl', 'int_setting')
int_setting(name = 'my_int_setting',
build_setting_default = 42,
)
EOF

bazel build --build_event_text_file=bep.txt \
--//:my_int_setting=666 \
//pkg:true || fail "Build failed but should have succeeded"
cat bep.txt >> "$TEST_log"
rm bep.txt

expect_log 'option_name: "//:my_int_setting"'
expect_log 'option_value: "666"'
}

run_suite "Integration tests for the build event stream"

0 comments on commit a4d93ca

Please sign in to comment.