@@ -32,6 +32,11 @@ public void info(String format, Object... arguments) {
3232 logs .add (String .format (format , arguments ));
3333 }
3434
35+ @ Override
36+ public void warn (String format , Object ... arguments ) {
37+ logs .add (String .format (format , arguments ));
38+ }
39+
3540 @ Override
3641 public void error (String format , Object ... arguments ) {
3742 logs .add (String .format (format , arguments ));
@@ -54,7 +59,7 @@ public void should_not_create_pre_hook_file_when_git_is_not_installed() throws E
5459 }
5560
5661 @ Test
57- public void should_not_create_pre_hook_file_when_gradlew_is_not_installed () throws Exception {
62+ public void should_use_global_gradle_when_gradlew_is_not_installed () throws Exception {
5863 // given
5964 final var gradle = new GitPrePushHookInstallerGradle (logger , rootFolder ());
6065 setFile (".git/config" ).toContent ("" );
@@ -63,10 +68,14 @@ public void should_not_create_pre_hook_file_when_gradlew_is_not_installed() thro
6368 gradle .install ();
6469
6570 // then
66- assertThat (logs ).hasSize (2 );
71+ assertThat (logs ).hasSize (4 );
6772 assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
68- assertThat (logs ).element (1 ).isEqualTo ("Failed to find gradlew in root directory" );
69- assertThat (newFile (".git/hooks/pre-push" )).doesNotExist ();
73+ 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" );
75+ assertThat (logs ).element (3 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
76+
77+ final var content = gradleHookContent ("git_pre_hook/pre-push.created" , false );
78+ assertFile (".git/hooks/pre-push" ).hasContent (content );
7079 }
7180
7281 @ Test
@@ -92,7 +101,7 @@ public void should_not_create_pre_hook_file_when_hook_already_installed() throws
92101 public void should_create_pre_hook_file_when_hook_file_does_not_exists () throws Exception {
93102 // given
94103 final var gradle = new GitPrePushHookInstallerGradle (logger , rootFolder ());
95- final var gradlew = setFile ("gradlew" ).toContent ("" );
104+ setFile ("gradlew" ).toContent ("" );
96105 setFile (".git/config" ).toContent ("" );
97106
98107 // when
@@ -104,15 +113,15 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
104113 assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook not found, creating it" );
105114 assertThat (logs ).element (2 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
106115
107- final var content = gradleHookContent ("git_pre_hook/pre-push.created" );
116+ final var content = gradleHookContent ("git_pre_hook/pre-push.created" , true );
108117 assertFile (".git/hooks/pre-push" ).hasContent (content );
109118 }
110119
111120 @ Test
112121 public void should_append_to_existing_pre_hook_file_when_hook_file_exists () throws Exception {
113122 // given
114123 final var gradle = new GitPrePushHookInstallerGradle (logger , rootFolder ());
115- final var gradlew = setFile ("gradlew" ).toContent ("" );
124+ setFile ("gradlew" ).toContent ("" );
116125 setFile (".git/config" ).toContent ("" );
117126 setFile (".git/hooks/pre-push" ).toResource ("git_pre_hook/pre-push.existing" );
118127
@@ -124,14 +133,15 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
124133 assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
125134 assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
126135
127- final var content = gradleHookContent ("git_pre_hook/pre-push.existing-added" );
136+ final var content = gradleHookContent ("git_pre_hook/pre-push.existing-added" , true );
128137 assertFile (".git/hooks/pre-push" ).hasContent (content );
129138 }
130139
131140 @ Test
132141 public void should_create_pre_hook_file_for_maven_when_hook_file_does_not_exists () throws Exception {
133142 // given
134143 final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
144+ setFile ("mvnw" ).toContent ("" );
135145 setFile (".git/config" ).toContent ("" );
136146
137147 // when
@@ -143,20 +153,40 @@ public void should_create_pre_hook_file_for_maven_when_hook_file_does_not_exists
143153 assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook not found, creating it" );
144154 assertThat (logs ).element (2 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
145155
146- final var content = mavenHookContent ("git_pre_hook/pre-push.created" );
156+ final var content = mavenHookContent ("git_pre_hook/pre-push.created" , true );
157+ assertFile (".git/hooks/pre-push" ).hasContent (content );
158+ }
159+
160+ @ Test
161+ public void should_use_global_maven_when_maven_wrapper_is_not_installed () throws Exception {
162+ // given
163+ final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
164+ setFile (".git/config" ).toContent ("" );
165+
166+ // when
167+ gradle .install ();
168+
169+ // then
170+ assertThat (logs ).hasSize (4 );
171+ assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
172+ assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook not found, creating it" );
173+ assertThat (logs ).element (2 ).isEqualTo ("Maven wrapper is not installed, using global maven" );
174+ assertThat (logs ).element (3 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
175+
176+ final var content = mavenHookContent ("git_pre_hook/pre-push.created" , false );
147177 assertFile (".git/hooks/pre-push" ).hasContent (content );
148178 }
149179
150- private String gradleHookContent (String resourcePath ) {
180+ private String gradleHookContent (String resourcePath , boolean isWrapper ) {
151181 return getTestResource (resourcePath )
152- .replace ("${executor}" , setFile ("gradlew" ).toContent ( "" ). getAbsolutePath ())
182+ .replace ("${executor}" , isWrapper ? newFile ("gradlew" ).getAbsolutePath () : "gradle" )
153183 .replace ("${checkCommand}" , "spotlessCheck" )
154184 .replace ("${applyCommand}" , "spotlessApply" );
155185 }
156186
157- private String mavenHookContent (String resourcePath ) {
187+ private String mavenHookContent (String resourcePath , boolean isWrapper ) {
158188 return getTestResource (resourcePath )
159- .replace ("${executor}" , "mvn" )
189+ .replace ("${executor}" , isWrapper ? newFile ( "mvnw" ). getAbsolutePath () : "mvn" )
160190 .replace ("${checkCommand}" , "spotless:check" )
161191 .replace ("${applyCommand}" , "spotless:apply" );
162192 }
0 commit comments