diff --git a/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/VcsTestUtil.java b/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/VcsTestUtil.java index a0e4f1e85521..51b6fdd1226e 100644 --- a/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/VcsTestUtil.java +++ b/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/VcsTestUtil.java @@ -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 void assertEqualCollections(@NotNull Collection actual, - @NotNull Collection expected, + @NotNull Collection expected, @NotNull EqualityChecker 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 boolean contains2(@NotNull Collection collection, + private static boolean contains2(@NotNull Collection collection, @NotNull T object, @NotNull EqualityChecker equalityChecker) { for (E e : collection) { diff --git a/plugins/git4idea/testData/repo/dot_git/index b/plugins/git4idea/testData/repo/dot_git/index deleted file mode 100644 index 42745c4f0841..000000000000 Binary files a/plugins/git4idea/testData/repo/dot_git/index and /dev/null differ diff --git a/plugins/git4idea/testData/repo/example1/current-branch.txt b/plugins/git4idea/testData/repo/example1/current-branch.txt new file mode 100644 index 000000000000..816e00492539 --- /dev/null +++ b/plugins/git4idea/testData/repo/example1/current-branch.txt @@ -0,0 +1 @@ +0e1d130689bc52f140c5c374aa9cc2b8916c0ad7 master \ No newline at end of file diff --git a/plugins/git4idea/testData/repo/dot_git/HEAD b/plugins/git4idea/testData/repo/example1/dot_git/HEAD similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/HEAD rename to plugins/git4idea/testData/repo/example1/dot_git/HEAD diff --git a/plugins/git4idea/testData/repo/dot_git/config b/plugins/git4idea/testData/repo/example1/dot_git/config similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/config rename to plugins/git4idea/testData/repo/example1/dot_git/config diff --git a/plugins/git4idea/testData/repo/dot_git/packed-refs b/plugins/git4idea/testData/repo/example1/dot_git/packed-refs similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/packed-refs rename to plugins/git4idea/testData/repo/example1/dot_git/packed-refs diff --git a/plugins/git4idea/testData/repo/dot_git/refs/heads/feature b/plugins/git4idea/testData/repo/example1/dot_git/refs/heads/feature similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/heads/feature rename to plugins/git4idea/testData/repo/example1/dot_git/refs/heads/feature diff --git a/plugins/git4idea/testData/repo/dot_git/refs/heads/folder/subref b/plugins/git4idea/testData/repo/example1/dot_git/refs/heads/folder/subref similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/heads/folder/subref rename to plugins/git4idea/testData/repo/example1/dot_git/refs/heads/folder/subref diff --git a/plugins/git4idea/testData/repo/dot_git/refs/heads/hot_fix b/plugins/git4idea/testData/repo/example1/dot_git/refs/heads/hot_fix similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/heads/hot_fix rename to plugins/git4idea/testData/repo/example1/dot_git/refs/heads/hot_fix diff --git a/plugins/git4idea/testData/repo/dot_git/refs/heads/master b/plugins/git4idea/testData/repo/example1/dot_git/refs/heads/master similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/heads/master rename to plugins/git4idea/testData/repo/example1/dot_git/refs/heads/master diff --git a/plugins/git4idea/testData/repo/dot_git/refs/remotes/origin/feature b/plugins/git4idea/testData/repo/example1/dot_git/refs/remotes/origin/feature similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/remotes/origin/feature rename to plugins/git4idea/testData/repo/example1/dot_git/refs/remotes/origin/feature diff --git a/plugins/git4idea/testData/repo/dot_git/refs/remotes/origin/master b/plugins/git4idea/testData/repo/example1/dot_git/refs/remotes/origin/master similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/remotes/origin/master rename to plugins/git4idea/testData/repo/example1/dot_git/refs/remotes/origin/master diff --git a/plugins/git4idea/testData/repo/dot_git/refs/stash b/plugins/git4idea/testData/repo/example1/dot_git/refs/stash similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/stash rename to plugins/git4idea/testData/repo/example1/dot_git/refs/stash diff --git a/plugins/git4idea/testData/repo/dot_git/refs/tags/tag1 b/plugins/git4idea/testData/repo/example1/dot_git/refs/tags/tag1 similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/tags/tag1 rename to plugins/git4idea/testData/repo/example1/dot_git/refs/tags/tag1 diff --git a/plugins/git4idea/testData/repo/dot_git/refs/tags/tag2 b/plugins/git4idea/testData/repo/example1/dot_git/refs/tags/tag2 similarity index 100% rename from plugins/git4idea/testData/repo/dot_git/refs/tags/tag2 rename to plugins/git4idea/testData/repo/example1/dot_git/refs/tags/tag2 diff --git a/plugins/git4idea/testData/repo/example1/head.txt b/plugins/git4idea/testData/repo/example1/head.txt new file mode 100644 index 000000000000..f75e53246e61 --- /dev/null +++ b/plugins/git4idea/testData/repo/example1/head.txt @@ -0,0 +1 @@ +0e1d130689bc52f140c5c374aa9cc2b8916c0ad7 diff --git a/plugins/git4idea/testData/repo/example1/local-branches.txt b/plugins/git4idea/testData/repo/example1/local-branches.txt new file mode 100644 index 000000000000..d5ddb285d17b --- /dev/null +++ b/plugins/git4idea/testData/repo/example1/local-branches.txt @@ -0,0 +1,5 @@ +63fcb501752b1b98d5cda1f34915f846b3b4bbe2 refs/heads/feature +32f76141b6a5dbef8d7e9096702ff7bfad218190 refs/heads/folder/subref +db5664aa94df9e7a41343fa92ddb10f340e4a991 refs/heads/hot_fix +0e1d130689bc52f140c5c374aa9cc2b8916c0ad7 refs/heads/master + diff --git a/plugins/git4idea/testData/repo/example1/remote-branches.txt b/plugins/git4idea/testData/repo/example1/remote-branches.txt new file mode 100644 index 000000000000..b2eaeb012408 --- /dev/null +++ b/plugins/git4idea/testData/repo/example1/remote-branches.txt @@ -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 + diff --git a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java index c7d8e32f9cff..89229c5c7247 100644 --- a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java +++ b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java @@ -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 remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes(); - GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes); - GitBranch currentBranch = myRepositoryReader.readCurrentBranch(); - Collection localBranches = branchesCollection.getLocalBranches(); - Collection 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 actualBranches, Collection expectedBranches) { - VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker() { + @NotNull + private static GitLocalBranch readBranchFromLine(@NotNull String branch) { + List 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 remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes(); + GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes); + GitLocalBranch currentBranch = myRepositoryReader.readCurrentBranch(); + Collection localBranches = branchesCollection.getLocalBranches(); + Collection remoteBranches = branchesCollection.getRemoteBranches(); + + assertEqualBranches(readCurrentBranch(resultDir), currentBranch); + assertBranches(localBranches, readBranches(resultDir, true)); + assertBranches(remoteBranches, readBranches(resultDir, false)); + } + }); + } + + private static void assertBranches(Collection actualBranches, Collection expectedBranches) { + VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker() { + @Override + public boolean areEqual(@NotNull GitBranch actual, @NotNull GitBranch expected) { return branchesAreEqual(actual, expected); } }); } - private Collection readBranches(boolean local) throws IOException { - final Collection branches = new ArrayList(); - final File refsHeads = new File(new File(myGitDir, "refs"), local ? "heads" : "remotes"); - FileUtil.processFilesRecursively(refsHeads, new Processor() { - @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 readBranches(@NotNull File resultDir, boolean local) throws IOException { + String content = FileUtil.loadFile(new File(resultDir, local ? "local-branches.txt" : "remote-branches.txt")); + Collection 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 myLocalBranches; - private Collection 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; } }