@@ -32,6 +32,11 @@ public void info(String format, Object... arguments) {
32
32
logs .add (String .format (format , arguments ));
33
33
}
34
34
35
+ @ Override
36
+ public void warn (String format , Object ... arguments ) {
37
+ logs .add (String .format (format , arguments ));
38
+ }
39
+
35
40
@ Override
36
41
public void error (String format , Object ... arguments ) {
37
42
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
54
59
}
55
60
56
61
@ 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 {
58
63
// given
59
64
final var gradle = new GitPrePushHookInstallerGradle (logger , rootFolder ());
60
65
setFile (".git/config" ).toContent ("" );
@@ -63,10 +68,14 @@ public void should_not_create_pre_hook_file_when_gradlew_is_not_installed() thro
63
68
gradle .install ();
64
69
65
70
// then
66
- assertThat (logs ).hasSize (2 );
71
+ assertThat (logs ).hasSize (4 );
67
72
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 );
70
79
}
71
80
72
81
@ Test
@@ -92,7 +101,7 @@ public void should_not_create_pre_hook_file_when_hook_already_installed() throws
92
101
public void should_create_pre_hook_file_when_hook_file_does_not_exists () throws Exception {
93
102
// given
94
103
final var gradle = new GitPrePushHookInstallerGradle (logger , rootFolder ());
95
- final var gradlew = setFile ("gradlew" ).toContent ("" );
104
+ setFile ("gradlew" ).toContent ("" );
96
105
setFile (".git/config" ).toContent ("" );
97
106
98
107
// when
@@ -104,15 +113,15 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
104
113
assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook not found, creating it" );
105
114
assertThat (logs ).element (2 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
106
115
107
- final var content = gradleHookContent ("git_pre_hook/pre-push.created" );
116
+ final var content = gradleHookContent ("git_pre_hook/pre-push.created" , true );
108
117
assertFile (".git/hooks/pre-push" ).hasContent (content );
109
118
}
110
119
111
120
@ Test
112
121
public void should_append_to_existing_pre_hook_file_when_hook_file_exists () throws Exception {
113
122
// given
114
123
final var gradle = new GitPrePushHookInstallerGradle (logger , rootFolder ());
115
- final var gradlew = setFile ("gradlew" ).toContent ("" );
124
+ setFile ("gradlew" ).toContent ("" );
116
125
setFile (".git/config" ).toContent ("" );
117
126
setFile (".git/hooks/pre-push" ).toResource ("git_pre_hook/pre-push.existing" );
118
127
@@ -124,14 +133,15 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
124
133
assertThat (logs ).element (0 ).isEqualTo ("Installing git pre-push hook" );
125
134
assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
126
135
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 );
128
137
assertFile (".git/hooks/pre-push" ).hasContent (content );
129
138
}
130
139
131
140
@ Test
132
141
public void should_create_pre_hook_file_for_maven_when_hook_file_does_not_exists () throws Exception {
133
142
// given
134
143
final var gradle = new GitPrePushHookInstallerMaven (logger , rootFolder ());
144
+ setFile ("mvnw" ).toContent ("" );
135
145
setFile (".git/config" ).toContent ("" );
136
146
137
147
// when
@@ -143,20 +153,40 @@ public void should_create_pre_hook_file_for_maven_when_hook_file_does_not_exists
143
153
assertThat (logs ).element (1 ).isEqualTo ("Git pre-push hook not found, creating it" );
144
154
assertThat (logs ).element (2 ).isEqualTo ("Git pre-push hook installed successfully to the file " + newFile (".git/hooks/pre-push" ).getAbsolutePath ());
145
155
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 );
147
177
assertFile (".git/hooks/pre-push" ).hasContent (content );
148
178
}
149
179
150
- private String gradleHookContent (String resourcePath ) {
180
+ private String gradleHookContent (String resourcePath , boolean isWrapper ) {
151
181
return getTestResource (resourcePath )
152
- .replace ("${executor}" , setFile ("gradlew" ).toContent ( "" ). getAbsolutePath ())
182
+ .replace ("${executor}" , isWrapper ? newFile ("gradlew" ).getAbsolutePath () : "gradle" )
153
183
.replace ("${checkCommand}" , "spotlessCheck" )
154
184
.replace ("${applyCommand}" , "spotlessApply" );
155
185
}
156
186
157
- private String mavenHookContent (String resourcePath ) {
187
+ private String mavenHookContent (String resourcePath , boolean isWrapper ) {
158
188
return getTestResource (resourcePath )
159
- .replace ("${executor}" , "mvn" )
189
+ .replace ("${executor}" , isWrapper ? newFile ( "mvnw" ). getAbsolutePath () : "mvn" )
160
190
.replace ("${checkCommand}" , "spotless:check" )
161
191
.replace ("${applyCommand}" , "spotless:apply" );
162
192
}
0 commit comments