diff --git a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java index 4bf9b43cb..a72af9a1c 100644 --- a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java +++ b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java @@ -48,11 +48,13 @@ import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.eclipse.jgit.junit.JGitTestUtil; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.pgm.CLIGitCommand; +import org.eclipse.jgit.pgm.Die; import org.junit.Before; public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase { @@ -79,7 +81,7 @@ public void setUp() throws Exception { * @return command output * @throws Exception */ - protected String[] execute(String... cmds) throws Exception { + protected String[] executeUnchecked(String... cmds) throws Exception { List result = new ArrayList(cmds.length); for (String cmd : cmds) { result.addAll(CLIGitCommand.execute(cmd, db)); @@ -87,6 +89,28 @@ protected String[] execute(String... cmds) throws Exception { return result.toArray(new String[0]); } + /** + * Executes specified git commands (with arguments), throws exception and + * stops execution on first command which output contains a 'fatal:' error + * + * @param cmds + * each string argument must be a valid git command line, e.g. + * "git branch -h" + * @return command output + * @throws Exception + */ + protected String[] execute(String... cmds) throws Exception { + List result = new ArrayList(cmds.length); + for (String cmd : cmds) { + List out = CLIGitCommand.execute(cmd, db); + if (contains(out, "fatal: ")) { + throw new Die(toString(out)); + } + result.addAll(out); + } + return result.toArray(new String[0]); + } + /** * @param link * the path of the symbolic link to create @@ -196,15 +220,32 @@ protected void assertStringArrayEquals(String expected, String[] actual) { } protected void assertArrayOfLinesEquals(String[] expected, String[] actual) { - assertEquals(toText(expected), toText(actual)); + assertEquals(toString(expected), toString(actual)); } - private static String toText(String[] lines) { + public static String toString(String[] lines) { + return toString(Arrays.asList(lines)); + } + + public static String toString(List lines) { StringBuilder b = new StringBuilder(); for (String s : lines) { - b.append(s); - b.append('\n'); + if (s != null && !s.isEmpty()) { + b.append(s); + if (!s.endsWith("\n")) { + b.append('\n'); + } + } } return b.toString(); } + + public static boolean contains(List lines, String str) { + for (String s : lines) { + if (s.contains(str)) { + return true; + } + } + return false; + } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java index ca3f00f49..597091369 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/AddTest.java @@ -65,7 +65,7 @@ public void setUp() throws Exception { @Test public void testAddNothing() throws Exception { assertEquals("fatal: Argument \"filepattern\" is required", // - execute("git add")[0]); + executeUnchecked("git add")[0]); } @Test diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java index 9aca2d823..2e02c762b 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java @@ -102,7 +102,8 @@ public void testEmptyTar() throws Exception { @Test public void testUnrecognizedFormat() throws Exception { String[] expect = new String[] { "fatal: Unknown archive format 'nonsense'" }; - String[] actual = execute("git archive --format=nonsense " + emptyTree); + String[] actual = executeUnchecked( + "git archive --format=nonsense " + emptyTree); assertArrayEquals(expect, actual); } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java index 4200cd05c..f369577ba 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java @@ -89,6 +89,6 @@ public void testListContains() throws Exception { @Test public void testExistingBranch() throws Exception { assertEquals("fatal: A branch named 'master' already exists.", - execute("git branch master")[0]); + executeUnchecked("git branch master")[0]); } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java index 167d8ab51..6175b6c06 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java @@ -109,13 +109,13 @@ public void testCheckoutNewBranchThatAlreadyExists() throws Exception { assertStringArrayEquals( "fatal: A branch named 'master' already exists.", - execute("git checkout -b master")); + executeUnchecked("git checkout -b master")); } @Test public void testCheckoutNewBranchOnBranchToBeBorn() throws Exception { assertStringArrayEquals("fatal: You are on a branch yet to be born", - execute("git checkout -b side")); + executeUnchecked("git checkout -b side")); } @Test diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitTest.java index 360ee5a6e..721ed1569 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CommitTest.java @@ -98,16 +98,4 @@ public void testCommitAll() throws Exception { result.trim().equals("On branch master")); } - String toString(String[] arr) { - StringBuilder sb = new StringBuilder(); - for (String s : arr) { - if (s != null && !s.isEmpty()) { - sb.append(s); - if (!s.endsWith("\n")) { - sb.append('\n'); - } - } - } - return sb.toString(); - } } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java index 1c8ba0c2f..a50b243ee 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java @@ -73,7 +73,7 @@ private void initialCommitAndTag() throws Exception { public void testNoHead() throws Exception { assertArrayEquals( new String[] { "fatal: No names found, cannot describe anything." }, - execute("git describe")); + executeUnchecked("git describe")); } @Test @@ -81,7 +81,7 @@ public void testHeadNoTag() throws Exception { git.commit().setMessage("initial commit").call(); assertArrayEquals( new String[] { "fatal: No names found, cannot describe anything." }, - execute("git describe")); + executeUnchecked("git describe")); } @Test @@ -119,7 +119,7 @@ public void testHelpArgumentBeforeUnknown() throws Exception { @Test public void testHelpArgumentAfterUnknown() throws Exception { - String[] output = execute("git describe -XYZ -h"); + String[] output = executeUnchecked("git describe -XYZ -h"); String all = Arrays.toString(output); assertTrue("Unexpected help output: " + all, all.contains("jgit describe")); diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java index 975e8c4f7..641e59b04 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java @@ -195,7 +195,7 @@ public void testNoFastForward() throws Exception { @Test public void testNoFastForwardAndSquash() throws Exception { assertEquals("fatal: You cannot combine --squash with --no-ff.", - execute("git merge master --no-ff --squash")[0]); + executeUnchecked("git merge master --no-ff --squash")[0]); } @Test @@ -210,7 +210,7 @@ public void testFastForwardOnly() throws Exception { git.commit().setMessage("commit#2").call(); assertEquals("fatal: Not possible to fast-forward, aborting.", - execute("git merge master --ff-only")[0]); + executeUnchecked("git merge master --ff-only")[0]); } @Test diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java index 85fc1dbb4..715682755 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/RepoTest.java @@ -103,7 +103,7 @@ public void setUp() throws Exception { @Test public void testMissingPath() throws Exception { assertEquals("fatal: Argument \"path\" is required", - execute("git repo")[0]); + executeUnchecked("git repo")[0]); } /** diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java index ab09db5a5..0fe25f550 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/TagTest.java @@ -68,6 +68,6 @@ public void testTagTwice() throws Exception { git.commit().setMessage("commit").call(); assertEquals("fatal: tag 'test' already exists", - execute("git tag test")[0]); + executeUnchecked("git tag test")[0]); } }