Skip to content

Commit 776f7d1

Browse files
lazer-devntwigg
authored andcommitted
execute pre-push hook
1 parent b8e80bc commit 776f7d1

File tree

4 files changed

+125
-11
lines changed

4 files changed

+125
-11
lines changed

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenRunner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public MavenRunner withArguments(String... args) {
5656
return this;
5757
}
5858

59+
public MavenRunner withEnvironment(String key, String value) {
60+
environment.put(key, value);
61+
return this;
62+
}
63+
5964
public MavenRunner withRunner(ProcessRunner runner) {
6065
this.runner = runner;
6166
return this;

plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojoTest.java

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,26 @@
1919

2020
import java.io.File;
2121
import java.io.IOException;
22+
import java.util.List;
2223
import java.util.Locale;
24+
import java.util.Map;
25+
import java.util.Optional;
2326

2427
import org.junit.jupiter.api.Test;
2528

29+
import com.diffplug.spotless.ProcessRunner;
30+
2631
class SpotlessInstallPrePushHookMojoTest extends MavenIntegrationHarness {
2732

2833
@Test
2934
public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws Exception {
3035
// given
3136
setFile(".git/config").toContent("");
32-
setFile("license.txt").toResource("license/TestLicense");
37+
setFile("license.txt").toResource("git_pre_hook/TestLicense.txt");
3338
writePomWithJavaLicenseHeaderStep();
3439

3540
// when
36-
final var output = mavenRunner()
37-
.withArguments("spotless:install-git-pre-push-hook")
38-
.runNoError()
39-
.stdOutUtf8();
41+
final var output = runMaven("spotless:install-git-pre-push-hook");
4042

4143
// then
4244
assertThat(output).contains("Installing git pre-push hook");
@@ -51,16 +53,13 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
5153
public void should_append_to_existing_pre_hook_file_when_hook_file_exists() throws Exception {
5254
// given
5355
setFile(".git/config").toContent("");
54-
setFile("license.txt").toResource("license/TestLicense");
56+
setFile("license.txt").toResource("git_pre_hook/TestLicense.txt");
5557
setFile(".git/hooks/pre-push").toResource("git_pre_hook/pre-push.existing");
5658

5759
writePomWithJavaLicenseHeaderStep();
5860

5961
// when
60-
final var output = mavenRunner()
61-
.withArguments("spotless:install-git-pre-push-hook")
62-
.runNoError()
63-
.stdOutUtf8();
62+
final var output = runMaven("spotless:install-git-pre-push-hook");
6463

6564
// then
6665
assertThat(output).contains("Installing git pre-push hook");
@@ -70,6 +69,30 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
7069
assertFile(".git/hooks/pre-push").hasContent(hookContent);
7170
}
7271

72+
@Test
73+
public void should_execute_pre_push_script() throws Exception {
74+
// given
75+
setFile("license.txt").toResource("git_pre_hook/TestLicense.txt");
76+
setFile(".git/config").toContent("");
77+
setFile("src/main/java/com.github.youribonnaffe.gradle.format/Java8Test.java").toResource("git_pre_hook/MissingLicense.test");
78+
writePomWithJavaLicenseHeaderStep();
79+
80+
// when
81+
// install pre-hook
82+
final var output = runMaven("spotless:install-git-pre-push-hook");
83+
84+
final var result = executeHookScript(".git/hooks/pre-push");
85+
86+
// then
87+
assertThat(output).contains("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push"));
88+
89+
assertThat(result.stdErrUtf8()).startsWith("spotless found problems, running spotless:apply; commit the result and re-push");
90+
assertThat(result.exitCode()).isEqualTo(1);
91+
92+
final var fileContent = read("src/main/java/com.github.youribonnaffe.gradle.format/Java8Test.java");
93+
assertThat(fileContent).startsWith("this is a test license!\n");
94+
}
95+
7396
private void writePomWithJavaLicenseHeaderStep() throws IOException {
7497
writePomWithJavaSteps(
7598
"<licenseHeader>",
@@ -86,8 +109,12 @@ private String getHookContent(String resourceFile) {
86109
.replace("${applyCommand}", "spotless:apply");
87110
}
88111

112+
private boolean isWindows() {
113+
return System.getProperty("os.name").toLowerCase(Locale.ROOT).startsWith("win");
114+
}
115+
89116
private File executorWrapperFile() {
90-
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).startsWith("win")) {
117+
if (isWindows()) {
91118
final var bat = newFile("mvnw.bat");
92119
if (bat.exists()) {
93120
return bat;
@@ -98,4 +125,57 @@ private File executorWrapperFile() {
98125

99126
return newFile("mvnw");
100127
}
128+
129+
private String runMaven(String command) throws Exception {
130+
return mavenRunner()
131+
.withArguments(command)
132+
.withEnvironment("JAVA_HOME", System.getProperty("java.home"))
133+
.runNoError()
134+
.stdOutUtf8();
135+
}
136+
137+
private ProcessRunner.Result executeHookScript(String hookFile) throws Exception {
138+
try (final var runner = new ProcessRunner()) {
139+
String executor = "sh";
140+
if (isWindows()) {
141+
final var bashPath = findGitBashExecutable();
142+
if (bashPath.isEmpty()) {
143+
throw new RuntimeException("Could not find git bash executable");
144+
}
145+
146+
executor = bashPath.get();
147+
}
148+
149+
return runner.exec(rootFolder(), Map.of("JAVA_HOME", System.getProperty("java.home")), null, List.of(executor, hookFile));
150+
}
151+
}
152+
153+
private Optional<String> findGitBashExecutable() {
154+
// 1. Check environment variable
155+
final var envPath = System.getenv("GIT_BASH");
156+
if (envPath != null && new File(envPath).exists()) {
157+
return Optional.of(envPath);
158+
}
159+
160+
// 2. Check common install paths
161+
final var commonPaths = List.of(
162+
"C:\\Program Files\\Git\\bin\\bash.exe",
163+
"C:\\Program Files (x86)\\Git\\bin\\bash.exe",
164+
"C:\\Program Files\\Git\\usr\\bin\\bash.exe");
165+
166+
for (var path : commonPaths) {
167+
if (new File(path).exists()) {
168+
return Optional.of(path);
169+
}
170+
}
171+
172+
// 3. Try bash from PATH
173+
try {
174+
Process process = new ProcessBuilder("bash", "--version").start();
175+
process.waitFor();
176+
return Optional.of("bash"); // just use "bash"
177+
} catch (Exception e) {
178+
return Optional.empty();
179+
}
180+
}
101181
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.youribonnaffe.gradle.format;
2+
3+
import java.util.function.Function;
4+
5+
6+
public class Java8Test {
7+
public void doStuff() throws Exception {
8+
Function<String, Integer> example = Integer::parseInt;
9+
example.andThen(val -> {
10+
return val + 2;
11+
} );
12+
SimpleEnum val = SimpleEnum.A;
13+
switch (val) {
14+
case A:
15+
break;
16+
case B:
17+
break;
18+
case C:
19+
break;
20+
default:
21+
throw new Exception();
22+
}
23+
}
24+
25+
public enum SimpleEnum {
26+
A, B, C;
27+
}
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a test license!

0 commit comments

Comments
 (0)