1515 */
1616package com .diffplug .spotless ;
1717
18+ import static com .diffplug .spotless .GitPrePushHookInstaller .Executor .GRADLE ;
19+ import static com .diffplug .spotless .GitPrePushHookInstaller .Executor .MAVEN ;
1820import static org .assertj .core .api .Assertions .assertThat ;
1921
2022import java .util .ArrayList ;
2123import java .util .List ;
2224
25+ import org .junit .jupiter .api .AfterEach ;
26+ import org .junit .jupiter .api .BeforeEach ;
2327import org .junit .jupiter .api .Test ;
2428
2529import com .diffplug .spotless .GitPrePushHookInstaller .GitPreHookLogger ;
2630
2731class GitPrePushHookInstallerTest extends ResourceHarness {
32+ private final static String OS = System .getProperty ("os.name" );
33+
2834 private final List <String > logs = new ArrayList <>();
2935 private final GitPreHookLogger logger = new GitPreHookLogger () {
3036 @ Override
@@ -43,6 +49,16 @@ public void error(String format, Object... arguments) {
4349 }
4450 };
4551
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+
4662 @ Test
4763 public void should_not_create_pre_hook_file_when_git_is_not_installed () throws Exception {
4864 // given
@@ -71,7 +87,7 @@ public void should_use_global_gradle_when_gradlew_is_not_installed() throws Exce
7187 assertThat (logs ).hasSize (4 );
7288 assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
7389 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' " );
7591 assertThat (logs ).element (3 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
7692
7793 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
217233 assertThat (logs ).hasSize (4 );
218234 assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
219235 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' " );
221237 assertThat (logs ).element (3 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
222238
223239 final var content = mavenHookContent ("git_pre_hook/pre-push.created-tpl" , ExecutorType .GLOBAL );
224240 assertFile (".git/hooks/pre-push" ).hasContent (content );
225241 }
226242
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+
227337 private String gradleHookContent (String resourcePath , ExecutorType executorType ) {
228338 return getTestResource (resourcePath )
229- .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? newFile ("gradlew" ).getAbsolutePath () : "gradle" )
339+ .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? "./" + newFile ("gradlew" ).getName () : "gradle" )
230340 .replace ("${checkCommand}" , "spotlessCheck" )
231341 .replace ("${applyCommand}" , "spotlessApply" );
232342 }
233343
234344 private String mavenHookContent (String resourcePath , ExecutorType executorType ) {
235345 return getTestResource (resourcePath )
236- .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? newFile ("mvnw" ).getAbsolutePath () : "mvn" )
346+ .replace ("${executor}" , executorType == ExecutorType .WRAPPER ? "./" + newFile ("mvnw" ).getName () : "mvn" )
237347 .replace ("${checkCommand}" , "spotless:check" )
238348 .replace ("${applyCommand}" , "spotless:apply" );
239349 }
0 commit comments