-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathGitIntegrationTest.java
More file actions
215 lines (190 loc) · 8.67 KB
/
GitIntegrationTest.java
File metadata and controls
215 lines (190 loc) · 8.67 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.eclipse.jgit.api.Git;
import org.jspecify.annotations.NonNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.provider.Arguments;
import pl.project13.core.CommitIdPropertiesOutputFormat;
/**
* Base class for various Testcases to verify that the git-commit-id-plugin works properly.
*/
public abstract class GitIntegrationTest {
private static final String SANDBOX_DIR = "target" + File.separator + "sandbox" + File.separator;
protected static final String evaluateOnCommit = "HEAD";
private static final boolean UseJGit = false;
private static final boolean UseNativeGit = true;
public static Stream<Arguments> useNativeGit() {
return Stream.of(
Arguments.of(UseJGit),
Arguments.of(UseNativeGit)
);
}
/** Sandbox directory with unique name for current test. */
private String currSandbox;
protected GitCommitIdMojo mojo;
protected FileSystemMavenSandbox mavenSandbox;
@BeforeEach
public void setUp() throws Exception {
// generate unique sandbox for this test
File sandbox;
do {
currSandbox =
SANDBOX_DIR
+ "sandbox"
+ Integer.toString(ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE));
sandbox = new File(currSandbox);
} while (sandbox.exists());
mavenSandbox = new FileSystemMavenSandbox(currSandbox);
mojo = spy(GitCommitIdMojo.class);
initializeMojoWithDefaults(mojo);
}
@AfterEach
public void tearDown() throws Exception {
final boolean keep = mavenSandbox != null && mavenSandbox.isKeepSandboxWhenFinishedTest();
mojo = null;
mavenSandbox = null;
final File sandbox = new File(currSandbox);
try {
if (sandbox.exists() && !keep) {
FileUtils.deleteDirectory(sandbox);
}
} catch (IOException e) {
System.out.println("Unable to delete sandbox. Scheduling deleteOnExit: " + currSandbox);
sandbox.deleteOnExit();
}
}
protected Git git(String dir) throws IOException, InterruptedException {
return Git.open(dotGitDir(Optional.of(dir)));
}
protected Git git() throws IOException, InterruptedException {
return Git.open(dotGitDir(projectDir()));
}
protected Optional<String> projectDir() {
return Optional.empty();
}
@NonNull
protected File dotGitDir(@NonNull Optional<String> projectDir) {
if (projectDir.isPresent()) {
return new File(currSandbox + File.separator + projectDir.get() + File.separator + ".git");
} else {
return new File(currSandbox + File.separator + ".git");
}
}
public static void initializeMojoWithDefaults(GitCommitIdMojo mojo) {
mojo.verbose = false;
mojo.skipPoms = true;
mojo.abbrevLength = 7;
mojo.generateGitPropertiesFile = false;
mojo.format = CommitIdPropertiesOutputFormat.PROPERTIES.toString();
mojo.generateGitPropertiesFilename = "src/main/resources/git.properties";
mojo.generateGitPropertiesFileWithEscapedUnicode = true;
mojo.prefix = "git";
mojo.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
mojo.failOnNoGitDirectory = true;
mojo.useNativeGit = false;
mojo.commitIdGenerationMode = "full";
mojo.evaluateOnCommit = evaluateOnCommit;
mojo.nativeGitTimeoutInMs = (30 * 1000);
mojo.session = mockSession();
mojo.settings = mockSettings();
}
public void setProjectToExecuteMojoIn(@NonNull MavenProject project) {
mojo.project = project;
mojo.dotGitDirectory = new File(project.getBasedir(), ".git");
mojo.reactorProjects = getReactorProjects(project);
}
private static MavenSession mockSession() {
MavenSession session = mock(MavenSession.class);
when(session.getUserProperties()).thenReturn(new Properties());
when(session.getSystemProperties()).thenReturn(new Properties());
return session;
}
private static Settings mockSettings() {
Settings settings = mock(Settings.class);
when(settings.isOffline()).thenReturn(false);
return settings;
}
private static List<MavenProject> getReactorProjects(@NonNull MavenProject project) {
List<MavenProject> reactorProjects = new ArrayList<>();
MavenProject mavenProject = project;
while (mavenProject != null) {
reactorProjects.add(mavenProject);
mavenProject = mavenProject.getParent();
}
return reactorProjects;
}
public static void assertPropertyPresentAndEqual(
Properties properties, String key, String expected) {
assertThat(properties.stringPropertyNames()).contains(key);
assertThat(properties.getProperty(key)).isEqualTo(expected);
}
/**
* Ensures that the provided properties contain the properties the plugin can generate.
* See also {@link pl.project13.core.GitCommitPropertyConstant}
*
* @param properties The properties that should be verified
*/
public static void assertGitPropertiesPresentInProject(Properties properties) {
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.local.branch.ahead"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.local.branch.behind"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.dirty"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.describe"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.describe-short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.version"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.host"));
// assertThat(properties).satisfies(new ContainsKeyCondition("git.build.number"));
// assertThat(properties).satisfies(new ContainsKeyCondition("git.build.number.unique"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.author.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.committer.time"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.tags"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.name"));
// assertThat(properties).satisfies(new ContainsKeyCondition("git.tag"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.closest.tag.commit.count"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.total.commit.count"));
}
}