Skip to content

Commit

Permalink
[Gradle] Add functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage committed Sep 4, 2023
1 parent a170d82 commit c4607a6
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 71 deletions.
29 changes: 9 additions & 20 deletions gradle-twirl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ repositories {
dependencies {
compileOnly("com.typesafe.play:twirl-compiler_2.13:$compilerVersion")
testImplementation("org.assertj:assertj-core:3.24.2")
testImplementation("commons-io:commons-io:2.13.0")
testImplementation("org.freemarker:freemarker:2.3.32")
}

tasks.jar {
Expand All @@ -44,23 +46,17 @@ tasks.jar {

testing {
suites {
// Configure the built-in test suite
val test by getting(JvmTestSuite::class) {
// Use JUnit Jupiter test framework
useJUnitJupiter("5.9.1")
}

// Create a new test suite
val functionalTest by registering(JvmTestSuite::class) {
dependencies {
// functionalTest test suite depends on the production code in tests
implementation(project())
}

targets {
all {
// This test suite should run after the built-in test suite has run its tests
testTask.configure { shouldRunAfter(test) }
testTask.configure {
systemProperty("twirl.version", compilerVersion)
project.findProperty("scala.version")?.let { scalaVersion ->
val ver = (scalaVersion as String).trimEnd { !it.isDigit() }
systemProperty("scala.version", ver)
}
}
}
}
}
Expand Down Expand Up @@ -94,8 +90,6 @@ gradlePlugin {
}
}

gradlePlugin.testSourceSets.add(sourceSets["functionalTest"])

val headerLicense = "Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>"
val headerLicenseHash = "# $headerLicense"
val headerLicenseJava = "/*\n * $headerLicense\n */"
Expand All @@ -114,8 +108,3 @@ spotless {
licenseHeader(headerLicenseHash, "[^#]")
}
}

tasks.named<Task>("check") {
// Include functionalTest as part of the check lifecycle
dependsOn(testing.suites.named("functionalTest"))
}

This file was deleted.

4 changes: 2 additions & 2 deletions gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/** A simple 'hello world' plugin. */
public class TwirlPlugin implements Plugin<Project> {

private static final String DEFAULT_SCALA_VERSION = "2.13";
static final String DEFAULT_SCALA_VERSION = "2.13";

private static final Map<String, String> DEFAULT_TEMPLATE_FORMATS =
Map.of(
Expand Down Expand Up @@ -58,7 +58,7 @@ public void apply(final Project project) {

/** Get Twirl version from Gradle Plugin MANIFEST.MF */
private String getDefaultTwirlVersion() {
return getClass().getPackage().getImplementationVersion();
return System.getProperty("twirl.version", getClass().getPackage().getImplementationVersion());
}

private Configuration createDefaultTwirlConfiguration(
Expand Down
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();
}
}
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 gradle-twirl/src/test/resources/simple/build.gradle.kts.ftlh
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}")
}
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.
2 changes: 1 addition & 1 deletion scripts/test-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

sbt "++$MATRIX_SCALA test" || exit 1
sbt +publishLocal +compiler/publishM2 plugin/test plugin/scripted || exit 1
(cd gradle-twirl && ./gradlew clean check -x spotlessCheck --no-daemon) || exit 1
(cd gradle-twirl && ./gradlew clean check -x spotlessCheck --no-daemon -Pscala.version="$MATRIX_SCALA") || exit 1

0 comments on commit c4607a6

Please sign in to comment.