Skip to content

Commit b41750c

Browse files
author
Vladimir Kotal
committed
more test refactoring
1 parent 04e1cc9 commit b41750c

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/history/GitRepositoryTest.java

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -639,16 +639,42 @@ public void testRenamedSingleHistory() throws Exception {
639639
}
640640

641641
@Test
642-
public void testBuildTagList() throws Exception {
642+
public void testBuildTagListEmpty() throws Exception {
643643
File root = new File(repository.getSourceRoot(), "git");
644-
GitRepository gitrepo
645-
= (GitRepository) RepositoryFactory.getRepository(root);
646-
gitrepo.buildTagList(new File(gitrepo.getDirectoryName()), CommandTimeoutType.INDEXER);
647-
assertEquals(0, gitrepo.getTagList().size());
644+
// Clone under source root to avoid problems with prohibited symlinks.
645+
File localPath = new File(repository.getSourceRoot(), "testBuildTagListEmpty");
646+
String cloneUrl = root.toURI().toString();
647+
try (Git gitClone = Git.cloneRepository()
648+
.setURI(cloneUrl)
649+
.setDirectory(localPath)
650+
.call()) {
651+
652+
File cloneRoot = gitClone.getRepository().getWorkTree();
653+
654+
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(cloneRoot);
655+
gitrepo.buildTagList(new File(gitrepo.getDirectoryName()), CommandTimeoutType.INDEXER);
656+
assertEquals(0, gitrepo.getTagList().size());
657+
658+
FileUtilities.removeDirs(cloneRoot);
659+
}
660+
}
661+
662+
@Test
663+
public void testBuildTagListMultipleTags() throws Exception {
664+
File root = new File(repository.getSourceRoot(), "git");
665+
// Clone under source root to avoid problems with prohibited symlinks.
666+
File localPath = new File(repository.getSourceRoot(), "testBuildTagListMultipleTags");
667+
String cloneUrl = root.toURI().toString();
668+
try (Git gitClone = Git.cloneRepository()
669+
.setURI(cloneUrl)
670+
.setDirectory(localPath)
671+
.call()) {
672+
673+
File cloneRoot = gitClone.getRepository().getWorkTree();
674+
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(cloneRoot);
648675

649-
try (Git git = Git.open(root)) {
650676
// Tag the HEAD first.
651-
Ref ref = git.tag().setName("one").call();
677+
Ref ref = gitClone.tag().setName("one").call();
652678
assertNotNull(ref);
653679
gitrepo.buildTagList(new File(gitrepo.getDirectoryName()), CommandTimeoutType.INDEXER);
654680
Set<TagEntry> tags = gitrepo.getTagList();
@@ -658,7 +684,7 @@ public void testBuildTagList() throws Exception {
658684
assertEquals(tagEntry, tags.toArray()[0]);
659685

660686
// Tag again so that single changeset has multiple tags.
661-
ref = git.tag().setName("two").call();
687+
ref = gitClone.tag().setName("two").call();
662688
assertNotNull(ref);
663689
gitrepo.buildTagList(new File(gitrepo.getDirectoryName()), CommandTimeoutType.INDEXER);
664690
tags = gitrepo.getTagList();
@@ -669,23 +695,44 @@ public void testBuildTagList() throws Exception {
669695
expectedTags.add(tagEntry);
670696
assertEquals(expectedTags, tags);
671697

698+
FileUtilities.removeDirs(cloneRoot);
699+
}
700+
}
701+
702+
@Test
703+
public void testBuildTagListNotHead() throws Exception {
704+
File root = new File(repository.getSourceRoot(), "git");
705+
// Clone under source root to avoid problems with prohibited symlinks.
706+
File localPath = new File(repository.getSourceRoot(), "testBuildTagListNotHead");
707+
String cloneUrl = root.toURI().toString();
708+
try (Git gitClone = Git.cloneRepository()
709+
.setURI(cloneUrl)
710+
.setDirectory(localPath)
711+
.call()) {
712+
713+
File cloneRoot = gitClone.getRepository().getWorkTree();
714+
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(cloneRoot);
715+
672716
// Tag specific changeset (not HEAD) and recheck.
673-
org.eclipse.jgit.lib.Repository repo = git.getRepository();
717+
org.eclipse.jgit.lib.Repository repo = gitClone.getRepository();
674718
RevCommit commit;
675719
ObjectId objectId = repo.resolve("b6413947a5");
676720
try (RevWalk walk = new RevWalk(repo)) {
677721
commit = walk.parseCommit(objectId);
678722
}
679723
assertNotNull(commit);
680-
ref = git.tag().setName("three").setObjectId(commit).call();
724+
Ref ref = gitClone.tag().setName("three").setObjectId(commit).call();
681725
assertNotNull(ref);
682726
gitrepo.buildTagList(new File(gitrepo.getDirectoryName()), CommandTimeoutType.INDEXER);
683-
tags = gitrepo.getTagList();
684-
assertEquals(2, tags.size());
685-
date = new Date((long) (1485263264) * 1000);
686-
tagEntry = new GitTagEntry("b6413947a59f481ddc0a05e0d181731233557f6e", date, "three");
727+
Set<TagEntry> tags = gitrepo.getTagList();
728+
assertEquals(1, tags.size());
729+
Date date = new Date((long) (1485263264) * 1000);
730+
Set<TagEntry> expectedTags = new TreeSet<>();
731+
TagEntry tagEntry = new GitTagEntry("b6413947a59f481ddc0a05e0d181731233557f6e", date, "three");
687732
expectedTags.add(tagEntry);
688733
assertEquals(expectedTags, tags);
734+
735+
FileUtilities.removeDirs(cloneRoot);
689736
}
690737
}
691738
}

0 commit comments

Comments
 (0)