fix tests

This commit is contained in:
Clément Fournier
2022-07-27 15:05:20 +02:00
parent 815cc2912e
commit acc4979a85
4 changed files with 22 additions and 28 deletions

View File

@@ -58,6 +58,11 @@
<artifactId>system-rules</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>

View File

@@ -8,14 +8,12 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.regex.Pattern;
import org.apache.tools.ant.util.TeeOutputStream;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
@@ -28,6 +26,8 @@ import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMD.StatusCode;
import net.sourceforge.pmd.internal.util.AssertionUtil;
import com.github.stefanbirkner.systemlambda.SystemLambda;
/**
* @author Romain Pelisse &lt;belaran@gmail.com&gt;
*
@@ -121,15 +121,18 @@ public abstract class BaseCLITest {
}
protected String runTest(StatusCode expectedExitCode, String... args) {
ByteArrayOutputStream console = new ByteArrayOutputStream();
PrintStream out = new PrintStream(new TeeOutputStream(console, System.out));
PrintStream err = new PrintStream(new TeeOutputStream(console, System.err));
System.setOut(out);
System.setErr(err);
StatusCode statusCode = PMD.runPmd(args);
assertEquals(expectedExitCode, statusCode);
return console.toString();
try {
return SystemLambda.tapSystemErrAndOut(() -> {
// restoring system properties: -debug might change logging properties
// See Slf4jSimpleConfigurationForAnt and resetLogging
SystemLambda.restoreSystemProperties(() -> {
StatusCode statusCode = PMD.runPmd(args);
assertEquals(expectedExitCode, statusCode);
});
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**