diff options
author | Andrey Loskutov <loskutov@gmx.de> | 2016-01-03 12:29:55 +0100 |
---|---|---|
committer | Andrey Loskutov <loskutov@gmx.de> | 2016-01-06 17:27:31 -0500 |
commit | 7780a4ee31df0e73f9519aaa51fd349a70b18e71 (patch) | |
tree | 7fca7d0a26d211ada7b5826fae60699f20fd484a /org.eclipse.jgit.pgm.test/src/org | |
parent | fb5056c2c5e96b815abe568af588157083c66197 (diff) | |
download | jgit-7780a4ee31df0e73f9519aaa51fd349a70b18e71.tar.gz jgit-7780a4ee31df0e73f9519aaa51fd349a70b18e71.zip |
Make sure CLIGitCommand and Main produce (almost) same results
Currently execution of tests in pgm uses CLIGitCommand which
re-implements few things from Main. Unfortunately this can results in a
different test behavior compared to the real CLI runtime.
The change let CLIGitCommand extend Main and only slightly modifies the
runtime (stream redirection and undesired exit() termination).
Change-Id: I87b7b61d1c84a89e5917610d84409f01be90b70b
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Diffstat (limited to 'org.eclipse.jgit.pgm.test/src/org')
-rw-r--r-- | org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java | 15 | ||||
-rw-r--r-- | org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java | 163 |
2 files changed, 113 insertions, 65 deletions
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 a3436a0179..a6af077aa5 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 @@ -54,7 +54,8 @@ 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.eclipse.jgit.pgm.CLIGitCommand.Result; +import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException; import org.junit.Before; public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase { @@ -84,7 +85,7 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase { protected String[] executeUnchecked(String... cmds) throws Exception { List<String> result = new ArrayList<String>(cmds.length); for (String cmd : cmds) { - result.addAll(CLIGitCommand.execute(cmd, db)); + result.addAll(CLIGitCommand.executeUnchecked(cmd, db)); } return result.toArray(new String[0]); } @@ -102,11 +103,13 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase { protected String[] execute(String... cmds) throws Exception { List<String> result = new ArrayList<String>(cmds.length); for (String cmd : cmds) { - List<String> out = CLIGitCommand.execute(cmd, db); - if (contains(out, "fatal: ")) { - throw new Die(toString(out)); + Result r = CLIGitCommand.executeRaw(cmd, db); + if (r.ex instanceof TerminatedByHelpException) { + result.addAll(r.errLines()); + } else if (r.ex != null) { + throw r.ex; } - result.addAll(out); + result.addAll(r.outLines()); } return result.toArray(new String[0]); } diff --git a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java index 6a12019d7c..3f396563c2 100644 --- a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java +++ b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java @@ -42,33 +42,31 @@ */ package org.eclipse.jgit.pgm; +import static org.junit.Assert.assertNull; + import java.io.ByteArrayOutputStream; import java.io.File; + +import java.io.IOException; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.pgm.TextBuiltin.TerminatedByHelpException; -import org.eclipse.jgit.pgm.internal.CLIText; -import org.eclipse.jgit.pgm.opt.CmdLineParser; -import org.eclipse.jgit.pgm.opt.SubcommandHandler; import org.eclipse.jgit.util.IO; -import org.kohsuke.args4j.Argument; -public class CLIGitCommand { - @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class) - private TextBuiltin subcommand; +public class CLIGitCommand extends Main { - @Argument(index = 1, metaVar = "metaVar_arg") - private List<String> arguments = new ArrayList<String>(); + private final Result result; - public TextBuiltin getSubcommand() { - return subcommand; - } + private final Repository db; - public List<String> getArguments() { - return arguments; + public CLIGitCommand(Repository db) { + super(); + this.db = db; + result = new Result(); } /** @@ -102,57 +100,82 @@ public class CLIGitCommand { public static List<String> execute(String str, Repository db) throws Exception { + Result result = executeRaw(str, db); + return getOutput(result); + } + + public static Result executeRaw(String str, Repository db) + throws Exception { + CLIGitCommand cmd = new CLIGitCommand(db); + cmd.run(str); + return cmd.result; + } + + public static List<String> executeUnchecked(String str, Repository db) + throws Exception { + CLIGitCommand cmd = new CLIGitCommand(db); + try { + cmd.run(str); + return getOutput(cmd.result); + } catch (Throwable e) { + return cmd.result.errLines(); + } + } + + private static List<String> getOutput(Result result) { + if (result.ex instanceof TerminatedByHelpException) { + return result.errLines(); + } + return result.outLines(); + } + + private void run(String commandLine) throws Exception { + String[] argv = convertToMainArgs(commandLine); try { - return IO.readLines(new String(rawExecute(str, db))); - } catch (Die e) { - return IO.readLines(CLIText.fatalError(e.getMessage())); + super.run(argv); + } catch (TerminatedByHelpException e) { + // this is not a failure, super called exit() on help + } finally { + writer.flush(); } } - public static byte[] rawExecute(String str, Repository db) + private static String[] convertToMainArgs(String str) throws Exception { String[] args = split(str); - if (!args[0].equalsIgnoreCase("git") || args.length < 2) + if (!args[0].equalsIgnoreCase("git") || args.length < 2) { throw new IllegalArgumentException( "Expected 'git <command> [<args>]', was:" + str); + } String[] argv = new String[args.length - 1]; System.arraycopy(args, 1, argv, 0, args.length - 1); + return argv; + } - CLIGitCommand bean = new CLIGitCommand(); - final CmdLineParser clp = new TestCmdLineParser(bean); - clp.parseArgument(argv); - - final TextBuiltin cmd = bean.getSubcommand(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - cmd.outs = baos; - ByteArrayOutputStream errs = new ByteArrayOutputStream(); - cmd.errs = errs; - boolean seenHelp = TextBuiltin.containsHelp(argv); - if (cmd.requiresRepository()) - cmd.init(db, null); - else - cmd.init(null, null); - try { - cmd.execute(bean.getArguments().toArray( - new String[bean.getArguments().size()])); - } catch (TerminatedByHelpException e) { - seenHelp = true; - // this is not a failure, command execution should just not happen - } finally { - if (cmd.outw != null) { - cmd.outw.flush(); - } - if (cmd.errw != null) { - cmd.errw.flush(); - } - if (seenHelp) { - return errs.toByteArray(); - } else if (errs.size() > 0) { - // forward the errors to the standard err - System.err.print(errs.toString()); - } + @Override + PrintWriter createErrorWriter() { + return new PrintWriter(result.err); + } + + void init(final TextBuiltin cmd) throws IOException { + cmd.outs = result.out; + cmd.errs = result.err; + super.init(cmd); + } + + @Override + protected Repository openGitDir(String aGitdir) throws IOException { + assertNull(aGitdir); + return db; + } + + @Override + void exit(int status, Exception t) throws Exception { + if (t == null) { + t = new IllegalStateException(Integer.toString(status)); } - return baos.toByteArray(); + result.ex = t; + throw t; } /** @@ -210,14 +233,36 @@ public class CLIGitCommand { return list.toArray(new String[list.size()]); } - static class TestCmdLineParser extends CmdLineParser { - public TestCmdLineParser(Object bean) { - super(bean); + public static class Result { + public final ByteArrayOutputStream out = new ByteArrayOutputStream(); + + public final ByteArrayOutputStream err = new ByteArrayOutputStream(); + + public Exception ex; + + public byte[] outBytes() { + return out.toByteArray(); + } + + public byte[] errBytes() { + return err.toByteArray(); + } + + public String outString() { + return out.toString(); } - @Override - protected boolean containsHelp(String... args) { - return false; + public List<String> outLines() { + return IO.readLines(out.toString()); + } + + public String errString() { + return err.toString(); + } + + public List<String> errLines() { + return IO.readLines(err.toString()); } } + } |