[git] GitRepositoryReaderTest rewrite

Prepare data in beforehand, don't parse it twice.
This commit is contained in:
Kirill Likhodedov
2014-04-11 16:50:15 +04:00
parent d89e625f33
commit 13eb661ee4
19 changed files with 107 additions and 121 deletions
@@ -180,7 +180,7 @@ public class VcsTestUtil {
* Testng compares by iterating over 2 collections, but it won't work for sets which may have different order.
*/
public static <T, E> void assertEqualCollections(@NotNull Collection<? extends T> actual,
@NotNull Collection<E> expected,
@NotNull Collection<? extends E> expected,
@NotNull EqualityChecker<T, E> equalityChecker) {
if (actual.size() != expected.size()) {
fail("Collections don't have the same size. " + stringifyActualExpected(actual, expected));
@@ -209,7 +209,7 @@ public class VcsTestUtil {
return false;
}
private static <T, E> boolean contains2(@NotNull Collection<E> collection,
private static <T, E> boolean contains2(@NotNull Collection<? extends E> collection,
@NotNull T object,
@NotNull EqualityChecker<T, E> equalityChecker) {
for (E e : collection) {
Binary file not shown.
@@ -0,0 +1 @@
0e1d130689bc52f140c5c374aa9cc2b8916c0ad7 master
@@ -0,0 +1 @@
0e1d130689bc52f140c5c374aa9cc2b8916c0ad7
@@ -0,0 +1,5 @@
63fcb501752b1b98d5cda1f34915f846b3b4bbe2 refs/heads/feature
32f76141b6a5dbef8d7e9096702ff7bfad218190 refs/heads/folder/subref
db5664aa94df9e7a41343fa92ddb10f340e4a991 refs/heads/hot_fix
0e1d130689bc52f140c5c374aa9cc2b8916c0ad7 refs/heads/master
@@ -0,0 +1,8 @@
d85a972e14b616273c5a6f9dd858a02f9f5b12ad refs/remotes/brother/master
d85a972e14b616273c5a6f9dd858a02f9f5b12ad refs/remotes/smallteam/brother/master
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/coolfeature
6bce43bed0bc3eb069086ad16d6d200be51f93f2 refs/remotes/smallteam/feature
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/master
4a6e262820fe3d5ac1c7ed52ea0dd42287780447 refs/remotes/origin/feature
06d720e2dfe18592a3b7ebc938c57b0c6940176b refs/remotes/origin/master
@@ -2,9 +2,12 @@ package git4idea.repo;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsTestUtil;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.impl.HashImpl;
import git4idea.GitBranch;
import git4idea.GitLocalBranch;
import git4idea.branch.GitBranchesCollection;
import git4idea.test.GitPlatformTest;
import junit.framework.TestCase;
@@ -12,28 +15,34 @@ import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class GitRepositoryReaderTest extends GitPlatformTest {
private File myDataDir;
private File myTempDir;
private GitRepositoryReader myRepositoryReader;
private File myGitDir;
@Override
protected void setUp() throws Exception {
super.setUp();
File myTempDir = new File(myProjectRoot.getPath(), "test");
myTempDir = new File(myProjectRoot.getPath(), "test");
myTempDir.mkdir();
File pluginRoot = new File(PluginPathManager.getPluginHomePath("git4idea"));
File dataDir = new File(new File(pluginRoot, "testData"), "repo");
myDataDir = new File(new File(pluginRoot, "testData"), "repo");
}
FileUtil.copyDir(dataDir, myTempDir);
private void prepareTest(File testDir) throws IOException {
FileUtil.copyDir(testDir, myTempDir);
myGitDir = new File(myTempDir, ".git");
FileUtil.rename(new File(myTempDir, "dot_git"), myGitDir);
TestCase.assertTrue(myGitDir.exists());
myRepositoryReader = new GitRepositoryReader(myGitDir);
myLocalBranches = readBranches(true);
myRemoteBranches = readBranches(false);
}
@Override
@@ -41,135 +50,97 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
super.tearDown();
}
public void testHEAD() {
TestCase.assertEquals("0e1d130689bc52f140c5c374aa9cc2b8916c0ad7", myRepositoryReader.readCurrentRevision());
private void doTest(@NotNull ResultConsumer test) throws Exception {
for (File dir : myDataDir.listFiles()) {
if (!dir.isDirectory()) {
continue;
}
prepareTest(dir);
test.consume(dir);
}
}
public void testCurrentBranch() {
assertBranch(myRepositoryReader.readCurrentBranch(), new GitTestBranch("master", "0e1d130689bc52f140c5c374aa9cc2b8916c0ad7"));
@NotNull
private static String readHead(@NotNull File dir) throws IOException {
return FileUtil.loadFile(new File(dir, "head.txt")).trim();
}
public void testBranches() {
Collection<GitRemote> remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes();
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes);
GitBranch currentBranch = myRepositoryReader.readCurrentBranch();
Collection<? extends GitBranch> localBranches = branchesCollection.getLocalBranches();
Collection<? extends GitBranch> remoteBranches = branchesCollection.getRemoteBranches();
assertBranch(currentBranch, new GitTestBranch("master", "0e1d130689bc52f140c5c374aa9cc2b8916c0ad7"));
assertBranches(localBranches, myLocalBranches);
assertBranches(remoteBranches, myRemoteBranches);
@NotNull
private static GitLocalBranch readCurrentBranch(@NotNull File resultDir) throws IOException {
String branch = FileUtil.loadFile(new File(resultDir, "current-branch.txt")).trim();
return readBranchFromLine(branch);
}
private static void assertBranches(Collection<? extends GitBranch> actualBranches, Collection<GitTestBranch> expectedBranches) {
VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker<GitBranch, GitTestBranch>() {
@NotNull
private static GitLocalBranch readBranchFromLine(@NotNull String branch) {
List<String> branchAndHash = StringUtil.split(branch, " ");
return new GitLocalBranch(branchAndHash.get(1), HashImpl.build(branchAndHash.get(0)));
}
public void testHEAD() throws Exception {
doTest(new ResultConsumer() {
@Override
public boolean areEqual(@NotNull GitBranch actual, @NotNull GitTestBranch expected) {
public void consume(@NotNull File resultDir) throws Exception {
assertEquals("HEAD is incorrect", readHead(resultDir), myRepositoryReader.readCurrentRevision());
}
});
}
public void testCurrentBranch() throws Exception {
doTest(new ResultConsumer() {
@Override
public void consume(@NotNull File resultDir) throws Exception {
assertEqualBranches(readCurrentBranch(resultDir), myRepositoryReader.readCurrentBranch());
}
});
}
private static void assertEqualBranches(@NotNull GitLocalBranch expected, @NotNull GitLocalBranch actual) {
assertEquals(expected.getName(), actual.getName());
assertEquals(expected.getHash(), actual.getHash());
}
public void testBranches() throws Exception {
doTest(new ResultConsumer() {
@Override
public void consume(@NotNull File resultDir) throws Exception {
Collection<GitRemote> remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes();
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes);
GitLocalBranch currentBranch = myRepositoryReader.readCurrentBranch();
Collection<? extends GitBranch> localBranches = branchesCollection.getLocalBranches();
Collection<? extends GitBranch> remoteBranches = branchesCollection.getRemoteBranches();
assertEqualBranches(readCurrentBranch(resultDir), currentBranch);
assertBranches(localBranches, readBranches(resultDir, true));
assertBranches(remoteBranches, readBranches(resultDir, false));
}
});
}
private static void assertBranches(Collection<? extends GitBranch> actualBranches, Collection<? extends GitBranch> expectedBranches) {
VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker<GitBranch, GitBranch>() {
@Override
public boolean areEqual(@NotNull GitBranch actual, @NotNull GitBranch expected) {
return branchesAreEqual(actual, expected);
}
});
}
private Collection<GitTestBranch> readBranches(boolean local) throws IOException {
final Collection<GitTestBranch> branches = new ArrayList<GitTestBranch>();
final File refsHeads = new File(new File(myGitDir, "refs"), local ? "heads" : "remotes");
FileUtil.processFilesRecursively(refsHeads, new Processor<File>() {
@Override
public boolean process(File file) {
if (FileUtil.filesEqual(file, refsHeads)) {// don't process the root
return true;
}
if (file.isDirectory()) {// don't process dirs
return true;
}
String relativePath = FileUtil.getRelativePath(refsHeads, file);
if (relativePath == null) {
return true;
}
String name = FileUtil.toSystemIndependentName(relativePath);
GitTestBranch branch = null;
try {
branch = new GitTestBranch(name, FileUtil.loadFile(file));
}
catch (IOException e) {
TestCase.fail(e.toString());
e.printStackTrace();
}
if (!branches.contains(branch)) {
branches.add(branch);
}
return true;
}
});
// read from packed-refs, these have less priority, so the won't overwrite hashes from branch files
String packedRefs = FileUtil.loadFile(new File(myGitDir, "packed-refs"));
for (String ref : packedRefs.split("\n")) {
String[] refAndName = ref.split(" ");
String name = refAndName[1];
String prefix = local ? "refs/heads/" : "refs/remotes/";
if (name.startsWith(prefix)) {
GitTestBranch branch = new GitTestBranch(name.substring(prefix.length()), refAndName[0]);
if (!branches.contains(branch)) {
branches.add(branch);
}
}
@NotNull
private static Collection<GitBranch> readBranches(@NotNull File resultDir, boolean local) throws IOException {
String content = FileUtil.loadFile(new File(resultDir, local ? "local-branches.txt" : "remote-branches.txt"));
Collection<GitBranch> branches = ContainerUtil.newArrayList();
for (String line : StringUtil.splitByLines(content)) {
branches.add(readBranchFromLine(line));
}
return branches;
}
private static void assertBranch(GitBranch actual, GitTestBranch expected) {
TestCase.assertTrue(String.format("Branches are not equal. Actual: %s:%sExpected: %s", actual.getName(), actual.getHash(), expected),
branchesAreEqual(actual, expected));
}
private static boolean branchesAreEqual(GitBranch actual, GitTestBranch expected) {
private static boolean branchesAreEqual(GitBranch actual, GitBranch expected) {
return actual.getName().equals(expected.getName()) && actual.getHash().equals(expected.getHash());
}
private GitRepositoryReader myRepositoryReader;
private File myGitDir;
private Collection<GitTestBranch> myLocalBranches;
private Collection<GitTestBranch> myRemoteBranches;
private static class GitTestBranch {
private GitTestBranch(String name, String hash) {
myName = name.trim();
myHash = hash.trim();
}
public String getName() {
return myName;
}
public String getHash() {
return myHash;
}
@Override
public String toString() {
return myName + ":" + myHash;
}
@Override
public boolean equals(Object o) {
GitTestBranch branch = (GitTestBranch)o;
if (!myName.equals(branch.myName)) return false;
return true;
}
@Override
public int hashCode() {
return myName.hashCode();
}
private final String myName;
private final String myHash;
private abstract static class ResultConsumer {
public abstract void consume(@NotNull File resultDir) throws Exception;
}
}