Skip to content

Commit 1fa8c08

Browse files
committed
test: add gitversioning tests
1 parent 74bb3a5 commit 1fa8c08

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

src/test/java/me/qoomon/gitversioning/GitUtilTest.java

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.File;
1111
import java.io.IOException;
1212
import java.nio.file.Path;
13+
import java.util.Arrays;
1314
import java.util.List;
1415

1516
import static me.qoomon.gitversioning.GitConstants.NO_COMMIT;
@@ -293,4 +294,117 @@ void headSituation_detachedHeadWithTag() throws GitAPIException {
293294
softly.assertThat(it.getHeadTags()).containsExactly(givenTag);
294295
}));
295296
}
296-
}
297+
298+
@Test
299+
void situation_annotatedTagOnMaster() throws Exception {
300+
301+
// Given
302+
Git git = Git.init().setDirectory(tempDir.toFile()).call();
303+
RevCommit givenCommit = git.commit().setMessage("initial commit").setAllowEmpty(true).call();
304+
305+
String givenTag = "v1";
306+
git.tag().setAnnotated(true).setName(givenTag).call();
307+
308+
// When
309+
GitRepoSituation repoSituation = GitUtil.situation(tempDir.toFile());
310+
311+
// Then
312+
assertThat(repoSituation).satisfies(it -> assertSoftly(softly -> {
313+
softly.assertThat(it.isClean()).isTrue();
314+
softly.assertThat(it.getHeadCommit()).isEqualTo(givenCommit.getName());
315+
softly.assertThat(it.getHeadBranch()).isEqualTo("master");
316+
softly.assertThat(it.getHeadTags()).containsExactly(givenTag);
317+
}));
318+
}
319+
320+
@Test
321+
void situation_annotatedTagDetached() throws Exception {
322+
323+
// Given
324+
Git git = Git.init().setDirectory(tempDir.toFile()).call();
325+
RevCommit givenCommit = git.commit().setMessage("initial commit").setAllowEmpty(true).call();
326+
327+
String givenTag = "v1";
328+
git.tag().setAnnotated(true).setName(givenTag).setObjectId(givenCommit).call();
329+
git.checkout().setName(givenTag).call();
330+
331+
// When
332+
GitRepoSituation repoSituation = GitUtil.situation(tempDir.toFile());
333+
334+
// Then
335+
assertThat(repoSituation).satisfies(it -> assertSoftly(softly -> {
336+
softly.assertThat(it.isClean()).isTrue();
337+
softly.assertThat(it.getHeadCommit()).isEqualTo(givenCommit.getName());
338+
softly.assertThat(it.getHeadBranch()).isNull();
339+
softly.assertThat(it.getHeadTags()).containsExactly(givenTag);
340+
}));
341+
}
342+
343+
@Test
344+
void situation_lightweightTagOnMaster() throws Exception {
345+
346+
// Given
347+
Git git = Git.init().setDirectory(tempDir.toFile()).call();
348+
RevCommit givenCommit = git.commit().setMessage("initial commit").setAllowEmpty(true).call();
349+
350+
String givenTag = "v1";
351+
git.tag().setAnnotated(false).setName(givenTag).call();
352+
353+
// When
354+
GitRepoSituation repoSituation = GitUtil.situation(tempDir.toFile());
355+
356+
// Then
357+
assertThat(repoSituation).satisfies(it -> assertSoftly(softly -> {
358+
softly.assertThat(it.isClean()).isTrue();
359+
softly.assertThat(it.getHeadCommit()).isEqualTo(givenCommit.getName());
360+
softly.assertThat(it.getHeadBranch()).isEqualTo("master");
361+
softly.assertThat(it.getHeadTags()).containsExactly(givenTag);
362+
}));
363+
}
364+
365+
@Test
366+
void situation_lightweightTagDetached() throws Exception {
367+
368+
// Given
369+
Git git = Git.init().setDirectory(tempDir.toFile()).call();
370+
RevCommit givenCommit = git.commit().setMessage("initial commit").setAllowEmpty(true).call();
371+
372+
String givenTag = "v1";
373+
git.tag().setAnnotated(false).setName(givenTag).call();
374+
git.checkout().setName(givenTag).call();
375+
376+
// When
377+
GitRepoSituation repoSituation = GitUtil.situation(tempDir.toFile());
378+
379+
// Then
380+
assertThat(repoSituation).satisfies(it -> assertSoftly(softly -> {
381+
softly.assertThat(it.isClean()).isTrue();
382+
softly.assertThat(it.getHeadCommit()).isEqualTo(givenCommit.getName());
383+
softly.assertThat(it.getHeadBranch()).isNull();
384+
softly.assertThat(it.getHeadTags()).containsExactly(givenTag);
385+
}));
386+
}
387+
388+
@Test
389+
void situation_multipleTags() throws Exception {
390+
391+
// Given
392+
Git git = Git.init().setDirectory(tempDir.toFile()).call();
393+
git.commit().setMessage("initial commit").setAllowEmpty(true).call();
394+
395+
String givenTag1 = "v2";
396+
git.tag().setAnnotated(false).setName(givenTag1).call();
397+
String givenTag2 = "v1";
398+
git.tag().setAnnotated(false).setName(givenTag2).call();
399+
String givenTag3 = "v2.1";
400+
git.tag().setAnnotated(false).setName(givenTag3).call();
401+
402+
// When
403+
GitRepoSituation repoSituation = GitUtil.situation(tempDir.toFile());
404+
405+
// Then
406+
// expect tags to be sorted alphanumerically
407+
List<String> expectedTags = Arrays.asList(givenTag2, givenTag1, givenTag3);
408+
assertThat(repoSituation.getHeadTags()).isEqualTo(expectedTags);
409+
}
410+
}

