From 13eb661ee4f329cda9d0f0595632bc69a9cd1ec4 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Tue, 1 Apr 2014 19:02:31 +0400 Subject: [PATCH] [git] GitRepositoryReaderTest rewrite Prepare data in beforehand, don't parse it twice. --- .../com/intellij/openapi/vcs/VcsTestUtil.java | 4 +- plugins/git4idea/testData/repo/dot_git/index | Bin 2897 -> 0 bytes .../testData/repo/example1/current-branch.txt | 1 + .../testData/repo/{ => example1}/dot_git/HEAD | 0 .../repo/{ => example1}/dot_git/config | 0 .../repo/{ => example1}/dot_git/packed-refs | 0 .../{ => example1}/dot_git/refs/heads/feature | 0 .../dot_git/refs/heads/folder/subref | 0 .../{ => example1}/dot_git/refs/heads/hot_fix | 0 .../{ => example1}/dot_git/refs/heads/master | 0 .../dot_git/refs/remotes/origin/feature | 0 .../dot_git/refs/remotes/origin/master | 0 .../repo/{ => example1}/dot_git/refs/stash | 0 .../{ => example1}/dot_git/refs/tags/tag1 | 0 .../{ => example1}/dot_git/refs/tags/tag2 | 0 .../git4idea/testData/repo/example1/head.txt | 1 + .../testData/repo/example1/local-branches.txt | 5 + .../repo/example1/remote-branches.txt | 8 + .../repo/GitRepositoryReaderTest.java | 209 ++++++++---------- 19 files changed, 107 insertions(+), 121 deletions(-) delete mode 100644 plugins/git4idea/testData/repo/dot_git/index create mode 100644 plugins/git4idea/testData/repo/example1/current-branch.txt rename plugins/git4idea/testData/repo/{ => example1}/dot_git/HEAD (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/config (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/packed-refs (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/heads/feature (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/heads/folder/subref (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/heads/hot_fix (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/heads/master (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/remotes/origin/feature (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/remotes/origin/master (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/stash (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/tags/tag1 (100%) rename plugins/git4idea/testData/repo/{ => example1}/dot_git/refs/tags/tag2 (100%) create mode 100644 plugins/git4idea/testData/repo/example1/head.txt create mode 100644 plugins/git4idea/testData/repo/example1/local-branches.txt create mode 100644 plugins/git4idea/testData/repo/example1/remote-branches.txt 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 42745c4f08413aaaa376c9c98211561c34cf9e3f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2897 zcma*p2~ZPP7y#f02vsEE#$u}+qM||)t{UZ1!YMQeq9~;$afu;7j^-i^QWd4*2qG#2 znXyvAi+Uhn1rH861VIj|1sz2TRxZ&RQ9wlLZdR1tk`6nwd713y`~QF6wVr;#Gys4)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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; } }