Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,20 @@ private ProcessResult run(String... commands) throws IOException, InterruptedExc
}

private String processOutputFrom(Process process) {
return asReader(process.getInputStream())
String stdout = asReader(process.getInputStream())
.lines()
.peek(logConsumer)
.collect(Collectors.joining(System.lineSeparator()));

String stderr = asReader(process.getErrorStream())
.lines()
.peek(logConsumer)
.collect(Collectors.joining(System.lineSeparator()));

return String.format(
"STDERR: %s\n\n------\n\nSTDOUT: %s\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just use multi line strings here?

stderr.isEmpty() ? "<empty>" : stderr,
stdout.isEmpty() ? "<empty>" : stdout);
}

private static String waitForResultFrom(Future<String> outputProcessing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void before() throws IOException {
dockerComposeCommand = new Command(dockerComposeExecutable, logConsumer);

givenTheUnderlyingProcessHasOutput("");
givenTheUnderlyingProcessHasError("");
givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(0);
}

Expand All @@ -72,7 +73,7 @@ public void return_output_when_exit_code_of_the_executed_process_is_non_0()
givenTheUnderlyingProcessHasOutput(expectedOutput);
String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");

assertThat(commandOutput).isEqualTo(expectedOutput);
assertThat(commandOutput).isEqualTo("STDERR: <empty>\n\n------\n\nSTDOUT: " + expectedOutput + "\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with multi line strings you should be able to do something like:

assertThat(commandOutput).isEqualTo(
  """
  STDERR: <empty>
  
  ------
  
  STDOUT:  %s
  """, expectedOutput);

assertj has some nice utility methods to do string formatting inside

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also gives me a way to see what the actual error message would look like.

So much so, I would actually just put the content for each stream on a new line

e.g.

STDERR:
<empty>

------
STDOUT:
%s

}

@Test
Expand All @@ -81,7 +82,29 @@ public void return_output_when_exit_code_of_the_executed_process_is_0() throws I
givenTheUnderlyingProcessHasOutput(expectedOutput);
String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");

assertThat(commandOutput).isEqualTo(expectedOutput);
assertThat(commandOutput).isEqualTo("STDERR: <empty>\n\n------\n\nSTDOUT: " + expectedOutput + "\n");
}

@Test
public void return_output_when_only_stderr_is_present() throws IOException, InterruptedException {
String expectedStderr = "error output";
givenTheUnderlyingProcessHasError(expectedStderr);
String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");

assertThat(commandOutput)
.isEqualTo("STDERR: " + expectedStderr + "\n\n------\n\nSTDOUT: <empty>\n");
}

@Test
public void return_output_containing_both_stderr_and_stdout() throws IOException, InterruptedException {
String expectedStdout = "standard output";
String expectedStderr = "error output";
givenTheUnderlyingProcessHasOutput(expectedStdout);
givenTheUnderlyingProcessHasError(expectedStderr);
String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");

assertThat(commandOutput)
.isEqualTo("STDERR: " + expectedStderr + "\n\n------\n\nSTDOUT: " + expectedStdout + "\n");
}

@Test
Expand All @@ -94,6 +117,16 @@ public void give_the_output_to_the_specified_consumer_as_it_is_available()
assertThat(consumedLogLines).containsExactly("line 1", "line 2");
}

@Test
public void give_both_stdout_and_stderr_to_the_specified_consumer() throws IOException, InterruptedException {
givenTheUnderlyingProcessHasOutput("stdout line");
givenTheUnderlyingProcessHasError("stderr line");

dockerComposeCommand.execute(errorHandler, "rm", "-f");

assertThat(consumedLogLines).containsExactly("stdout line", "stderr line");
}

// flaky test: https://circleci.com/gh/palantir/docker-compose-rule/378, 370, 367, 366
@Ignore
@Test
Expand All @@ -110,6 +143,10 @@ private void givenTheUnderlyingProcessHasOutput(String output) {
when(executedProcess.getInputStream()).thenReturn(toInputStream(output));
}

private void givenTheUnderlyingProcessHasError(String error) {
when(executedProcess.getErrorStream()).thenReturn(toInputStream(error));
}

private void givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(int exitCode) {
when(executedProcess.exitValue()).thenReturn(exitCode);
}
Expand Down