[vcs] IJPL-72638 Added tests for tag loading.

GitOrigin-RevId: 1ce7b33c5487d02f4fce9ad1dfb4cc8b56364ccd
This commit is contained in:
Denis Zaichenko
2024-06-09 15:05:21 +02:00
committed by intellij-monorepo-bot
parent 5e9471c5b1
commit febb2fd10f
7 changed files with 1821 additions and 16 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -4,3 +4,7 @@ d85a972e14b616273c5a6f9dd858a02f9f5b12ad refs/remotes/smallteam/brother/master
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/coolfeature
6bce43bed0bc3eb069086ad16d6d200be51f93f2 refs/remotes/smallteam/feature
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/master
#comment
c5cf9cb49c25899442bd98b77c2e60b41cd8b85d refs/tags/tag1
204fce32d1105f382a7a477b207c97948b215a76 refs/tags/tag3
^eb355ef03aaa139b76c3a938c27458f6ee5ca002

View File

@@ -0,0 +1,3 @@
471f3d78906e17ff2db8839381f42a3041b988b1 refs/tags/tag1
f06e234d52d165b74ac615b5ef28bd871404c528 refs/tags/tag2
eb355ef03aaa139b76c3a938c27458f6ee5ca002 refs/tags/tag3

View File

@@ -4,3 +4,7 @@ d85a972e14b616273c5a6f9dd858a02f9f5b12ad refs/remotes/smallteam/brother/master
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/coolfeature
6bce43bed0bc3eb069086ad16d6d200be51f93f2 refs/remotes/smallteam/feature
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/master
#comment
c5cf9cb49c25899442bd98b77c2e60b41cd8b85d refs/tags/tag1
204fce32d1105f382a7a477b207c97948b215a76 refs/tags/tag3
^eb355ef03aaa139b76c3a938c27458f6ee5ca002

View File

@@ -0,0 +1,2 @@
c5cf9cb49c25899442bd98b77c2e60b41cd8b85d refs/tags/tag1
eb355ef03aaa139b76c3a938c27458f6ee5ca002 refs/tags/tag3

View File

