Improved code and tests

Bumped Exec extension to 1.0.1
Bumped PMD extension to 1.1.0
This commit is contained in:
Erik C. Thauvin 2024-06-19 08:19:34 -07:00
parent c54672a7a0
commit c901065b3a
Signed by: erik
GPG key ID: 776702A6A2DA330E
7 changed files with 235 additions and 137 deletions

View file

@ -18,7 +18,10 @@ package rife.bld.extension;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import rife.bld.Project;
import rife.bld.blueprints.BaseProjectBlueprint;
import rife.bld.extension.kotlin.CompileOptions;
import rife.bld.extension.kotlin.CompilerPlugin;
import rife.tools.FileUtils;
import java.io.File;
@ -26,6 +29,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.logging.ConsoleHandler;
@ -46,6 +50,48 @@ class CompileKotlinOperationTest {
logger.setUseParentHandlers(false);
}
@Test
void testCollections() {
var op = new CompileKotlinOperation()
.fromProject(new Project())
.compileMainClasspath("path1", "path2")
.compileOptions(new CompileOptions().jdkRelease("17").verbose(true))
.mainSourceDirectories("dir1", "dir2")
.mainSourceDirectories(List.of(new File("dir3"), new File("dir4")))
.mainSourceFiles("file1", "file2")
.mainSourceFiles(List.of(new File("file3"), new File("file4")))
.mainSourceFiles(new File("file5"), new File("file6"))
.testSourceDirectories("tdir1", "tdir2")
.testSourceDirectories(List.of(new File("tdir3"), new File("tdir4")))
.testSourceFiles("tfile1", "tfile2")
.testSourceFiles(List.of(new File("tfile3"), new File("tfile4")))
.testSourceFiles(new File("tfile5"), new File("tfile6"))
.plugins("plugin1", "plugin2")
.plugins(CompilerPlugin.KOTLIN_SERIALIZATION, CompilerPlugin.ASSIGNMENT)
.plugins(new File("lib/compile"), CompilerPlugin.LOMBOK, CompilerPlugin.POWER_ASSERT)
.plugins(List.of("plugin3", "plugin4"))
.plugins(Arrays.stream(Objects.requireNonNull(new File("lib/compile").listFiles())).toList(),
CompilerPlugin.ALL_OPEN, CompilerPlugin.SAM_WITH_RECEIVER);
assertThat(op.compileMainClasspath()).as("compileMainClassPath")
.containsAll(List.of("path1", "path2"));
assertThat(op.compileOptions().hasRelease()).as("hasRelease").isTrue();
assertThat(op.compileOptions().isVerbose()).as("isVerbose").isTrue();
assertThat(op.mainSourceDirectories()).as("mainSourceDirectories").containsExactly(
Path.of("src", "main", "kotlin").toFile().getAbsoluteFile(), new File("dir1"),
new File("dir2"), new File("dir3"), new File("dir4"));
assertThat(op.testSourceDirectories()).as("testSourceDirectories").containsOnly(
Path.of("src", "test", "kotlin").toFile().getAbsoluteFile(), new File("tdir1"),
new File("tdir2"), new File("tdir3"), new File("tdir4"));
assertThat(op.mainSourceFiles()).as("mainSourceFiles").containsOnly(
new File("file1"), new File("file2"), new File("file3"),
new File("file4"), new File("file5"), new File("file6"));
assertThat(op.testSourceFiles()).as("testSourceFiles").containsOnly(
new File("tfile1"), new File("tfile2"), new File("tfile3"),
new File("tfile4"), new File("tfile5"), new File("tfile6"));
assertThat(op.plugins()).hasSize(10);
}
@Test
void testExecute() throws IOException {
var tmpDir = Files.createTempDirectory("bld-kotlin").toFile();

View file

@ -14,7 +14,7 @@
* limitations under the License.
*/
package rife.bld.extension;
package rife.bld.extension.kotlin;
import org.junit.jupiter.api.Test;
@ -23,61 +23,39 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
class CompileKotlinOptionsTest {
@Test
void argsCollectionTest() {
var args = new CompileKotlinOptions()
.advancedOptions(List.of("Xoption1", "Xoption2"))
.argFile(List.of("arg1.txt", "arg2.txt"))
.classpath(List.of("path1", "path2"))
.jvmOptions(List.of("option1", "option2"))
.noStdLib(false)
.optIn(List.of("opt1", "opt2"))
.options(List.of("-foo", "-bar"))
.scriptTemplates(List.of("temp1", "temp2"))
.args();
var matches = List.of(
"@arg1.txt", "@arg2.txt",
"-classpath", "path1:path2",
"-Joption1", "-Joption2",
"-opt-in", "opt1",
"-opt-in", "opt2",
"-foo", "-bar",
"-script-templates",
"temp1,temp2",
"-XXoption1", "-XXoption2");
for (var arg : args) {
var found = false;
for (var match : matches) {
if (match.equals(arg)) {
found = true;
break;
}
}
assertThat(found).as(arg).isTrue();
}
class CompileOptionsTest {
/**
* Returns the local path of the given file names.
*
* @param fileNames The file names
* @return the local path
*/
private String localPath(String... fileNames) {
return Arrays.stream(fileNames).map(it -> new File(it).getAbsolutePath())
.collect(Collectors.joining(File.pathSeparator));
}
@Test
void argsTest() {
var options = new CompileKotlinOptions()
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
void testArgs() {
var options = new CompileOptions()
.apiVersion("11")
.argFile("file.txt", "file2.txt")
.classpath("path1", "path2")
.argFile(new File("file.txt"), new File("file2.txt"))
.classpath(new File("path1"), new File("path2"))
.javaParameters(true)
.jvmTarget("11")
.includeRuntime(true)
.jdkHome("path")
.jdkHome(new File("path"))
.jdkRelease("11")
.kotlinHome("path")
.kotlinHome(new File("path"))
.languageVersion("1.0")
.moduleName("module")
.noJdk(true)
@ -94,14 +72,14 @@ class CompileKotlinOptionsTest {
var matches = List.of(
"-api-version", "11",
"@file.txt", "@file2.txt",
"-classpath", "path1" + File.pathSeparator + "path2",
"@" + localPath("file.txt"), "@" + localPath("file2.txt"),
"-classpath", localPath("path1", "path2"),
"-java-parameters",
"-jvm-target", "11",
"-include-runtime",
"-jdk-home", "path",
"-jdk-home", localPath("path"),
"-Xjdk-release=11",
"-kotlin-home", "path",
"-kotlin-home", localPath("path"),
"-language-version", "1.0",
"-module-name", "module",
"-no-jdk",
@ -111,7 +89,7 @@ class CompileKotlinOptionsTest {
"-opt-in", "opt2",
"-foo",
"-bar",
"-d", "path",
"-d", localPath("path"),
"-P", "plugin:id:name:value",
"-progressive",
"-script-templates", "name,name2",
@ -123,18 +101,86 @@ class CompileKotlinOptionsTest {
args.add(options.apiVersion(11).jvmTarget(11).args());
for (var a : args) {
assertThat(a).hasSize(matches.size());
IntStream.range(0, a.size()).forEach(i -> assertThat(a.get(i)).isEqualTo(matches.get(i)));
}
}
@Test
void checkAllParamsTest() throws IOException {
void testArgsCollections() {
var advanceOptions = List.of("Xoption1", "Xoption2");
var argFile = List.of(new File("arg1.txt"), new File("arg2.txt"));
var classpath = List.of(new File("path1"), new File("path2"));
var jvmOptions = List.of("option1", "option2");
var optIn = List.of("opt1", "opt2");
var options = List.of("-foo", "-bar");
var plugin = List.of("id:name:value", "id2:name2:value2");
var scriptTemplates = List.of("temp1", "temp2");
var op = new CompileOptions()
.advancedOptions(advanceOptions)
.argFile(argFile)
.classpath(classpath)
.jvmOptions(jvmOptions)
.noStdLib(false)
.optIn(optIn)
.options(options)
.scriptTemplates(scriptTemplates);
plugin.forEach(it -> {
var p = it.split(":");
op.plugin(p[0], p[1], p[2]);
});
assertThat(op.advancedOptions()).as("advancedOptions")
.hasSize(advanceOptions.size()).containsAll(advanceOptions);
assertThat(op.argFile()).as("argFile")
.hasSize(argFile.size()).containsAll(argFile);
assertThat(op.classpath()).as("classpath")
.hasSize(classpath.size()).containsAll(classpath);
assertThat(op.jvmOptions()).as("jvmOptions")
.hasSize(jvmOptions.size()).containsAll(jvmOptions);
assertThat(op.optIn()).as("optIn")
.hasSize(optIn.size()).containsAll(optIn);
assertThat(op.options()).as("options")
.hasSize(options.size()).containsAll(options);
assertThat(op.plugin()).as("plugin")
.hasSize(plugin.size()).containsAll(plugin);
assertThat(op.scriptTemplates()).as("scriptTemplates")
.hasSize(scriptTemplates.size()).containsAll(scriptTemplates);
var matches = List.of(
'@' + localPath("arg1.txt"), '@' + localPath("arg2.txt"),
"-classpath", localPath("path1", "path2"),
"-Joption1", "-Joption2",
"-opt-in", "opt1",
"-opt-in", "opt2",
"-foo", "-bar",
"-script-templates",
"temp1,temp2",
"-XXoption1", "-XXoption2",
"-P", "plugin:id:name:value",
"-P", "plugin:id2:name2:value2");
var args = op.args();
for (var arg : args) {
var found = false;
for (var match : matches) {
if (match.equals(arg)) {
found = true;
break;
}
}
assertThat(found).as(arg).isTrue();
}
}
@Test
void testCheckAllParams() throws IOException {
var args = Files.readAllLines(Paths.get("src", "test", "resources", "kotlinc-args.txt"));
assertThat(args).isNotEmpty();
var params = new CompileKotlinOptions()
var params = new CompileOptions()
.advancedOptions("Xoption")
.argFile("file")
.classpath("classpath")
@ -152,7 +198,7 @@ class CompileKotlinOptionsTest {
.noWarn(true)
.optIn("annotation")
.options("option")
.path("path")
.path(new File("path"))
.plugin("id", "option", "value")
.progressive(true)
.scriptTemplates("template")