aboutsummaryrefslogtreecommitdiff
path: root/src/functionalTest/java/ch/fhnw/thga/gradleplugins/SharedFunctionalTestLogic.java
blob: a0d46a4153c023d183cfe9d1387077e88e16baeb (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
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 java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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;

public class SharedFunctionalTestLogic
{

   static String createFregeSection(FregeDTO fregeDTO) 
   {
        return String.format(
            "%s {%s  %s%s}",
            FREGE_EXTENSION_NAME,
            System.lineSeparator(),
            fregeDTO.toBuildFile(),
            System.lineSeparator());
    }

    private static void writeFile(
        File destination,
        String content,
        boolean append) 
        throws IOException
    {
        try (BufferedWriter output = new BufferedWriter(new FileWriter(destination, append)))
        {
                output.write(content);
        }
    
    }

    static File writeToFile(File destination, String content) throws IOException
    {
        writeFile(destination, content, false);
        return destination;
    }

    static File appendToFile(File destination, String content) throws IOException
    {
        writeFile(destination, System.lineSeparator() + content, true);
        return destination;
    }

    static BuildResult runGradleTask(File testProjectDir, String... taskName)
    {
        return GradleRunner
            .create()
            .withProjectDir(testProjectDir)
            .withPluginClasspath()
            .withArguments(taskName)
            .build();
    }

    static File createSettingsFile(File testProjectDir) throws IOException 
        {
            File settingsFile = new File(testProjectDir, "settings.gradle");
            return writeToFile(settingsFile, "rootProject.name='frege-plugin'");
        }
    
        private static File createFregePluginBuildFile(
            File testProjectDir)
            throws IOException
        {
            File buildFile = new File(testProjectDir, "build.gradle");
            return writeToFile(
                buildFile,
                createPluginsSection(Stream.of(FREGE_PLUGIN_ID)));
        }
    
        static File createFregeBuildFile(
            File testProjectDir,
            String fregeBuildFileConfig)
            throws IOException
        {
            return appendToFile(
                createFregePluginBuildFile(
                    testProjectDir),
                    fregeBuildFileConfig);
        }
    
        static Project createFregeGradleProject(
            File testProjectDir,
            String fregeBuildFileConfig)
            throws Exception
        {
            createSettingsFile(testProjectDir);
            createFregeBuildFile(testProjectDir, fregeBuildFileConfig);
            Project project = ProjectBuilder
                .builder()
                .withProjectDir(testProjectDir)
                .build();
            project.getPluginManager().apply(FREGE_PLUGIN_ID);
            return project;
        }
}