1818import static org .assertj .core .api .Assertions .assertThat ;
1919
2020import java .io .IOException ;
21+ import java .util .Locale ;
2122
2223import org .junit .jupiter .api .Test ;
2324
@@ -41,11 +42,8 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
4142 assertThat (output ).contains ("Git pre-push hook not found, creating it" );
4243 assertThat (output ).contains ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ));
4344
44- final var content = getTestResource ("git_pre_hook/pre-push.created-tpl" )
45- .replace ("${executor}" , newFile ("mvnw" ).getAbsolutePath ())
46- .replace ("${checkCommand}" , "spotless:check" )
47- .replace ("${applyCommand}" , "spotless:apply" );
48- assertFile (".git/hooks/pre-push" ).hasContent (content );
45+ final var hookContent = getHookContent ("git_pre_hook/pre-push.created-tpl" );
46+ assertFile (".git/hooks/pre-push" ).hasContent (hookContent );
4947 }
5048
5149 @ Test
@@ -67,11 +65,8 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
6765 assertThat (output ).contains ("Installing git pre-push hook" );
6866 assertThat (output ).contains ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ));
6967
70- final var content = getTestResource ("git_pre_hook/pre-push.existing-installed-end-tpl" )
71- .replace ("${executor}" , newFile ("mvnw" ).getAbsolutePath ())
72- .replace ("${checkCommand}" , "spotless:check" )
73- .replace ("${applyCommand}" , "spotless:apply" );
74- assertFile (".git/hooks/pre-push" ).hasContent (content );
68+ final var hookContent = getHookContent ("git_pre_hook/pre-push.existing-installed-end-tpl" );
69+ assertFile (".git/hooks/pre-push" ).hasContent (hookContent );
7570 }
7671
7772 private void writePomWithJavaLicenseHeaderStep () throws IOException {
@@ -80,4 +75,24 @@ private void writePomWithJavaLicenseHeaderStep() throws IOException {
8075 " <file>${basedir}/license.txt</file>" ,
8176 "</licenseHeader>" );
8277 }
78+
79+ private String getHookContent (String resourceFile ) {
80+ String executorPath = "mvn" ;
81+ if (System .getProperty ("os.name" ).toLowerCase (Locale .ROOT ).startsWith ("win" )) {
82+ if (newFile ("mvnw.bat" ).exists ()) {
83+ executorPath = newFile ("mvnw.bat" ).getName ();
84+ } else if (newFile ("mvnw.cmd" ).exists ()) {
85+ executorPath = newFile ("mvnw.cmd" ).getName ();
86+ }
87+ }
88+
89+ if (newFile ("mvnw" ).exists ()) {
90+ executorPath = newFile ("mvnw" ).getName ();
91+ }
92+
93+ return getTestResource (resourceFile )
94+ .replace ("${executor}" , "./" + executorPath )
95+ .replace ("${checkCommand}" , "spotless:check" )
96+ .replace ("${applyCommand}" , "spotless:apply" );
97+ }
8398}
0 commit comments