From a4879784e7be87b5ee184b47eb8faba635019a5d Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 23 Feb 2022 11:04:50 +0100 Subject: refactor: finishes the replFregeTask refactoring --- .../gradleplugins/ReplFregeTaskFunctionalTest.java | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java (limited to 'src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java') diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java new file mode 100644 index 0000000..979d1d4 --- /dev/null +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java @@ -0,0 +1,102 @@ +package ch.fhnw.thga.gradleplugins; + +import static ch.fhnw.thga.gradleplugins.FregePlugin.REPL_FREGE_TASK_NAME; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.COMPLETION_FR; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.MINIMAL_BUILD_FILE_CONFIG; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSection; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runAndFailGradleTask; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask; +import static org.gradle.testkit.runner.TaskOutcome.FAILED; +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.util.stream.Stream; + +import org.gradle.api.Project; +import org.gradle.testkit.runner.BuildResult; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.IndicativeSentencesGeneration; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import ch.fhnw.thga.gradleplugins.fregeproject.FregeProjectBuilder; + +public class ReplFregeTaskFunctionalTest +{ + @Nested + @IndicativeSentencesGeneration( + generator = DisplayNameGenerator.ReplaceUnderscores.class) + class Repl_frege_task_works + { + @Test + void given_minimal_build_file_config_with_repl_module( + @TempDir File testProjectDir) + throws Exception + { + String replModuleConfig = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .replModule("'ch.fhnw.thga.Completion'") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(replModuleConfig) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + + BuildResult result = runGradleTask(testProjectDir, REPL_FREGE_TASK_NAME); + + assertTrue( + project + .getTasks() + .getByName(REPL_FREGE_TASK_NAME) + instanceof ReplFregeTask); + assertEquals( + SUCCESS, + result.task(":" + REPL_FREGE_TASK_NAME).getOutcome()); + assertTrue(result.getOutput().contains("java -cp")); + assertTrue(result.getOutput().contains("frege3.25.84.jar")); + assertFalse(result.getOutput().contains("Completion.class")); + } + } + + @Nested + @IndicativeSentencesGeneration( + generator = DisplayNameGenerator.ReplaceUnderscores.class) + class Repl_frege_task_fails + { + @Test + void given_minimal_build_file_config_without_repl_module( + @TempDir File testProjectDir) + throws Exception + { + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + + BuildResult result = runAndFailGradleTask(testProjectDir, REPL_FREGE_TASK_NAME); + + assertTrue( + project + .getTasks() + .getByName(REPL_FREGE_TASK_NAME) + instanceof ReplFregeTask + ); + assertEquals( + FAILED, + result.task(":" + REPL_FREGE_TASK_NAME).getOutcome() + ); + } + } +} \ No newline at end of file -- cgit From 049f8d3029b30dc58221e90aa8ddf69b3aa3b61e Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 23 Feb 2022 19:29:54 +0100 Subject: feat: simplifies the replFrege task The replFregeTask has the following new logic: 1. Compiles the specified fregeRepl module (either in the `build.gradle` via command line option `--replModule=...`) and all its dependencies. 2. Sets up the correct classpath so that dependent modules don't have to be imported manually. In addition, it solves the shadowing problem by removing the replModule java and class file from the classpath. 3. It prints one single command to directly start the repl and load the specified module. Bonus: I designed the task so that you can even automate step 3 with the following bash command: `eval $(./gradlew -q replFrege)`. --- example-project/build.gradle | 2 +- .../CompileFregeTaskFunctionalTest.java | 16 +---- .../gradleplugins/ReplFregeTaskFunctionalTest.java | 80 +++++++++++++++++++++- .../gradleplugins/SharedFunctionalTestLogic.java | 28 ++++++++ .../fhnw/thga/gradleplugins/CompileFregeTask.java | 19 +++-- .../ch/fhnw/thga/gradleplugins/FregePlugin.java | 1 + .../ch/fhnw/thga/gradleplugins/ReplFregeTask.java | 69 +++++++++++++++---- 7 files changed, 180 insertions(+), 35 deletions(-) (limited to 'src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java') diff --git a/example-project/build.gradle b/example-project/build.gradle index 6fefaec..ae7217b 100644 --- a/example-project/build.gradle +++ b/example-project/build.gradle @@ -6,5 +6,5 @@ frege { version = '3.25.84' release = '3.25alpha' mainModule = 'ch.fhnw.thga.Test' - replSource = 'ch.fhnw.thga.Test' + replModule = 'ch.fhnw.thga.Test' } \ No newline at end of file diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java index 31e6d05..90a459c 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java @@ -8,6 +8,7 @@ import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.NEW_LINE; import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.MINIMAL_BUILD_FILE_CONFIG; import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.COMPLETION_FR; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.assertFileExists; import static org.gradle.testkit.runner.TaskOutcome.FAILED; import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE; import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; @@ -31,17 +32,6 @@ import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile; public class CompileFregeTaskFunctionalTest { - private static final boolean assertFileExists( - File testProjectDir, - String relativeFilePath) - { - return testProjectDir - .toPath() - .resolve(relativeFilePath) - .toFile() - .exists(); - } - @Nested @IndicativeSentencesGeneration( separator = " -> ", @@ -159,11 +149,11 @@ public class CompileFregeTaskFunctionalTest ); assertFileExists( testProjectDir, - "build/classes/main/frege/ch/fhnw/thga/Completion.java" + "build/frege/ch/fhnw/thga/Completion.java" ); assertFileExists( testProjectDir, - "build/classes/main/frege/ch/fhnw/thga/Completion.class" + "build/frege/ch/fhnw/thga/Completion.class" ); } @Test diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java index 979d1d4..52bfbff 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java @@ -1,15 +1,18 @@ package ch.fhnw.thga.gradleplugins; +import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR; import static ch.fhnw.thga.gradleplugins.FregePlugin.REPL_FREGE_TASK_NAME; import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.COMPLETION_FR; import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.MINIMAL_BUILD_FILE_CONFIG; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.NEW_LINE; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.assertFileDoesNotExist; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.assertFileExists; import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSection; import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runAndFailGradleTask; import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask; import static org.gradle.testkit.runner.TaskOutcome.FAILED; import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; @@ -24,6 +27,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import ch.fhnw.thga.gradleplugins.fregeproject.FregeProjectBuilder; +import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile; public class ReplFregeTaskFunctionalTest { @@ -64,7 +68,79 @@ public class ReplFregeTaskFunctionalTest result.task(":" + REPL_FREGE_TASK_NAME).getOutcome()); assertTrue(result.getOutput().contains("java -cp")); assertTrue(result.getOutput().contains("frege3.25.84.jar")); - assertFalse(result.getOutput().contains("Completion.class")); + assertTrue(result.getOutput().contains(COMPLETION_FR.getFregeModulePath())); + assertFileDoesNotExist( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.java" + ); + assertFileDoesNotExist( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.class" + ); + } + + @Test + void given_dependent_frege_files_with_command_line_repl_module_option( + @TempDir File testProjectDir) + throws Exception + { + String frobCode = String.join( + NEW_LINE, + "module ch.fhnw.thga.Frob where", + NEW_LINE, + NEW_LINE, + "import ch.fhnw.thga.Completion (complete)", + NEW_LINE, + "frob i = complete $ i + i", + NEW_LINE + ); + FregeSourceFile frob_FR = new FregeSourceFile( + String.format( + "%s/%s", + DEFAULT_RELATIVE_SOURCE_DIR, + "ch/fhnw/thga/Frob.fr" + ), + frobCode); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR, frob_FR)) + .build(); + + BuildResult result = runGradleTask( + testProjectDir, + REPL_FREGE_TASK_NAME, + "--replModule=ch.fhnw.thga.Frob" + ); + + assertTrue( + project + .getTasks() + .getByName(REPL_FREGE_TASK_NAME) + instanceof ReplFregeTask); + assertEquals( + SUCCESS, + result.task(":" + REPL_FREGE_TASK_NAME).getOutcome()); + assertTrue(result.getOutput().contains("java -cp")); + assertTrue(result.getOutput().contains("frege3.25.84.jar")); + assertTrue(result.getOutput().contains(frob_FR.getFregeModulePath())); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.java" + ); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.class" + ); + assertFileDoesNotExist( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Frob.java" + ); + assertFileDoesNotExist( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Frob.class" + ); } } diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java index 51fa256..f935b28 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java @@ -3,6 +3,8 @@ package ch.fhnw.thga.gradleplugins; import static ch.fhnw.thga.gradleplugins.FregePlugin.FREGE_EXTENSION_NAME; import static ch.fhnw.thga.gradleplugins.FregePlugin.FREGE_PLUGIN_ID; import static ch.fhnw.thga.gradleplugins.GradleBuildFileConversionTest.createPluginsSection; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR; import java.io.BufferedWriter; @@ -44,6 +46,32 @@ public class SharedFunctionalTestLogic NEW_LINE ) ); + + public static final boolean fileExists( + File testProjectDir, + String relativeFilePath) + { + return testProjectDir + .toPath() + .resolve(relativeFilePath) + .toFile() + .exists(); + } + + public static final void assertFileExists( + File testProjectDir, + String relativeFilePath) + { + assertTrue(fileExists(testProjectDir, relativeFilePath)); + } + + + public static final void assertFileDoesNotExist( + File testProjectDir, + String relativeFilePath) + { + assertFalse(fileExists(testProjectDir, relativeFilePath)); + } static String createFregeSection(FregeDTO fregeDTO) { diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java index a3c42d2..a39a07b 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java @@ -11,6 +11,7 @@ import javax.inject.Inject; import org.gradle.api.DefaultTask; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.logging.LogLevel; import org.gradle.api.model.ObjectFactory; import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; @@ -30,7 +31,7 @@ import org.gradle.api.tasks.options.Option; @CacheableTask public abstract class CompileFregeTask extends DefaultTask { private static final String FREGE_FILES_GLOB_PATTERN = "**/*.fr"; - private final JavaExec javaExec; + private JavaExec javaExec; @InputFile @PathSensitive(PathSensitivity.RELATIVE) @@ -113,12 +114,18 @@ public abstract class CompileFregeTask extends DefaultTask { } @TaskAction - public void compileFrege() { + public void compileFrege() + { + this.getLogging().captureStandardOutput(LogLevel.LIFECYCLE); List targetDirectoryArg = List.of( "-d", - getFregeOutputDir().getAsFile().get().getAbsolutePath()); - - javaExec.setClasspath(getProject().files(getFregeCompilerJar())) - .setArgs(buildCompilerArgsFromProperties(targetDirectoryArg)).exec(); + getFregeOutputDir().getAsFile().get().getAbsolutePath() + ); + javaExec.setClasspath( + getProject() + .files(getFregeCompilerJar())) + .setErrorOutput(System.out) + .setArgs(buildCompilerArgsFromProperties(targetDirectoryArg)) + .exec(); } } \ No newline at end of file diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java index 9b725ac..e1f410a 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java @@ -94,6 +94,7 @@ public class FregePlugin implements Plugin setupFregeCompilerTask.get().getFregeCompilerOutputPath()); task.getFregeOutputDir().set(extension.getOutputDir()); task.getFregeDependencies().set(implementation.getAsPath()); + task.getFregeMainSourceDir().set(extension.getMainSourceDir()); } ); } diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java index 67af55e..1a47f8f 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java @@ -5,6 +5,8 @@ import org.gradle.api.DefaultTask; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.FileTree; import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import org.gradle.api.provider.Property; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; @@ -15,6 +17,7 @@ import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.options.Option; public abstract class ReplFregeTask extends DefaultTask { + private static final Logger LOGGER = Logging.getLogger(ReplFregeTask.class); public static final String REPL_MAIN_CLASS = "frege.repl.FregeRepl"; @InputFile @@ -26,6 +29,9 @@ public abstract class ReplFregeTask extends DefaultTask { @InputDirectory public abstract DirectoryProperty getFregeOutputDir(); + @InputDirectory + public abstract DirectoryProperty getFregeMainSourceDir(); + @Input @Option(option = "replModule", description = "The full name of the module which you want to load into the repl, e.g. 'my.mod.Name'") @@ -39,30 +45,67 @@ public abstract class ReplFregeTask extends DefaultTask { } @Internal - public final Provider getClasspathWithoutReplClassFile() + public final Provider getReplClassFiles() { return getFregeOutputDir() .map(outDir -> outDir.getAsFileTree()) - .map(tree -> tree.matching(pattern -> pattern.exclude("**/*.java"))) - .map(tree -> tree.matching(pattern -> pattern.exclude( - String.format( - "**/%s.class", - getReplClassName().get() + .map(tree -> tree.matching( + pattern -> pattern.include( + String.format( + "**/%s.class", + getReplClassName().get() + ) ) - ))); + )); + } + + @Internal + public final Provider getReplJavaFiles() + { + return getFregeOutputDir() + .map(outDir -> outDir.getAsFileTree()) + .map(tree -> tree.matching( + pattern -> pattern.include( + String.format( + "**/%s.java", + getReplClassName().get() + ) + )) + ); + } + + @Internal + public final Provider getReplFregeFile() + { + return getFregeMainSourceDir() + .map(outDir -> outDir.getAsFileTree()) + .map(tree -> tree.matching( + pattern -> pattern.include( + String.format( + "**/%s.fr", + getReplClassName().get() + ) + )) + ); } @TaskAction - public void printStartFregeReplCommand() { - System.out.println("Execute the following command to start the Frege Repl:"); - System.out.println(String.format( - "java -cp %s %s", + public void printStartFregeReplCommand() + { + getProject().delete(getReplJavaFiles()); + getProject().delete(getReplClassFiles()); + LOGGER.lifecycle( + "Execute the following command to start the Frege Repl and load the Frege module:"); + LOGGER.quiet(String.format( + "(echo :l %s && cat) | java -cp %s %s", + getReplFregeFile().get().getAsPath(), SharedTaskLogic.setupClasspath( getProject(), getFregeDependencies(), getFregeCompilerJar(), - getClasspathWithoutReplClassFile()) + getFregeOutputDir()) .get().getAsPath(), - REPL_MAIN_CLASS)); + REPL_MAIN_CLASS) + ); } } -- cgit From e45ba9993b9465a088e0ec08a099e65caa993f9e Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 23 Feb 2022 20:25:11 +0100 Subject: fix: normalize path to make test pass on windows as well --- .../ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java') diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java index 52bfbff..652a3c2 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java @@ -16,6 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; +import java.nio.file.Paths; import java.util.stream.Stream; import org.gradle.api.Project; @@ -68,7 +69,9 @@ public class ReplFregeTaskFunctionalTest result.task(":" + REPL_FREGE_TASK_NAME).getOutcome()); assertTrue(result.getOutput().contains("java -cp")); assertTrue(result.getOutput().contains("frege3.25.84.jar")); - assertTrue(result.getOutput().contains(COMPLETION_FR.getFregeModulePath())); + assertTrue(result.getOutput().contains( + Paths.get(COMPLETION_FR.getFregeModulePath()).normalize().toString()) + ); assertFileDoesNotExist( testProjectDir, "build/classes/main/frege/ch/fhnw/thga/Completion.java" @@ -124,7 +127,9 @@ public class ReplFregeTaskFunctionalTest result.task(":" + REPL_FREGE_TASK_NAME).getOutcome()); assertTrue(result.getOutput().contains("java -cp")); assertTrue(result.getOutput().contains("frege3.25.84.jar")); - assertTrue(result.getOutput().contains(frob_FR.getFregeModulePath())); + assertTrue(result.getOutput().contains( + Paths.get(frob_FR.getFregeModulePath()).normalize().toString()) + ); assertFileExists( testProjectDir, "build/classes/main/frege/ch/fhnw/thga/Completion.java" -- cgit