Added tests

This commit is contained in:
Erik C. Thauvin 2023-11-04 02:00:42 -07:00
parent 6e161e1445
commit 573681ea6c
5 changed files with 173 additions and 14 deletions

View file

@ -70,8 +70,6 @@ public class CompileKotlinOperationBuild extends Project {
.url("https://github.com/rife2/bld-kotlin"))
.signKey(property("sign.key"))
.signPassphrase(property("sign.passphrase"));
testOperation().mainClass("rife.bld.extension.CompileKotlinOperationTest");
}
public static void main(String[] args) {

View file

@ -37,8 +37,8 @@ import java.util.regex.Pattern;
public class CompileKotlinOperation extends AbstractOperation<CompileKotlinOperation> {
public static final Pattern KOTLIN_FILE_PATTERN = Pattern.compile("^.*\\.kt$");
private static final Logger LOGGER = Logger.getLogger(CompileKotlinOperation.class.getName());
public final Collection<String> compileOptions_ = new ArrayList<>();
private final Collection<String> compileMainClasspath_ = new ArrayList<>();
private final Collection<String> compileOptions_ = new ArrayList<>();
private final Collection<String> compileTestClasspath_ = new ArrayList<>();
private final Collection<File> mainSourceDirectories_ = new ArrayList<>();
private final Collection<File> mainSourceFiles_ = new ArrayList<>();
@ -50,6 +50,11 @@ public class CompileKotlinOperation extends AbstractOperation<CompileKotlinOpera
public static Collection<File> getKotlinFileList(File directory) {
if (directory == null) {
return Collections.emptyList();
} else if (!directory.exists()) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("Directory not found: " + directory.getAbsolutePath());
}
return Collections.emptyList();
} else {
var dir_abs = directory.getAbsoluteFile();
return FileUtils.getFileList(dir_abs, KOTLIN_FILE_PATTERN, null).stream().map((file) ->
@ -114,7 +119,7 @@ public class CompileKotlinOperation extends AbstractOperation<CompileKotlinOpera
* @param classpath a list of classpath entries
* @return this operation instance
*/
public CompileKotlinOperation compileMainClasspath(List<String> classpath) {
public CompileKotlinOperation compileMainClasspath(Collection<String> classpath) {
compileMainClasspath_.addAll(classpath);
return this;
}

View file

@ -16,17 +16,78 @@
package rife.bld.extension;
@SuppressWarnings({"PMD.TestClassWithoutTestCases", "PMD.SystemPrintln"})
public class CompileKotlinOperationTest {
void verifyHello() {
if (!"Hello World!".equals(new CompileKotlinOperation().getMessage())) {
throw new AssertionError();
} else {
System.out.println("Succeeded");
}
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import rife.tools.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Objects;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.assertThat;
class CompileKotlinOperationTest {
@BeforeAll
static void beforeAll() {
var level = Level.ALL;
var logger = Logger.getLogger("rife.bld.extension");
var consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(level);
logger.addHandler(consoleHandler);
logger.setLevel(level);
logger.setUseParentHandlers(false);
}
public static void main(String[] args) {
new CompileKotlinOperationTest().verifyHello();
@Test
void testExecute() throws IOException {
var tmpDir = Files.createTempDirectory("bld-kotlin").toFile();
try {
var buildDir = new File(tmpDir, "build");
var mainDir = new File(buildDir, "main");
var testDir = new File(buildDir, "test");
assertThat(mainDir.mkdirs()).isTrue();
assertThat(testDir.mkdirs()).isTrue();
var compileJars = new ArrayList<String>();
for (var f : Objects.requireNonNull(new File("examples/lib/compile").listFiles())) {
compileJars.add(f.getAbsolutePath());
}
var testJars = new ArrayList<String>();
for (var f : Objects.requireNonNull(new File("examples/lib/test").listFiles())) {
testJars.add(f.getAbsolutePath());
}
var op = new CompileKotlinOperation()
.compileOptions("-verbose")
.buildMainDirectory(mainDir)
.buildTestDirectory(testDir)
.compileMainClasspath(compileJars)
.compileTestClasspath(testJars)
.compileTestClasspath(compileJars)
.compileTestClasspath(mainDir.getAbsolutePath())
.mainSourceFiles(CompileKotlinOperation.getKotlinFileList(new File("examples/src/main/kotlin")))
.testSourceFiles(CompileKotlinOperation.getKotlinFileList(new File("examples/src/test/kotlin")));
op.execute();
assertThat(tmpDir).isNotEmptyDirectory();
assertThat(mainDir).isNotEmptyDirectory();
assertThat(testDir).isNotEmptyDirectory();
var exampleClass = Path.of(mainDir.getAbsolutePath(), "com", "example", "Example.class").toFile();
assertThat(exampleClass).exists();
} finally {
FileUtils.deleteDirectory(tmpDir);
}
}
}