view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTest.java @ 227:061486453bd9 remote-run/dmitry.neverov/escaping

Escape quotes in command line parameters
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Thu, 14 Apr 2011 14:39:31 +0400
parents
children 26febf4e58a2
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;

import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.util.SystemInfo;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.Constants;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.Util;
import jetbrains.buildServer.vcs.impl.VcsRootImpl;
import junit.framework.TestCase;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author dmitry.neverov
 */
@Test
public class BaseCommandTest extends TestCase {

  public void should_quote_command_line_arguments() throws IOException {
    VcsRootImpl root = new VcsRootImpl(1, "rootForTest");
    root.addProperty(Constants.REPOSITORY_PROP, "http://some.org/repo.hg");
    root.addProperty(Constants.HG_COMMAND_PATH_PROP, Util.getHgPath());
    File workingDir = new File("some dir");
    Settings settings = new Settings(root);

    BaseCommand command = new BaseCommand(settings, workingDir);
    GeneralCommandLine cl = command.createCommandLine();
    cl.addParameter("param with spaces");
    cl.addParameter("param with quote \" rm -rf /");
    List<String> params = cl.getParametersList().getList();
    if (SystemInfo.isWindows) {
      assertEquals("\"param with spaces\"", params.get(0));
      assertEquals("\"param with quote \\\" rm -rf /\"", params.get(1));
    } else {
      assertEquals("param with spaces", params.get(0));
      assertEquals("param with quote \" rm -rf /", params.get(1));
    }
  }

}