From 07a1d17858cacb28f4aab9315845341de69c2c4f Mon Sep 17 00:00:00 2001 From: Christian Halstrick Date: Mon, 16 Aug 2010 18:44:26 +0200 Subject: [PATCH] Add blob-data to utility method indexState() indexState() encodes the complete state of the index into one readable String. This helps to write tests against the index. indexState() is enhanced to optionally also contain the content of the files in the index. Change-Id: Ie988f93768d864f4cbd55809a786bd5759fc24a5 Signed-off-by: Christian Halstrick --- .../org/eclipse/jgit/api/AddCommandTest.java | 86 +++++++++---------- .../org/eclipse/jgit/lib/RacyGitTests.java | 12 +-- .../eclipse/jgit/lib/RepositoryTestCase.java | 36 ++++---- 3 files changed, 68 insertions(+), 66 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java index a7d098468..2dbf92998 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java @@ -91,8 +91,8 @@ public class AddCommandTest extends RepositoryTestCase { git.add().addFilepattern("a.txt").call(); assertEquals( - "[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, content:content]", + indexState(CONTENT)); } public void testAddExistingSingleFileInSubDir() throws IOException, NoFilepatternException { @@ -108,8 +108,8 @@ public class AddCommandTest extends RepositoryTestCase { git.add().addFilepattern("sub/a.txt").call(); assertEquals( - "[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:content]", + indexState(CONTENT)); } public void testAddExistingSingleFileTwice() throws IOException, NoFilepatternException { @@ -131,8 +131,8 @@ public class AddCommandTest extends RepositoryTestCase { dc = git.add().addFilepattern("a.txt").call(); assertEquals( - "[a.txt, mode:100644, sha1:4f41554f6e0045ef53848fc0c3f33b6a9abc24a9]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, content:other content]", + indexState(CONTENT)); } public void testAddExistingSingleFileTwiceWithCommit() throws Exception { @@ -156,8 +156,8 @@ public class AddCommandTest extends RepositoryTestCase { dc = git.add().addFilepattern("a.txt").call(); assertEquals( - "[a.txt, mode:100644, sha1:4f41554f6e0045ef53848fc0c3f33b6a9abc24a9]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, content:other content]", + indexState(CONTENT)); } public void testAddRemovedFile() throws Exception { @@ -177,8 +177,8 @@ public class AddCommandTest extends RepositoryTestCase { dc = git.add().addFilepattern("a.txt").call(); assertEquals( - "[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, content:content]", + indexState(CONTENT)); } public void testAddRemovedCommittedFile() throws Exception { @@ -200,8 +200,8 @@ public class AddCommandTest extends RepositoryTestCase { dc = git.add().addFilepattern("a.txt").call(); assertEquals( - "[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, content:content]", + indexState(CONTENT)); } public void testAddWithConflicts() throws Exception { @@ -240,11 +240,11 @@ public class AddCommandTest extends RepositoryTestCase { builder.commit(); assertEquals( - "[a.txt, mode:100644, stage:1, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + - "[a.txt, mode:100644, stage:2, sha1:b9f89ff733bdaf49e02711535867bb821f9db55e]" + - "[a.txt, mode:100644, stage:3, sha1:4f41554f6e0045ef53848fc0c3f33b6a9abc24a9]" + - "[b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, stage:1, content:content]" + + "[a.txt, mode:100644, stage:2, content:our content]" + + "[a.txt, mode:100644, stage:3, content:other content]" + + "[b.txt, mode:100644, content:content b]", + indexState(CONTENT)); // now the test begins @@ -252,9 +252,9 @@ public class AddCommandTest extends RepositoryTestCase { dc = git.add().addFilepattern("a.txt").call(); assertEquals( - "[a.txt, mode:100644, sha1:b9f89ff733bdaf49e02711535867bb821f9db55e]" + - "[b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, content:our content]" + + "[b.txt, mode:100644, content:content b]", + indexState(CONTENT)); } public void testAddTwoFiles() throws Exception { @@ -273,9 +273,9 @@ public class AddCommandTest extends RepositoryTestCase { Git git = new Git(db); git.add().addFilepattern("a.txt").addFilepattern("b.txt").call(); assertEquals( - "[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + - "[b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", - indexState(CONTENT_ID)); + "[a.txt, mode:100644, content:content]" + + "[b.txt, mode:100644, content:content b]", + indexState(CONTENT)); } public void testAddFolder() throws Exception { @@ -295,9 +295,9 @@ public class AddCommandTest extends RepositoryTestCase { Git git = new Git(db); git.add().addFilepattern("sub").call(); assertEquals( - "[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + - "[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:content]" + + "[sub/b.txt, mode:100644, content:content b]", + indexState(CONTENT)); } public void testAddIgnoredFile() throws Exception { @@ -324,8 +324,8 @@ public class AddCommandTest extends RepositoryTestCase { git.add().addFilepattern("sub").call(); assertEquals( - "[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:content]", + indexState(CONTENT)); } public void testAddWholeRepo() throws Exception { @@ -345,9 +345,9 @@ public class AddCommandTest extends RepositoryTestCase { Git git = new Git(db); git.add().addFilepattern(".").call(); assertEquals( - "[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + - "[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:content]" + + "[sub/b.txt, mode:100644, content:content b]", + indexState(CONTENT)); } // the same three cases as in testAddWithParameterUpdate @@ -372,9 +372,9 @@ public class AddCommandTest extends RepositoryTestCase { git.add().addFilepattern("sub").call(); assertEquals( - "[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + - "[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:content]" + + "[sub/b.txt, mode:100644, content:content b]", + indexState(CONTENT)); git.commit().setMessage("commit").call(); @@ -398,10 +398,10 @@ public class AddCommandTest extends RepositoryTestCase { // deletion of sub/b.txt is not staged // sub/c.txt is staged assertEquals( - "[sub/a.txt, mode:100644, sha1:268af4e306cfcf6e79edd50fed9c553d211f68e3]" + - "[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]" + - "[sub/c.txt, mode:100644, sha1:fa08654474ae2ddc4f61ee3a43d017ba65a439c3]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:modified content]" + + "[sub/b.txt, mode:100644, content:content b]" + + "[sub/c.txt, mode:100644, content:content c]", + indexState(CONTENT)); } // file a exists in workdir and in index -> added @@ -425,9 +425,9 @@ public class AddCommandTest extends RepositoryTestCase { git.add().addFilepattern("sub").call(); assertEquals( - "[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + - "[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:content]" + + "[sub/b.txt, mode:100644, content:content b]", + indexState(CONTENT)); git.commit().setMessage("commit").call(); @@ -451,8 +451,8 @@ public class AddCommandTest extends RepositoryTestCase { git.add().addFilepattern("sub").setUpdate(true).call(); // change in sub/a.txt is staged assertEquals( - "[sub/a.txt, mode:100644, sha1:268af4e306cfcf6e79edd50fed9c553d211f68e3]", - indexState(CONTENT_ID)); + "[sub/a.txt, mode:100644, content:modified content]", + indexState(CONTENT)); } private DirCacheEntry addEntryToBuilder(String path, File file, diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java index 89ad25201..5e04f32f8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java @@ -133,9 +133,9 @@ public class RacyGitTests extends RepositoryTestCase { resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes)); assertEquals( - "[a, mode:100644, time:t0, length:1, sha1:2e65efe2a145dda7ee51d1741299f848e5bf752e]" + - "[b, mode:100644, time:t0, length:1, sha1:63d8dbd40c23542e740659a7168a0ce3138ea748]", - indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT_ID)); + "[a, mode:100644, time:t0, length:1, content:a]" + + "[b, mode:100644, time:t0, length:1, content:b]", + indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT)); // Remember the last modTime of index file. All modifications times of // further modification are translated to this value so it looks that @@ -152,9 +152,9 @@ public class RacyGitTests extends RepositoryTestCase { db.readDirCache(); // although racily clean a should not be reported as being dirty assertEquals( - "[a, mode:100644, time:t1, smudged, length:0]" + - "[b, mode:100644, time:t0, length:1]", - indexState(SMUDGE|MOD_TIME|LENGTH)); + "[a, mode:100644, time:t1, smudged, length:0, content:a2]" + + "[b, mode:100644, time:t0, length:1, content:b]", + indexState(SMUDGE|MOD_TIME|LENGTH|CONTENT)); } private File addToWorkDir(String path, String content) throws IOException { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java index a98350c8d..33adf19bb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java @@ -59,11 +59,9 @@ import java.util.TreeSet; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheEntry; -import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.storage.file.FileRepository; import org.eclipse.jgit.treewalk.FileTreeIterator; -import org.eclipse.jgit.treewalk.NameConflictTreeWalk; /** * Base class for most JGit unit tests. @@ -132,6 +130,8 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase { public static final int CONTENT_ID = 8; + public static final int CONTENT = 16; + /** * Represent the state of the index in one String. This representation is * useful when writing tests which do assertions on the state of the index. @@ -142,13 +142,15 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase { * The format of the returned string is described with this BNF: * *
-	 * result = ( "[" path mode stage? time? smudge? length? sha1? "]" )* .
+	 * result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
 	 * mode = ", mode:" number .
 	 * stage = ", stage:" number .
 	 * time = ", time:t" timestamp-index .
 	 * smudge = "" | ", smudged" .
 	 * length = ", length:" number .
 	 * sha1 = ", sha1:" hex-sha1 .
+	 * content = ", content:" blob-data .
+	 * 
* * 'stage' is only presented when the stage is different from 0. All * reported time stamps are mapped to strings like "t0", "t1", ... "tn". The @@ -158,7 +160,7 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase { * * @param includedOptions * a bitmask constructed out of the constants {@link #MOD_TIME}, - * {@link #SMUDGE}, {@link #LENGTH} and {@link #CONTENT_ID} + * {@link #SMUDGE}, {@link #LENGTH}, {@link #CONTENT_ID} and {@link #CONTENT} * controlling which info is present in the resulting string. * @return a string encoding the index state * @throws IllegalStateException @@ -178,29 +180,29 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase { } // iterate again, now produce the result string - NameConflictTreeWalk tw = new NameConflictTreeWalk(db); - tw.setRecursive(true); - tw.reset(); - tw.addTree(new DirCacheIterator(dc)); - while (tw.next()) { - DirCacheIterator dcIt = tw.getTree(0, DirCacheIterator.class); - sb.append("["+tw.getPathString()+", mode:" + dcIt.getEntryFileMode()); - int stage = dcIt.getDirCacheEntry().getStage(); + for (int i=0; i