aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java
diff options
context:
space:
mode:
authorGravatar ccalvarin <ccalvarin@google.com>2017-10-16 19:21:08 +0200
committerGravatar Jakob Buchgraber <buchgr@google.com>2017-10-18 10:27:48 +0200
commit93c080ad752d029f9f9f9466414430d609105257 (patch)
treeeec7484a61c401779372276a243dde765719a27e /src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java
parent044bc6df1ab73206a955229bdd420795fb7fc2da (diff)
Accept command lines from tools invoking Bazel.
For tools that wrap Bazel in some way, the original way that the tool was invoked can be a useful piece of information to track when logging what Bazel did and why. In order to output this information in the same way that Bazel outputs its command lines, we accept --tool_command_line in the structure command line format that Bazel uses in the BEP. These structured command lines are protos that we expect as a base64 encoded byte array. For simple scripts that wish to use this feature without compiling the proto, we will also accept any old string (that cannot be interpreted as a base64 encoding) as a single "chunk" in a structured command line. This is experimental for now and users should not get attached to the format. We will remove the experimental_ prefix when it is stable. RELNOTES: None. PiperOrigin-RevId: 172341216
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java b/src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java
index be02e6dcba..44724f1166 100644
--- a/src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java
+++ b/src/test/java/com/google/devtools/build/lib/runtime/CommandLineEventTest.java
@@ -16,9 +16,16 @@ package com.google.devtools.build.lib.runtime;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableList;
+import com.google.common.io.BaseEncoding;
+import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.StructuredCommandLineId;
import com.google.devtools.build.lib.runtime.CommandLineEvent.CanonicalCommandLineEvent;
import com.google.devtools.build.lib.runtime.CommandLineEvent.OriginalCommandLineEvent;
+import com.google.devtools.build.lib.runtime.CommandLineEvent.ToolCommandLineEvent;
+import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.ChunkList;
import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.CommandLine;
+import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.CommandLineSection;
+import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.CommandLineSection.SectionTypeCase;
+import com.google.devtools.build.lib.runtime.proto.CommandLineOuterClass.OptionList;
import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.common.options.OptionPriority;
import com.google.devtools.common.options.OptionsParser;
@@ -425,4 +432,86 @@ public class CommandLineEventTest {
.isEqualTo("--test_implicit_requirement=foo");
assertThat(line.getSections(4).getChunkList().getChunkCount()).isEqualTo(0);
}
+
+ @Test
+ public void testDefaultToolCommandLine() throws OptionsParsingException {
+ OptionsParser parser = OptionsParser.newOptionsParser(CommonCommandOptions.class);
+ ToolCommandLineEvent event = parser.getOptions(CommonCommandOptions.class).toolCommandLine;
+ // Test that the actual default value is an empty command line.
+ assertThat(event.asStreamProto(null).getStructuredCommandLine())
+ .isEqualTo(CommandLine.getDefaultInstance());
+ }
+
+ @Test
+ public void testLabelessParsingOfCompiledToolCommandLine() throws OptionsParsingException {
+ OptionsParser parser = OptionsParser.newOptionsParser(CommonCommandOptions.class);
+ CommandLine original =
+ CommandLine.newBuilder().addSections(CommandLineSection.getDefaultInstance()).build();
+ parser.parse(
+ "--experimental_tool_command_line=" + BaseEncoding.base64().encode(original.toByteArray()));
+
+ ToolCommandLineEvent event = parser.getOptions(CommonCommandOptions.class).toolCommandLine;
+ StructuredCommandLineId id = event.getEventId().asStreamProto().getStructuredCommandLine();
+ CommandLine line = event.asStreamProto(null).getStructuredCommandLine();
+
+ assertThat(id.getCommandLineLabel()).isEqualTo("tool");
+ assertThat(line.getSectionsCount()).isEqualTo(1);
+ }
+
+ @Test
+ public void testParsingOfCompiledToolCommandLine() throws OptionsParsingException {
+ OptionsParser parser = OptionsParser.newOptionsParser(CommonCommandOptions.class);
+ CommandLine original =
+ CommandLine.newBuilder()
+ .setCommandLineLabel("something meaningful")
+ .addSections(
+ CommandLineSection.newBuilder()
+ .setSectionLabel("command")
+ .setChunkList(ChunkList.newBuilder().addChunk("aCommand")))
+ .addSections(
+ CommandLineSection.newBuilder()
+ .setSectionLabel("someArguments")
+ .setChunkList(ChunkList.newBuilder().addChunk("arg1").addChunk("arg2")))
+ .addSections(
+ CommandLineSection.newBuilder()
+ .setSectionLabel("someOptions")
+ .setOptionList(OptionList.getDefaultInstance()))
+ .build();
+ parser.parse(
+ "--experimental_tool_command_line=" + BaseEncoding.base64().encode(original.toByteArray()));
+
+ ToolCommandLineEvent event = parser.getOptions(CommonCommandOptions.class).toolCommandLine;
+ StructuredCommandLineId id = event.getEventId().asStreamProto().getStructuredCommandLine();
+ CommandLine line = event.asStreamProto(null).getStructuredCommandLine();
+
+ assertThat(id.getCommandLineLabel()).isEqualTo("tool");
+ assertThat(line.getCommandLineLabel()).isEqualTo("something meaningful");
+ assertThat(line.getSectionsCount()).isEqualTo(3);
+ assertThat(line.getSections(0).getSectionTypeCase()).isEqualTo(SectionTypeCase.CHUNK_LIST);
+ assertThat(line.getSections(0).getChunkList().getChunkCount()).isEqualTo(1);
+ assertThat(line.getSections(0).getChunkList().getChunk(0)).isEqualTo("aCommand");
+ assertThat(line.getSections(1).getSectionTypeCase()).isEqualTo(SectionTypeCase.CHUNK_LIST);
+ assertThat(line.getSections(1).getChunkList().getChunkCount()).isEqualTo(2);
+ assertThat(line.getSections(1).getChunkList().getChunk(0)).isEqualTo("arg1");
+ assertThat(line.getSections(1).getChunkList().getChunk(1)).isEqualTo("arg2");
+ assertThat(line.getSections(2).getSectionTypeCase()).isEqualTo(SectionTypeCase.OPTION_LIST);
+ assertThat(line.getSections(2).getOptionList().getOptionCount()).isEqualTo(0);
+ }
+
+ @Test
+ public void testSimpleStringToolCommandLine() throws OptionsParsingException {
+ OptionsParser parser = OptionsParser.newOptionsParser(CommonCommandOptions.class);
+ parser.parse("--experimental_tool_command_line=The quick brown fox jumps over the lazy dog");
+
+ ToolCommandLineEvent event = parser.getOptions(CommonCommandOptions.class).toolCommandLine;
+ StructuredCommandLineId id = event.getEventId().asStreamProto().getStructuredCommandLine();
+ CommandLine line = event.asStreamProto(null).getStructuredCommandLine();
+
+ assertThat(id.getCommandLineLabel()).isEqualTo("tool");
+ assertThat(line.getCommandLineLabel()).isEqualTo("tool");
+ assertThat(line.getSectionsCount()).isEqualTo(1);
+ assertThat(line.getSections(0).getSectionTypeCase()).isEqualTo(SectionTypeCase.CHUNK_LIST);
+ assertThat(line.getSections(0).getChunkList().getChunk(0))
+ .isEqualTo("The quick brown fox jumps over the lazy dog");
+ }
}