changeset 346:e8f0eb6d4ca4 Eluru-6.5.x

Introduce CommandResult that filters out private data from ExecResult
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Wed, 11 Jan 2012 12:50:45 +0400
parents ec375e18374f
children 4a49a0baf30b 7d9620034403
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ArchiveCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BranchesCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ChangedFilesCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CloneCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandResult.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/PushCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/StatusCommand.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/CommandResultTest.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java mercurial-tests/src/testng.xml
diffstat 14 files changed, 119 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ArchiveCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ArchiveCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -14,7 +14,6 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.util.FileUtil;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
@@ -46,7 +45,7 @@
     setRevision(cli);
     setDestination(cli);
 
-    ExecResult res = runCommand(cli);
+    CommandResult res = runCommand(cli);
     failIfNotEmptyStdErr(cli, res);
     deleteHgArchival();
   }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -17,7 +17,6 @@
 
 import com.intellij.execution.configurations.GeneralCommandLine;
 import com.intellij.openapi.util.SystemInfo;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.util.StringUtil;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
@@ -108,19 +107,19 @@
     cli.setExePath(getSettings().getHgCommandPath());
   }
 
-  protected ExecResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
+  protected CommandResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
     return CommandUtil.runCommand(cli, getPrivateData());
   }
 
-  protected ExecResult runCommand(@NotNull GeneralCommandLine cli, boolean logErrorsInDebug) throws VcsException {
+  protected CommandResult runCommand(@NotNull GeneralCommandLine cli, boolean logErrorsInDebug) throws VcsException {
     return CommandUtil.runCommand(cli, CommandUtil.DEFAULT_COMMAND_TIMEOUT_SEC, getPrivateData(), logErrorsInDebug);
   }
 
-  protected ExecResult runCommand(@NotNull GeneralCommandLine cli, int executionTimeout) throws VcsException {
+  protected CommandResult runCommand(@NotNull GeneralCommandLine cli, int executionTimeout) throws VcsException {
     return CommandUtil.runCommand(cli, executionTimeout, getPrivateData());
   }
 
-  protected void failIfNotEmptyStdErr(@NotNull GeneralCommandLine cli, @NotNull ExecResult res) throws VcsException {
+  protected void failIfNotEmptyStdErr(@NotNull GeneralCommandLine cli, @NotNull CommandResult res) throws VcsException {
     if (!StringUtil.isEmpty(res.getStderr())) {
       CommandUtil.commandFailed(cli.getCommandLineString(), res);
     }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BranchesCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BranchesCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -16,7 +16,6 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 
@@ -44,7 +43,7 @@
   public Map<String, ChangeSet> execute() throws VcsException {
     GeneralCommandLine cli = createCommandLine();
     cli.addParameter("branches");
-    ExecResult res = runCommand(cli);
+    CommandResult res = runCommand(cli);
     String stdout = res.getStdout();
     Map<String, ChangeSet> result = new HashMap<String, ChangeSet>();
     Pattern branchPattern = Pattern.compile("(.*)[\\s]+([0-9]+:[A-Za-z0-9]+).*");
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ChangedFilesCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ChangedFilesCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -16,7 +16,6 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.util.FileUtil;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
@@ -53,7 +52,7 @@
       cli.addParameter(myRevId + ":" + myRevId);
       cli.addParameter("--style=" + styleFile.getAbsolutePath());
 
-      ExecResult res = runCommand(cli);
+      CommandResult res = runCommand(cli);
       return parseFiles(res.getStdout());
     } finally {
       FileUtil.delete(styleFile);
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CloneCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CloneCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -16,7 +16,6 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 
@@ -75,6 +74,6 @@
     cli.addParameter(myRepository);
     cli.addParameter(myWorkingDir.getName());
 
-    ExecResult res = runCommand(cli, 24*3600); // some repositories are quite large, we set timeout to 24 hours
+    runCommand(cli, 24*3600); // some repositories are quite large, we set timeout to 24 hours
   }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandResult.java	Wed Jan 11 12:50:45 2012 +0400
@@ -0,0 +1,44 @@
+package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
+
+import jetbrains.buildServer.ExecResult;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Set;
+
+import static jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandUtil.removePrivateData;
+
+/**
+ * Decorator for ExecResult that filters out private data from stdout and strerr.
+ *
+ * @author dmitry.neverov
+ */
+public class CommandResult {
+
+  private final ExecResult myDelegate;
+  private final Set<String> myPrivateData;
+
+  public CommandResult(@NotNull final ExecResult execResult, @NotNull final Set<String> privateData) {
+    myDelegate = execResult;
+    myPrivateData = privateData;
+  }
+
+  @NotNull
+  public String getStdout() {
+    return removePrivateData(myDelegate.getStdout(), myPrivateData);
+  }
+
+  @NotNull
+  public String getStderr() {
+    return removePrivateData(myDelegate.getStderr(), myPrivateData);
+  }
+
+  @Nullable
+  public Throwable getException() {
+    return myDelegate.getException();
+  }
+
+  public int getExitCode() {
+    return myDelegate.getExitCode();
+  }
+}
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Wed Jan 11 12:50:45 2012 +0400
@@ -30,11 +30,11 @@
 public class CommandUtil {
   public static final int DEFAULT_COMMAND_TIMEOUT_SEC = 3600;
 
-  public static void checkCommandFailed(@NotNull String cmdName, @NotNull ExecResult res) throws VcsException {
+  public static void checkCommandFailed(@NotNull String cmdName, @NotNull CommandResult res) throws VcsException {
     checkCommandFailed(cmdName, res, false);
   }
 
-  private static void checkCommandFailed(@NotNull String cmdName, @NotNull ExecResult res, boolean logErrorsInDebug) throws VcsException {
+  private static void checkCommandFailed(@NotNull String cmdName, @NotNull CommandResult res, boolean logErrorsInDebug) throws VcsException {
     if (logErrorsInDebug) {
       if (res.getExitCode() != 0 || res.getException() != null)
         Loggers.VCS.debug(createCommandLogMessage(cmdName, res));
@@ -48,7 +48,7 @@
     }
   }
 
-  public static void commandFailed(final String cmdName, final ExecResult res) throws VcsException {
+  public static void commandFailed(final String cmdName, final CommandResult res) throws VcsException {
     final String message = createCommandLogMessage(cmdName, res);
     Loggers.VCS.warn(message);
     if (hasImportantException(res)) {
@@ -57,7 +57,7 @@
     throw new VcsException(message);
   }
 
-  private static String createCommandLogMessage(final String cmdName, final ExecResult res) {
+  private static String createCommandLogMessage(final String cmdName, final CommandResult res) {
     String stderr = res.getStderr();
     String stdout = res.getStdout();
     String exceptionMessage = getExceptionMessage(res);
@@ -68,7 +68,7 @@
   }
 
   @Nullable
-  private static String getExceptionMessage(ExecResult result) {
+  private static String getExceptionMessage(CommandResult result) {
     Throwable exception = result.getException();
     String message = null;
     if (exception != null) {
@@ -80,7 +80,7 @@
     return message;
   }
 
-  private static boolean hasImportantException(ExecResult result) {
+  private static boolean hasImportantException(CommandResult result) {
     Throwable exception = result.getException();
     if (exception != null) {
       return exception instanceof NullPointerException;
@@ -89,21 +89,28 @@
     }
   }
 
-  public static ExecResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
+  public static CommandResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
     return runCommand(cli, DEFAULT_COMMAND_TIMEOUT_SEC, Collections.<String>emptySet());
   }
 
-  public static ExecResult runCommand(@NotNull GeneralCommandLine cli, @NotNull Set<String> privateData) throws VcsException {
+  public static CommandResult runCommand(@NotNull GeneralCommandLine cli, @NotNull Set<String> privateData) throws VcsException {
     return runCommand(cli, DEFAULT_COMMAND_TIMEOUT_SEC, privateData);
   }
 
-  public static ExecResult runCommand(@NotNull GeneralCommandLine cli, final int executionTimeout, @NotNull Set<String> privateData) throws VcsException {
+  public static CommandResult runCommand(@NotNull GeneralCommandLine cli, final int executionTimeout, @NotNull Set<String> privateData) throws VcsException {
     return runCommand(cli, executionTimeout, privateData, false);
   }
 
-  public static ExecResult runCommand(@NotNull GeneralCommandLine cli, final int executionTimeout, @NotNull Set<String> privateData, boolean logErrorsInDebug) throws VcsException {
+  public static CommandResult runCommand(@NotNull GeneralCommandLine cli, final int executionTimeout, @NotNull Set<String> privateData, boolean logErrorsInDebug) throws VcsException {
     final String cmdStr = removePrivateData(cli.getCommandLineString(), privateData);
     Loggers.VCS.debug("Run command: " + cmdStr);
+    CommandResult res = run(cli, executionTimeout, cmdStr, privateData);
+    CommandUtil.checkCommandFailed(cmdStr, res, logErrorsInDebug);
+    Loggers.VCS.debug("Command " + cmdStr + " output:\n" + res.getStdout());
+    return res;
+  }
+
+  private static CommandResult run(@NotNull final GeneralCommandLine cli, final int executionTimeout, @NotNull final String cmdStr, @NotNull final Set<String> privateData) {
     final long start = System.currentTimeMillis();
     ExecResult res = SimpleCommandLineProcessRunner.runCommand(cli, null, new SimpleCommandLineProcessRunner.RunCommandEventsAdapter() {
       @Override
@@ -116,17 +123,7 @@
         Loggers.VCS.debug("Command " + cmdStr + " took " + duration + "ms");
       }
     });
-
-    removePrivateData(privateData, res);
-
-    CommandUtil.checkCommandFailed(cmdStr, res, logErrorsInDebug);
-    Loggers.VCS.debug("Command " + cmdStr + " output:\n" + res.getStdout());
-    return res;
-  }
-
-  private static void removePrivateData(final Set<String> privateData, final ExecResult res) {
-    res.setStdout(removePrivateData(res.getStdout(), privateData));
-    res.setStderr(removePrivateData(res.getStderr(), privateData));
+    return new CommandResult(res, privateData);
   }
 
   public static String removePrivateData(final String str, final Set<String> privateData) {
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -16,7 +16,6 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 
@@ -55,7 +54,7 @@
       cli.addParameter("--rev");
       cli.addParameter(myChangeSet.getId());
     }
-    ExecResult res = runCommand(cli);
+    CommandResult res = runCommand(cli);
     failIfNotEmptyStdErr(cli, res);
     return res.getStdout();
   }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -17,7 +17,6 @@
 
 import com.intellij.execution.configurations.GeneralCommandLine;
 import com.intellij.openapi.util.JDOMUtil;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jdom.Document;
 import org.jdom.Element;
@@ -80,7 +79,7 @@
       cli.addParameter("--limit");
       cli.addParameter(myLimit.toString());
     }
-    ExecResult res = runCommand(cli);
+    CommandResult res = runCommand(cli);
     try {
       return parseChangeSetsXml(res.getStdout());
     } catch (Exception e) {
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/PushCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/PushCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -16,7 +16,6 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 
@@ -43,7 +42,7 @@
       cli.addParameter("-f");
     }
     cli.addParameter(getSettings().getRepositoryUrl());
-    ExecResult res = runCommand(cli);
+    CommandResult res = runCommand(cli);
     failIfNotEmptyStdErr(cli, res);
   }
 }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/StatusCommand.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/StatusCommand.java	Wed Jan 11 12:50:45 2012 +0400
@@ -16,7 +16,6 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 
@@ -49,7 +48,7 @@
     String to = myToId;
     if (to == null) to = "0";
     cli.addParameter(from + ":" + to);
-    ExecResult res = runCommand(cli);
+    CommandResult res = runCommand(cli);
     return parseFiles(res.getStdout());
   }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/CommandResultTest.java	Wed Jan 11 12:50:45 2012 +0400
@@ -0,0 +1,41 @@
+package jetbrains.buildServer.buildTriggers.vcs.mercurial;
+
+import jetbrains.buildServer.ExecResult;
+import jetbrains.buildServer.StreamGobbler;
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandResult;
+import org.jetbrains.annotations.NotNull;
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayInputStream;
+import java.util.Collections;
+
+import static org.testng.AssertJUnit.assertFalse;
+
+/**
+ * @author dmitry.neverov
+ */
+@Test
+public class CommandResultTest {
+
+  public void output_should_not_contain_private_data() {
+    String password = "pass";
+    ExecResult result = createExecResult(password, password);
+    CommandResult commandResult = new CommandResult(result, Collections.singleton(password));
+    assertFalse(commandResult.getStdout().contains(password));
+    assertFalse(commandResult.getStderr().contains(password));
+  }
+
+  private ExecResult createExecResult(@NotNull final String output, @NotNull final String error) {
+    ExecResult result = new ExecResult();
+    result.setOutputGobbler(createStringGobbler(output));
+    result.setErrorGobbler(createStringGobbler(error));
+    return result;
+  }
+
+  private StreamGobbler createStringGobbler(@NotNull final String str) {
+    StreamGobbler gobbler = new StreamGobbler(new ByteArrayInputStream(str.getBytes()));
+    gobbler.start();
+    return gobbler;
+  }
+
+}
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java	Wed Jan 11 12:50:45 2012 +0400
@@ -16,7 +16,7 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial;
 
 import com.intellij.execution.configurations.GeneralCommandLine;
-import jetbrains.buildServer.ExecResult;
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandResult;
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommandUtil;
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.Settings;
 import jetbrains.buildServer.serverSide.BuildServerListener;
@@ -240,7 +240,7 @@
     cli.setExePath(vcsRoot.getProperty(Constants.HG_COMMAND_PATH_PROP));
     cli.setWorkDirectory(vcsRoot.getProperty(Constants.REPOSITORY_PROP));
     cli.addParameter("tags");
-    ExecResult res = CommandUtil.runCommand(cli);
+    CommandResult res = CommandUtil.runCommand(cli);
     assertTrue(res.getStdout().contains("new_tag"));
     assertTrue(res.getStdout().contains("1:1d446e82d356"));
   }
@@ -257,7 +257,7 @@
     cli.setExePath(vcsRoot.getProperty(Constants.HG_COMMAND_PATH_PROP));
     cli.setWorkDirectory(vcsRoot.getProperty(Constants.REPOSITORY_PROP));
     cli.addParameter("tags");
-    ExecResult res = CommandUtil.runCommand(cli);
+    CommandResult res = CommandUtil.runCommand(cli);
     assertTrue(res.getStdout().contains("branch_tag"));
     assertTrue(res.getStdout().contains("7:376dcf05cd2a"));
   }
--- a/mercurial-tests/src/testng.xml	Tue Jan 10 12:49:29 2012 +0400
+++ b/mercurial-tests/src/testng.xml	Wed Jan 11 12:50:45 2012 +0400
@@ -12,6 +12,7 @@
       <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.AgentSideCheckoutTest"/>
       <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.SettingsTest"/>
       <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.MirrorManagerTest"/>
+      <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.CommandResultTest"/>
     </classes>
   </test>
 </suite>