Browse Source

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 <christian.halstrick@sap.com>
stable-0.9
Christian Halstrick 14 years ago committed by Shawn O. Pearce
parent
commit
07a1d17858
  1. 86
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java
  2. 12
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java
  3. 36
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java

86
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,

12
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 {

36
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:
*
* <pre>
* 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 .
* </pre>
*
* '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<dc.getEntryCount(); ++i) {
DirCacheEntry entry = dc.getEntry(i);
sb.append("["+entry.getPathString()+", mode:" + entry.getFileMode());
int stage = entry.getStage();
if (stage != 0)
sb.append(", stage:" + stage);
if (0 != (includedOptions & MOD_TIME)) {
sb.append(", time:t"+
timeStamps.headSet(Long.valueOf(dcIt.getDirCacheEntry().getLastModified())).size());
timeStamps.headSet(Long.valueOf(entry.getLastModified())).size());
}
if (0 != (includedOptions & SMUDGE))
if (dcIt.getDirCacheEntry().isSmudged())
if (entry.isSmudged())
sb.append(", smudged");
if (0 != (includedOptions & LENGTH))
sb.append(", length:"
+ Integer.toString(dcIt.getDirCacheEntry().getLength()));
+ Integer.toString(entry.getLength()));
if (0 != (includedOptions & CONTENT_ID))
sb.append(", sha1:" + ObjectId.toString(dcIt
.getEntryObjectId()));
sb.append(", sha1:" + ObjectId.toString(entry.getObjectId()));
if (0 != (includedOptions & CONTENT)) {
sb.append(", content:"
+ new String(db.open(entry.getObjectId(),
Constants.OBJ_BLOB).getCachedBytes(), "UTF-8"));
}
sb.append("]");
}
return sb.toString();

Loading…
Cancel
Save