-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathGitCommitIdMojoTest.java
More file actions
238 lines (195 loc) · 9.12 KB
/
GitCommitIdMojoTest.java
File metadata and controls
238 lines (195 loc) · 9.12 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
*
* git-commit-id-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-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-plugin. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.project13.maven.git;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.lib.Repository;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.*;
/**
* I'm not a big fan of this test - let's move to integration test from now on.
*
* @author <a href="mailto:konrad.malawski@java.pl">Konrad 'ktoso' Malawski</a>
*/
// todo remove this test in favor of complete intgration tests
public class GitCommitIdMojoTest {
GitCommitIdMojo mojo;
JGitProvider jGitProvider;
@Before
public void setUp() throws Exception {
File dotGitDirectory = AvailableGitTestRepo.GIT_COMMIT_ID.getDir();
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
gitDescribeConfig.setSkip(false);
String prefix = "git";
int abbrevLength = 7;
String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
boolean verbose = true;
mojo = new GitCommitIdMojo();
mojo.setDotGitDirectory(dotGitDirectory);
mojo.setPrefix(prefix);
mojo.setAbbrevLength(abbrevLength);
mojo.setDateFormat(dateFormat);
mojo.setVerbose(verbose);
mojo.useNativeGit(false);
mojo.setGitDescribe(gitDescribeConfig);
mojo.runningTests = true;
mojo.project = mock(MavenProject.class, RETURNS_MOCKS);
when(mojo.project.getPackaging()).thenReturn("jar");
jGitProvider = JGitProvider.on(mojo.lookupGitDirectory()).withLoggerBridge(mojo.getLoggerBridge());
}
@Test
@SuppressWarnings("")
public void shouldIncludeExpectedProperties() throws Exception {
mojo.execute();
Properties properties = mojo.getProperties();
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.files.dirty"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
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.remote.origin.url"));
}
@Test
@SuppressWarnings("")
public void shouldExcludeAsConfiguredProperties() throws Exception {
// given
mojo.setExcludeProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*"));
// when
mojo.execute();
// then
Properties properties = mojo.getProperties();
// explicitly excluded
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.remote.origin.url"));
// glob excluded
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.user.name"));
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.user.email"));
// these stay
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
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"));
}
@Test
@SuppressWarnings("")
public void shouldHaveNoPrefixWhenConfiguredPrefixIsEmptyStringAsConfiguredProperties() throws Exception {
// given
mojo.setPrefix("");
// when
mojo.execute();
// then
Properties properties = mojo.getProperties();
// explicitly excluded
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.remote.origin.url"));
assertThat(properties).satisfies(new DoesNotContainKeyCondition(".remote.origin.url"));
assertThat(properties).satisfies(new ContainsKeyCondition("remote.origin.url"));
}
@Test
public void shouldSkipDescribeWhenConfiguredToDoSo() throws Exception {
// given
GitDescribeConfig config = new GitDescribeConfig();
config.setSkip(true);
// when
mojo.setGitDescribe(config);
mojo.execute();
// then
assertThat(mojo.getProperties()).satisfies(new DoesNotContainKeyCondition("git.commit.id.describe"));
}
@Test
public void shouldUseJenkinsBranchInfoWhenAvailable() throws IOException {
// given
Repository git = mock(Repository.class);
Map<String, String> env = Maps.newHashMap();
String detachedHeadSHA1 = "16bb801934e652f5e291a003db05e364d83fba25";
String ciUrl = "http://myciserver.com";
when(git.getBranch()).thenReturn(detachedHeadSHA1);
jGitProvider.setRepository(git);
// when
// in a detached head state, getBranch() will return the SHA1...standard behavior
assertThat(detachedHeadSHA1).isEqualTo(jGitProvider.determineBranchName(env));
// again, SHA1 will be returned if we're in jenkins, but GIT_BRANCH is not set
env.put("JENKINS_URL", "http://myjenkinsserver.com");
assertThat(detachedHeadSHA1).isEqualTo(jGitProvider.determineBranchName(env));
// now set GIT_BRANCH too and see that the branch name from env var is returned
env.clear();
env.put("JENKINS_URL", ciUrl);
env.put("GIT_BRANCH", "mybranch");
assertThat("mybranch").isEqualTo(jGitProvider.determineBranchName(env));
// same, but for hudson
env.clear();
env.put("GIT_BRANCH", "mybranch");
env.put("HUDSON_URL", ciUrl);
assertThat("mybranch").isEqualTo(jGitProvider.determineBranchName(env));
// GIT_BRANCH but no HUDSON_URL or JENKINS_URL
env.clear();
env.put("GIT_BRANCH", "mybranch");
assertThat(detachedHeadSHA1).isEqualTo(jGitProvider.determineBranchName(env));
}
@Test
public void loadShortDescribe() {
assertShortDescribe("1.0.2-12-g19471", "1.0.2-12");
assertShortDescribe("1.0.2-12-g19471-DEV", "1.0.2-12-DEV");
assertShortDescribe("V-1.0.2-12-g19471-DEV", "V-1.0.2-12-DEV");
assertShortDescribe(null, null);
assertShortDescribe("12.4.0-1432", "12.4.0-1432");
assertShortDescribe("12.6.0", "12.6.0");
assertShortDescribe("", "");
}
private void assertShortDescribe(String commitDescribe, String expectedShortDescribe) {
GitCommitIdMojo commitIdMojo = new GitCommitIdMojo();
Properties prop = new Properties();
if (commitDescribe != null) {
prop.put(GitCommitIdMojo.COMMIT_DESCRIBE, commitDescribe);
}
commitIdMojo.loadShortDescribe(prop);
assertThat(prop.getProperty(GitCommitIdMojo.COMMIT_SHORT_DESCRIBE)).isEqualTo(expectedShortDescribe);
}
@Test
public void testCraftPropertiesOutputFileWithRelativePath() throws IOException {
GitCommitIdMojo commitIdMojo = new GitCommitIdMojo();
File baseDir = new File(".");
String targetDir = baseDir.getCanonicalPath() + "/";
String generateGitPropertiesFilename = "target/classes/git.properties";
File result = commitIdMojo.craftPropertiesOutputFile(baseDir, generateGitPropertiesFilename);
assertThat(result.getCanonicalPath()).isEqualTo(targetDir + generateGitPropertiesFilename);
}
@Test
public void testCraftPropertiesOutputFileWithFullPath() throws IOException {
GitCommitIdMojo commitIdMojo = new GitCommitIdMojo();
File baseDir = new File(".");
String targetDir = baseDir.getCanonicalPath() + "/";
String generateGitPropertiesFilename = targetDir + "target/classes/git.properties";
File result = commitIdMojo.craftPropertiesOutputFile(baseDir, generateGitPropertiesFilename);
assertThat(result.getCanonicalPath()).isEqualTo(generateGitPropertiesFilename);
}
}