|
19 | 19 |
|
20 | 20 | import java.io.File; |
21 | 21 | import java.io.IOException; |
| 22 | +import java.util.List; |
22 | 23 | import java.util.Locale; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
23 | 26 |
|
24 | 27 | import org.junit.jupiter.api.Test; |
25 | 28 |
|
| 29 | +import com.diffplug.spotless.ProcessRunner; |
| 30 | + |
26 | 31 | class SpotlessInstallPrePushHookMojoTest extends MavenIntegrationHarness { |
27 | 32 |
|
28 | 33 | @Test |
29 | 34 | public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws Exception { |
30 | 35 | // given |
31 | 36 | setFile(".git/config").toContent(""); |
32 | | - setFile("license.txt").toResource("license/TestLicense"); |
| 37 | + setFile("license.txt").toResource("git_pre_hook/TestLicense.txt"); |
33 | 38 | writePomWithJavaLicenseHeaderStep(); |
34 | 39 |
|
35 | 40 | // 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"); |
40 | 42 |
|
41 | 43 | // then |
42 | 44 | 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 |
51 | 53 | public void should_append_to_existing_pre_hook_file_when_hook_file_exists() throws Exception { |
52 | 54 | // given |
53 | 55 | setFile(".git/config").toContent(""); |
54 | | - setFile("license.txt").toResource("license/TestLicense"); |
| 56 | + setFile("license.txt").toResource("git_pre_hook/TestLicense.txt"); |
55 | 57 | setFile(".git/hooks/pre-push").toResource("git_pre_hook/pre-push.existing"); |
56 | 58 |
|
57 | 59 | writePomWithJavaLicenseHeaderStep(); |
58 | 60 |
|
59 | 61 | // 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"); |
64 | 63 |
|
65 | 64 | // then |
66 | 65 | 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 |
70 | 69 | assertFile(".git/hooks/pre-push").hasContent(hookContent); |
71 | 70 | } |
72 | 71 |
|
| 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 | + |
73 | 96 | private void writePomWithJavaLicenseHeaderStep() throws IOException { |
74 | 97 | writePomWithJavaSteps( |
75 | 98 | "<licenseHeader>", |
@@ -86,8 +109,12 @@ private String getHookContent(String resourceFile) { |
86 | 109 | .replace("${applyCommand}", "spotless:apply"); |
87 | 110 | } |
88 | 111 |
|
| 112 | + private boolean isWindows() { |
| 113 | + return System.getProperty("os.name").toLowerCase(Locale.ROOT).startsWith("win"); |
| 114 | + } |
| 115 | + |
89 | 116 | private File executorWrapperFile() { |
90 | | - if (System.getProperty("os.name").toLowerCase(Locale.ROOT).startsWith("win")) { |
| 117 | + if (isWindows()) { |
91 | 118 | final var bat = newFile("mvnw.bat"); |
92 | 119 | if (bat.exists()) { |
93 | 120 | return bat; |
@@ -98,4 +125,57 @@ private File executorWrapperFile() { |
98 | 125 |
|
99 | 126 | return newFile("mvnw"); |
100 | 127 | } |
| 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 | + } |
101 | 181 | } |