16
16
17
17
package io .spring .javaformat .gradle ;
18
18
19
+ import java .io .File ;
19
20
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 ;
20
27
21
28
import org .gradle .testkit .runner .BuildResult ;
22
29
import org .gradle .testkit .runner .TaskOutcome ;
23
30
import org .junit .Rule ;
24
31
import org .junit .Test ;
32
+ import org .junit .rules .TemporaryFolder ;
25
33
26
34
import io .spring .javaformat .gradle .testkit .GradleBuild ;
27
35
@@ -37,6 +45,9 @@ public class CheckTaskTests {
37
45
@ Rule
38
46
public final GradleBuild gradleBuild = new GradleBuild ();
39
47
48
+ @ Rule
49
+ public final TemporaryFolder temp = new TemporaryFolder ();
50
+
40
51
@ Test
41
52
public void checkOk () throws IOException {
42
53
BuildResult result = this .gradleBuild .source ("src/test/resources/check-ok" ).build ("check" );
@@ -52,6 +63,18 @@ public void whenFirstInvocationSucceedsThenSecondInvocationIsUpToDate() throws I
52
63
assertThat (result .task (":checkFormatMain" ).getOutcome ()).isEqualTo (TaskOutcome .UP_TO_DATE );
53
64
}
54
65
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
+
55
78
@ Test
56
79
public void checkBad () throws IOException {
57
80
BuildResult result = this .gradleBuild .source ("src/test/resources/check-bad" ).buildAndFail ("check" );
@@ -67,4 +90,21 @@ public void whenFirstInvocationFailsThenSecondInvocationFails() throws IOExcepti
67
90
assertThat (result .task (":checkFormatMain" ).getOutcome ()).isEqualTo (TaskOutcome .FAILED );
68
91
}
69
92
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
+
70
110
}
0 commit comments