17
17
18
18
import java .io .IOException ;
19
19
20
+ import org .gradle .testkit .runner .*;
21
+ import org .junit .Ignore ;
20
22
import org .junit .Test ;
21
23
22
24
public class SpecificFilesTest extends GradleIntegrationTest {
23
- private String testFile (int number , boolean absolute ) throws IOException {
24
- String rel = "src/main/java/test" + number + ".java" ;
25
+ private String testFilePath (int number ) {
26
+ return testFilePath (number , true );
27
+ }
28
+
29
+ private String testFilePath (int number , boolean absolute ) {
30
+ String relPath = "src/main/java/test" + number + ".java" ;
25
31
if (absolute ) {
26
- return rootFolder () + "/" + rel ;
32
+ return rootFolder () + "/" + relPath ;
27
33
} else {
28
- return rel ;
34
+ return relPath ;
29
35
}
30
36
}
31
37
32
- private String testFile ( int number ) throws IOException {
33
- return testFile ( number , false );
38
+ private String fixture () {
39
+ return fixture ( false );
34
40
}
35
41
36
42
private String fixture (boolean formatted ) {
37
43
return "java/googlejavaformat/JavaCode" + (formatted ? "F" : "Unf" ) + "ormatted.test" ;
38
44
}
39
45
40
- private void integration (String patterns , boolean firstFormatted , boolean secondFormatted , boolean thirdFormatted )
41
- throws IOException {
46
+ private void createBuildScript () throws IOException {
47
+ createBuildScript (false );
48
+ }
49
+
50
+ private void createBuildScript (boolean isKotlin ) throws IOException {
51
+ if (isKotlin ) {
52
+ setFile ("build.gradle.kts" ).toLines (
53
+ "import com.diffplug.gradle.spotless.SpotlessExtension" ,
54
+ "buildscript {" ,
55
+ " repositories {" ,
56
+ " mavenCentral()" ,
57
+ " }" ,
58
+ " dependencies {" ,
59
+ " classpath(\" com.diffplug.spotless:spotless-plugin-gradle:3.27.1\" )" ,
60
+ " }" ,
61
+ "}" ,
62
+ "plugins {" ,
63
+ " java" ,
64
+ " id(\" com.diffplug.gradle.spotless\" )" ,
65
+ "}" ,
66
+ "configure<SpotlessExtension> {" ,
67
+ " java {" ,
68
+ " googleJavaFormat(\" 1.2\" )" ,
69
+ " }" ,
70
+ "}" );
71
+ return ;
72
+ }
42
73
43
74
setFile ("build.gradle" ).toLines (
44
75
"buildscript { repositories { mavenCentral() } }" ,
@@ -51,32 +82,77 @@ private void integration(String patterns, boolean firstFormatted, boolean second
51
82
" googleJavaFormat('1.2')" ,
52
83
" }" ,
53
84
"}" );
85
+ }
54
86
55
- setFile (testFile (1 )).toResource (fixture (false ));
56
- setFile (testFile (2 )).toResource (fixture (false ));
57
- setFile (testFile (3 )).toResource (fixture (false ));
87
+ private void integration (String patterns ,
88
+ boolean firstFormatted , boolean secondFormatted , boolean thirdFormatted ) throws IOException {
89
+ integration (patterns , firstFormatted , secondFormatted , thirdFormatted , false );
90
+ }
91
+
92
+ private void integration (String patterns ,
93
+ boolean firstFormatted , boolean secondFormatted , boolean thirdFormatted ,
94
+ boolean isKotlin ) throws IOException {
95
+ String testFileOne = testFilePath (1 , false );
96
+ String testFileTwo = testFilePath (2 , false );
97
+ String testFileThree = testFilePath (3 , false );
58
98
59
- gradleRunner ()
60
- . withArguments ( "spotlessApply" , "-PspotlessFiles=" + patterns )
61
- . build ( );
99
+ setFile ( testFileOne ). toResource ( fixture ());
100
+ setFile ( testFileTwo ). toResource ( fixture ());
101
+ setFile ( testFileThree ). toResource ( fixture () );
62
102
63
- assertFile (testFile (1 )).sameAsResource (fixture (firstFormatted ));
64
- assertFile (testFile (2 )).sameAsResource (fixture (secondFormatted ));
65
- assertFile (testFile (3 )).sameAsResource (fixture (thirdFormatted ));
103
+ GradleRunner runner = gradleRunner ()
104
+ .withArguments ("spotlessApply" , "-PspotlessFiles=" + patterns );
105
+ if (isKotlin ) {
106
+ runner .withGradleVersion ("4.0" );
107
+ }
108
+ runner .build ();
109
+
110
+ assertFile (testFileOne ).sameAsResource (fixture (firstFormatted ));
111
+ assertFile (testFileTwo ).sameAsResource (fixture (secondFormatted ));
112
+ assertFile (testFileThree ).sameAsResource (fixture (thirdFormatted ));
66
113
}
67
114
68
115
@ Test
69
116
public void singleFile () throws IOException {
70
- integration (testFile (2 , true ), false , true , false );
117
+ createBuildScript (false );
118
+ integration (testFilePath (2 ), false , true , false );
71
119
}
72
120
73
121
@ Test
74
122
public void multiFile () throws IOException {
75
- integration (testFile (1 , true ) + "," + testFile (3 , true ), true , false , true );
123
+ createBuildScript ();
124
+ integration (testFilePath (1 ) + "," + testFilePath (3 ),
125
+ true , false , true );
126
+ }
127
+
128
+ @ Test
129
+ @ Ignore ("When spotlessFiles is specified without a value, Spotless runs on all files. It should run on none." )
130
+ public void emptyPattern_formatsNoFiles () throws IOException {
131
+ createBuildScript ();
132
+ integration ("" , false , false , false );
133
+ }
134
+
135
+ @ Test
136
+ public void matchesNoFiles_formatsNoFilesButDoesNotExitInError () throws IOException {
137
+ createBuildScript ();
138
+ integration (testFilePath (4 ), false , false , false );
76
139
}
77
140
78
141
@ Test
79
142
public void regexp () throws IOException {
143
+ createBuildScript ();
80
144
integration (".*/src/main/java/test(1|3).java" , true , false , true );
81
145
}
146
+
147
+ @ Test (expected = UnexpectedBuildFailure .class )
148
+ public void invalidRegexp_exitsInError () throws IOException {
149
+ createBuildScript (false );
150
+ integration ("./[?)!\\ " , false , false , false );
151
+ }
152
+
153
+ @ Test
154
+ public void kotlinBuildScript () throws IOException {
155
+ createBuildScript (true );
156
+ integration (testFilePath (2 ), false , true , false , true );
157
+ }
82
158
}
0 commit comments