Skip to content

Commit 535f238

Browse files
committed
Improve testing of check task's up-to-date checks
See gh-164
1 parent 5e07680 commit 535f238

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

spring-javaformat-gradle/spring-javaformat-gradle-plugin/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id 'java'
2+
id 'java-gradle-plugin'
33
id 'eclipse'
44
}
55

@@ -8,8 +8,6 @@ repositories {
88
}
99

1010
dependencies {
11-
compile localGroovy()
12-
compile gradleApi()
1311
compile fileTree(dir: 'target/dependencies/compile', include: '*.jar')
1412
testCompile gradleTestKit()
1513
testCompile fileTree(dir: 'target/dependencies/test', include: '*.jar')

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616

1717
package io.spring.javaformat.gradle;
1818

19+
import java.io.File;
1920
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.nio.file.StandardCopyOption;
24+
import java.nio.file.StandardOpenOption;
25+
import java.util.Collections;
26+
import java.util.stream.Stream;
2027

2128
import org.gradle.testkit.runner.BuildResult;
2229
import org.gradle.testkit.runner.TaskOutcome;
2330
import org.junit.Rule;
2431
import org.junit.Test;
32+
import org.junit.rules.TemporaryFolder;
2533

2634
import io.spring.javaformat.gradle.testkit.GradleBuild;
2735

@@ -37,6 +45,9 @@ public class CheckTaskTests {
3745
@Rule
3846
public final GradleBuild gradleBuild = new GradleBuild();
3947

48+
@Rule
49+
public final TemporaryFolder temp = new TemporaryFolder();
50+
4051
@Test
4152
public void checkOk() throws IOException {
4253
BuildResult result = this.gradleBuild.source("src/test/resources/check-ok").build("check");
@@ -52,6 +63,18 @@ public void whenFirstInvocationSucceedsThenSecondInvocationIsUpToDate() throws I
5263
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
5364
}
5465

66+
@Test
67+
public void whenFirstInvocationSucceedsAndSourceIsModifiedThenSecondInvocationSucceeds() throws IOException {
68+
copyFolder(new File("src/test/resources/check-ok").toPath(), this.temp.getRoot().toPath());
69+
GradleBuild gradleBuild = this.gradleBuild.source(this.temp.getRoot());
70+
BuildResult result = gradleBuild.build("check");
71+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
72+
Files.write(new File(this.temp.getRoot(), "src/main/java/simple/Simple.java").toPath(),
73+
Collections.singletonList("// A change to the file"), StandardOpenOption.APPEND);
74+
result = gradleBuild.build("--debug", "check");
75+
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
76+
}
77+
5578
@Test
5679
public void checkBad() throws IOException {
5780
BuildResult result = this.gradleBuild.source("src/test/resources/check-bad").buildAndFail("check");
@@ -67,4 +90,21 @@ public void whenFirstInvocationFailsThenSecondInvocationFails() throws IOExcepti
6790
assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED);
6891
}
6992

93+
private void copyFolder(Path source, Path target) throws IOException {
94+
try (Stream<Path> stream = Files.walk(source)) {
95+
stream.forEach((child) -> {
96+
try {
97+
Path relative = source.relativize(child);
98+
Path destination = target.resolve(relative);
99+
if (!destination.toFile().isDirectory()) {
100+
Files.copy(child, destination, StandardCopyOption.REPLACE_EXISTING);
101+
}
102+
}
103+
catch (Exception ex) {
104+
throw new IllegalStateException(ex);
105+
}
106+
});
107+
}
108+
}
109+
70110
}

0 commit comments

Comments
 (0)