mirror of
https://github.com/ethauvin/bld.git
synced 2025-04-25 00:07:12 -07:00
Capture and check stdout in tests
This commit is contained in:
parent
f6aa5258ef
commit
7a946b17d8
3 changed files with 149 additions and 100 deletions
|
@ -5,18 +5,29 @@
|
|||
|
||||
package rife.bld.operations;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import rife.bld.operations.exceptions.ExitStatusException;
|
||||
import rife.tools.FileUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestJlinkOperation {
|
||||
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||
private final PrintStream stdout = System.out;
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
System.setOut(stdout);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArguments() {
|
||||
var args = new HashMap<String, String>();
|
||||
|
@ -66,64 +77,76 @@ public class TestJlinkOperation {
|
|||
|
||||
@Test
|
||||
void testDisablePlugin() {
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jlink = new JlinkOperation()
|
||||
.disablePlugin("vm")
|
||||
.disablePlugin("system-modules")
|
||||
.listPlugins();
|
||||
assertDoesNotThrow(jlink::execute);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.contains("List of available plugins:"), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExecute() throws IOException {
|
||||
var tmpdir = Files.createTempDirectory("bld-jlink-test").toFile();
|
||||
tmpdir.deleteOnExit();
|
||||
try {
|
||||
var output = new File(tmpdir, "jlink");
|
||||
|
||||
var output = new File(tmpdir, "jlink");
|
||||
output.deleteOnExit();
|
||||
var options = new JlinkOptions()
|
||||
.modulePath("src/test/resources/jlink/build/jmod")
|
||||
.addModules("dev.mccue.tree")
|
||||
.launcher("tree", "dev.mccue.tree", "dev.mccue.tree.Tree")
|
||||
.output(output.getAbsolutePath());
|
||||
var jlink = new JlinkOperation().jlinkOptions(options);
|
||||
|
||||
var options = new JlinkOptions()
|
||||
.modulePath("src/test/resources/jlink/build/jmod")
|
||||
.addModules("dev.mccue.tree")
|
||||
.launcher("tree", "dev.mccue.tree", "dev.mccue.tree.Tree")
|
||||
.output(output.getAbsolutePath());
|
||||
var jlink = new JlinkOperation().jlinkOptions(options);
|
||||
assertDoesNotThrow(jlink::execute);
|
||||
assertTrue(output.exists(), "Output dir does not exist");
|
||||
} finally {
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFileOptions() {
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jlink = new JlinkOperation().fileOptions("src/test/resources/jlink/options_verbose.txt",
|
||||
"src/test/resources/jlink/options_version.txt");
|
||||
assertDoesNotThrow(jlink::execute);
|
||||
assertTrue(output.exists(), "Output dir does not exist");
|
||||
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.matches("\\d+.\\d+.\\d+[\\r\\n]+"), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHelp() {
|
||||
var jlink = new JlinkOperation().addArgs("--help");
|
||||
var jlink = new JlinkOperation().toolArgs("--help");
|
||||
assertDoesNotThrow(jlink::execute);
|
||||
assertTrue(jlink.toolArgs().isEmpty(), "args not empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNoArguments() {
|
||||
var jlink = new JlinkOperation();
|
||||
assertTrue(jlink.jlinkOptions().isEmpty(), "jlink options not empty");
|
||||
assertTrue(jlink.options().isEmpty(), "options not empty");
|
||||
assertTrue(jlink.fileOptions().isEmpty(), "file options not empty");
|
||||
assertThrows(ExitStatusException.class, jlink::execute);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOptions() {
|
||||
var jlink = new JlinkOperation().options("src/test/resources/jlink/options_verbose.txt",
|
||||
"src/test/resources/jlink/options_version.txt");
|
||||
assertDoesNotThrow(jlink::execute);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseOptions() {
|
||||
var jlink = new JlinkOperation().options("src/test/resources/jlink/options_jlink.txt");
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jlink = new JlinkOperation().fileOptions("src/test/resources/jlink/options_jlink.txt");
|
||||
assertDoesNotThrow(jlink::execute);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.contains("List of available plugins:"), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testVersion() {
|
||||
var jlink = new JlinkOperation().addArgs("--verbose", "--version");
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jlink = new JlinkOperation().toolArgs("--verbose", "--version");
|
||||
assertDoesNotThrow(jlink::execute);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.matches("\\d+.\\d+.\\d+[\\r\\n]+"), out);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,12 +5,15 @@
|
|||
|
||||
package rife.bld.operations;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import rife.bld.operations.exceptions.ExitStatusException;
|
||||
import rife.tools.FileUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
@ -20,6 +23,14 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||
import static rife.bld.operations.JmodOperation.OperationMode;
|
||||
|
||||
public class TestJmodOperation {
|
||||
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||
private final PrintStream stdout = System.out;
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
System.setOut(stdout);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArguments() {
|
||||
var args = new HashMap<String, String>();
|
||||
|
@ -76,83 +87,81 @@ public class TestJmodOperation {
|
|||
@Test
|
||||
void testCreate() throws IOException {
|
||||
var tmpdir = Files.createTempDirectory("bld-jmod-test").toFile();
|
||||
tmpdir.deleteOnExit();
|
||||
try {
|
||||
var mod = new File(tmpdir, "dev.mccue.tree.jmod");
|
||||
|
||||
var mod = new File(tmpdir, "dev.mccue.tree.jmod");
|
||||
mod.deleteOnExit();
|
||||
var options = new JmodOptions()
|
||||
.legalNotices("src/test/resources/jlink/dev.mccue.apple/legal")
|
||||
.classpath("src/test/resources/jlink/build/jar/dev.mccue.apple.jar");
|
||||
var jmod = new JmodOperation()
|
||||
.operationMode(OperationMode.CREATE)
|
||||
.jmodFile(mod.getAbsolutePath())
|
||||
.jmodOptions(options);
|
||||
|
||||
var options = new JmodOptions()
|
||||
.legalNotices("src/test/resources/jlink/dev.mccue.apple/legal")
|
||||
.classpath("src/test/resources/jlink/build/jar/dev.mccue.apple.jar");
|
||||
var jmod = new JmodOperation()
|
||||
.operationMode(OperationMode.CREATE)
|
||||
.jmodFile(mod.getAbsolutePath())
|
||||
.jmodOptions(options);
|
||||
|
||||
assertDoesNotThrow(jmod::execute);
|
||||
assertTrue(mod.exists(), "mod does not exist");
|
||||
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
assertDoesNotThrow(jmod::execute);
|
||||
assertTrue(mod.exists(), "mod does not exist");
|
||||
} finally {
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExecute() throws IOException {
|
||||
var tmpdir = Files.createTempDirectory("bld-jmod-test").toFile();
|
||||
tmpdir.deleteOnExit();
|
||||
try {
|
||||
var mod = new File(tmpdir, "dev.mccue.tree.jmod");
|
||||
|
||||
var mod = new File(tmpdir, "dev.mccue.tree.jmod");
|
||||
mod.deleteOnExit();
|
||||
var options = new JmodOptions().classpath("src/test/resources/jlink/build/jar/dev.mccue.tree.jar");
|
||||
var jmod = new JmodOperation()
|
||||
.operationMode(OperationMode.CREATE)
|
||||
.jmodFile(mod.getAbsolutePath())
|
||||
.jmodOptions(options);
|
||||
|
||||
var options = new JmodOptions().classpath("src/test/resources/jlink/build/jar/dev.mccue.tree.jar");
|
||||
var jmod = new JmodOperation()
|
||||
.operationMode(OperationMode.CREATE)
|
||||
.jmodFile(mod.getAbsolutePath())
|
||||
.jmodOptions(options);
|
||||
assertDoesNotThrow(jmod::execute);
|
||||
assertTrue(mod.exists(), "mod does not exist");
|
||||
|
||||
assertDoesNotThrow(jmod::execute);
|
||||
assertTrue(mod.exists(), "mod does not exist");
|
||||
jmod.jmodOptions().clear();
|
||||
|
||||
jmod.jmodOptions().clear();
|
||||
jmod.operationMode(OperationMode.DESCRIBE);
|
||||
assertDoesNotThrow(jmod::execute, "describe mod failed");
|
||||
|
||||
jmod.operationMode(OperationMode.DESCRIBE);
|
||||
assertDoesNotThrow(jmod::execute, "describe mod failed");
|
||||
|
||||
jmod.operationMode(OperationMode.LIST);
|
||||
assertDoesNotThrow(jmod::execute, "list mod failed");
|
||||
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
jmod.operationMode(OperationMode.LIST);
|
||||
assertDoesNotThrow(jmod::execute, "list mod failed");
|
||||
} finally {
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHelp() {
|
||||
var jmod = new JmodOperation()
|
||||
.operationMode(OperationMode.HASH)
|
||||
.jmodFile("foo")
|
||||
.addArgs("--help-extra");
|
||||
var jmod = new JmodOperation().toolArgs("--help-extra");
|
||||
assertDoesNotThrow(jmod::execute);
|
||||
assertTrue(jmod.toolArgs().isEmpty(), "args not empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNoArguments() {
|
||||
var jmod = new JmodOperation();
|
||||
assertTrue(jmod.options().isEmpty(), "options not empty");
|
||||
assertTrue(jmod.fileOptions().isEmpty(), "file options not empty");
|
||||
assertTrue(jmod.jmodOptions().isEmpty(), "jmod options not empty");
|
||||
assertThrows(ExitStatusException.class, jmod::execute);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOptions() {
|
||||
var jpackage = new JpackageOperation().options("src/test/resources/jlink/options_verbose.txt",
|
||||
"src/test/resources/jlink/options_version.txt");
|
||||
assertDoesNotThrow(jpackage::execute);
|
||||
void testFileOptions() {
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jmod = new JmodOperation().fileOptions("src/test/resources/jlink/options_version.txt");
|
||||
assertDoesNotThrow(jmod::execute);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.matches("\\d+.\\d+.\\d+[\\r\\n]+"), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testVersion() {
|
||||
var jmod = new JmodOperation()
|
||||
.operationMode(OperationMode.DESCRIBE)
|
||||
.jmodFile("foo")
|
||||
.addArgs("--version");
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jmod = new JmodOperation().toolArgs("--version");
|
||||
assertDoesNotThrow(jmod::execute);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.matches("\\d+.\\d+.\\d+[\\r\\n]+"), out);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,10 +5,13 @@
|
|||
|
||||
package rife.bld.operations;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import rife.bld.operations.exceptions.ExitStatusException;
|
||||
import rife.tools.FileUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
@ -17,6 +20,13 @@ import static rife.bld.operations.JpackageOptions.Launcher;
|
|||
import static rife.bld.operations.JpackageOptions.PackageType;
|
||||
|
||||
public class TestJpackageOperation {
|
||||
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
|
||||
private final PrintStream stdout = System.out;
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
System.setOut(stdout);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testArguments() {
|
||||
|
@ -156,57 +166,64 @@ public class TestJpackageOperation {
|
|||
@Test
|
||||
void testCreatePackage() throws Exception {
|
||||
var tmpdir = Files.createTempDirectory("bld-jpackage-test").toFile();
|
||||
tmpdir.deleteOnExit();
|
||||
try {
|
||||
var options = new JpackageOptions()
|
||||
.input("lib/bld")
|
||||
.name("bld")
|
||||
.mainJar("bld-wrapper.jar")
|
||||
.javaOptions("--enable-preview")
|
||||
.dest(tmpdir.getAbsolutePath())
|
||||
.verbose(true);
|
||||
|
||||
var options = new JpackageOptions()
|
||||
.input("lib/bld")
|
||||
.name("bld")
|
||||
.mainJar("bld-wrapper.jar")
|
||||
.javaOptions("--enable-preview")
|
||||
.dest(tmpdir.getAbsolutePath())
|
||||
.verbose(true);
|
||||
var os = System.getProperty("os.version");
|
||||
if (os.endsWith("MANJARO")) {
|
||||
options.type(PackageType.DEB);
|
||||
}
|
||||
|
||||
var os = System.getProperty("os.version");
|
||||
if (os.endsWith("MANJARO")) {
|
||||
options.type(PackageType.DEB);
|
||||
var jpackage = new JpackageOperation().jpackageOptions(options);
|
||||
jpackage.execute();
|
||||
|
||||
var files = tmpdir.listFiles();
|
||||
assertNotNull(files, "files should not be null");
|
||||
assertTrue(files.length > 0, "no files found");
|
||||
|
||||
assertTrue(files[0].getName().matches("bld.*\\.[A-Za-z]{3}"), "Package not found");
|
||||
} finally {
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
}
|
||||
}
|
||||
|
||||
var jpackage = new JpackageOperation().jpackageOptions(options);
|
||||
jpackage.execute();
|
||||
|
||||
var files = tmpdir.listFiles();
|
||||
assertNotNull(files, "files should not be null");
|
||||
assertTrue(files.length > 0, "No files found");
|
||||
|
||||
assertTrue(files[0].getName().matches("bld.*\\.[A-Za-z]{3}"), "Package not found");
|
||||
|
||||
FileUtils.deleteDirectory(tmpdir);
|
||||
@Test
|
||||
void testFileOptions() {
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jpackage = new JpackageOperation().fileOptions("src/test/resources/jlink/options_verbose.txt",
|
||||
"src/test/resources/jlink/options_version.txt");
|
||||
assertDoesNotThrow(jpackage::execute);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.matches("\\d+.\\d+.\\d+[\\r\\n]+"), out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHelp() {
|
||||
var jpackage = new JpackageOperation().addArgs("--help");
|
||||
var jpackage = new JpackageOperation().toolArgs("--help");
|
||||
assertDoesNotThrow(jpackage::execute);
|
||||
assertTrue(jpackage.toolArgs().isEmpty(), "args not empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNoArguments() {
|
||||
var jpackage = new JpackageOperation();
|
||||
assertTrue(jpackage.options().isEmpty(), "options not empty");
|
||||
assertTrue(jpackage.fileOptions().isEmpty(), "file options not empty");
|
||||
assertTrue(jpackage.jpackageOptions().isEmpty(), "jpackage options not empty");
|
||||
assertThrows(ExitStatusException.class, jpackage::execute);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOptions() {
|
||||
var jpackage = new JpackageOperation().options("src/test/resources/jlink/options_verbose.txt",
|
||||
"src/test/resources/jlink/options_version.txt");
|
||||
assertDoesNotThrow(jpackage::execute);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testVersion() {
|
||||
var jpackage = new JpackageOperation().addArgs("--verbose", "--version");
|
||||
System.setOut(new PrintStream(outputStreamCaptor));
|
||||
var jpackage = new JpackageOperation().toolArgs("--verbose", "--version");
|
||||
assertDoesNotThrow(jpackage::execute);
|
||||
var out = outputStreamCaptor.toString();
|
||||
assertTrue(out.matches("\\d+.\\d+.\\d+[\\r\\n]+"), out);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue