From 0aa4dd065ee78f9acc88b39625cc4a9b47f6c944 Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 9 Feb 2022 21:37:28 +0100 Subject: refactor: continues with staged builder refactoring --- .../CompileFregeTaskFunctionalTest.java | 188 +++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java (limited to 'src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java') diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java new file mode 100644 index 0000000..a0512c0 --- /dev/null +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java @@ -0,0 +1,188 @@ +package ch.fhnw.thga.gradleplugins; + +import static ch.fhnw.thga.gradleplugins.FregePlugin.COMPILE_FREGE_TASK_NAME; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSection; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask; +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; +import static org.junit.jupiter.api.Assertions.assertEquals; +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.Tag; +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; +import ch.fhnw.thga.gradleplugins.fregeproject.ProjectRoot; + +public class CompileFregeTaskFunctionalTest +{ + private static final String NEW_LINE = System.lineSeparator(); + private static final FregeSourceFile COMPLETION_FR = new FregeSourceFile( + "src/main/frege/ch/fhnw/thga/Completion.fr", + String.join + ( + NEW_LINE, + "module ch.fhnw.thga.Completion where", + NEW_LINE, + NEW_LINE, + " complete :: Int -> (Int, String)", + NEW_LINE, + " complete i = (i, \"Frege rocks\")", + NEW_LINE + ) + ); + @Nested + @IndicativeSentencesGeneration( + separator = " -> ", + generator = DisplayNameGenerator.ReplaceUnderscores.class + ) + class Compile_frege_task_works { + + @Test + void given_frege_code_in_default_source_dir_and_minimal_build_file_config( + @TempDir File testProjectDir) + throws Exception + { + String minimalBuildFileConfig = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .build() + ); + + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(minimalBuildFileConfig) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + + BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); + + assertTrue( + project + .getTasks() + .getByName(COMPILE_FREGE_TASK_NAME) + instanceof CompileFregeTask + ); + assertEquals( + SUCCESS, + result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() + ); + assertTrue(testProjectDir + .toPath() + .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java") + .toFile() + .exists()); + assertTrue(testProjectDir + .toPath() + .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class") + .toFile() + .exists()); + } + @Test + void given_frege_code_and_many_compiler_flags( + @TempDir File testProjectDir) + throws Exception + { + String buildConfigWithCompilerFlags = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .compilerFlags("['-v', '-make', '-O', '-hints']") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(buildConfigWithCompilerFlags) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + + BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); + + assertTrue( + project + .getTasks() + .getByName(COMPILE_FREGE_TASK_NAME) + instanceof CompileFregeTask + ); + assertEquals( + SUCCESS, + result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() + ); + assertTrue(testProjectDir + .toPath() + .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java") + .toFile() + .exists() + ); + assertTrue(testProjectDir + .toPath() + .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class") + .toFile() + .exists() + ); + } + @Test + void given_frege_code_in_custom_source_and_output_dir_and_minimal_build_file_config( + @TempDir File testProjectDir + ) + throws Exception + { + String customSourceAndOutputBuildFileConfig = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .mainSourceDir("layout.projectDirectory.dir('src/frege')") + .outputDir("layout.buildDirectory.dir('frege')") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(customSourceAndOutputBuildFileConfig) + .fregeSourceFiles(() -> Stream.of(new FregeSourceFile( + "src/frege/ch/fhnw/thga/Completion.fr", + COMPLETION_FR.getFregeSourceCode()))) + .build(); + + BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); + System.out.println(result.getOutput()); + assertTrue( + project + .getTasks() + .getByName(COMPILE_FREGE_TASK_NAME) + instanceof CompileFregeTask + ); + assertEquals( + SUCCESS, + result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() + ); + assertTrue(testProjectDir + .toPath() + .resolve("build/frege/ch/fhnw/thga/Completion.java") + .toFile() + .exists() + ); + assertTrue(testProjectDir + .toPath() + .resolve("build/frege/ch/fhnw/thga/Completion.class") + .toFile() + .exists() + ); + } + } +} -- cgit From 7ef464ab2c2a6a2e0b9ee857d6979ab98f1ec930 Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Fri, 11 Feb 2022 13:58:28 +0100 Subject: refactor: finishes the compileFregeTask refactoring --- build.gradle | 12 +- .../CompileFregeTaskFunctionalTest.java | 344 ++++++++++++++++++--- .../gradleplugins/FregePluginFunctionalTest.java | 298 +----------------- .../gradleplugins/SharedFunctionalTestLogic.java | 14 +- .../fregeproject/FregeProjectBuilder.java | 9 +- .../fhnw/thga/gradleplugins/CompileFregeTask.java | 8 +- .../ch/fhnw/thga/gradleplugins/FregePlugin.java | 6 +- 7 files changed, 329 insertions(+), 362 deletions(-) (limited to 'src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java') diff --git a/build.gradle b/build.gradle index fe6d9c0..9baa197 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,6 @@ dependencies testImplementation group: junit5Group, name: 'junit-jupiter-api', version: junit5Version testImplementation group: 'net.jqwik', name: 'jqwik', version: '1.6.3' testRuntimeOnly group: junit5Group, name: 'junit-jupiter-engine', version: junit5Version - testRuntimeOnly group: 'org.junit.platform', name: 'junit-platform-console', version: junit5Version testImplementation(enforcedPlatform("org.junit:junit-bom:${junit5Version}")) } @@ -56,15 +55,6 @@ configurations functionalRuntimeOnly.extendsFrom runtimeOnly } -tasks.register('consoleLauncher', JavaExec) -{ - dependsOn testClasses - def reportsDir = layout.buildDirectory.dir('test-results') - mainClass = 'org.junit.platform.console.ConsoleLauncher' - outputs.dir reportsDir - classpath sourceSets.test.runtimeClasspath + sourceSets.functionalTest.runtimeClasspath - args '--scan-classpath', '--details', 'summary', '--reports-dir', reportsDir.get() -} tasks.register('functionalTest', Test) { @@ -75,7 +65,7 @@ tasks.register('functionalTest', Test) shouldRunAfter test } -check.dependsOn consoleLauncher +check.dependsOn functionalTest tasks.withType(Test).configureEach { diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java index a0512c0..42a3de2 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java @@ -1,9 +1,14 @@ package ch.fhnw.thga.gradleplugins; +import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR; import static ch.fhnw.thga.gradleplugins.FregePlugin.COMPILE_FREGE_TASK_NAME; 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.FROM_CACHE; import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; +import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -15,19 +20,19 @@ 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.Tag; 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; -import ch.fhnw.thga.gradleplugins.fregeproject.ProjectRoot; public class CompileFregeTaskFunctionalTest { private static final String NEW_LINE = System.lineSeparator(); private static final FregeSourceFile COMPLETION_FR = new FregeSourceFile( - "src/main/frege/ch/fhnw/thga/Completion.fr", + String.format("%s/%s", + DEFAULT_RELATIVE_SOURCE_DIR, + "ch/fhnw/thga/Completion.fr"), String.join ( NEW_LINE, @@ -40,6 +45,25 @@ public class CompileFregeTaskFunctionalTest NEW_LINE ) ); + private static final String MINIMAL_BUILD_FILE_CONFIG = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .build() + ); + + private static final boolean assertFileExists( + File testProjectDir, + String relativeFilePath) + { + return testProjectDir + .toPath() + .resolve(relativeFilePath) + .toFile() + .exists(); + } + @Nested @IndicativeSentencesGeneration( separator = " -> ", @@ -52,18 +76,10 @@ public class CompileFregeTaskFunctionalTest @TempDir File testProjectDir) throws Exception { - String minimalBuildFileConfig = createFregeSection( - FregeDTOBuilder - .builder() - .version("'3.25.84'") - .release("'3.25alpha'") - .build() - ); - Project project = FregeProjectBuilder .builder() .projectRoot(testProjectDir) - .buildFile(minimalBuildFileConfig) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) .build(); @@ -79,16 +95,14 @@ public class CompileFregeTaskFunctionalTest SUCCESS, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() ); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java") - .toFile() - .exists()); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class") - .toFile() - .exists()); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.java" + ); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.class" + ); } @Test void given_frege_code_and_many_compiler_flags( @@ -122,23 +136,18 @@ public class CompileFregeTaskFunctionalTest SUCCESS, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() ); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java") - .toFile() - .exists() + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.java" ); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class") - .toFile() - .exists() + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.class" ); } @Test void given_frege_code_in_custom_source_and_output_dir_and_minimal_build_file_config( - @TempDir File testProjectDir - ) + @TempDir File testProjectDir) throws Exception { String customSourceAndOutputBuildFileConfig = createFregeSection( @@ -160,7 +169,6 @@ public class CompileFregeTaskFunctionalTest .build(); BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); - System.out.println(result.getOutput()); assertTrue( project .getTasks() @@ -171,18 +179,262 @@ public class CompileFregeTaskFunctionalTest SUCCESS, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() ); - assertTrue(testProjectDir - .toPath() - .resolve("build/frege/ch/fhnw/thga/Completion.java") - .toFile() - .exists() + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.java" + ); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.class" + ); + } + @Test + void and_is_up_to_date_given_no_code_changes( + @TempDir File testProjectDir) + throws Exception + { + FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + + BuildResult first = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); + + assertEquals( + SUCCESS, + first.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); + + BuildResult second = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); + + assertEquals( + UP_TO_DATE, + second.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); + } + @Test + void and_is_cached_given_cache_hit( + @TempDir File testProjectDir) + throws Exception + { + FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + + BuildResult first = runGradleTask( + testProjectDir, + COMPILE_FREGE_TASK_NAME, + "--build-cache" ); - assertTrue(testProjectDir - .toPath() - .resolve("build/frege/ch/fhnw/thga/Completion.class") - .toFile() - .exists() + + assertEquals( + SUCCESS, + first.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() + ); + + String codeChange = String.join( + NEW_LINE, + "module ch.fhnw.thga.Completion where", + NEW_LINE, + NEW_LINE, + " frob :: Int -> (Int, String)", + NEW_LINE, + " frob i = (i, \"Frege rocks\")", + NEW_LINE + ); + FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) + .fregeSourceFiles(() -> Stream.of(new FregeSourceFile( + COMPLETION_FR.getFregeModulePath(), + codeChange))) + .build(); + + BuildResult second = runGradleTask( + testProjectDir, + COMPILE_FREGE_TASK_NAME, + "--build-cache" + ); + + assertEquals( + SUCCESS, + second.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() + ); + + FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + BuildResult third = runGradleTask( + testProjectDir, + COMPILE_FREGE_TASK_NAME, + "--build-cache" + ); + + assertEquals( + FROM_CACHE, + third.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); + } + @Test + void given_two_dependent_frege_files_in_default_source_dir_and_minimal_build_file_config( + @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 + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR, new FregeSourceFile( + String.format( + "%s/%s", + DEFAULT_RELATIVE_SOURCE_DIR, + "ch/fhnw/thga/Frob.fr" + ), + frobCode))) + .build(); + + BuildResult result = runGradleTask( + testProjectDir, + COMPILE_FREGE_TASK_NAME, + "--compileItem=ch.fhnw.thga.Frob" + ); + + assertTrue( + project + .getTasks() + .getByName(COMPILE_FREGE_TASK_NAME) + instanceof CompileFregeTask + ); + assertEquals( + SUCCESS, + result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() + ); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.java" + ); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Completion.class" + ); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Frob.java" + ); + assertFileExists( + testProjectDir, + "build/classes/main/frege/ch/fhnw/thga/Frob.class" + ); + } + } + @Nested + @IndicativeSentencesGeneration( + separator = " -> ", + generator = DisplayNameGenerator.ReplaceUnderscores.class + ) + class Compile_frege_task_fails + { + @Test + void given_frege_code_and_illegal_compiler_flags( + @TempDir File testProjectDir) + throws Exception + { + String buildConfigWithIllegalCompilerFlags = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .compilerFlags("['-make', '-bla']") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(buildConfigWithIllegalCompilerFlags) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR)) + .build(); + + BuildResult result = runAndFailGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); + + assertTrue( + project + .getTasks() + .getByName(COMPILE_FREGE_TASK_NAME) + instanceof CompileFregeTask + ); + assertEquals( + FAILED, + result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() + ); + } + @Test + void given_two_dependent_frege_files_in_default_source_dir_and_without_make_compiler_flag( + @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 + ); + String minimalBuildFileConfigWithoutMake = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .compilerFlags("['-v']") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(minimalBuildFileConfigWithoutMake) + .fregeSourceFiles(() -> Stream.of(COMPLETION_FR, new FregeSourceFile( + String.format( + "%s/%s", + DEFAULT_RELATIVE_SOURCE_DIR, + "ch/fhnw/thga/Frob.fr" + ), + frobCode))) + .build(); + + BuildResult result = runAndFailGradleTask( + testProjectDir, + COMPILE_FREGE_TASK_NAME, + "--compileItem=ch.fhnw.thga.Frob" + ); + + assertTrue( + project + .getTasks() + .getByName(COMPILE_FREGE_TASK_NAME) + instanceof CompileFregeTask + ); + assertEquals( + FAILED, + result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() ); } - } + } } diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java index 9cd24c4..5159c92 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java @@ -43,299 +43,7 @@ import org.junit.jupiter.api.io.TempDir; public class FregePluginFunctionalTest { - private static final String NEW_LINE = System.lineSeparator(); - private static FregeDTOBuilder FREGE_BUILDER = FregeDTOBuilder.builder(); - private static final String FREGE_COMPLETION_MODULE_CODE = - String.join - ( - NEW_LINE, - "module ch.fhnw.thga.Completion where", - NEW_LINE, - NEW_LINE, - " complete :: Int -> (Int, String)", - NEW_LINE, - " complete i = (i, \"Frege rocks\")", - NEW_LINE - ); - - - - /*private BuildResult runAndFailGradleTask(String taskName, String... args) { - return GradleRunner.create().withProjectDir(testProjectDir).withPluginClasspath() - .withArguments(taskName) - .buildAndFail(); - }*/ - - static File createFregeSourceFile( - Path fregeFilePath, - String fregeSourceCode) - throws IOException - { - Files.createDirectories( - fregeFilePath - .getParent() - ); - File fregeFile = fregeFilePath.toFile(); - writeToFile(fregeFile, fregeSourceCode); - return fregeFile; - } - - static File setupLocalFregeCompiler(File testProjectDir) throws IOException - { - Path fregeCompiler = Paths.get("src/functionalTest/resources/frege3.25.84.jar"); - Files.createDirectories(testProjectDir.toPath().resolve("lib")); - return Files.copy( - fregeCompiler, - testProjectDir.toPath().resolve("lib/frege3.25.84.jar") - ).toFile(); - } - - @Nested - @IndicativeSentencesGeneration( - separator = " -> ", - generator = DisplayNameGenerator.ReplaceUnderscores.class - ) - class Compile_frege_task_works { - - /*@Test - void given_frege_code_in_default_source_dir_and_minimal_build_file_config( - @TempDir File testProjectDir) - throws Exception - { - String minimalBuildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'").build() - ); - Project project = createFregeGradleProject( - testProjectDir, - minimalBuildFileConfig - ); - Path completionFr = - testProjectDir - .toPath() - .resolve(Paths.get(DEFAULT_RELATIVE_SOURCE_DIR, "ch/fhnw/thga/Completion.fr") - ); - createFregeSourceFile(completionFr, FREGE_COMPLETION_MODULE_CODE); - setupLocalFregeCompiler(testProjectDir); - - BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); - - assertTrue( - project - .getTasks() - .getByName(COMPILE_FREGE_TASK_NAME) - instanceof CompileFregeTask - ); - assertEquals( - SUCCESS, - result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() - ); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java") - .toFile() - .exists()); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class") - .toFile() - .exists()); - } - - @Test - void given_frege_code_and_many_compiler_flags( - @TempDir File testProjectDir - ) throws Exception - { - String buildConfigWithCompilerFlags = createFregeSection( - FREGE_BUILDER - .version("'3.25.84'") - .release("'3.25alpha'") - .compilerFlags("['-v', '-make', '-O', '-hints']") - .build() - ); - Project project = createFregeGradleProject( - testProjectDir, - buildConfigWithCompilerFlags - ); - Path completionFr = - testProjectDir - .toPath() - .resolve(Paths.get(DEFAULT_RELATIVE_SOURCE_DIR, "ch/fhnw/thga/Completion.fr") - ); - createFregeSourceFile(completionFr, FREGE_COMPLETION_MODULE_CODE); - setupLocalFregeCompiler(testProjectDir); - - BuildResult result = runGradleTask(testProjectDir, COMPILE_FREGE_TASK_NAME); - - assertTrue( - project - .getTasks() - .getByName(COMPILE_FREGE_TASK_NAME) - instanceof CompileFregeTask - ); - assertEquals( - SUCCESS, - result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome() - ); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.java") - .toFile() - .exists()); - assertTrue(testProjectDir - .toPath() - .resolve("build/classes/main/frege/ch/fhnw/thga/Completion.class") - .toFile() - .exists()); - } - - @Test - void given_frege_code_in_custom_source_dir_and_custom_output_dir_and_minimal_build_file_config() - throws Exception { - Path customMainSourceDir = testProjectDir.toPath().resolve(Paths.get("src", "frege")); - Files.createDirectories(customMainSourceDir); - File completionFr = customMainSourceDir.resolve("Completion.fr").toFile(); - writeToFile(completionFr, FREGE_COMPLETION_MODULE_CODE); - String minimalBuildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'") - .mainSourceDir("layout.projectDirectory.dir('src/frege')") - .outputDir("layout.buildDirectory.dir('frege')").build()); - appendToFile(buildFile, minimalBuildFileConfig); - System.out.println(Files.readString(buildFile.toPath())); - - BuildResult result = runGradleTask(COMPILE_FREGE_TASK_NAME); - - assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask); - assertEquals(SUCCESS, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - assertTrue( - new File(testProjectDir.getAbsolutePath() - + "/build/frege/ch/fhnw/thga/Completion.java").exists()); - assertTrue( - new File(testProjectDir.getAbsolutePath() - + "/build/frege/ch/fhnw/thga/Completion.class").exists()); - } - - @Test - void and_is_up_to_date_given_no_code_changes() throws Exception { - String completionFr = "Completion.fr"; - String minimalBuildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'").build()); - setupDefaultFregeProjectStructure(FREGE_COMPLETION_MODULE_CODE, completionFr, minimalBuildFileConfig); - - System.out.println(Files.readString(buildFile.toPath())); - BuildResult first = runGradleTask(COMPILE_FREGE_TASK_NAME); - assertEquals(SUCCESS, first.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - - BuildResult second = runGradleTask(COMPILE_FREGE_TASK_NAME); - assertEquals(UP_TO_DATE, second.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - } - - @Test - void and_is_cached_given_cache_hit() throws Exception { - String completionFr = "Completion.fr"; - String minimalBuildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'").build()); - setupDefaultFregeProjectStructure(FREGE_COMPLETION_MODULE_CODE, completionFr, minimalBuildFileConfig); - - BuildResult first = runGradleTask(COMPILE_FREGE_TASK_NAME, "--build-cache"); - assertEquals(SUCCESS, first.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - - String codeChange = String.join(NEW_LINE, "module ch.fhnw.thga.Completion where", NEW_LINE, - NEW_LINE, - " frob :: Int -> (Int, String)", NEW_LINE, " frob i = (i, \"Frege rocks\")", - NEW_LINE); - setupDefaultFregeProjectStructure(codeChange, completionFr, ""); - - System.out.println(Files.readString(buildFile.toPath())); - BuildResult second = runGradleTask(COMPILE_FREGE_TASK_NAME, "--build-cache"); - assertEquals(SUCCESS, second.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - - setupDefaultFregeProjectStructure(FREGE_COMPLETION_MODULE_CODE, completionFr, ""); - BuildResult third = runGradleTask(COMPILE_FREGE_TASK_NAME, "--build-cache"); - assertEquals(FROM_CACHE, third.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - } - - @Test - void given_two_dependent_frege_files_in_default_source_dir_and_minimal_build_file_config() - throws Exception { - String completionFr = "Completion.fr"; - String frobFr = "Frob.fr"; - 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); - - String minimalBuildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'").build()); - setupDefaultFregeProjectStructure(FREGE_COMPLETION_MODULE_CODE, completionFr, minimalBuildFileConfig); - setupDefaultFregeProjectStructure(frobCode, frobFr, ""); - - System.out.println(Files.readString(buildFile.toPath())); - BuildResult result = runGradleTask(COMPILE_FREGE_TASK_NAME); - - assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask); - assertEquals(SUCCESS, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - assertTrue(new File( - testProjectDir.getAbsolutePath() - + "/build/classes/main/frege/ch/fhnw/thga/Completion.java") - .exists()); - assertTrue(new File( - testProjectDir.getAbsolutePath() - + "/build/classes/main/frege/ch/fhnw/thga/Completion.class") - .exists()); - assertTrue(new File(testProjectDir.getAbsolutePath() - + "/build/classes/main/frege/ch/fhnw/thga/Frob.java") - .exists()); - assertTrue(new File(testProjectDir.getAbsolutePath() - + "/build/classes/main/frege/ch/fhnw/thga/Frob.class") - .exists()); - } - } - - @Nested - @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) - class Compile_frege_task_fails { - @Test - void given_frege_code_and_illegal_compiler_flags() throws Exception { - String completionFr = "Completion.fr"; - String buildConfigWithIllegalCompilerFlags = createFregeSection(FREGE_BUILDER - .version("'3.25.84'") - .release("'3.25alpha'").compilerFlags("['-make', '-bla']").build()); - setupDefaultFregeProjectStructure(FREGE_COMPLETION_MODULE_CODE, completionFr, - buildConfigWithIllegalCompilerFlags); - - BuildResult result = runAndFailGradleTask(COMPILE_FREGE_TASK_NAME); - - assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask); - assertEquals(FAILED, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - } - - @Test - void given_two_dependent_frege_files_in_default_source_dir_and_without_make_compiler_flag() - throws Exception { - String completionFr = "Completion.fr"; - String frobFr = "Frob.fr"; - 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); - - String minimalBuildFileConfigWithoutMake = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'").compilerFlags("['-v']") - .build()); - setupDefaultFregeProjectStructure(FREGE_COMPLETION_MODULE_CODE, completionFr, - minimalBuildFileConfigWithoutMake); - setupDefaultFregeProjectStructure(frobCode, frobFr, ""); - - System.out.println("Build File: " + Files.readString(buildFile.toPath())); - - BuildResult result = runAndFailGradleTask(COMPILE_FREGE_TASK_NAME); - - - assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask); - assertEquals(FAILED, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - } - } - - @Nested + /*@Nested @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) class Run_frege_task_works { @Test @@ -444,6 +152,6 @@ public class FregePluginFunctionalTest project.getTasks().getByName(REPL_FREGE_TASK_NAME) instanceof ReplFregeTask); assertEquals(FAILED, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - }*/ - } + } + }*/ } \ No newline at end of file diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java index a0d46a4..3b4ee49 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java @@ -53,15 +53,25 @@ public class SharedFunctionalTestLogic return destination; } - static BuildResult runGradleTask(File testProjectDir, String... taskName) + static BuildResult runGradleTask(File testProjectDir, String... args) { return GradleRunner .create() .withProjectDir(testProjectDir) .withPluginClasspath() - .withArguments(taskName) + .withArguments(args) .build(); } + + static BuildResult runAndFailGradleTask(File testProjectDir, String... args) + { + return GradleRunner + .create() + .withProjectDir(testProjectDir) + .withPluginClasspath() + .withArguments(args) + .buildAndFail(); + } static File createSettingsFile(File testProjectDir) throws IOException { diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java index bb87cdc..d2c6aeb 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java @@ -4,9 +4,11 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.List; import java.util.function.Supplier; import java.util.stream.Stream; @@ -98,8 +100,9 @@ public final class FregeProjectBuilder implements ProjectRoot, BuildFile, Build projectRoot .toPath() .resolve(DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR) - .resolve(LOCAL_COMPILER_PATH.getFileName()) - ).toFile(); + .resolve(LOCAL_COMPILER_PATH.getFileName()), + StandardCopyOption.REPLACE_EXISTING) + .toFile(); } private FregeProjectBuilder() {} @@ -162,7 +165,7 @@ public final class FregeProjectBuilder implements ProjectRoot, BuildFile, Build createGradleFile("settings.gradle", settingsFile); createGradleFile("build.gradle", buildFile); if (useLocalFregeCompiler) setupLocalFregeCompilerInDefaultPath(); - fregeSourceFiles.get().map(this::writeToFile).findFirst(); + fregeSourceFiles.get().map(this::writeToFile).toArray(); Project project = ProjectBuilder .builder() .withProjectDir(projectRoot) diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java index c280607..a3c42d2 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java @@ -25,6 +25,7 @@ import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.PathSensitive; import org.gradle.api.tasks.PathSensitivity; import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.options.Option; @CacheableTask public abstract class CompileFregeTask extends DefaultTask { @@ -46,7 +47,10 @@ public abstract class CompileFregeTask extends DefaultTask { public abstract Property getFregeDependencies(); @Input - public abstract Property getFregeMainModuleName(); + @Option(option = "compileItem", + description = "The absolute path to the frege file or the module name" + ) + public abstract Property getFregeCompileItem(); @OutputDirectory public abstract DirectoryProperty getFregeOutputDir(); @@ -63,7 +67,7 @@ public abstract class CompileFregeTask extends DefaultTask { @Internal public final Provider> getCompileItems() { - return getFregeMainModuleName() + return getFregeCompileItem() .map(name -> { return name.isEmpty() ? getFregeSourceFiles().get() diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java index ce2ce01..957dc83 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java @@ -52,7 +52,7 @@ public class FregePlugin implements Plugin task.getFregeOutputDir().set(extension.getOutputDir()); task.getFregeCompilerFlags().set(extension.getCompilerFlags()); task.getFregeDependencies().set(implementation.getAsPath()); - task.getFregeMainModuleName().set(""); + task.getFregeCompileItem().set(""); } ); @@ -66,7 +66,7 @@ public class FregePlugin implements Plugin task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getFregeMainModuleName().set(task.getMainModule()); + compileTask.getFregeCompileItem().set(task.getMainModule()); return compileTask; }) .get()); @@ -86,7 +86,7 @@ public class FregePlugin implements Plugin task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getFregeMainModuleName().set(task.getReplSource()); + compileTask.getFregeCompileItem().set(task.getReplSource()); return compileTask; }) .get()); -- cgit From adbe33ac3ce08e4c5d73ba659313c7904273887a Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Sat, 12 Feb 2022 19:15:16 +0100 Subject: refactor: finishes the runFregeTask refctoring --- build.gradle | 1 + .../CompileFregeTaskFunctionalTest.java | 2 +- .../gradleplugins/RunFregeTaskFunctionalTest.java | 185 +++++++++++++++++++++ .../SetupFregeTaskFunctionalTest.java | 2 + .../gradleplugins/SharedFunctionalTestLogic.java | 4 +- .../fregeproject/FregeSourceFile.java | 2 - 6 files changed, 191 insertions(+), 5 deletions(-) create mode 100644 src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java (limited to 'src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java') diff --git a/build.gradle b/build.gradle index 9baa197..084e5f1 100644 --- a/build.gradle +++ b/build.gradle @@ -73,5 +73,6 @@ tasks.withType(Test).configureEach { includeEngines 'jqwik', 'junit-jupiter' maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1 + excludeTags 'network' } } diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java index 42a3de2..af3225d 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java @@ -5,6 +5,7 @@ import static ch.fhnw.thga.gradleplugins.FregePlugin.COMPILE_FREGE_TASK_NAME; 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 ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.NEW_LINE; 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; @@ -28,7 +29,6 @@ import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile; public class CompileFregeTaskFunctionalTest { - private static final String NEW_LINE = System.lineSeparator(); private static final FregeSourceFile COMPLETION_FR = new FregeSourceFile( String.format("%s/%s", DEFAULT_RELATIVE_SOURCE_DIR, diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java new file mode 100644 index 0000000..c0c870a --- /dev/null +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java @@ -0,0 +1,185 @@ +package ch.fhnw.thga.gradleplugins; + +import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR; +import static ch.fhnw.thga.gradleplugins.FregePlugin.RUN_FREGE_TASK_NAME; +import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.NEW_LINE; +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.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; +import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile; + +public class RunFregeTaskFunctionalTest +{ + private static final FregeSourceFile MAIN_FR = new FregeSourceFile( + String.format( + "%s/%s", + DEFAULT_RELATIVE_SOURCE_DIR, + "ch/fhnw/thga/Main.fr" + ), + String.join( + NEW_LINE, + "module ch.fhnw.thga.Main where", + NEW_LINE, + NEW_LINE, + " main = do", + NEW_LINE, + " println \"Frege rocks\"", + NEW_LINE + ) + ); + @Nested + @IndicativeSentencesGeneration( + separator = " -> ", + generator = DisplayNameGenerator.ReplaceUnderscores.class + ) + class Run_frege_task_works + { + @Test + void given_frege_file_with_main_function_and_main_module_config( + @TempDir File testProjectDir) + throws Exception + { + String mainBuildConfig = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .mainModule("'ch.fhnw.thga.Main'") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(mainBuildConfig) + .fregeSourceFiles(() -> Stream.of(MAIN_FR)) + .build(); + + BuildResult result = runGradleTask(testProjectDir, RUN_FREGE_TASK_NAME); + + assertTrue( + project + .getTasks() + .getByName(RUN_FREGE_TASK_NAME) + instanceof RunFregeTask + ); + assertEquals( + SUCCESS, + result.task(":" + RUN_FREGE_TASK_NAME).getOutcome() + ); + assertTrue(result.getOutput().contains("Frege rocks")); + } + + @Test + void given_frege_file_with_main_function_and_main_module_command_line_option( + @TempDir File testProjectDir) + throws Exception + { + String mainBuildConfig = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(mainBuildConfig) + .fregeSourceFiles(() -> Stream.of(MAIN_FR)) + .build(); + + BuildResult result = runGradleTask( + testProjectDir, + RUN_FREGE_TASK_NAME, + "--mainModule=ch.fhnw.thga.Main"); + + assertTrue( + project + .getTasks() + .getByName(RUN_FREGE_TASK_NAME) + instanceof RunFregeTask + ); + assertEquals( + SUCCESS, + result.task(":" + RUN_FREGE_TASK_NAME).getOutcome() + ); + assertTrue(result.getOutput().contains("Frege rocks")); + } + } + @Nested + @IndicativeSentencesGeneration( + separator = " -> ", + generator = DisplayNameGenerator.ReplaceUnderscores.class + ) + class Run_frege_task_fails + { + @Test + void given_frege_file_without_main_function( + @TempDir File testProjectDir) + throws Exception + { + String mainBuildConfig = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .mainModule("'ch.fhnw.thga.Main'") + .build() + ); + Project project = FregeProjectBuilder + .builder() + .projectRoot(testProjectDir) + .buildFile(mainBuildConfig) + .fregeSourceFiles(() -> Stream.of(new FregeSourceFile( + String.format( + "%s/%s", + DEFAULT_RELATIVE_SOURCE_DIR, + "ch/fhnw/thga/Main.fr" + ), + String.join( + NEW_LINE, + "module ch.fhnw.thga.Main where", + NEW_LINE, + NEW_LINE, + " add a b = a + b", + NEW_LINE + ) + ))) + .build(); + + BuildResult result = runAndFailGradleTask( + testProjectDir, + RUN_FREGE_TASK_NAME + ); + + assertTrue( + project + .getTasks() + .getByName(RUN_FREGE_TASK_NAME) + instanceof RunFregeTask + ); + assertEquals( + FAILED, + result.task(":" + RUN_FREGE_TASK_NAME).getOutcome() + ); + assertTrue(result.getOutput().contains("Main method not found")); + } + } +} diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SetupFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SetupFregeTaskFunctionalTest.java index 825c555..6781764 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SetupFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SetupFregeTaskFunctionalTest.java @@ -17,11 +17,13 @@ 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.Tag; import org.junit.jupiter.api.io.TempDir; import ch.fhnw.thga.gradleplugins.fregeproject.FregeProjectBuilder; import ch.fhnw.thga.gradleplugins.fregeproject.ProjectRoot; +@Tag("network") class SetupFregeTaskFunctionalTest { private static FregeDTOBuilder FREGE_BUILDER = FregeDTOBuilder.builder(); diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java index 3b4ee49..c98d7ac 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java @@ -17,8 +17,8 @@ import org.gradle.testkit.runner.GradleRunner; public class SharedFunctionalTestLogic { - - static String createFregeSection(FregeDTO fregeDTO) + public static final String NEW_LINE = System.lineSeparator(); + static String createFregeSection(FregeDTO fregeDTO) { return String.format( "%s {%s %s%s}", diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java index d45e158..c161dbd 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java @@ -1,7 +1,5 @@ package ch.fhnw.thga.gradleplugins.fregeproject; -import java.nio.file.Path; - public class FregeSourceFile { private final String modulePath; -- cgit 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 --- .gitignore | 3 +- .../CompileFregeTaskFunctionalTest.java | 26 +--- .../gradleplugins/FregePluginFunctionalTest.java | 157 --------------------- .../gradleplugins/ReplFregeTaskFunctionalTest.java | 102 +++++++++++++ .../gradleplugins/RunFregeTaskFunctionalTest.java | 10 +- .../gradleplugins/SharedFunctionalTestLogic.java | 27 ++++ .../fregeproject/FregeProjectBuilder.java | 18 +-- .../fregeproject/FregeSourceFile.java | 4 +- .../ch/fhnw/thga/gradleplugins/FregeExtension.java | 8 +- .../ch/fhnw/thga/gradleplugins/FregePlugin.java | 12 +- .../ch/fhnw/thga/gradleplugins/ReplFregeTask.java | 28 +++- .../fhnw/thga/gradleplugins/SharedTaskLogic.java | 8 +- .../java/ch/fhnw/thga/gradleplugins/Builder.java | 2 +- .../java/ch/fhnw/thga/gradleplugins/FregeDTO.java | 10 +- .../fhnw/thga/gradleplugins/FregeDTOBuilder.java | 8 +- .../thga/gradleplugins/SharedTaskLogicTest.java | 42 +++++- 16 files changed, 231 insertions(+), 234 deletions(-) delete mode 100644 src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java create mode 100644 src/functionalTest/java/ch/fhnw/thga/gradleplugins/ReplFregeTaskFunctionalTest.java (limited to 'src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java') diff --git a/.gitignore b/.gitignore index 151dd40..0292f5c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ build .settings bin .DS_Store -lib \ No newline at end of file +lib +.jqwik-database \ 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 af3225d..31e6d05 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/CompileFregeTaskFunctionalTest.java @@ -6,6 +6,8 @@ import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSe import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runAndFailGradleTask; 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 org.gradle.testkit.runner.TaskOutcome.FAILED; import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE; import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; @@ -29,30 +31,6 @@ import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile; public class CompileFregeTaskFunctionalTest { - private static final FregeSourceFile COMPLETION_FR = new FregeSourceFile( - String.format("%s/%s", - DEFAULT_RELATIVE_SOURCE_DIR, - "ch/fhnw/thga/Completion.fr"), - String.join - ( - NEW_LINE, - "module ch.fhnw.thga.Completion where", - NEW_LINE, - NEW_LINE, - " complete :: Int -> (Int, String)", - NEW_LINE, - " complete i = (i, \"Frege rocks\")", - NEW_LINE - ) - ); - private static final String MINIMAL_BUILD_FILE_CONFIG = createFregeSection( - FregeDTOBuilder - .builder() - .version("'3.25.84'") - .release("'3.25alpha'") - .build() - ); - private static final boolean assertFileExists( File testProjectDir, String relativeFilePath) diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java deleted file mode 100644 index 5159c92..0000000 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java +++ /dev/null @@ -1,157 +0,0 @@ -package ch.fhnw.thga.gradleplugins; - -import static ch.fhnw.thga.gradleplugins.FregePlugin.COMPILE_FREGE_TASK_NAME; -import static ch.fhnw.thga.gradleplugins.FregePlugin.SETUP_FREGE_TASK_NAME; -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.FregePlugin.REPL_FREGE_TASK_NAME; -import static ch.fhnw.thga.gradleplugins.FregePlugin.RUN_FREGE_TASK_NAME; -import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR; -import static ch.fhnw.thga.gradleplugins.GradleBuildFileConversionTest.createPluginsSection; -import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSection; -import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.writeToFile; -import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeGradleProject; -import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask; -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; -import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.stream.Stream; - -import org.gradle.api.Project; -import org.gradle.testfixtures.ProjectBuilder; -import org.gradle.testkit.runner.BuildResult; -import org.gradle.testkit.runner.GradleRunner; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -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; - -public class FregePluginFunctionalTest -{ - /*@Nested - @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) - class Run_frege_task_works { - @Test - void given_frege_file_with_main_function_and_main_module_config() throws Exception { - String fregeCode = String.join(NEW_LINE, "module ch.fhnw.thga.Main where", NEW_LINE, NEW_LINE, - " main = do", NEW_LINE, " println \"Frege rocks\"", NEW_LINE); - String mainFr = "Main.fr"; - String buildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'") - .mainModule("'ch.fhnw.thga.Main'").build()); - setupDefaultFregeProjectStructure(fregeCode, mainFr, buildFileConfig); - - BuildResult result = runGradleTask(RUN_FREGE_TASK_NAME); - assertTrue(project.getTasks().getByName(RUN_FREGE_TASK_NAME) instanceof RunFregeTask); - assertEquals(SUCCESS, result.task(":" + RUN_FREGE_TASK_NAME).getOutcome()); - assertTrue(result.getOutput().contains("Frege rocks")); - } - - @Test - void given_frege_file_without_main_function() throws Exception { - String completionFr = "Completion.fr"; - String buildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'") - .mainModule("'ch.fhnw.thga.Completion'").build()); - setupDefaultFregeProjectStructure(FREGE_COMPLETION_MODULE_CODE, completionFr, buildFileConfig); - - BuildResult result = runAndFailGradleTask(RUN_FREGE_TASK_NAME); - assertTrue(project.getTasks().getByName(RUN_FREGE_TASK_NAME) instanceof RunFregeTask); - assertEquals(FAILED, result.task(":" + RUN_FREGE_TASK_NAME).getOutcome()); - assertTrue(result.getOutput().contains("Main method not found")); - } - - @Test - void given_frege_file_with_main_function_and_main_module_command_line_option() throws Exception { - String fregeCode = String.join(NEW_LINE, "module ch.fhnw.thga.Main where", NEW_LINE, NEW_LINE, - " main = do", NEW_LINE, " println \"Frege rocks\"", NEW_LINE); - String mainFr = "Main.fr"; - String buildFileConfig = createFregeSection( - FREGE_BUILDER.version("'3.25.84'").release("'3.25alpha'").build()); - setupDefaultFregeProjectStructure(fregeCode, mainFr, buildFileConfig); - - BuildResult result = runGradleTask(RUN_FREGE_TASK_NAME, "--mainModule=ch.fhnw.thga.Main"); - assertTrue(project.getTasks().getByName(RUN_FREGE_TASK_NAME) instanceof RunFregeTask); - assertEquals(SUCCESS, result.task(":" + RUN_FREGE_TASK_NAME).getOutcome()); - assertTrue(result.getOutput().contains("Frege rocks")); - } - } - - - @Nested - @IndicativeSentencesGeneration( - separator = " -> ", - generator = DisplayNameGenerator.ReplaceUnderscores.class) - class Repl_frege_task_works - { - @Test - void given_minimal_build_file_config_with_replModule() throws Exception - { - String completionFr = "Completion.fr"; - String minimalReplModuleConfig = createFregeSection( - FREGE_BUILDER - .version("'3.25.84'") - .release("'3.25alpha'") - .replSource(String.format("'ch.fhnw.thga.Completion'")) - .build()); - setupDefaultFregeProjectStructure( - FREGE_COMPLETION_MODULE_CODE, - completionFr, - minimalReplModuleConfig); - - BuildResult result = runGradleTask(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")); - assertTrue(result.getOutput().contains("Completion.java")); - } - } - - @Nested - @IndicativeSentencesGeneration( - separator = " -> ", - generator = DisplayNameGenerator.ReplaceUnderscores.class) - class Repl_frege_task_fails - { - @Test - void given_minimal_build_file_config_without_repl_module() throws Exception - { - String completionFr = "Completion.fr"; - String minimalBuildFileConfig = createFregeSection( - FREGE_BUILDER - .version("'3.25.84'") - .release("'3.25alpha'") - .build()); - setupDefaultFregeProjectStructure( - FREGE_COMPLETION_MODULE_CODE, - completionFr, - minimalBuildFileConfig); - - BuildResult result = runAndFailGradleTask(REPL_FREGE_TASK_NAME); - - assertTrue( - project.getTasks().getByName(REPL_FREGE_TASK_NAME) - instanceof ReplFregeTask); - assertEquals(FAILED, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome()); - } - }*/ -} \ No newline at end of file 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 diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java index c0c870a..c4d5bad 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/RunFregeTaskFunctionalTest.java @@ -6,6 +6,7 @@ import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.NEW_LINE; 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 ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.MINIMAL_BUILD_FILE_CONFIG; import static org.gradle.testkit.runner.TaskOutcome.FAILED; import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -91,17 +92,10 @@ public class RunFregeTaskFunctionalTest @TempDir File testProjectDir) throws Exception { - String mainBuildConfig = createFregeSection( - FregeDTOBuilder - .builder() - .version("'3.25.84'") - .release("'3.25alpha'") - .build() - ); Project project = FregeProjectBuilder .builder() .projectRoot(testProjectDir) - .buildFile(mainBuildConfig) + .buildFile(MINIMAL_BUILD_FILE_CONFIG) .fregeSourceFiles(() -> Stream.of(MAIN_FR)) .build(); diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java index c98d7ac..51fa256 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java @@ -3,6 +3,7 @@ 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 ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR; import java.io.BufferedWriter; import java.io.File; @@ -15,9 +16,35 @@ import org.gradle.testfixtures.ProjectBuilder; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.GradleRunner; +import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile; + public class SharedFunctionalTestLogic { public static final String NEW_LINE = System.lineSeparator(); + public static final String MINIMAL_BUILD_FILE_CONFIG = createFregeSection( + FregeDTOBuilder + .builder() + .version("'3.25.84'") + .release("'3.25alpha'") + .build() + ); + public static final FregeSourceFile COMPLETION_FR = new FregeSourceFile( + String.format("%s/%s", + DEFAULT_RELATIVE_SOURCE_DIR, + "ch/fhnw/thga/Completion.fr"), + String.join + ( + NEW_LINE, + "module ch.fhnw.thga.Completion where", + NEW_LINE, + NEW_LINE, + " complete :: Int -> (Int, String)", + NEW_LINE, + " complete i = (i, \"Frege rocks\")", + NEW_LINE + ) + ); + static String createFregeSection(FregeDTO fregeDTO) { return String.format( diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java index d2c6aeb..0cb0746 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeProjectBuilder.java @@ -1,27 +1,23 @@ package ch.fhnw.thga.gradleplugins.fregeproject; +import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR; +import static ch.fhnw.thga.gradleplugins.FregePlugin.FREGE_PLUGIN_ID; +import static ch.fhnw.thga.gradleplugins.GradleBuildFileConversionTest.createPluginsSection; + import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.List; import java.util.function.Supplier; import java.util.stream.Stream; import org.gradle.api.Project; import org.gradle.testfixtures.ProjectBuilder; -import ch.fhnw.thga.gradleplugins.FregeDTO; -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 ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR; - public final class FregeProjectBuilder implements ProjectRoot, BuildFile, Build { private static final String LATEST_COMPILER_VERSION = "3.25.84"; @@ -82,12 +78,6 @@ public final class FregeProjectBuilder implements ProjectRoot, BuildFile, Build throw new RuntimeException(e.getMessage(), e.getCause()); } } - - private static File appendToFile(File destination, String content) throws IOException - { - writeFile(destination, System.lineSeparator() + content, true); - return destination; - } private File setupLocalFregeCompilerInDefaultPath() throws IOException { diff --git a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java index c161dbd..858f6d4 100644 --- a/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java +++ b/src/functionalTest/java/ch/fhnw/thga/gradleplugins/fregeproject/FregeSourceFile.java @@ -7,8 +7,8 @@ public class FregeSourceFile public FregeSourceFile(String modulePath, String sourceCode) { - this.modulePath = modulePath; - this.sourceCode = sourceCode; + this.modulePath = modulePath; + this.sourceCode = sourceCode; } public String getFregeModulePath() diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java index cfcd393..18a9500 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java @@ -9,7 +9,8 @@ import org.gradle.api.file.ProjectLayout; import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; -public abstract class FregeExtension { +public abstract class FregeExtension +{ public static final String DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR = "lib"; public static final String DEFAULT_RELATIVE_OUTPUT_DIR = "classes/main/frege"; public static final String DEFAULT_RELATIVE_SOURCE_DIR = "src/main/frege"; @@ -29,10 +30,11 @@ public abstract class FregeExtension { public abstract ListProperty getCompilerFlags(); - public abstract Property getReplSource(); + public abstract Property getReplModule(); @Inject - public FregeExtension(ProjectLayout projectLayout) { + public FregeExtension(ProjectLayout projectLayout) + { getCompilerDownloadDir() .convention(projectLayout.getProjectDirectory().dir(DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR)); diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java index 957dc83..9b725ac 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java @@ -68,8 +68,8 @@ public class FregePlugin implements Plugin { compileTask.getFregeCompileItem().set(task.getMainModule()); return compileTask; - }) - .get()); + } + )); task.getFregeCompilerJar().set( setupFregeCompilerTask.get().getFregeCompilerOutputPath()); task.getFregeOutputDir().set(extension.getOutputDir()); @@ -82,14 +82,14 @@ public class FregePlugin implements Plugin ReplFregeTask.class, task -> { - task.getReplSource().set(extension.getReplSource()); + task.getReplModule().set(extension.getReplModule()); task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getFregeCompileItem().set(task.getReplSource()); + compileTask.getFregeCompileItem().set(task.getReplModule()); return compileTask; - }) - .get()); + } + )); task.getFregeCompilerJar().set( setupFregeCompilerTask.get().getFregeCompilerOutputPath()); task.getFregeOutputDir().set(extension.getOutputDir()); diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java index 7903d7e..67af55e 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java @@ -1,7 +1,9 @@ package ch.fhnw.thga.gradleplugins; +import static ch.fhnw.thga.gradleplugins.SharedTaskLogic.extractClassNameFromFregeModuleName; 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.provider.Property; import org.gradle.api.provider.Provider; @@ -25,15 +27,29 @@ public abstract class ReplFregeTask extends DefaultTask { public abstract DirectoryProperty getFregeOutputDir(); @Input - @Option(option = "replSource", - description = "The filename which you want to load into the repl, e.g. 'myFregeFile.fr'") - public abstract Property getReplSource(); + @Option(option = "replModule", + description = "The full name of the module which you want to load into the repl, e.g. 'my.mod.Name'") + public abstract Property getReplModule(); @Internal public final Provider getReplClassName() { - return getReplSource() - .map(replSource -> replSource.substring(replSource.lastIndexOf("."))); + return getReplModule() + .map(replSource -> extractClassNameFromFregeModuleName(replSource)); + } + + @Internal + public final Provider getClasspathWithoutReplClassFile() + { + 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() + ) + ))); } @TaskAction @@ -45,7 +61,7 @@ public abstract class ReplFregeTask extends DefaultTask { getProject(), getFregeDependencies(), getFregeCompilerJar(), - getFregeOutputDir()) + getClasspathWithoutReplClassFile()) .get().getAsPath(), REPL_MAIN_CLASS)); } diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java b/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java index 57af617..e6191a3 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java @@ -5,7 +5,8 @@ import org.gradle.api.file.FileCollection; import org.gradle.api.provider.Provider; import org.gradle.api.provider.Property; -public final class SharedTaskLogic { +public final class SharedTaskLogic +{ private SharedTaskLogic() {}; public static final Provider setupClasspath( @@ -19,4 +20,9 @@ public final class SharedTaskLogic { : project.files(depsClasspath, paths); }); } + + public static final String extractClassNameFromFregeModuleName(String moduleName) + { + return moduleName.substring(moduleName.lastIndexOf(".") + 1); + } } diff --git a/src/test/java/ch/fhnw/thga/gradleplugins/Builder.java b/src/test/java/ch/fhnw/thga/gradleplugins/Builder.java index df070a2..d379013 100644 --- a/src/test/java/ch/fhnw/thga/gradleplugins/Builder.java +++ b/src/test/java/ch/fhnw/thga/gradleplugins/Builder.java @@ -15,7 +15,7 @@ public interface Builder { Builder compilerFlags(String compilerFlags); - Builder replSource(String replSource); + Builder replModule(String replModule); FregeDTO build(); } diff --git a/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTO.java b/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTO.java index e092eac..bea1e81 100644 --- a/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTO.java +++ b/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTO.java @@ -14,7 +14,7 @@ public class FregeDTO { public final String outputDir; public final String mainModule; public final String compilerFlags; - public final String replSource; + public final String replModule; public FregeDTO( String version, @@ -24,7 +24,7 @@ public class FregeDTO { String outputDir, String mainModule, String compilerFlags, - String replSource) { + String replModule) { this.version = version; this.release = release; this.compilerDownloadDir = compilerDownloadDir; @@ -32,7 +32,7 @@ public class FregeDTO { this.outputDir = outputDir; this.mainModule = mainModule; this.compilerFlags = compilerFlags; - this.replSource = replSource; + this.replModule = replModule; } public String getVersion() { @@ -63,8 +63,8 @@ public class FregeDTO { return compilerFlags; } - public String getReplSource() { - return replSource; + public String getReplModule() { + return replModule; } private String getFieldValue(Field field) { diff --git a/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTOBuilder.java b/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTOBuilder.java index d7fe9f1..58e8396 100644 --- a/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTOBuilder.java +++ b/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTOBuilder.java @@ -8,7 +8,7 @@ public final class FregeDTOBuilder implements Builder { private String outputDir = ""; private String mainModule = ""; private String compilerFlags = ""; - private String replSource = ""; + private String replModule = ""; private FregeDTOBuilder() {} @@ -65,9 +65,9 @@ public final class FregeDTOBuilder implements Builder { } @Override - public Builder replSource(String replSource) + public Builder replModule(String replModule) { - this.replSource = replSource; + this.replModule = replModule; return this; } @@ -80,6 +80,6 @@ public final class FregeDTOBuilder implements Builder { outputDir, mainModule, compilerFlags, - replSource); + replModule); } } diff --git a/src/test/java/ch/fhnw/thga/gradleplugins/SharedTaskLogicTest.java b/src/test/java/ch/fhnw/thga/gradleplugins/SharedTaskLogicTest.java index 6f10d70..bb353a8 100644 --- a/src/test/java/ch/fhnw/thga/gradleplugins/SharedTaskLogicTest.java +++ b/src/test/java/ch/fhnw/thga/gradleplugins/SharedTaskLogicTest.java @@ -1,14 +1,52 @@ package ch.fhnw.thga.gradleplugins; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static ch.fhnw.thga.gradleplugins.SharedTaskLogic.extractClassNameFromFregeModuleName; + import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; +import net.jqwik.api.Example; +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; + @TestInstance(Lifecycle.PER_CLASS) public class SharedTaskLogicTest { - @Test + @Example void given_valid_frege_module_name_then_can_extract_class_name() { + assertEquals( + extractClassNameFromFregeModuleName("ch.fhnw.thga.Completion"), + "Completion" + ); + } + + @Example + void given_empty_frege_module_name_then_returns_empty_string() + { + assertEquals( + extractClassNameFromFregeModuleName(""), + "" + ); + } + + @Example + void module_name_without_a_package_equals_class_name() + { + assertEquals( + extractClassNameFromFregeModuleName("Completion"), + "Completion" + ); + } + + @Property + void class_name_is_the_suffix_of_the_full_module_name( + @ForAll String aString) + { + String expectedFregeClassName = "Completion"; + assertEquals( + extractClassNameFromFregeModuleName(String.join(".", expectedFregeClassName)), + expectedFregeClassName); } } \ 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/CompileFregeTaskFunctionalTest.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