Skip to content

Commit

Permalink
Merge pull request testmycode#74 from cxcorp/mavenplugin-log-files
Browse files Browse the repository at this point in the history
Fix MavenPlugin returning empty stdout&stderr data
  • Loading branch information
nygrenh authored Dec 16, 2016
2 parents 017a5b0 + 3c0d92c commit fce2329
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public MavenExecutionResult exec(Path projectPath, String[] mavenArgs) {
invoker.setMavenHome(new File(mavenHome));

final ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
final ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
final PrintStream out = new PrintStream(outBuf);
final PrintStream err = new PrintStream(errBuf);

InvocationResult result = null;
request.setPomFile(projectPath.resolve("pom.xml").toFile());
Expand All @@ -73,18 +75,29 @@ public MavenExecutionResult exec(Path projectPath, String[] mavenArgs) {

@Override
public void consumeLine(String line) {
log.info("MavenInvokator: m{}", line);
out.append(line);
log.info("MavenInvokator: {}", line);
out.println(line);
}
});
request.setErrorHandler(
new InvocationOutputHandler() {

@Override
public void consumeLine(String line) {
log.info("MavenInvokator: {}", line);
err.println(line);
}
});

request.setGoals(Arrays.asList(mavenArgs));

MavenExecutionResult compilationResult =
new MavenExecutionResult().setStdOut(outBuf.toByteArray()).setStdErr(new byte[0]);
MavenExecutionResult compilationResult = new MavenExecutionResult();
try {
result = invoker.execute(request);
compilationResult.setExitCode(result.getExitCode());
// outBuf and errBuf are empty until invoker is executed
compilationResult.setStdOut(outBuf.toByteArray());
compilationResult.setStdErr(errBuf.toByteArray());
CommandLineException exp = result.getExecutionException();
if (exp != null) {
throw new MavenExecutorException(exp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected TestRunFileAndLogs createRunResultFile(Path path)
return new TestRunFileAndLogs(
path.toAbsolutePath().resolve(RESULT_FILE).toFile(),
result.getStdOut(),
result.getStdOut());
result.getStdErr());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -76,6 +75,19 @@ public void testRunTestsWhenBuildFailing() {
assertEquals(RunResult.Status.COMPILE_FAILED, runResult.status);
}

@Test
public void testRunTestsOutputLogIsNotEmptyWhenBuildFails() throws IOException {
Path project = TestUtils.getPath(getClass(), "failing_maven_exercise");
RunResult result = mavenPlugin.runTests(project);

byte[] stdoutBytes = result.logs.get("stdout");
String stdoutLog = new String(stdoutBytes, "utf-8");
assertTrue( "Expected stdout log of failed maven test build to contain any text",
stdoutLog.length() > 0);
assertTrue("Expected stdout log of failed maven test build to contain \"BUILD_FAILURE\"!",
stdoutLog.contains("BUILD FAILURE"));
}

@Test
public void testMavenProjectWithFailingTestsCompilesAndFailsTests() {
Path path = TestUtils.getPath(getClass(), "maven_exercise");
Expand Down

0 comments on commit fce2329

Please sign in to comment.