quote parameters if win command processor launched with extra switches (e.g. /D)

This commit is contained in:
Sergey Simonchik
2014-05-26 12:34:14 +04:00
parent f587cf6706
commit 40420a048f
3 changed files with 24 additions and 13 deletions
@@ -183,6 +183,15 @@ public class GeneralCommandLineTest {
}
}
@Test
public void winShellQuotingWithExtraSwitch() throws Exception {
assumeTrue(SystemInfo.isWindows);
String param = "a&b";
GeneralCommandLine commandLine = new GeneralCommandLine("cmd", "/D", "/C", "echo", param);
String output = execAndGetOutput(commandLine, null);
assertEquals(StringUtil.wrapWithDoubleQuote(param), output.trim());
}
@Test
public void hackyEnvMap () throws Exception {
GeneralCommandLine commandLine = new GeneralCommandLine();
@@ -50,7 +50,7 @@ public class CommandLineUtil {
commandLine.add(FileUtilRt.toSystemDependentName(command, platform.fileSeparator));
boolean isWindows = platform == Platform.WINDOWS;
boolean winShell = isWindows && isWinShell(command, parameters);
boolean winShell = isWindows && isWinShell(command);
for (String parameter : parameters) {
if (isWindows) {
@@ -76,12 +76,13 @@ public class CommandLineUtil {
return commandLine;
}
private static boolean isWinShell(@NotNull String command, @NotNull List<String> parameters) {
if (command.endsWith(".cmd") || command.endsWith(".bat")) {
return true;
}
return ("cmd".equalsIgnoreCase(command) || "cmd.exe".equalsIgnoreCase(command)) &&
parameters.size() > 1 && "/c".equalsIgnoreCase(parameters.get(0));
private static boolean isWinShell(@NotNull String command) {
return endsWithIgnoreCase(command, ".cmd") || endsWithIgnoreCase(command, ".bat") ||
"cmd".equalsIgnoreCase(command) || "cmd.exe".equalsIgnoreCase(command);
}
private static boolean endsWithIgnoreCase(@NotNull String str, @NotNull String suffix) {
return str.regionMatches(true, str.length() - suffix.length(), suffix, 0, suffix.length());
}
private static String quote(String s, char ch) {
@@ -86,12 +86,13 @@ public class ProcessBuilder {
return Runtime.getRuntime().exec(commandLine, null, myWorkingDir);
}
private boolean isWinShell(String command) {
if (command.endsWith(".cmd") || command.endsWith(".bat")) {
return true;
}
return ("cmd".equalsIgnoreCase(command) || "cmd.exe".equalsIgnoreCase(command)) &&
myParameters.size() > 1 && "/c".equalsIgnoreCase(myParameters.get(0).toString());
private static boolean isWinShell(String command) {
return endsWithIgnoreCase(command, ".cmd") || endsWithIgnoreCase(command, ".bat") ||
"cmd".equalsIgnoreCase(command) || "cmd.exe".equalsIgnoreCase(command);
}
private static boolean endsWithIgnoreCase(String str, String suffix) {
return str.regionMatches(true, str.length() - suffix.length(), suffix, 0, suffix.length());
}
private static boolean containsAnyChar(String value, String chars) {