-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathGitDirLocatorTest.java
More file actions
129 lines (115 loc) · 3.94 KB
/
GitDirLocatorTest.java
File metadata and controls
129 lines (115 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* This file is part of git-commit-id-maven-plugin
* Originally invented by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
*
* git-commit-id-maven-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-maven-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.project13.maven.git;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.nio.file.Files;
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;
@RunWith(MockitoJUnitRunner.class)
public class GitDirLocatorTest {
@Mock MavenProject project;
List<MavenProject> reactorProjects = Collections.emptyList();
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void shouldUseTheManuallySpecifiedDirectory() throws Exception {
// given
File dotGitDir = folder.newFolder("temp");
try {
// when
GitDirLocator locator = new GitDirLocator(project, reactorProjects);
File foundDirectory = locator.lookupGitDirectory(dotGitDir);
// then
assertThat(foundDirectory).isNotNull();
assertThat(foundDirectory.getAbsolutePath()).isEqualTo(dotGitDir.getAbsolutePath());
} finally {
if (!dotGitDir.delete()) {
dotGitDir.deleteOnExit();
}
}
}
@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() {
// 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")))
.isEqualTo(new File("/a/.git"));
}
}