Skip to content

Commit

Permalink
#701: add testcases to demonstrate the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSnoozer committed Mar 2, 2024
1 parent 239d73d commit 660a9a4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/main/java/pl/project13/maven/git/GitDirLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,27 @@ 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;
}
}

/**
* 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();
Expand Down
66 changes: 62 additions & 4 deletions src/test/java/pl/project13/maven/git/GitDirLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,18 +39,20 @@ public class GitDirLocatorTest {

List<MavenProject> 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()) {
Expand All @@ -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")))
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/pl/project13/maven/git/GitSubmodulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources
Submodule resources updated 54 files
+1 −0 _git_with_remote_submodules/COMMIT_EDITMSG
+1 −0 _git_with_remote_submodules/HEAD
+8 −0 _git_with_remote_submodules/config
+1 −0 _git_with_remote_submodules/description
+15 −0 _git_with_remote_submodules/hooks/applypatch-msg.sample
+24 −0 _git_with_remote_submodules/hooks/commit-msg.sample
+173 −0 _git_with_remote_submodules/hooks/fsmonitor-watchman.sample
+8 −0 _git_with_remote_submodules/hooks/post-update.sample
+14 −0 _git_with_remote_submodules/hooks/pre-applypatch.sample
+49 −0 _git_with_remote_submodules/hooks/pre-commit.sample
+13 −0 _git_with_remote_submodules/hooks/pre-merge-commit.sample
+53 −0 _git_with_remote_submodules/hooks/pre-push.sample
+169 −0 _git_with_remote_submodules/hooks/pre-rebase.sample
+24 −0 _git_with_remote_submodules/hooks/pre-receive.sample
+42 −0 _git_with_remote_submodules/hooks/prepare-commit-msg.sample
+78 −0 _git_with_remote_submodules/hooks/push-to-checkout.sample
+128 −0 _git_with_remote_submodules/hooks/update.sample
+ _git_with_remote_submodules/index
+6 −0 _git_with_remote_submodules/info/exclude
+1 −0 _git_with_remote_submodules/logs/HEAD
+1 −0 _git_with_remote_submodules/logs/refs/heads/master
+1 −0 _git_with_remote_submodules/modules/remote-module/HEAD
+15 −0 _git_with_remote_submodules/modules/remote-module/config
+1 −0 _git_with_remote_submodules/modules/remote-module/description
+15 −0 _git_with_remote_submodules/modules/remote-module/hooks/applypatch-msg.sample
+24 −0 _git_with_remote_submodules/modules/remote-module/hooks/commit-msg.sample
+173 −0 _git_with_remote_submodules/modules/remote-module/hooks/fsmonitor-watchman.sample
+8 −0 _git_with_remote_submodules/modules/remote-module/hooks/post-update.sample
+14 −0 _git_with_remote_submodules/modules/remote-module/hooks/pre-applypatch.sample
+49 −0 _git_with_remote_submodules/modules/remote-module/hooks/pre-commit.sample
+13 −0 _git_with_remote_submodules/modules/remote-module/hooks/pre-merge-commit.sample
+53 −0 _git_with_remote_submodules/modules/remote-module/hooks/pre-push.sample
+169 −0 _git_with_remote_submodules/modules/remote-module/hooks/pre-rebase.sample
+24 −0 _git_with_remote_submodules/modules/remote-module/hooks/pre-receive.sample
+42 −0 _git_with_remote_submodules/modules/remote-module/hooks/prepare-commit-msg.sample
+78 −0 _git_with_remote_submodules/modules/remote-module/hooks/push-to-checkout.sample
+128 −0 _git_with_remote_submodules/modules/remote-module/hooks/update.sample
+ _git_with_remote_submodules/modules/remote-module/index
+6 −0 _git_with_remote_submodules/modules/remote-module/info/exclude
+2 −0 _git_with_remote_submodules/modules/remote-module/logs/HEAD
+1 −0 _git_with_remote_submodules/modules/remote-module/logs/refs/heads/empty-branch
+1 −0 _git_with_remote_submodules/modules/remote-module/logs/refs/heads/master
+1 −0 _git_with_remote_submodules/modules/remote-module/logs/refs/remotes/origin/HEAD
+ ...with_remote_submodules/modules/remote-module/objects/pack/pack-d9286cd854d21f91ec99e39e2bd77b285b65369f.idx
+ ...ith_remote_submodules/modules/remote-module/objects/pack/pack-d9286cd854d21f91ec99e39e2bd77b285b65369f.pack
+3 −0 _git_with_remote_submodules/modules/remote-module/packed-refs
+1 −0 _git_with_remote_submodules/modules/remote-module/refs/heads/empty-branch
+1 −0 _git_with_remote_submodules/modules/remote-module/refs/heads/master
+1 −0 _git_with_remote_submodules/modules/remote-module/refs/remotes/origin/HEAD
+ _git_with_remote_submodules/objects/48/3af0b2862f9e7c6afc84d4cc4757dd7ecc1d7a
+ _git_with_remote_submodules/objects/64/55ccdd44935492173b2bb24061ea55deface15
+ _git_with_remote_submodules/objects/6c/c27180276224938bef4b8791d0f2131a2287e9
+ _git_with_remote_submodules/objects/71/a6c2eb9ca81d61e485f30509e03b98c6e276f2
+1 −0 _git_with_remote_submodules/refs/heads/master

0 comments on commit 660a9a4

Please sign in to comment.