GitRepositoryReaderTest: test reading branches

Added a sample repository to testData
This commit is contained in:
Kirill Likhodedov
2011-11-24 19:10:35 +03:00
parent 4a32c8f541
commit 8d24a778a8
17 changed files with 252 additions and 2 deletions
+1
View File
@@ -35,6 +35,7 @@
<orderEntry type="library" name="Guava" level="project" />
<orderEntry type="module" module-name="lang-impl" />
<orderEntry type="module" module-name="images" />
<orderEntry type="module" module-name="testFramework-java" scope="TEST" />
<orderEntry type="module-library">
<library name="ini4j-0.5.2-patched">
<CLASSES>
@@ -0,0 +1 @@
ref: refs/heads/master
@@ -0,0 +1,15 @@
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "origin"]
url = /Users/loki/sandbox/git/parent.git/
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "newBranch"]
remote = origin
merge = refs/heads/newBranch
Binary file not shown.
@@ -0,0 +1,6 @@
d85a972e14b616273c5a6f9dd858a02f9f5b12ad refs/remotes/brother/master
8a3d3a8d03e357b9baa39401faa5c9156ea4ca10 refs/remotes/origin/master
d85a972e14b616273c5a6f9dd858a02f9f5b12ad refs/remotes/smallteam/brother/master
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/coolfeature
6bce43bed0bc3eb069086ad16d6d200be51f93f2 refs/remotes/smallteam/feature
a86078364a9ddc9f0b61f40b91ea9f4c8929f54b refs/remotes/smallteam/master
@@ -0,0 +1 @@
63fcb501752b1b98d5cda1f34915f846b3b4bbe2
@@ -0,0 +1 @@
32f76141b6a5dbef8d7e9096702ff7bfad218190
@@ -0,0 +1 @@
db5664aa94df9e7a41343fa92ddb10f340e4a991
@@ -0,0 +1 @@
0e1d130689bc52f140c5c374aa9cc2b8916c0ad7
@@ -0,0 +1 @@
4a6e262820fe3d5ac1c7ed52ea0dd42287780447
@@ -0,0 +1 @@
06d720e2dfe18592a3b7ebc938c57b0c6940176b
@@ -0,0 +1 @@
32f76141b6a5dbef8d7e9096702ff7bfad218190
@@ -0,0 +1 @@
471f3d78906e17ff2db8839381f42a3041b988b1
@@ -0,0 +1 @@
f06e234d52d165b74ac615b5ef28bd871404c528
@@ -0,0 +1,173 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package git4idea.repo;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.testFramework.LightIdeaTestCase;
import com.intellij.util.Processor;
import git4idea.GitBranch;
import git4idea.branch.GitBranchesCollection;
import git4idea.tests.GitTestUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author Kirill Likhodedov
*/
public class GitRepositoryReaderTest extends LightIdeaTestCase {
private GitRepositoryReader myRepositoryReader;
private File myTempDir;
private File myGitDir;
private Collection<GitTestBranch> myLocalBranches;
private Collection<GitTestBranch> myRemoteBranches;
protected void setUp() throws Exception {
super.setUp();
myTempDir = FileUtil.createTempDirectory("GitRepositoryReaderTest", null);
File pluginRoot = new File(PluginPathManager.getPluginHomePath("git4idea"));
File dataDir = new File(new File(pluginRoot, "testData"), "repo");
FileUtil.copyDir(dataDir, myTempDir);
myGitDir = new File(myTempDir, ".git");
FileUtil.rename(new File(myTempDir, "dot_git"), myGitDir);
assertTrue(myGitDir.exists());
myRepositoryReader = new GitRepositoryReader(myGitDir);
myLocalBranches = readBranches(true);
myRemoteBranches = readBranches(false);
}
protected void tearDown() throws Exception {
FileUtil.delete(myTempDir);
super.tearDown();
}
public void testHEAD() {
assertEquals("0e1d130689bc52f140c5c374aa9cc2b8916c0ad7", myRepositoryReader.readCurrentRevision());
}
public void testBranches(){
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches();
GitBranch currentBranch = branchesCollection.getCurrentBranch();
Collection<GitBranch> localBranches = branchesCollection.getLocalBranches();
Collection<GitBranch> remoteBranches = branchesCollection.getRemoteBranches();
assertBranch(currentBranch, new GitTestBranch("master"));
assertBranches(localBranches, myLocalBranches);
assertBranches(remoteBranches, myRemoteBranches);
}
private static void assertBranches(Collection<GitBranch> actual, Collection<GitTestBranch> expected) {
GitTestUtil.assertEqualCollections(actual, expected, new GitTestUtil.EqualityChecker<GitBranch, GitTestBranch>() {
@Override
public boolean areEqual(@NotNull GitBranch actual, @NotNull GitTestBranch expected) {
return actual.getName().equals(expected.getName());
}
});
}
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 (file.equals(refsHeads)) { // don't process the root
return true;
}
if (file.isDirectory()) { // don't process dirs
return true;
}
String name = FileUtil.getRelativePath(refsHeads, file);
GitTestBranch branch = new GitTestBranch(name);
if (!branches.contains(branch)) {
branches.add(branch);
}
return true;
}
});
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()));
if (!branches.contains(branch)) {
branches.add(branch);
}
}
}
return branches;
}
private static void assertBranch(GitBranch actual, GitTestBranch expected) {
assertEquals(actual.getName(), expected.getName());
// TODO test revision
}
private static class GitTestBranch {
private final String myName;
private final String myHash = "";
private GitTestBranch(String name) {
myName = name;
}
String getName() {
return myName;
}
String getHash() {
return myHash;
}
@Override
public String toString() {
return myName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GitTestBranch branch = (GitTestBranch)o;
if (myHash != null ? !myHash.equals(branch.myHash) : branch.myHash != null) return false;
if (myName != null ? !myName.equals(branch.myName) : branch.myName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = myName != null ? myName.hashCode() : 0;
result = 31 * result + (myHash != null ? myHash.hashCode() : 0);
return result;
}
}
}
@@ -15,6 +15,7 @@
*/
package git4idea.repo;
import com.intellij.openapi.vfs.VfsUtil;
import git4idea.GitBranch;
import git4idea.tests.GitTest;
import org.jetbrains.annotations.NotNull;
@@ -22,6 +23,7 @@ import org.jetbrains.annotations.Nullable;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -46,7 +48,7 @@ public class GitRepositoryTest extends GitTest {
public void setUp() throws Exception {
super.setUp();
myRepository = GitRepository.getFullInstance(myRepo.getVFRootDir(), myProject, myProject);
myReader = new GitRepositoryReader(myRepository);
myReader = new GitRepositoryReader(new File(VfsUtil.virtualToIoFile(myRepository.getRoot()), ".git"));
}
@Test
@@ -133,7 +133,50 @@ public class GitTestUtil {
}
}
private static <T> String stringifyActualExpected(@NotNull Collection<T> actual, @NotNull Collection<T> expected) {
/**
* 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<T> actual, @NotNull Collection<E> expected, @NotNull EqualityChecker<T, E> equalityChecker) {
if (actual.size() != expected.size()) {
fail("Collections don't have the same size. " + stringifyActualExpected(actual, expected));
}
for (T act : actual) {
if (!contains2(expected, act, equalityChecker)) {
fail("Unexpected object " + act + stringifyActualExpected(actual, expected));
}
}
// backwards is needed for collections which may contain duplicates, e.g. Lists.
for (E exp : expected) {
if (!contains(actual, exp, equalityChecker)) {
fail("Object " + exp + " not found in actual collection." + stringifyActualExpected(actual, expected));
}
}
}
private static <T, E> boolean contains(@NotNull Collection<T> collection, @NotNull E object, @NotNull EqualityChecker<T, E> equalityChecker) {
for (T t : collection) {
if (equalityChecker.areEqual(t, object)) {
return true;
}
}
return false;
}
private static <T, E> boolean contains2(Collection<E> collection, T object, EqualityChecker<T, E> equalityChecker) {
for (E e : collection) {
if (equalityChecker.areEqual(object, e)) {
return true;
}
}
return false;
}
public interface EqualityChecker<T, E> {
boolean areEqual(T actual, E expected);
}
@NotNull
public static String stringifyActualExpected(@NotNull Object actual, @NotNull Object expected) {
return "\nExpected:\n" + expected + "\nActual:\n" + actual;
}
}