Skip to content

Commit b0c2b26

Browse files
ZitraxAndreas Tolf Tolfsen
authored and
Andreas Tolf Tolfsen
committed
WindowsUtils.kill() fix
On Windows 8 the kill() function did not correctly locate the process in the list. There were two issues: * The executable name in the regexp was duplicated. This patch changed the iteration over the arguments to not include the executable again. * If the process was started without using .exe in the name kill() could not find it as the regexp did not have the .exe there. This patch adds .exe if not already present as an optional match. Signed-off-by: Andreas Tolf Tolfsen <[email protected]>
1 parent ef4c5e8 commit b0c2b26

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

java/client/src/org/openqa/selenium/os/WindowsUtils.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,22 @@ public static void kill(String[] cmdarray) throws Exception {
109109
* quote (\"?)
110110
*/
111111
// TODO We should be careful, in case Windows has ~1-ified the executable name as well
112-
pattern.append("\"?.*?\\\\");
113-
pattern.append(executable.getName());
112+
pattern.append("(\"?.*?\\\\)?");
113+
String execName = executable.getName();
114+
pattern.append(execName);
115+
if (!execName.endsWith(".exe")) {
116+
pattern.append("(\\.exe)?");
117+
}
114118
pattern.append("\"?");
115-
for (String arg : cmdarray) {
119+
for (int i = 1; i < cmdarray.length; i++) {
116120
/*
117121
* There may be a space, but maybe not (\\s?), may be a quote or maybe not (\"?), but then
118122
* turn on block quotation (as if *everything* had a regex backslash in front of it) with \Q.
119123
* Then look for the next argument (which may have ?s, \s, "s, who knows), turning off block
120124
* quotation. Now ignore a final quote if any (\"?)
121125
*/
122126
pattern.append("\\s?\"?\\Q");
123-
pattern.append(arg);
127+
pattern.append(cmdarray[i]);
124128
pattern.append("\\E\"?");
125129
}
126130
pattern.append("\\s*");

java/client/test/org/openqa/selenium/os/WindowsUtilsUnitTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.Before;
2626
import org.junit.Test;
2727

28+
import java.util.Arrays;
2829
import java.util.Properties;
2930
import java.util.regex.Matcher;
3031
import java.util.regex.Pattern;
@@ -73,6 +74,20 @@ public void testTaskKill() {
7374
assertFalse("taskkill should be found", "taskkill".equals(WindowsUtils.findTaskKill()));
7475
}
7576

77+
private void tryKill(String... cmd) throws Exception {
78+
CommandLine cl = new CommandLine(cmd);
79+
cl.executeAsync();
80+
WindowsUtils.kill(cmd);
81+
assertFalse("Should be able to kill " + Arrays.toString(cmd), cl.isRunning());
82+
}
83+
84+
@Test
85+
public void testKill() throws Exception {
86+
if (!WindowsUtils.thisIsWindows()) return;
87+
tryKill(new String[]{"sleep.exe", "10"});
88+
tryKill(new String[]{"sleep", "10"});
89+
}
90+
7691
@Test
7792
public void testRegistry() {
7893
if (!WindowsUtils.thisIsWindows()) return;

0 commit comments

Comments
 (0)