-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
189 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
gradle-twirl/src/functionalTest/java/play/twirl/gradle/TwirlPluginFunctionalTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
gradle-twirl/src/test/java/play/twirl/gradle/AbstractFunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.gradle; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import freemarker.template.Configuration; | ||
import freemarker.template.Template; | ||
import freemarker.template.TemplateException; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import org.apache.commons.io.FileUtils; | ||
import org.gradle.testkit.runner.BuildResult; | ||
import org.gradle.testkit.runner.GradleRunner; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
abstract class AbstractFunctionalTest { | ||
|
||
@TempDir File projectDir; | ||
|
||
File projectSourceDir; | ||
|
||
GradleRunner runner; | ||
|
||
Configuration freemarkerConf; | ||
|
||
protected abstract File getProjectSourceDir(); | ||
|
||
protected abstract String getBuildFileContent(); | ||
|
||
protected String getSettingsFileContent() { | ||
return ""; | ||
} | ||
|
||
static String getScalaVersion() { | ||
return System.getProperty("scala.version", TwirlPlugin.DEFAULT_SCALA_VERSION); | ||
} | ||
|
||
static String getTwirlVersion() { | ||
return System.getProperty("twirl.version"); | ||
} | ||
|
||
protected Path projectSourcePath(String path) { | ||
return Paths.get(projectSourceDir.getAbsolutePath(), path); | ||
} | ||
|
||
protected Path projectPath(String path) { | ||
return Paths.get(projectDir.getAbsolutePath(), path); | ||
} | ||
|
||
protected Path projectBuildPath(String path) { | ||
return Paths.get(projectDir.getAbsolutePath(), "build/" + path); | ||
} | ||
|
||
@BeforeEach | ||
void init() throws IOException, TemplateException { | ||
projectSourceDir = getProjectSourceDir(); | ||
runner = GradleRunner.create().withProjectDir(projectDir).withPluginClasspath().forwardOutput(); | ||
|
||
initFreemarker(); | ||
|
||
FileUtils.writeStringToFile( | ||
projectPath("build.gradle.kts").toFile(), getBuildFileContent(), UTF_8); | ||
FileUtils.writeStringToFile( | ||
projectPath("settings.gradle.kts").toFile(), getSettingsFileContent(), UTF_8); | ||
} | ||
|
||
protected void initFreemarker() throws IOException { | ||
freemarkerConf = new Configuration(Configuration.VERSION_2_3_32); | ||
freemarkerConf.setDirectoryForTemplateLoading(projectSourceDir); | ||
} | ||
|
||
protected String templateProcess(String template, Map<String, Object> params) { | ||
StringWriter writer = new StringWriter(); | ||
try { | ||
Template buildGradle = freemarkerConf.getTemplate(template); | ||
buildGradle.process(params, writer); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return writer.toString(); | ||
} | ||
|
||
protected BuildResult build(String... args) { | ||
return runner.withArguments(args).build(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
gradle-twirl/src/test/java/play/twirl/gradle/TwirlPluginFunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
package play.twirl.gradle; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.groovy.util.Maps; | ||
import org.gradle.testkit.runner.BuildResult; | ||
import org.gradle.testkit.runner.BuildTask; | ||
import org.gradle.testkit.runner.TaskOutcome; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** A simple functional test to check a Twirl Gradle Plugin. */ | ||
public class TwirlPluginFunctionalTest extends AbstractFunctionalTest { | ||
|
||
@Override | ||
protected File getProjectSourceDir() { | ||
return new File("src/test/resources/simple"); | ||
} | ||
|
||
@Override | ||
protected String getBuildFileContent() { | ||
Map<String, Object> params = | ||
Maps.of( | ||
"scalaVersion", getScalaVersion(), | ||
"twirlVersion", getTwirlVersion()); | ||
return templateProcess("build.gradle.kts.ftlh", params); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test simple Gradle project with Twirl HTML template") | ||
void testSimpleGradleProject() throws IOException { | ||
File simpleSources = projectPath("src").toFile(); | ||
FileUtils.copyDirectory(projectSourcePath("src").toFile(), simpleSources); | ||
|
||
BuildResult result = build("build"); | ||
|
||
BuildTask compileTwirlResult = result.task(":compileTwirl"); | ||
assertThat(compileTwirlResult).isNotNull(); | ||
assertThat(compileTwirlResult.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); | ||
assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/c.template.scala")) | ||
.isNotEmptyFile(); | ||
|
||
BuildTask compileScalaResult = result.task(":compileScala"); | ||
assertThat(compileScalaResult).isNotNull(); | ||
assertThat(compileScalaResult.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); | ||
assertThat(projectBuildPath("classes/scala/main/a/b/html/c.class")).isNotEmptyFile(); | ||
|
||
result = build("build"); | ||
|
||
compileTwirlResult = result.task(":compileTwirl"); | ||
assertThat(compileTwirlResult).isNotNull(); | ||
assertThat(compileTwirlResult.getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
gradle-twirl/src/test/resources/simple/build.gradle.kts.ftlh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
plugins { | ||
application | ||
id("com.typesafe.play.twirl") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
mavenLocal() | ||
} | ||
|
||
dependencies { | ||
implementation("com.typesafe.play:twirl-api_${scalaVersion}:${twirlVersion}") | ||
} | ||
|
||
twirl { | ||
scalaVersion.set("${scalaVersion}") | ||
} |
6 changes: 6 additions & 0 deletions
6
gradle-twirl/src/test/resources/simple/src/main/twirl/a/b/c.scala.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@**************************************************************************************************************************************************** | ||
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com> * | ||
****************************************************************************************************************************************************@ | ||
|
||
@(name: String) | ||
Hello, @name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters