aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java
blob: 31f8d21a603574a1a56b22b82ddb657dfce1b3bb (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package ch.fhnw.thga.gradleplugins;

import static ch.fhnw.thga.gradleplugins.FregeExtension.FREGE_COMPILER_OUTPUT_DIRECTORY_KEY;
import static ch.fhnw.thga.gradleplugins.FregeExtension.FREGE_RELEASE_BUILD_FILE_KEY;
import static ch.fhnw.thga.gradleplugins.FregeExtension.FREGE_VERSION_BUILD_FILE_KEY;
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_COMPILER_TASK_NAME;
import static ch.fhnw.thga.gradleplugins.FregePlugin.FREGE_COMPILE_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.util.Optional;
import java.util.stream.Collectors;
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 {
    @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, content, true);
    }

    private static String buildFilePluginString(String pluginId) {
        return String.format("id '%s'", pluginId);
    }

    private static String buildFileFregeExtension(String fregeVersion, String fregeRelease,
            Optional<String> compilerPath) {
        String optionalCompilerPathLine = compilerPath.isPresent() ? String.format("  %s = %s\n", FREGE_COMPILER_OUTPUT_DIRECTORY_KEY, compilerPath.get()) : "";
        return String.format("%s {\n  %s = %s\n  %s = %s\n%s}\n", FREGE_EXTENSION_NAME, FREGE_VERSION_BUILD_FILE_KEY,
                fregeVersion, FREGE_RELEASE_BUILD_FILE_KEY, fregeRelease, optionalCompilerPathLine);
    }

    private static String writeBuildFilePlugins(Stream<String> pluginIds) {
        String plugins = pluginIds.map(pluginId -> buildFilePluginString(pluginId)).collect(Collectors.joining("\n  "));
        return String.format("plugins {\n  %s\n}\n", plugins);
    }

    private void assertSetupFregeCompilerTask(String fregeConfig) throws Exception {
        System.out.println(fregeConfig);
        appendToFile(buildFile, fregeConfig);
        assertTrue(project.getTasks().getByName(SETUP_FREGE_COMPILER_TASK_NAME) instanceof SetupFregeCompilerTask);
        BuildResult result = GradleRunner.create().withProjectDir(testProjectDir).withPluginClasspath()
                .withArguments(SETUP_FREGE_COMPILER_TASK_NAME).build();
        System.out.println(result.getOutput());
        assertEquals(SUCCESS, result.task(":" + SETUP_FREGE_COMPILER_TASK_NAME).getOutcome());
    }

    @BeforeEach
    void setup() throws Exception {
        buildFile = new File(testProjectDir, "build.gradle");
        settingsFile = new File(testProjectDir, "settings.gradle");
        writeToFile(buildFile, writeBuildFilePlugins(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_single_plugin_id_then_it_is_correctly_converted_to_build_file_string() {
        String pluginId = "frege";
        Stream<String> pluginIds = Stream.of(pluginId);
        String expected = "plugins {\n" + "  id '" + pluginId + "'\n" + "}\n";
        assertEquals(expected, writeBuildFilePlugins(pluginIds));
    }

    @Test
    void given_multiple_plugin_ids_then_they_are_correctly_converted_to_build_file_string() {
        String fregeId = "frege";
        String javaId = "java";
        Stream<String> pluginIds = Stream.of(fregeId, javaId);
        String expected = "plugins {\n" + "  id '" + fregeId + "'\n" + "  id '" + javaId + "'\n" + "}\n";
        assertEquals(expected, writeBuildFilePlugins(pluginIds));
    }

    @Test
    void given_default_plugin_ids_then_they_are_correctly_converted_to_build_file_string() {
        String fregeId = "frege";
        String javaId = "java";
        Stream<String> pluginIds = Stream.of(fregeId, javaId);
        String expected = "plugins {\n" + "  id '" + fregeId + "'\n" + "  id '" + javaId + "'\n" + "}\n";
        assertEquals(expected, writeBuildFilePlugins(pluginIds));
    }

    @Test
    void given_frege_version_and_frege_release_then_they_are_correctly_converted_to_build_file_string() {
        String fregeVersion = "'3.25.84'";
        String fregeRelease = "'3.25alpha'";
        String expectedFregeConfig = FREGE_EXTENSION_NAME + " {\n" + "  fregeVersion = " + fregeVersion + "\n"
                + "  fregeRelease = " + fregeRelease + "\n" + "}\n";
        assertEquals(expectedFregeConfig, buildFileFregeExtension(fregeVersion, fregeRelease, Optional.empty()));
    }

    @Test
    void given_frege_version_and_frege_release_and_frege_compiler_path_then_they_are_correctly_converted_to_build_file_string() {
        String fregeVersion = "'3.25.84'";
        String fregeRelease = "'3.25alpha'";
        String fregeCompilerPath = "layout.projectDirectory.file('lib/frege3.25.84')";
        String expectedFregeConfig =
            FREGE_EXTENSION_NAME + " {\n" + "  fregeVersion = " + fregeVersion + "\n"
        + "  fregeRelease = " + fregeRelease + "\n"
        + "  fregeCompilerOutputDirectory = " + fregeCompilerPath + "\n" + "}\n";
        assertEquals(expectedFregeConfig, buildFileFregeExtension(fregeVersion, fregeRelease, Optional.of(fregeCompilerPath)));
    }

    @Test
    void given_frege_compiler_version_3_25_84_and_3_25_alpha_release_without_frege_compiler_path_when_running_the_setup_frege_compiler_task_then_the_frege_compiler_is_correctly_setup()
            throws Exception {
        String fregeConfig = buildFileFregeExtension("'3.25.84'", "'3.25alpha'", Optional.empty());
        assertSetupFregeCompilerTask(fregeConfig);
        assertTrue(new File(testProjectDir.getAbsolutePath() + "/lib/frege3.25.84.jar").exists());

    }

   @Test
   void given_frege_compiler_version_3_25_84_when_running_the_setup_frege_compiler_task_then_the_frege_compiler_is_correctly_setup()
           throws Exception {
       String fregeConfig = buildFileFregeExtension("'3.25.84'", "'3.25alpha'", Optional.of("layout.buildDirectory.dir('dist')"));
       assertSetupFregeCompilerTask(fregeConfig);
       assertTrue(new File(testProjectDir.getAbsolutePath() + "/build/dist/frege3.25.84.jar").exists());
   }

   @Test
   void frege_compile_task_is_correctly_executed()
           throws Exception {
               String fregeConfig = buildFileFregeExtension("'3.25.84'", "'3.25alpha'", Optional.of("layout.buildDirectory.dir('dist')"));
               String fregeCode = "module ch.fhnw.thga.Completion where\n\n" + 
                                  "complete :: Int -> (Int, String)\n" +
                                  "complete i = (i, \"Frege rocks\")\n";
               File completionFr = new File(testProjectDir, "Completion.fr");
               writeToFile(completionFr, fregeCode);
               System.out.println(fregeConfig);
               appendToFile(buildFile, fregeConfig);
               assertTrue(project.getTasks().getByName(FREGE_COMPILE_TASK_NAME) instanceof FregeCompileTask);
               BuildResult result = GradleRunner.create().withProjectDir(testProjectDir).withPluginClasspath()
                .withArguments(FREGE_COMPILE_TASK_NAME).build();
                System.out.println(result.getOutput());
                assertEquals(SUCCESS, result.task(":" + FREGE_COMPILE_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());
   }
}