CheckoutTest: Create Git instances in try-with-resource

Change-Id: I49a03f7bee0b61c062ce160674f9aa0cd1bcc8ba
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
This commit is contained in:
David Pursehouse 2016-01-14 17:49:26 +09:00 committed by Matthias Sohn
parent 9d721de69b
commit 7f84e40f31
1 changed files with 272 additions and 251 deletions

View File

@ -66,28 +66,34 @@ public class CheckoutTest extends CLIRepositoryTestCase {
@Test
public void testCheckoutSelf() throws Exception {
new Git(db).commit().setMessage("initial commit").call();
try (Git git = new Git(db)) {
git.commit().setMessage("initial commit").call();
assertStringArrayEquals("Already on 'master'",
execute("git checkout master"));
}
}
@Test
public void testCheckoutBranch() throws Exception {
new Git(db).commit().setMessage("initial commit").call();
new Git(db).branchCreate().setName("side").call();
try (Git git = new Git(db)) {
git.commit().setMessage("initial commit").call();
git.branchCreate().setName("side").call();
assertStringArrayEquals("Switched to branch 'side'",
execute("git checkout side"));
}
}
@Test
public void testCheckoutNewBranch() throws Exception {
new Git(db).commit().setMessage("initial commit").call();
try (Git git = new Git(db)) {
git.commit().setMessage("initial commit").call();
assertStringArrayEquals("Switched to a new branch 'side'",
execute("git checkout -b side"));
}
}
@Test
public void testCheckoutNonExistingBranch() throws Exception {
@ -98,12 +104,14 @@ public void testCheckoutNonExistingBranch() throws Exception {
@Test
public void testCheckoutNewBranchThatAlreadyExists() throws Exception {
new Git(db).commit().setMessage("initial commit").call();
try (Git git = new Git(db)) {
git.commit().setMessage("initial commit").call();
assertStringArrayEquals(
"fatal: A branch named 'master' already exists.",
execute("git checkout -b master"));
}
}
@Test
public void testCheckoutNewBranchOnBranchToBeBorn() throws Exception {
@ -120,14 +128,16 @@ public void testCheckoutUnresolvedHead() throws Exception {
@Test
public void testCheckoutHead() throws Exception {
new Git(db).commit().setMessage("initial commit").call();
try (Git git = new Git(db)) {
git.commit().setMessage("initial commit").call();
assertStringArrayEquals("", execute("git checkout HEAD"));
}
}
@Test
public void testCheckoutExistingBranchWithConflict() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("a", "Hello world a");
git.add().addFilepattern(".").call();
git.commit().setMessage("commit file a").call();
@ -147,6 +157,7 @@ public void testCheckoutExistingBranchWithConflict() throws Exception {
execute[0]);
assertEquals("\ta", execute[1]);
}
}
/**
* Steps:
@ -167,7 +178,7 @@ public void testCheckoutExistingBranchWithConflict() throws Exception {
*/
@Test
public void testCheckoutWithMissingWorkingTreeFile() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
File fileA = writeTrashFile("a", "Hello world a");
writeTrashFile("b", "Hello world b");
git.add().addFilepattern(".").call();
@ -190,10 +201,11 @@ public void testCheckoutWithMissingWorkingTreeFile() throws Exception {
assertEquals(FileMode.REGULAR_FILE, entry.getMode());
assertEquals("Hello world a", read(fileA));
}
}
@Test
public void testCheckoutOrphan() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
git.commit().setMessage("initial commit").call();
assertStringArrayEquals("Switched to a new branch 'new_branch'",
@ -203,6 +215,7 @@ public void testCheckoutOrphan() throws Exception {
RevCommit commit = git.commit().setMessage("orphan commit").call();
assertEquals(0, commit.getParentCount());
}
}
/**
* Steps:
@ -224,7 +237,7 @@ public void testCheckoutOrphan() throws Exception {
@Test
public void fileModeTestMissingThenFolderWithFileInWorkingTree()
throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("b", "Hello world b");
git.add().addFilepattern(".").call();
git.commit().setMessage("add file b").call();
@ -252,6 +265,7 @@ public void fileModeTestMissingThenFolderWithFileInWorkingTree()
db.getFS());
assertEquals(FileMode.REGULAR_FILE, entry.getMode());
}
}
/**
* Steps:
@ -272,7 +286,7 @@ public void fileModeTestMissingThenFolderWithFileInWorkingTree()
*/
@Test
public void fileModeTestFolderWithMissingInWorkingTree() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("b", "Hello world b");
writeTrashFile("a", "b");
git.add().addFilepattern(".").call();
@ -297,6 +311,7 @@ public void fileModeTestFolderWithMissingInWorkingTree() throws Exception {
db.getFS());
assertEquals(FileMode.REGULAR_FILE, entry.getMode());
}
}
/**
* Steps:
@ -317,7 +332,7 @@ public void fileModeTestFolderWithMissingInWorkingTree() throws Exception {
*/
@Test
public void fileModeTestMissingWithFolderInWorkingTree() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("b", "Hello world b");
writeTrashFile("a", "b");
git.add().addFilepattern(".").call();
@ -344,6 +359,7 @@ public void fileModeTestMissingWithFolderInWorkingTree() throws Exception {
assertEquals("a", exception.getConflictingPaths().get(0));
assertEquals("a/c", exception.getConflictingPaths().get(1));
}
}
/**
* Steps:
@ -364,7 +380,7 @@ public void fileModeTestMissingWithFolderInWorkingTree() throws Exception {
@Test
public void fileModeTestFolderThenMissingWithFileInWorkingTree()
throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
writeTrashFile("a/c", "Hello world c");
writeTrashFile("b", "Hello world b");
@ -399,6 +415,7 @@ public void fileModeTestFolderThenMissingWithFileInWorkingTree()
assertEquals(1, exception.getConflictingPaths().size());
assertEquals("a", exception.getConflictingPaths().get(0));
}
}
/**
* Steps:
@ -420,7 +437,7 @@ public void fileModeTestFolderThenMissingWithFileInWorkingTree()
@Test
public void fileModeTestFolderThenFileWithMissingInWorkingTree()
throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
writeTrashFile("a/c", "Hello world c");
writeTrashFile("b", "Hello world b");
@ -445,6 +462,7 @@ public void fileModeTestFolderThenFileWithMissingInWorkingTree()
db.getFS());
assertEquals(FileMode.TREE, entry.getMode());
}
}
/**
* Steps:
@ -464,7 +482,7 @@ public void fileModeTestFolderThenFileWithMissingInWorkingTree()
*/
@Test
public void fileModeTestFileThenFileWithFolderInIndex() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("a", "Hello world a");
writeTrashFile("b", "Hello world b");
git.add().addFilepattern(".").call();
@ -497,6 +515,7 @@ public void fileModeTestFileThenFileWithFolderInIndex() throws Exception {
assertEquals(1, exception.getConflictingPaths().size());
assertEquals("a", exception.getConflictingPaths().get(0));
}
}
/**
* Steps:
@ -517,7 +536,7 @@ public void fileModeTestFileThenFileWithFolderInIndex() throws Exception {
*/
@Test
public void fileModeTestFileWithFolderInIndex() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("b", "Hello world b");
writeTrashFile("a", "b");
git.add().addFilepattern(".").call();
@ -556,10 +575,11 @@ public void fileModeTestFileWithFolderInIndex() throws Exception {
// assertEquals("a", exception.getConflictingPaths().get(0));
// assertEquals("a/c", exception.getConflictingPaths().get(1));
}
}
@Test
public void testCheckoutPath() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
writeTrashFile("a", "Hello world a");
git.add().addFilepattern(".").call();
git.commit().setMessage("commit file a").call();
@ -578,4 +598,5 @@ public void testCheckoutPath() throws Exception {
execute("git branch"));
assertEquals("Hello world b", read(b));
}
}
}