1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
package ch.fhnw.thga.gradleplugins;
import static ch.fhnw.thga.gradleplugins.FregeDTOTest.createPluginsSection;
import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_DOWNLOAD_DIRECTORY;
import static ch.fhnw.thga.gradleplugins.FregePlugin.COMPILE_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.SETUP_FREGE_TASK_NAME;
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.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class FregePluginFunctionalTest {
private final FregeDTOBuilder fregeBuilder = new FregeDTOBuilder();
@TempDir
File testProjectDir;
private File buildFile;
private File settingsFile;
private Project project;
private void writeFile(File destination, String content, boolean append) throws IOException {
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(destination, append));
output.write(content);
} finally {
if (output != null) {
output.close();
}
}
}
private void writeToFile(File destination, String content) throws IOException {
writeFile(destination, content, false);
}
private void appendToFile(File destination, String content) throws IOException {
writeFile(destination, "\n" + content, true);
}
private static String createFregeSection(FregeDTO fregeDTO) {
return String.format("%s {\n %s\n}", FREGE_EXTENSION_NAME, fregeDTO.toBuildFile());
}
private void assertGradleTaskOutcome(String buildFileTaskConfig, String taskName) throws Exception {
appendToFile(buildFile, buildFileTaskConfig);
BuildResult result = GradleRunner.create().withProjectDir(testProjectDir).withPluginClasspath()
.withArguments(taskName).build();
System.out.println(result.getOutput());
assertEquals(SUCCESS, result.task(":" + taskName).getOutcome());
}
@BeforeEach
void setup() throws Exception {
buildFile = new File(testProjectDir, "build.gradle");
settingsFile = new File(testProjectDir, "settings.gradle");
writeToFile(buildFile, createPluginsSection(Stream.of(FREGE_PLUGIN_ID)));
writeToFile(settingsFile, "rootProject.name='frege-plugin'");
project = ProjectBuilder.builder().withProjectDir(testProjectDir).build();
project.getPluginManager().apply(FREGE_PLUGIN_ID);
}
@Test
void given_setup_frege_compiler_task_when_frege_version_and_frege_release_is_specified_then_frege_compiler_is_successfully_downloaded_to_default_directory()
throws Exception {
FregeDTO minimalFregeDTO = fregeBuilder.version("'3.25.84'").release("'3.25alpha'").build();
assertTrue(project.getTasks().getByName(SETUP_FREGE_TASK_NAME) instanceof SetupFregeTask);
assertGradleTaskOutcome(createFregeSection(minimalFregeDTO), SETUP_FREGE_TASK_NAME);
assertTrue(testProjectDir.toPath().resolve(Paths.get(DEFAULT_DOWNLOAD_DIRECTORY, "frege3.25.84.jar")).toFile()
.exists());
}
@Test
void given_setup_frege_compiler_task_when_frege_version_and_frege_release_and_download_directory_is_specified_then_frege_compiler_is_successfully_downloaded_to_specified_directory()
throws Exception {
FregeDTO fregeDTO = fregeBuilder.version("'3.25.84'").release("'3.25alpha'")
.compilerDownloadDir("layout.projectDirectory.dir('dist')").build();
assertTrue(project.getTasks().getByName(SETUP_FREGE_TASK_NAME) instanceof SetupFregeTask);
assertGradleTaskOutcome(createFregeSection(fregeDTO), SETUP_FREGE_TASK_NAME);
assertTrue(testProjectDir.toPath().resolve(Paths.get("dist", "frege3.25.84.jar")).toFile().exists());
}
@Test
void frege_compile_task_is_correctly_executed() throws Exception {
String fregeCode = "module ch.fhnw.thga.Completion where\n\n" + "complete :: Int -> (Int, String)\n"
+ "complete i = (i, \"Frege rocks\")\n";
Files.createDirectories(testProjectDir.toPath().resolve(Paths.get("src", "main", "frege")));
File completionFr = testProjectDir.toPath().resolve(Paths.get("src", "main", "frege", "Completion.fr"))
.toFile();
writeToFile(completionFr, fregeCode);
FregeDTO minimalFregeDTO = fregeBuilder.version("'3.25.84'").release("'3.25alpha'").build();
assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask);
assertGradleTaskOutcome(createFregeSection(minimalFregeDTO), COMPILE_FREGE_TASK_NAME);
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());
}
}
|