Skip to content

Commit

Permalink
Enable Java Format check for Gradle Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage committed Jun 23, 2023
1 parent 50c44f0 commit ef4681b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 57 deletions.
7 changes: 7 additions & 0 deletions gradle-twirl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
plugins {
// Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins
`java-gradle-plugin`
id("com.diffplug.spotless") version "6.19.0"
}

repositories {
Expand Down Expand Up @@ -52,6 +53,12 @@ gradlePlugin {

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

spotless {
java {
googleJavaFormat()
}
}

tasks.named<Task>("check") {
// Include functionalTest as part of the check lifecycle
dependsOn(testing.suites.named("functionalTest"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,38 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* A simple functional test for the 'com.playframework.twirl' plugin.
*/
/** A simple functional test for the 'com.playframework.twirl' plugin. */
class TwirlPluginFunctionalTest {
@TempDir
File projectDir;

private File getBuildFile() {
return new File(projectDir, "build.gradle");
}

private File getSettingsFile() {
return new File(projectDir, "settings.gradle");
}

@Test void canRunTask() throws IOException {
writeString(getSettingsFile(), "");
writeString(getBuildFile(),
"plugins {" +
" id('com.playframework.twirl')" +
"}");

// Run the build
GradleRunner runner = GradleRunner.create();
runner.forwardOutput();
runner.withPluginClasspath();
runner.withArguments("greeting");
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

// Verify the result
assertTrue(result.getOutput().contains("Hello from plugin 'com.playframework.twirl'"));
}

private void writeString(File file, String string) throws IOException {
try (Writer writer = new FileWriter(file)) {
writer.write(string);
}
@TempDir File projectDir;

private File getBuildFile() {
return new File(projectDir, "build.gradle");
}

private File getSettingsFile() {
return new File(projectDir, "settings.gradle");
}

@Test
void canRunTask() throws IOException {
writeString(getSettingsFile(), "");
writeString(getBuildFile(), "plugins {" + " id('com.playframework.twirl')" + "}");

// Run the build
GradleRunner runner = GradleRunner.create();
runner.forwardOutput();
runner.withPluginClasspath();
runner.withArguments("greeting");
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

// Verify the result
assertTrue(result.getOutput().contains("Hello from plugin 'com.playframework.twirl'"));
}

private void writeString(File file, String string) throws IOException {
try (Writer writer = new FileWriter(file)) {
writer.write(string);
}
}
}
22 changes: 12 additions & 10 deletions gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
*/
package play.twirl.gradle;

import org.gradle.api.Project;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

/**
* A simple 'hello world' plugin.
*/
/** A simple 'hello world' plugin. */
public class TwirlPlugin implements Plugin<Project> {
public void apply(Project project) {
// Register a task
project.getTasks().register("greeting", task -> {
task.doLast(s -> System.out.println("Hello from plugin 'com.playframework.twirl'"));
});
}
public void apply(Project project) {
// Register a task
project
.getTasks()
.register(
"greeting",
task -> {
task.doLast(s -> System.out.println("Hello from plugin 'com.playframework.twirl'"));
});
}
}
19 changes: 9 additions & 10 deletions gradle-twirl/src/test/java/play/twirl/gradle/TwirlPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.Test;

/**
* A simple unit test for the 'com.playframework.twirl' plugin.
*/
/** A simple unit test for the 'com.playframework.twirl' plugin. */
class TwirlPluginTest {
@Test void pluginRegistersATask() {
// Create a test project and apply the plugin
Project project = ProjectBuilder.builder().build();
project.getPlugins().apply("com.playframework.twirl");
@Test
void pluginRegistersATask() {
// Create a test project and apply the plugin
Project project = ProjectBuilder.builder().build();
project.getPlugins().apply("com.playframework.twirl");

// Verify the result
assertNotNull(project.getTasks().findByName("greeting"));
}
// Verify the result
assertNotNull(project.getTasks().findByName("greeting"));
}
}

0 comments on commit ef4681b

Please sign in to comment.