From 40420a048fc541cb3ed5e4fd3303b79b0abec213 Mon Sep 17 00:00:00 2001 From: Sergey Simonchik Date: Mon, 26 May 2014 12:34:14 +0400 Subject: [PATCH] quote parameters if win command processor launched with extra switches (e.g. /D) --- .../execution/GeneralCommandLineTest.java | 9 +++++++++ .../com/intellij/execution/CommandLineUtil.java | 15 ++++++++------- .../rt/execution/junit/ProcessBuilder.java | 13 +++++++------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/execution/GeneralCommandLineTest.java b/platform/platform-tests/testSrc/com/intellij/execution/GeneralCommandLineTest.java index 50e75ca49cda..131fcf6e41cc 100644 --- a/platform/platform-tests/testSrc/com/intellij/execution/GeneralCommandLineTest.java +++ b/platform/platform-tests/testSrc/com/intellij/execution/GeneralCommandLineTest.java @@ -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(); diff --git a/platform/util/src/com/intellij/execution/CommandLineUtil.java b/platform/util/src/com/intellij/execution/CommandLineUtil.java index b5ddac8970a3..15c2c8605e2d 100644 --- a/platform/util/src/com/intellij/execution/CommandLineUtil.java +++ b/platform/util/src/com/intellij/execution/CommandLineUtil.java @@ -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 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) { diff --git a/plugins/junit_rt/src/com/intellij/rt/execution/junit/ProcessBuilder.java b/plugins/junit_rt/src/com/intellij/rt/execution/junit/ProcessBuilder.java index a23b3e32ca43..79d420552da1 100644 --- a/plugins/junit_rt/src/com/intellij/rt/execution/junit/ProcessBuilder.java +++ b/plugins/junit_rt/src/com/intellij/rt/execution/junit/ProcessBuilder.java @@ -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) {