legacyPreCommitHookBaseScriptCalls() {
+ gitBaseDir().relativize(getOrCreateHooksDirectory())
+ "/"
+ pluginPreCommitHookFileName());
+ calls.add("$(git rev-parse --git-dir)/" + HOOKS_DIR + "/" + pluginPreCommitHookFileName());
return calls;
}
diff --git a/core/src/test/java/com/cosium/code/format/MavenWrapperTest.java b/core/src/test/java/com/cosium/code/format/MavenWrapperTest.java
index e354daf..15330e8 100644
--- a/core/src/test/java/com/cosium/code/format/MavenWrapperTest.java
+++ b/core/src/test/java/com/cosium/code/format/MavenWrapperTest.java
@@ -26,6 +26,8 @@
public class MavenWrapperTest extends AbstractTest {
private static final String MAVEN_WRAPPER = "mvnw";
+ private static final String WORK_TREE_MAVEN_WRAPPER =
+ "\"$(git rev-parse --show-toplevel)/" + MAVEN_WRAPPER + "\"";
private static final String BAD_FORMAT_JAVA = "src/main/java/BadFormat.java";
private static final Path MAVEN_WRAPPER_PROPERTIES =
Paths.get(".mvn/wrapper/maven-wrapper.properties");
@@ -37,11 +39,11 @@ public MavenWrapperTest(MavenRuntime.MavenRuntimeBuilder mavenBuilder) throws Ex
@MavenPluginTest
@DisplayName("GIVEN a maven wrapper WHEN installing the hooks THEN the hook runs the wrapper")
public void test1() throws Exception {
- Path wrapper = installMavenWrapper(projectRoot());
+ installMavenWrapper(projectRoot());
installHooks();
- assertThat(readHookScript()).contains(wrapper.toAbsolutePath().toString());
+ assertThat(readHookScript()).contains(WORK_TREE_MAVEN_WRAPPER);
}
@MavenPluginTest
@@ -50,13 +52,13 @@ public void test1() throws Exception {
+ " wrapper")
public void test2() throws Exception {
// The build is run from the module, while the wrapper sits at the root of the repository.
- Path wrapper = installMavenWrapper(projectRoot());
+ installMavenWrapper(projectRoot());
Path module = Files.createDirectories(projectRoot().resolve("module"));
Files.copy(projectRoot().resolve("pom.xml"), module.resolve("pom.xml"));
buildMavenExecution(module).execute("initialize").assertErrorFreeLog();
- assertThat(readHookScript()).contains(wrapper.toAbsolutePath().toString());
+ assertThat(readHookScript()).contains(WORK_TREE_MAVEN_WRAPPER);
}
@MavenPluginTest
@@ -142,7 +144,7 @@ private void installHooks() throws Exception {
* pins the maven version the build already runs with, hence no distribution left to download. The
* executable bit has to be set back, as a plain copy drops it.
*/
- private Path installMavenWrapper(Path directory) throws IOException {
+ private void installMavenWrapper(Path directory) throws IOException {
Path repositoryRoot = Paths.get("..");
Path wrapper = directory.resolve(MAVEN_WRAPPER);
@@ -152,8 +154,6 @@ private Path installMavenWrapper(Path directory) throws IOException {
Path properties = directory.resolve(MAVEN_WRAPPER_PROPERTIES);
Files.createDirectories(properties.getParent());
Files.copy(repositoryRoot.resolve(MAVEN_WRAPPER_PROPERTIES), properties);
-
- return wrapper;
}
private String readHookScript() throws IOException {
diff --git a/core/src/test/java/com/cosium/code/format/WorktreeTest.java b/core/src/test/java/com/cosium/code/format/WorktreeTest.java
new file mode 100644
index 0000000..a0d109d
--- /dev/null
+++ b/core/src/test/java/com/cosium/code/format/WorktreeTest.java
@@ -0,0 +1,102 @@
+package com.cosium.code.format;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.takari.maven.testing.executor.MavenRuntime;
+import io.takari.maven.testing.executor.MavenVersions;
+import io.takari.maven.testing.executor.junit.MavenPluginTest;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Covers issue 390.
+ *
+ * JGit cannot add a linked worktree, hence the git command line.
+ *
+ * @author Réda Housni Alaoui
+ */
+@MavenVersions({"3.5.0"})
+public class WorktreeTest extends AbstractTest {
+
+ private static final String BAD_FORMAT_JAVA = "src/main/java/BadFormat.java";
+ private static final String FORMATTED_CONTENT =
+ "public class BadFormat {\n" + "\n" + " void a() {}\n" + "}\n";
+
+ @TempDir private Path worktreeParent;
+ private Path worktree;
+
+ public WorktreeTest(MavenRuntime.MavenRuntimeBuilder mavenBuilder) throws Exception {
+ super(mavenBuilder, "single-module");
+ }
+
+ @BeforeEach
+ void addWorktree() throws Exception {
+ worktree = worktreeParent.resolve("wt");
+ runGit(projectRoot(), "worktree", "add", "-b", "wt", worktree.toString());
+ }
+
+ @MavenPluginTest
+ @DisplayName(
+ "GIVEN hooks installed from the main checkout WHEN committing a bad formatted file from a"
+ + " linked worktree THEN the file of the worktree has the correct format")
+ public void test1() throws Exception {
+ buildMavenExecution(projectRoot()).execute("initialize").assertErrorFreeLog();
+
+ commitBadFormattedFileFromWorktree();
+
+ assertThat(worktree.resolve(BAD_FORMAT_JAVA))
+ .content(StandardCharsets.UTF_8)
+ .isEqualTo(FORMATTED_CONTENT);
+ }
+
+ @MavenPluginTest
+ @DisplayName(
+ "GIVEN hooks installed from a linked worktree WHEN committing a bad formatted file from the"
+ + " worktree THEN the file of the worktree has the correct format")
+ public void test2() throws Exception {
+ buildMavenExecution(worktree).execute("initialize").assertErrorFreeLog();
+
+ commitBadFormattedFileFromWorktree();
+
+ assertThat(worktree.resolve(BAD_FORMAT_JAVA))
+ .content(StandardCharsets.UTF_8)
+ .isEqualTo(FORMATTED_CONTENT);
+ }
+
+ private void commitBadFormattedFileFromWorktree() throws Exception {
+ Files.write(
+ worktree.resolve(BAD_FORMAT_JAVA),
+ ("public class BadFormat {\n" + "\n" + " void a( ){}\n" + "}\n")
+ .getBytes(StandardCharsets.UTF_8));
+ runGit(worktree, "add", BAD_FORMAT_JAVA);
+ runGit(
+ worktree,
+ "-c",
+ "user.name=" + gitIdentity().getName(),
+ "-c",
+ "user.email=" + gitIdentity().getEmailAddress(),
+ "commit",
+ "-m",
+ "Committing a badly formatted file");
+ }
+
+ private void runGit(Path directory, String... arguments)
+ throws IOException, InterruptedException {
+ List command = new ArrayList<>();
+ command.add("git");
+ command.addAll(Arrays.asList(arguments));
+ Process process =
+ new ProcessBuilder(command).directory(directory.toFile()).redirectErrorStream(true).start();
+ String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);
+ assertThat(process.waitFor()).as(output).isZero();
+ }
+}