15
15
*/
16
16
package com .diffplug .spotless ;
17
17
18
+ import static com .diffplug .spotless .GitPrePushHookInstaller .Executor .GRADLE ;
19
+ import static com .diffplug .spotless .GitPrePushHookInstaller .Executor .MAVEN ;
18
20
import static org .assertj .core .api .Assertions .assertThat ;
19
21
20
22
import java .util .ArrayList ;
21
23
import java .util .List ;
22
24
25
+ import org .junit .jupiter .api .AfterEach ;
26
+ import org .junit .jupiter .api .BeforeEach ;
23
27
import org .junit .jupiter .api .Test ;
24
28
25
29
import com .diffplug .spotless .GitPrePushHookInstaller .GitPreHookLogger ;
26
30
27
31
class GitPrePushHookInstallerTest extends ResourceHarness {
32
+ private final static String OS = System .getProperty ("os.name" );
33
+
28
34
private final List <String > logs = new ArrayList <>();
29
35
private final GitPreHookLogger logger = new GitPreHookLogger () {
30
36
@ Override
@@ -43,6 +49,16 @@ public void error(String format, Object... arguments) {
43
49
}
44
50
};
45
51
52
+ @ BeforeEach
53
+ public void beforeEach () {
54
+ System .setProperty ("os.name" , "linux" );
55
+ }
56
+
57
+ @ AfterEach
58
+ public void afterEach () {
59
+ System .setProperty ("os.name" , OS );
60
+ }
61
+
46
62
@ Test
47
63
public void should_not_create_pre_hook_file_when_git_is_not_installed () throws Exception {
48
64
// given
@@ -71,7 +87,7 @@ public void should_use_global_gradle_when_gradlew_is_not_installed() throws Exce
71
87
assertThat (logs ).hasSize (4 );
72
88
assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
73
89
assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook not found, creating it" );
74
- assertThat (logs ).element (2 ).isEqualTo ("Gradle wrapper is not installed, using global gradle" );
90
+ assertThat (logs ).element (2 ).isEqualTo ("Local gradle wrapper (gradlew) not found, falling back to global command ' gradle' " );
75
91
assertThat (logs ).element (3 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
76
92
77
93
final var content = gradleHookContent ("git_pre_hook/pre-push.created-tpl" , ExecutorType .GLOBAL );
@@ -217,23 +233,117 @@ public void should_use_global_maven_when_maven_wrapper_is_not_installed() throws
217
233
assertThat (logs ).hasSize (4 );
218
234
assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
219
235
assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook not found, creating it" );
220
- assertThat (logs ).element (2 ).isEqualTo ("Maven wrapper is not installed, using global maven " );
236
+ assertThat (logs ).element (2 ).isEqualTo ("Local maven wrapper (mvnw) not found, falling back to global command 'mvn' " );
221
237
assertThat (logs ).element (3 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
222
238
223
239
final var content = mavenHookContent ("git_pre_hook/pre-push.created-tpl" , ExecutorType .GLOBAL );
224
240
assertFile (".git/hooks/pre-push" ).hasContent (content );
225
241
}
226
242
243
+ @ Test
244
+ public void should_use_maven_bat_wrapper_when_exists_for_windows () {
245
+ // given
246
+ System .setProperty ("os.name" , "Windows 10" );
247
+ setFile ("mvnw.bat" ).toContent ("" );
248
+ setFile ("mvnw.cmd" ).toContent ("" );
249
+
250
+ final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
251
+
252
+ // when
253
+ final var hook = gradle .preHookTemplate (MAVEN , "spotless:check" , "spotless:apply" );
254
+
255
+ // then
256
+ assertThat (hook ).contains ("SPOTLESS_EXECUTOR=./mvnw.bat" );
257
+ }
258
+
259
+ @ Test
260
+ public void should_use_maven_cmd_wrapper_when_exists_for_windows () {
261
+ // given
262
+ System .setProperty ("os.name" , "Windows 10" );
263
+ setFile ("mvnw.cmd" ).toContent ("" );
264
+
265
+ final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
266
+
267
+ // when
268
+ final var hook = gradle .preHookTemplate (MAVEN , "spotless:check" , "spotless:apply" );
269
+
270
+ // then
271
+ assertThat (hook ).contains ("SPOTLESS_EXECUTOR=./mvnw.cmd" );
272
+ }
273
+
274
+ @ Test
275
+ public void should_use_maven_global_when_bat_and_cmd_files_not_exists_for_windows () {
276
+ // given
277
+ System .setProperty ("os.name" , "Windows 10" );
278
+ setFile ("mvnw" ).toContent ("" );
279
+
280
+ final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
281
+
282
+ // when
283
+ final var hook = gradle .preHookTemplate (MAVEN , "spotless:check" , "spotless:apply" );
284
+
285
+ // then
286
+ assertThat (hook ).contains ("SPOTLESS_EXECUTOR=mvn" );
287
+ }
288
+
289
+ @ Test
290
+ public void should_use_gradle_bat_wrapper_when_exists_for_windows () {
291
+ // given
292
+ System .setProperty ("os.name" , "Windows 10" );
293
+ setFile ("gradlew.bat" ).toContent ("" );
294
+ setFile ("gradlew.cmd" ).toContent ("" );
295
+ setFile ("gradlew" ).toContent ("" );
296
+
297
+ final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
298
+
299
+ // when
300
+ final var hook = gradle .preHookTemplate (GRADLE , "spotlessCheck" , "spotlessApply" );
301
+
302
+ // then
303
+ assertThat (hook ).contains ("SPOTLESS_EXECUTOR=./gradlew.bat" );
304
+ }
305
+
306
+ @ Test
307
+ public void should_use_gradle_cmd_wrapper_when_exists_for_windows () {
308
+ // given
309
+ System .setProperty ("os.name" , "Windows 10" );
310
+ setFile ("gradlew.cmd" ).toContent ("" );
311
+ setFile ("gradlew" ).toContent ("" );
312
+
313
+ final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
314
+
315
+ // when
316
+ final var hook = gradle .preHookTemplate (GRADLE , "spotlessCheck" , "spotlessApply" );
317
+
318
+ // then
319
+ assertThat (hook ).contains ("SPOTLESS_EXECUTOR=./gradlew.cmd" );
320
+ }
321
+
322
+ @ Test
323
+ public void should_use_gradle_global_when_bat_and_cmd_files_not_exists_for_windows () {
324
+ // given
325
+ System .setProperty ("os.name" , "Windows 10" );
326
+ setFile ("gradlew" ).toContent ("" );
327
+
328
+ final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
329
+
330
+ // when
331
+ final var hook = gradle .preHookTemplate (GRADLE , "spotlessCheck" , "spotlessApply" );
332
+
333
+ // then
334
+ assertThat (hook ).contains ("SPOTLESS_EXECUTOR=gradle" );
335
+ }
336
+
227
337
private String gradleHookContent (String resourcePath , ExecutorType executorType ) {
228
338
return getTestResource (resourcePath )
229
- .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? newFile ("gradlew" ).getAbsolutePath () : "gradle" )
339
+ .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? "./" + newFile ("gradlew" ).getName () : "gradle" )
230
340
.replace ("${checkCommand}" , "spotlessCheck" )
231
341
.replace ("${applyCommand}" , "spotlessApply" );
232
342
}
233
343
234
344
private String mavenHookContent (String resourcePath , ExecutorType executorType ) {
235
345
return getTestResource (resourcePath )
236
- .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? newFile ("mvnw" ).getAbsolutePath () : "mvn" )
346
+ .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? "./" + newFile ("mvnw" ).getName () : "mvn" )
237
347
.replace ("${checkCommand}" , "spotless:check" )
238
348
.replace ("${applyCommand}" , "spotless:apply" );
239
349
}
0 commit comments