2525import java .util .Collections ;
2626import java .util .List ;
2727import org .apache .maven .project .MavenProject ;
28+ import org .junit .Rule ;
2829import org .junit .Test ;
30+ import org .junit .rules .TemporaryFolder ;
2931import org .junit .runner .RunWith ;
3032import org .mockito .Mock ;
3133import org .mockito .junit .MockitoJUnitRunner ;
@@ -37,18 +39,20 @@ public class GitDirLocatorTest {
3739
3840 List <MavenProject > reactorProjects = Collections .emptyList ();
3941
42+ @ Rule
43+ public TemporaryFolder folder = new TemporaryFolder ();
44+
4045 @ Test
4146 public void shouldUseTheManuallySpecifiedDirectory () throws Exception {
4247 // given
43- File dotGitDir = Files . createTempDirectory ("temp" ). toFile ( );
48+ File dotGitDir = folder . newFolder ("temp" );
4449 try {
45-
4650 // when
4751 GitDirLocator locator = new GitDirLocator (project , reactorProjects );
4852 File foundDirectory = locator .lookupGitDirectory (dotGitDir );
4953
5054 // then
51- assert foundDirectory != null ;
55+ assertThat ( foundDirectory ). isNotNull () ;
5256 assertThat (foundDirectory .getAbsolutePath ()).isEqualTo (dotGitDir .getAbsolutePath ());
5357 } finally {
5458 if (!dotGitDir .delete ()) {
@@ -57,12 +61,66 @@ public void shouldUseTheManuallySpecifiedDirectory() throws Exception {
5761 }
5862 }
5963
64+ @ Test
65+ public void shouldResolveRelativeSubmodule () throws Exception {
66+ // given
67+ folder .newFolder ("main-project" );
68+ folder .newFolder ("main-project" , ".git" , "modules" , "sub-module" );
69+ folder .newFolder ("main-project" , "sub-module" );
70+
71+ // and a .git dir in submodule that points to the main's project .git/modules/submodule
72+ File dotGitDir = folder .getRoot ().toPath ()
73+ .resolve ("main-project" )
74+ .resolve ("sub-module" )
75+ .resolve (".git" )
76+ .toFile ();
77+ Files .write (
78+ dotGitDir .toPath (),
79+ "gitdir: ../.git/modules/sub-module" .getBytes ()
80+ );
81+
82+ try {
83+ // when
84+ GitDirLocator locator = new GitDirLocator (project , reactorProjects );
85+ File foundDirectory = locator .lookupGitDirectory (dotGitDir );
86+
87+ // then
88+ assertThat (foundDirectory ).isNotNull ();
89+ assertThat (
90+ foundDirectory .getCanonicalFile ()
91+ ).isEqualTo (
92+ folder .getRoot ().toPath ()
93+ .resolve ("main-project" )
94+ .resolve (".git" )
95+ .resolve ("modules" )
96+ .resolve ("sub-module" )
97+ .toFile ()
98+ );
99+ } finally {
100+ if (!dotGitDir .delete ()) {
101+ dotGitDir .deleteOnExit ();
102+ }
103+ }
104+ }
105+
60106 @ Test
61107 public void testWorktreeResolution () {
62- String [] noopCases = {"" , "a" , "a/b" , ".git/worktrees" , ".git/worktrees/" , "a.git/worktrees/b" };
108+ // tests to ensure we do not try to modify things that should not be modified
109+ String [] noopCases = {
110+ "" ,
111+ "a" ,
112+ "a/b" ,
113+ ".git/worktrees" ,
114+ ".git/worktrees/" ,
115+ "a.git/worktrees/b" ,
116+ ".git/modules" ,
117+ ".git/modules/" ,
118+ "a.git/modules/b" ,
119+ };
63120 for (String path : noopCases ) {
64121 assertThat (GitDirLocator .resolveWorktree (new File (path ))).isEqualTo (new File (path ));
65122 }
123+ // tests that worktree resolution works
66124 assertThat (GitDirLocator .resolveWorktree (new File ("a/.git/worktrees/b" )))
67125 .isEqualTo (new File ("a/.git" ));
68126 assertThat (GitDirLocator .resolveWorktree (new File ("/a/.git/worktrees/b" )))
0 commit comments