Skip to content

Commit fce2329

Browse files
authored
Merge pull request #74 from cxcorp/mavenplugin-log-files
Fix MavenPlugin returning empty stdout&stderr data
2 parents 017a5b0 + 3c0d92c commit fce2329

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

tmc-langs-java/src/main/java/fi/helsinki/cs/tmc/langs/java/maven/MavenInvokatorMavenTaskRunner.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public MavenExecutionResult exec(Path projectPath, String[] mavenArgs) {
6363
invoker.setMavenHome(new File(mavenHome));
6464

6565
final ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
66+
final ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
6667
final PrintStream out = new PrintStream(outBuf);
68+
final PrintStream err = new PrintStream(errBuf);
6769

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

7476
@Override
7577
public void consumeLine(String line) {
76-
log.info("MavenInvokator: m{}", line);
77-
out.append(line);
78+
log.info("MavenInvokator: {}", line);
79+
out.println(line);
80+
}
81+
});
82+
request.setErrorHandler(
83+
new InvocationOutputHandler() {
84+
85+
@Override
86+
public void consumeLine(String line) {
87+
log.info("MavenInvokator: {}", line);
88+
err.println(line);
7889
}
7990
});
8091

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

83-
MavenExecutionResult compilationResult =
84-
new MavenExecutionResult().setStdOut(outBuf.toByteArray()).setStdErr(new byte[0]);
94+
MavenExecutionResult compilationResult = new MavenExecutionResult();
8595
try {
8696
result = invoker.execute(request);
8797
compilationResult.setExitCode(result.getExitCode());
98+
// outBuf and errBuf are empty until invoker is executed
99+
compilationResult.setStdOut(outBuf.toByteArray());
100+
compilationResult.setStdErr(errBuf.toByteArray());
88101
CommandLineException exp = result.getExecutionException();
89102
if (exp != null) {
90103
throw new MavenExecutorException(exp);

tmc-langs-java/src/main/java/fi/helsinki/cs/tmc/langs/java/maven/MavenPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected TestRunFileAndLogs createRunResultFile(Path path)
106106
return new TestRunFileAndLogs(
107107
path.toAbsolutePath().resolve(RESULT_FILE).toFile(),
108108
result.getStdOut(),
109-
result.getStdOut());
109+
result.getStdErr());
110110
}
111111

112112
@Override

tmc-langs-java/src/test/java/fi/helsinki/cs/tmc/langs/java/maven/MavenPluginTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import java.io.File;
1818
import java.io.IOException;
19-
import java.io.UnsupportedEncodingException;
2019
import java.nio.file.Path;
2120
import java.util.List;
2221
import java.util.Locale;
@@ -76,6 +75,19 @@ public void testRunTestsWhenBuildFailing() {
7675
assertEquals(RunResult.Status.COMPILE_FAILED, runResult.status);
7776
}
7877

78+
@Test
79+
public void testRunTestsOutputLogIsNotEmptyWhenBuildFails() throws IOException {
80+
Path project = TestUtils.getPath(getClass(), "failing_maven_exercise");
81+
RunResult result = mavenPlugin.runTests(project);
82+
83+
byte[] stdoutBytes = result.logs.get("stdout");
84+
String stdoutLog = new String(stdoutBytes, "utf-8");
85+
assertTrue( "Expected stdout log of failed maven test build to contain any text",
86+
stdoutLog.length() > 0);
87+
assertTrue("Expected stdout log of failed maven test build to contain \"BUILD_FAILURE\"!",
88+
stdoutLog.contains("BUILD FAILURE"));
89+
}
90+
7991
@Test
8092
public void testMavenProjectWithFailingTestsCompilesAndFailsTests() {
8193
Path path = TestUtils.getPath(getClass(), "maven_exercise");

0 commit comments

Comments
 (0)