src/test/java/me/qoomon/gitversioning/GitVersioningTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,64 @@ void determineVersion_forBranchWithTag() {
119119
);
120120
}
121121

122+
@Test
123+
void determineVersion_forBranchWithTag_withTagRulePreferred() {
124+
125+
// given
126+
GitRepoSituation repoSituation = new GitRepoSituation();
127+
repoSituation.setHeadBranch("develop");
128+
repoSituation.setHeadTags(singletonList("v1"));
129+
130+
String currentVersion = "undefined";
131+
132+
// when
133+
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
134+
new VersionDescription(),
135+
singletonList(new VersionDescription(null, "${branch}-branch")),
136+
singletonList(new VersionDescription(null, "${tag}-tag")),
137+
true);
138+
139+
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
140+
141+
// then
142+
assertAll(
143+
() -> assertThat(gitVersionDetails.isClean()).isTrue(),
144+
() -> assertThat(gitVersionDetails.getCommit()).isEqualTo(repoSituation.getHeadCommit()),
145+
() -> assertThat(gitVersionDetails.getCommitRefType()).isEqualTo("tag"),
146+
() -> assertThat(gitVersionDetails.getCommitRefName()).isEqualTo(repoSituation.getHeadTags().get(0)),
147+
() -> assertThat(gitVersion).isEqualTo(repoSituation.getHeadTags().get(0) + "-tag")
148+
);
149+
}
150+
151+
@Test
152+
void determineVersion_forBranchWithTag_withTagRuleNotPreferred() {
153+
154+
// given
155+
GitRepoSituation repoSituation = new GitRepoSituation();
156+
repoSituation.setHeadBranch("develop");
157+
repoSituation.setHeadTags(singletonList("v1"));
158+
159+
String currentVersion = "undefined";
160+
161+
// when
162+
GitVersionDetails gitVersionDetails = GitVersioning.determineVersion(repoSituation,
163+
new VersionDescription(),
164+
singletonList(new VersionDescription(null, "${branch}-branch")),
165+
singletonList(new VersionDescription(null, "${tag}-tag")),
166+
false);
167+
168+
String gitVersion = gitVersionDetails.getVersionTransformer().apply(currentVersion);
169+
170+
// then
171+
assertAll(
172+
() -> assertThat(gitVersionDetails.isClean()).isTrue(),
173+
() -> assertThat(gitVersionDetails.getCommit()).isEqualTo(repoSituation.getHeadCommit()),
174+
() -> assertThat(gitVersionDetails.getCommitRefType()).isEqualTo("branch"),
175+
() -> assertThat(gitVersionDetails.getCommitRefName()).isEqualTo(repoSituation.getHeadBranch()),
176+
() -> assertThat(gitVersion).isEqualTo(repoSituation.getHeadBranch() + "-branch")
177+
);
178+
}
179+
122180
@Test
123181
void determineVersion_detachedHead() {
124182

0 commit comments

Comments
 (0)