AddCommandTest: Create Git instances in try-with-resource

Change-Id: Idf42f03099eeb9975fef9492ea8a75776afc2a3c
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
This commit is contained in:
David Pursehouse 2016-01-21 10:21:12 +09:00
parent a833fec219
commit a8d99d9a4f
1 changed files with 314 additions and 293 deletions

View File

@ -77,9 +77,7 @@ public class AddCommandTest extends RepositoryTestCase {
@Test
public void testAddNothing() throws GitAPIException {
Git git = new Git(db);
try {
try (Git git = new Git(db)) {
git.add().call();
fail("Expected IllegalArgumentException");
} catch (NoFilepatternException e) {
@ -90,11 +88,10 @@ public void testAddNothing() throws GitAPIException {
@Test
public void testAddNonExistingSingleFile() throws GitAPIException {
Git git = new Git(db);
try (Git git = new Git(db)) {
DirCache dc = git.add().addFilepattern("a.txt").call();
assertEquals(0, dc.getEntryCount());
}
}
@Test
@ -105,14 +102,14 @@ public void testAddExistingSingleFile() throws IOException, GitAPIException {
writer.print("content");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern("a.txt").call();
assertEquals(
"[a.txt, mode:100644, content:content]",
indexState(CONTENT));
}
}
@Test
public void testCleanFilter() throws IOException,
@ -124,7 +121,7 @@ public void testCleanFilter() throws IOException,
writeTrashFile("src/a.txt", "foo\n");
File script = writeTempFile("sed s/o/e/g");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "clean",
"sh " + slashify(script.getPath()));
@ -137,6 +134,7 @@ public void testCleanFilter() throws IOException,
"[src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:fee\n]",
indexState(CONTENT));
}
}
@Test
public void testCleanFilterEnvironment()
@ -145,7 +143,7 @@ public void testCleanFilterEnvironment()
writeTrashFile("src/a.txt", "foo");
File script = writeTempFile("echo $GIT_DIR; echo 1 >xyz");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "clean",
"sh " + slashify(script.getPath()));
@ -157,6 +155,7 @@ public void testCleanFilterEnvironment()
+ "\n]", indexState(CONTENT));
assertTrue(new File(db.getWorkTree(), "xyz").exists());
}
}
@Test
public void testMultipleCleanFilter() throws IOException, GitAPIException {
@ -169,7 +168,7 @@ public void testMultipleCleanFilter() throws IOException, GitAPIException {
File script = writeTempFile("sed s/o/e/g");
File script2 = writeTempFile("sed s/f/x/g");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "clean",
"sh " + slashify(script.getPath()));
@ -186,6 +185,7 @@ public void testMultipleCleanFilter() throws IOException, GitAPIException {
// TODO: multiple clean filters for one file???
}
}
/**
* The path of an added file name contains ';' and afterwards malicious
@ -202,7 +202,7 @@ public void testCommandInjection() throws IOException, GitAPIException {
writeTrashFile("; echo virus", "foo\n");
File script = writeTempFile("sed s/o/e/g");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "clean",
"sh " + slashify(script.getPath()) + " %f");
@ -214,13 +214,14 @@ public void testCommandInjection() throws IOException, GitAPIException {
assertEquals("[; echo virus, mode:100644, content:fee\n]",
indexState(CONTENT));
}
}
@Test
public void testBadCleanFilter() throws IOException, GitAPIException {
writeTrashFile("a.txt", "foo");
File script = writeTempFile("sedfoo s/o/e/g");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "clean",
"sh " + script.getPath());
@ -234,13 +235,14 @@ public void testBadCleanFilter() throws IOException, GitAPIException {
assertEquals(127, e.getReturnCode());
}
}
}
@Test
public void testBadCleanFilter2() throws IOException, GitAPIException {
writeTrashFile("a.txt", "foo");
File script = writeTempFile("sed s/o/e/g");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "clean",
"shfoo " + script.getPath());
@ -254,6 +256,7 @@ public void testBadCleanFilter2() throws IOException, GitAPIException {
assertEquals(127, e.getReturnCode());
}
}
}
@Test
public void testCleanFilterReturning12() throws IOException,
@ -261,7 +264,7 @@ public void testCleanFilterReturning12() throws IOException,
writeTrashFile("a.txt", "foo");
File script = writeTempFile("exit 12");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "clean",
"sh " + slashify(script.getPath()));
@ -275,13 +278,14 @@ public void testCleanFilterReturning12() throws IOException,
assertEquals(12, e.getReturnCode());
}
}
}
@Test
public void testNotApplicableFilter() throws IOException, GitAPIException {
writeTrashFile("a.txt", "foo");
File script = writeTempFile("sed s/o/e/g");
Git git = new Git(db);
try (Git git = new Git(db)) {
StoredConfig config = git.getRepository().getConfig();
config.setString("filter", "tstFilter", "something",
"sh " + script.getPath());
@ -290,7 +294,9 @@ public void testNotApplicableFilter() throws IOException, GitAPIException {
git.add().addFilepattern("a.txt").call();
assertEquals("[a.txt, mode:100644, content:foo]", indexState(CONTENT));
assertEquals("[a.txt, mode:100644, content:foo]",
indexState(CONTENT));
}
}
private File writeTempFile(String body) throws IOException {
@ -308,7 +314,7 @@ public void testAddExistingSingleSmallFileWithNewLine() throws IOException,
writer.print("row1\r\nrow2");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
db.getConfig().setString("core", null, "autocrlf", "false");
git.add().addFilepattern("a.txt").call();
assertEquals("[a.txt, mode:100644, content:row1\r\nrow2]",
@ -322,6 +328,7 @@ public void testAddExistingSingleSmallFileWithNewLine() throws IOException,
assertEquals("[a.txt, mode:100644, content:row1\nrow2]",
indexState(CONTENT));
}
}
@Test
public void testAddExistingSingleMediumSizeFileWithNewLine()
@ -337,7 +344,7 @@ public void testAddExistingSingleMediumSizeFileWithNewLine()
writer.print(crData);
writer.close();
String lfData = data.toString().replaceAll("\r", "");
Git git = new Git(db);
try (Git git = new Git(db)) {
db.getConfig().setString("core", null, "autocrlf", "false");
git.add().addFilepattern("a.txt").call();
assertEquals("[a.txt, mode:100644, content:" + data + "]",
@ -351,6 +358,7 @@ public void testAddExistingSingleMediumSizeFileWithNewLine()
assertEquals("[a.txt, mode:100644, content:" + lfData + "]",
indexState(CONTENT));
}
}
@Test
public void testAddExistingSingleBinaryFile() throws IOException,
@ -361,7 +369,7 @@ public void testAddExistingSingleBinaryFile() throws IOException,
writer.print("row1\r\nrow2\u0000");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
db.getConfig().setString("core", null, "autocrlf", "false");
git.add().addFilepattern("a.txt").call();
assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
@ -375,6 +383,7 @@ public void testAddExistingSingleBinaryFile() throws IOException,
assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
indexState(CONTENT));
}
}
@Test
public void testAddExistingSingleFileInSubDir() throws IOException,
@ -386,14 +395,14 @@ public void testAddExistingSingleFileInSubDir() throws IOException,
writer.print("content");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern("sub/a.txt").call();
assertEquals(
"[sub/a.txt, mode:100644, content:content]",
indexState(CONTENT));
}
}
@Test
public void testAddExistingSingleFileTwice() throws IOException,
@ -404,7 +413,7 @@ public void testAddExistingSingleFileTwice() throws IOException,
writer.print("content");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
DirCache dc = git.add().addFilepattern("a.txt").call();
dc.getEntry(0).getObjectId();
@ -419,6 +428,7 @@ public void testAddExistingSingleFileTwice() throws IOException,
"[a.txt, mode:100644, content:other content]",
indexState(CONTENT));
}
}
@Test
public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
@ -428,7 +438,7 @@ public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
writer.print("content");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
DirCache dc = git.add().addFilepattern("a.txt").call();
dc.getEntry(0).getObjectId();
@ -445,6 +455,7 @@ public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
"[a.txt, mode:100644, content:other content]",
indexState(CONTENT));
}
}
@Test
public void testAddRemovedFile() throws Exception {
@ -454,7 +465,7 @@ public void testAddRemovedFile() throws Exception {
writer.print("content");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
DirCache dc = git.add().addFilepattern("a.txt").call();
dc.getEntry(0).getObjectId();
@ -467,6 +478,7 @@ public void testAddRemovedFile() throws Exception {
"[a.txt, mode:100644, content:content]",
indexState(CONTENT));
}
}
@Test
public void testAddRemovedCommittedFile() throws Exception {
@ -476,7 +488,7 @@ public void testAddRemovedCommittedFile() throws Exception {
writer.print("content");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
DirCache dc = git.add().addFilepattern("a.txt").call();
git.commit().setMessage("commit a.txt").call();
@ -491,6 +503,7 @@ public void testAddRemovedCommittedFile() throws Exception {
"[a.txt, mode:100644, content:content]",
indexState(CONTENT));
}
}
@Test
public void testAddWithConflicts() throws Exception {
@ -537,7 +550,7 @@ public void testAddWithConflicts() throws Exception {
// now the test begins
Git git = new Git(db);
try (Git git = new Git(db)) {
dc = git.add().addFilepattern("a.txt").call();
assertEquals(
@ -545,6 +558,7 @@ public void testAddWithConflicts() throws Exception {
"[b.txt, mode:100644, content:content b]",
indexState(CONTENT));
}
}
@Test
public void testAddTwoFiles() throws Exception {
@ -560,13 +574,14 @@ public void testAddTwoFiles() throws Exception {
writer.print("content b");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
assertEquals(
"[a.txt, mode:100644, content:content]" +
"[b.txt, mode:100644, content:content b]",
indexState(CONTENT));
}
}
@Test
public void testAddFolder() throws Exception {
@ -583,13 +598,14 @@ public void testAddFolder() throws Exception {
writer.print("content b");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern("sub").call();
assertEquals(
"[sub/a.txt, mode:100644, content:content]" +
"[sub/b.txt, mode:100644, content:content b]",
indexState(CONTENT));
}
}
@Test
public void testAddIgnoredFile() throws Exception {
@ -612,13 +628,14 @@ public void testAddIgnoredFile() throws Exception {
writer.print("content b");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern("sub").call();
assertEquals(
"[sub/a.txt, mode:100644, content:content]",
indexState(CONTENT));
}
}
@Test
public void testAddWholeRepo() throws Exception {
@ -635,13 +652,14 @@ public void testAddWholeRepo() throws Exception {
writer.print("content b");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern(".").call();
assertEquals(
"[sub/a.txt, mode:100644, content:content]" +
"[sub/b.txt, mode:100644, content:content b]",
indexState(CONTENT));
}
}
// the same three cases as in testAddWithParameterUpdate
// file a exists in workdir and in index -> added
@ -662,7 +680,7 @@ public void testAddWithoutParameterUpdate() throws Exception {
writer.print("content b");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern("sub").call();
assertEquals(
@ -697,6 +715,7 @@ public void testAddWithoutParameterUpdate() throws Exception {
"[sub/c.txt, mode:100644, content:content c]",
indexState(CONTENT));
}
}
// file a exists in workdir and in index -> added
// file b exists not in workdir but in index -> deleted
@ -716,7 +735,7 @@ public void testAddWithParameterUpdate() throws Exception {
writer.print("content b");
writer.close();
Git git = new Git(db);
try (Git git = new Git(db)) {
git.add().addFilepattern("sub").call();
assertEquals(
@ -749,10 +768,11 @@ public void testAddWithParameterUpdate() throws Exception {
"[sub/a.txt, mode:100644, content:modified content]",
indexState(CONTENT));
}
}
@Test
public void testAssumeUnchanged() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
String path = "a.txt";
writeTrashFile(path, "content");
git.add().addFilepattern(path).call();
@ -782,6 +802,7 @@ public void testAssumeUnchanged() throws Exception {
indexState(CONTENT
| ASSUME_UNCHANGED));
}
}
@Test
public void testExecutableRetention() throws Exception {