@@ -12,16 +12,21 @@ import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.io.ZipUtil;
import com.intellij.vcs.log.Hash;
import com.intellij.vcs.log.impl.HashImpl;
import git4idea.GitBranch;
import git4idea.GitLocalBranch;
import git4idea.GitReference;
import git4idea.GitTag;
import git4idea.config.GitVcsSettings;
import git4idea.test.GitPlatformTest;
import junit.framework.TestCase;
import kotlinx.coroutines.GlobalScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mockito;
import java.io.File;
import java.io.IOException;
@@ -37,13 +42,15 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
private File myTempDir;
private GitRepositoryReader myRepositoryReader;
private File myGitDir;
private @Nullable VirtualFile myRootDir;
private @NotNull GitRepositoryFiles myRepoFiles;
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
File pluginRoot = new File(PluginPathManager.getPluginHomePath("git4idea"));
File dataDir = new File(new File(pluginRoot, "testData"), "repo");
File[] testCases = dataDir.listFiles(FileFilters.DIRECTORIES);
return ContainerUtil.map(testCases, file -> new Object[] { file.getName(), file });
return ContainerUtil.map(testCases, file -> new Object[]{file.getName(), file});
}
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors"})
@@ -76,9 +83,10 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
}
FileUtil.rename(dotGit, myGitDir);
TestCase.assertTrue(myGitDir.exists());
VirtualFile rootDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(myTempDir);
myRootDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(myTempDir);
VirtualFile gitDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(myGitDir);
myRepositoryReader = new GitRepositoryReader(GitRepositoryFiles.createInstance(rootDir, gitDir));
myRepoFiles = GitRepositoryFiles.createInstance(myRootDir, gitDir);
myRepositoryReader = new GitRepositoryReader(myRepoFiles);
}
@NotNull
@@ -105,8 +113,31 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
assertEquals("HEAD revision is incorrect", readHead(myTempDir), state.getCurrentRevision());
assertEqualBranches(readCurrentBranch(myTempDir), state.getCurrentBranch(), state.getLocalBranches().get(state.getCurrentBranch()));
assertBranches(state.getLocalBranches(), readBranches(myTempDir, true));
assertBranches(state.getRemoteBranches(), readBranches(myTempDir, false));
assertReferences(state.getLocalBranches(), readRefs(myTempDir, RefType.LOCAL_BRANCH));
assertReferences(state.getRemoteBranches(), readRefs(myTempDir, RefType.REMOTE_BRANCH));
}
@Test
public void testTags() throws Exception {
try {
GitVcsSettings.getInstance(myProject).getState().setShowTags(true);
GitRepository repo = Mockito.mock(GitRepository.class);
Mockito.when(repo.getProject()).thenReturn(myProject);
Mockito.when(repo.getRepositoryFiles()).thenReturn(myRepoFiles);
Mockito.when(repo.getCoroutineScope()).thenReturn(GlobalScope.INSTANCE);
GitTagHolder holder = new GitTagHolder(repo);
holder.updateEnabled();
do {
//noinspection BusyWait
Thread.sleep(100);
}
while (holder.isLoading());
Map<GitTag, Hash> tags = holder.getTags();
assertReferences(tags, readRefs(myTempDir, RefType.TAG));
}
finally {
GitVcsSettings.getInstance(myProject).getState().setShowTags(false);
}
}
private static void assertEqualBranches(@NotNull Branch expected, @NotNull GitLocalBranch actual, @NotNull Hash hash) {
@@ -114,18 +145,20 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
assertEquals("Incorrect hash of branch " + actual.getName(), expected.hash, hash);
}
private static void assertBranches(Map<? extends GitBranch, Hash> actualBranches, Collection<Branch> expectedBranches) {
VcsTestUtil.assertEqualCollections(actualBranches.entrySet(), expectedBranches, new VcsTestUtil.EqualityChecker<Map.Entry<? extends GitBranch, Hash>, Branch>() {
@Override
public boolean areEqual(Map.Entry<? extends GitBranch, Hash> actual, Branch expected) {
return branchesAreEqual(actual.getKey(), actual.getValue(), expected);
}
});
private static void assertReferences(Map<? extends GitReference, Hash> actualBranches, Collection<Branch> expectedBranches) {
VcsTestUtil.assertEqualCollections(actualBranches.entrySet(), expectedBranches,
new VcsTestUtil.EqualityChecker<Map.Entry<? extends GitReference, Hash>, Branch>() {
@Override
public boolean areEqual(Map.Entry<? extends GitReference, Hash> actual, Branch expected) {
return referencesAreEqual(actual.getKey(), actual.getValue(), expected);
}
});
}
@NotNull
private static Collection<Branch> readBranches(@NotNull File resultDir, boolean local) throws IOException {
String content = FileUtil.loadFile(new File(resultDir, local ? "local-branches.txt" : "remote-branches.txt"));
private static Collection<Branch> readRefs(@NotNull File resultDir, RefType refType) throws IOException {
File file = new File(resultDir, refType.myPath);
String content = FileUtil.loadFile(file);
Collection<Branch> branches = new ArrayList<>();
for (String line : StringUtil.splitByLines(content)) {
branches.add(readBranchFromLine(line));
@@ -133,7 +166,7 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
return branches;
}
private static boolean branchesAreEqual(GitBranch actualBranch, Hash actualHash, Branch expected) {
private static boolean referencesAreEqual(GitReference actualBranch, Hash actualHash, Branch expected) {
return actualBranch.getFullName().equals(expected.name) && actualHash.equals(expected.hash);
}
@@ -152,4 +185,15 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
}
}
private enum RefType {
LOCAL_BRANCH("local-branches.txt"),
REMOTE_BRANCH("remote-branches.txt"),
TAG("tags.txt");
final String myPath;
RefType(String path) {
myPath = path;
}
}
}