Skip to content

Commit 0ff19c6

Browse files
ulfjackcopybara-github
authored andcommitted
Fix StandaloneTestStrategy.appendStderr
As of 4a5e1b7, it was using getErrorPath twice, which could cause it to loop indefinitely, trying to append the error file to itself. This happened rarely, as the test runner script redirects stderr to stdout. However, it could happen if the SpawnRunner wrote any extra output to stderr, which the RemoteSpawnRunner does in some cases. I have manually checked that this fixes the issue, and also added a regression test. Fixes bazelbuild#8320. PiperOrigin-RevId: 249258656
1 parent 6c0dc36 commit 0ff19c6

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private static void appendStderr(FileOutErr outErr) throws IOException {
323323
if (stat != null) {
324324
try {
325325
if (stat.getSize() > 0) {
326-
Path stdOut = outErr.getErrorPath();
326+
Path stdOut = outErr.getOutputPath();
327327
if (stdOut.exists()) {
328328
stdOut.setWritable(true);
329329
}

src/test/java/com/google/devtools/build/lib/exec/StandaloneTestStrategyTest.java

+45
Original file line numberDiff line numberDiff line change
@@ -655,4 +655,49 @@ public void testEmptyOutputCreatesEmptyLogFile() throws Exception {
655655
}
656656
assertThat(outErr.getErrorPath().exists()).isFalse();
657657
}
658+
659+
@Test
660+
public void testAppendStdErrDoesNotBusyLoop() throws Exception {
661+
ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class);
662+
executionOptions.testOutput = TestOutputFormat.ALL;
663+
Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions);
664+
BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools());
665+
TestedStandaloneTestStrategy standaloneTestStrategy =
666+
new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot);
667+
668+
// setup a test action
669+
scratch.file("standalone/empty_test.sh", "this does not get executed, it is mocked out");
670+
scratch.file(
671+
"standalone/BUILD",
672+
"sh_test(",
673+
" name = \"empty_test\",",
674+
" size = \"small\",",
675+
" srcs = [\"empty_test.sh\"],",
676+
")");
677+
TestRunnerAction testRunnerAction = getTestAction("//standalone:empty_test");
678+
679+
SpawnResult expectedSpawnResult =
680+
new SpawnResult.Builder().setStatus(Status.SUCCESS).setRunnerName("test").build();
681+
when(spawnActionContext.beginExecution(any(), any()))
682+
.then(
683+
(invocation) -> {
684+
((ActionExecutionContext) invocation.getArgument(1)).getFileOutErr().printErr("Foo");
685+
return SpawnContinuation.immediate(expectedSpawnResult);
686+
});
687+
688+
FileOutErr outErr = createTempOutErr(tmpDirRoot);
689+
ActionExecutionContext actionExecutionContext =
690+
new FakeActionExecutionContext(outErr, spawnActionContext);
691+
692+
// actual StandaloneTestStrategy execution
693+
execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy);
694+
695+
// check that the test stdout contains all the expected output
696+
try {
697+
String outData = FileSystemUtils.readContent(outErr.getOutputPath(), UTF_8);
698+
assertThat(outData).contains("Foo");
699+
} catch (IOException e) {
700+
fail("Test stdout file missing: " + outErr.getOutputPath());
701+
}
702+
}
658703
}

0 commit comments

Comments
 (0)