Browse Source

Added CLIText.fatalError(String) API for tests

In different places (Main, TextBuiltin, CLIGitCommand) we report fatal
errors and at same time want to check for fatal errors in the tests.
Using common API simplifies the error testing and helps to navigate to
the actual error check implementation.

Change-Id: Iecde79beb33ea595171f168f46b0b10ab2f339bb
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
stable-4.3
Andrey Loskutov 9 years ago
parent
commit
24468f09e3
  1. 4
      org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java
  2. 12
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java
  3. 6
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java
  4. 8
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java
  5. 2
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java
  6. 13
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java

4
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java

@ -44,7 +44,6 @@ package org.eclipse.jgit.pgm;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -106,8 +105,7 @@ public class CLIGitCommand {
try { try {
return IO.readLines(new String(rawExecute(str, db))); return IO.readLines(new String(rawExecute(str, db)));
} catch (Die e) { } catch (Die e) {
return IO.readLines(MessageFormat.format(CLIText.get().fatalError, return IO.readLines(CLIText.fatalError(e.getMessage()));
e.getMessage()));
} }
} }

12
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DescribeTest.java

@ -43,6 +43,7 @@
package org.eclipse.jgit.pgm; package org.eclipse.jgit.pgm;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -50,6 +51,7 @@ import java.util.Arrays;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.CLIRepositoryTestCase;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -71,17 +73,15 @@ public class DescribeTest extends CLIRepositoryTestCase {
@Test @Test
public void testNoHead() throws Exception { public void testNoHead() throws Exception {
assertArrayEquals( assertEquals(CLIText.fatalError(CLIText.get().noNamesFound),
new String[] { "fatal: No names found, cannot describe anything." }, toString(executeUnchecked("git describe")));
executeUnchecked("git describe"));
} }
@Test @Test
public void testHeadNoTag() throws Exception { public void testHeadNoTag() throws Exception {
git.commit().setMessage("initial commit").call(); git.commit().setMessage("initial commit").call();
assertArrayEquals( assertEquals(CLIText.fatalError(CLIText.get().noNamesFound),
new String[] { "fatal: No names found, cannot describe anything." }, toString(executeUnchecked("git describe")));
executeUnchecked("git describe"));
} }
@Test @Test

6
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java

@ -50,6 +50,7 @@ import java.util.Iterator;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.CLIRepositoryTestCase;
import org.eclipse.jgit.merge.MergeStrategy; import org.eclipse.jgit.merge.MergeStrategy;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -194,7 +195,8 @@ public class MergeTest extends CLIRepositoryTestCase {
@Test @Test
public void testNoFastForwardAndSquash() throws Exception { public void testNoFastForwardAndSquash() throws Exception {
assertEquals("fatal: You cannot combine --squash with --no-ff.", assertEquals(
CLIText.fatalError(CLIText.get().cannotCombineSquashWithNoff),
executeUnchecked("git merge master --no-ff --squash")[0]); executeUnchecked("git merge master --no-ff --squash")[0]);
} }
@ -209,7 +211,7 @@ public class MergeTest extends CLIRepositoryTestCase {
git.add().addFilepattern("file").call(); git.add().addFilepattern("file").call();
git.commit().setMessage("commit#2").call(); git.commit().setMessage("commit#2").call();
assertEquals("fatal: Not possible to fast-forward, aborting.", assertEquals(CLIText.fatalError(CLIText.get().ffNotPossibleAborting),
executeUnchecked("git merge master --ff-only")[0]); executeUnchecked("git merge master --ff-only")[0]);
} }

8
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java

@ -128,7 +128,7 @@ public class Main {
} catch (Die err) { } catch (Die err) {
if (err.isAborted()) if (err.isAborted())
System.exit(1); System.exit(1);
System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); System.err.println(CLIText.fatalError(err.getMessage()));
if (showStackTrace) if (showStackTrace)
err.printStackTrace(); err.printStackTrace();
System.exit(128); System.exit(128);
@ -147,10 +147,10 @@ public class Main {
} }
if (!showStackTrace && err.getCause() != null if (!showStackTrace && err.getCause() != null
&& err instanceof TransportException) && err instanceof TransportException)
System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getCause().getMessage())); System.err.println(CLIText.fatalError(err.getCause().getMessage()));
if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) { //$NON-NLS-1$ if (err.getClass().getName().startsWith("org.eclipse.jgit.errors.")) { //$NON-NLS-1$
System.err.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); System.err.println(CLIText.fatalError(err.getMessage()));
if (showStackTrace) if (showStackTrace)
err.printStackTrace(); err.printStackTrace();
System.exit(128); System.exit(128);
@ -176,7 +176,7 @@ public class Main {
clp.parseArgument(argv); clp.parseArgument(argv);
} catch (CmdLineException err) { } catch (CmdLineException err) {
if (argv.length > 0 && !help && !version) { if (argv.length > 0 && !help && !version) {
writer.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); writer.println(CLIText.fatalError(err.getMessage()));
writer.flush(); writer.flush();
System.exit(1); System.exit(1);
} }

2
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/TextBuiltin.java

@ -216,7 +216,7 @@ public abstract class TextBuiltin {
try { try {
clp.parseArgument(args); clp.parseArgument(args);
} catch (CmdLineException err) { } catch (CmdLineException err) {
this.errw.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage())); this.errw.println(CLIText.fatalError(err.getMessage()));
if (help) { if (help) {
printUsage("", clp); //$NON-NLS-1$ printUsage("", clp); //$NON-NLS-1$
} }

13
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java

@ -74,6 +74,19 @@ public class CLIText extends TranslationBundle {
return MessageFormat.format(get().lineFormat, line); return MessageFormat.format(get().lineFormat, line);
} }
/**
* Format the given argument as fatal error using the format defined by
* {@link #fatalError} ("fatal: " by default).
*
* @param message
* the message to format
* @return the formatted line
* @since 4.2
*/
public static String fatalError(String message) {
return MessageFormat.format(get().fatalError, message);
}
// @formatter:off // @formatter:off
/***/ public String alreadyOnBranch; /***/ public String alreadyOnBranch;
/***/ public String alreadyUpToDate; /***/ public String alreadyUpToDate;

Loading…
Cancel
Save