From 660a9a422388b29f98b24da90ca6c5f133f3967c Mon Sep 17 00:00:00 2001 From: TheSnoozer Date: Sat, 2 Mar 2024 08:25:31 +0100 Subject: [PATCH] https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/701: add testcases to demonstrate the issue --- .../pl/project13/maven/git/GitDirLocator.java | 13 ++-- .../maven/git/GitDirLocatorTest.java | 66 +++++++++++++++++-- .../maven/git/GitSubmodulesTest.java | 29 ++++++++ src/test/resources | 2 +- 4 files changed, 101 insertions(+), 9 deletions(-) diff --git a/src/main/java/pl/project13/maven/git/GitDirLocator.java b/src/main/java/pl/project13/maven/git/GitDirLocator.java index 4926ab61..ac400437 100644 --- a/src/main/java/pl/project13/maven/git/GitDirLocator.java +++ b/src/main/java/pl/project13/maven/git/GitDirLocator.java @@ -135,13 +135,14 @@ private File processGitDirFile(@Nonnull File file) { } // All seems ok so return the "gitdir" value read from the file. - File gitDir = resolveWorktree(new File(parts[1])); + String extractFromConfig = parts[1]; + File gitDir = resolveWorktree(new File(extractFromConfig)); if (gitDir.isAbsolute()) { // gitdir value is an absolute path. Return as-is return gitDir; } else { // gitdir value is relative. - return new File(file.getParentFile(), parts[1]); + return new File(file.getParentFile(), extractFromConfig); } } catch (IOException e) { return null; @@ -149,8 +150,12 @@ private File processGitDirFile(@Nonnull File file) { } /** - * If the file looks like the location of a worktree, return the .git folder of the git repository - * of the worktree. If not, return the file as is. + * Attempts to resolve the actual location of the .git folder for a given + * worktree. + * For example for a worktree like {@code a/.git/worktrees/X} structure would return {@code a/.git}. + * + * If the conditions for a git worktree like file structure are met simply return the provided + * argument as is. */ static File resolveWorktree(File fileLocation) { Path parent = fileLocation.toPath().getParent(); diff --git a/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java b/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java index e4ac9e12..237047f2 100644 --- a/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java +++ b/src/test/java/pl/project13/maven/git/GitDirLocatorTest.java @@ -25,7 +25,9 @@ import java.util.Collections; import java.util.List; import org.apache.maven.project.MavenProject; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; @@ -37,18 +39,20 @@ public class GitDirLocatorTest { List reactorProjects = Collections.emptyList(); + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + @Test public void shouldUseTheManuallySpecifiedDirectory() throws Exception { // given - File dotGitDir = Files.createTempDirectory("temp").toFile(); + File dotGitDir = folder.newFolder("temp"); try { - // when GitDirLocator locator = new GitDirLocator(project, reactorProjects); File foundDirectory = locator.lookupGitDirectory(dotGitDir); // then - assert foundDirectory != null; + assertThat(foundDirectory).isNotNull(); assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath()); } finally { if (!dotGitDir.delete()) { @@ -57,12 +61,66 @@ public void shouldUseTheManuallySpecifiedDirectory() throws Exception { } } + @Test + public void shouldResolveRelativeSubmodule() throws Exception { + // given + folder.newFolder("main-project"); + folder.newFolder("main-project", ".git", "modules", "sub-module"); + folder.newFolder("main-project", "sub-module"); + + // and a .git dir in submodule that points to the main's project .git/modules/submodule + File dotGitDir = folder.getRoot().toPath() + .resolve("main-project") + .resolve("sub-module") + .resolve(".git") + .toFile(); + Files.write( + dotGitDir.toPath(), + "gitdir: ../.git/modules/sub-module".getBytes() + ); + + try { + // when + GitDirLocator locator = new GitDirLocator(project, reactorProjects); + File foundDirectory = locator.lookupGitDirectory(dotGitDir); + + // then + assertThat(foundDirectory).isNotNull(); + assertThat( + foundDirectory.getCanonicalFile() + ).isEqualTo( + folder.getRoot().toPath() + .resolve("main-project") + .resolve(".git") + .resolve("modules") + .resolve("sub-module") + .toFile() + ); + } finally { + if (!dotGitDir.delete()) { + dotGitDir.deleteOnExit(); + } + } + } + @Test public void testWorktreeResolution() { - String[] noopCases = {"", "a", "a/b", ".git/worktrees", ".git/worktrees/", "a.git/worktrees/b"}; + // tests to ensure we do not try to modify things that should not be modified + String[] noopCases = { + "", + "a", + "a/b", + ".git/worktrees", + ".git/worktrees/", + "a.git/worktrees/b", + ".git/modules", + ".git/modules/", + "a.git/modules/b", + }; for (String path : noopCases) { assertThat(GitDirLocator.resolveWorktree(new File(path))).isEqualTo(new File(path)); } + // tests that worktree resolution works assertThat(GitDirLocator.resolveWorktree(new File("a/.git/worktrees/b"))) .isEqualTo(new File("a/.git")); assertThat(GitDirLocator.resolveWorktree(new File("/a/.git/worktrees/b"))) diff --git a/src/test/java/pl/project13/maven/git/GitSubmodulesTest.java b/src/test/java/pl/project13/maven/git/GitSubmodulesTest.java index 4c4ba853..d7efba59 100644 --- a/src/test/java/pl/project13/maven/git/GitSubmodulesTest.java +++ b/src/test/java/pl/project13/maven/git/GitSubmodulesTest.java @@ -21,6 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; +import java.nio.file.Files; import java.util.Properties; import javax.annotation.Nonnull; import org.apache.maven.project.MavenProject; @@ -49,6 +50,34 @@ public void shouldResolvePropertiesOnDefaultSettingsForNonPomProject() throws Ex assertGitPropertiesPresentInProject(targetProject.getProperties()); } + @Test + public void shouldGeneratePropertiesWithSubmodules() throws Exception { + // given + mavenSandbox + .withParentProject("my-pom-project", "pom") + .withGitRepoInParent(AvailableGitTestRepo.WITH_REMOTE_SUBMODULES) + .withChildProject("remote-module", "jar") + .create(); + MavenProject targetProject = mavenSandbox.getChildProject(); + setProjectToExecuteMojoIn(targetProject); + + // create a relative pointer to trigger the relative path logic in GitDirLocator#lookupGitDirectory + // makes the dotGitDirectory look like "my-pom-project/.git/modules/remote-module" + Files.write( + mavenSandbox.getChildProject().getBasedir().toPath().resolve(".git"), + "gitdir: ../.git/modules/remote-module".getBytes() + ); + + mojo.useNativeGit = true; // FIXME: make me a parameter + + // when + mojo.execute(); + + // then + assertGitPropertiesPresentInProject(targetProject.getProperties()); + assertThat(targetProject.getProperties().getProperty("git.commit.id.abbrev")).isEqualTo("945bfe6"); + } + public void setProjectToExecuteMojoIn(@Nonnull MavenProject project) { mojo.project = project; mojo.dotGitDirectory = new File(project.getBasedir(), ".git"); diff --git a/src/test/resources b/src/test/resources index f2d84eea..0e549504 160000 --- a/src/test/resources +++ b/src/test/resources @@ -1 +1 @@ -Subproject commit f2d84eeaa87c2a28e482bbd64eae47402a08bff4 +Subproject commit 0e549504984403f5e9a29b9c104d027a705d9963