Skip to content

WindowsUtils.kill() fix #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions java/client/src/org/openqa/selenium/os/WindowsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,22 @@ public static void kill(String[] cmdarray) throws Exception {
* quote (\"?)
*/
// TODO We should be careful, in case Windows has ~1-ified the executable name as well
pattern.append("\"?.*?\\\\");
pattern.append(executable.getName());
pattern.append("(\"?.*?\\\\)?");
String execName = executable.getName();
pattern.append(execName);
if (!execName.endsWith(".exe")) {
pattern.append("(\\.exe)?");
}
pattern.append("\"?");
for (String arg : cmdarray) {
for (int i = 1; i < cmdarray.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this from a foreach? I can't see it gaining anything here and code styles shouldn't change just for the sake of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason was to iterate from 1 instead of 0, see the commit comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I completely missed that. Thanks. I assumed I was missing something, but I couldn't figure out what.

/*
* There may be a space, but maybe not (\\s?), may be a quote or maybe not (\"?), but then
* turn on block quotation (as if *everything* had a regex backslash in front of it) with \Q.
* Then look for the next argument (which may have ?s, \s, "s, who knows), turning off block
* quotation. Now ignore a final quote if any (\"?)
*/
pattern.append("\\s?\"?\\Q");
pattern.append(arg);
pattern.append(cmdarray[i]);
pattern.append("\\E\"?");
}
pattern.append("\\s*");
Expand Down
14 changes: 14 additions & 0 deletions java/client/test/org/openqa/selenium/os/WindowsUtilsUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -73,6 +74,19 @@ public void testTaskKill() {
assertFalse("taskkill should be found", "taskkill".equals(WindowsUtils.findTaskKill()));
}

private void tryKill(String[] cmd) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use a vararg

CommandLine cl = new CommandLine(cmd);
cl.executeAsync();
WindowsUtils.kill(cmd);
assertFalse("Should be able to kill " + Arrays.toString(cmd), cl.isRunning());
}

@Test
public void testKill() throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing guard against OS not being Windows

tryKill(new String[]{"sleep.exe", "10"});
tryKill(new String[]{"sleep", "10"});
}

@Test
public void testRegistry() {
if (!WindowsUtils.thisIsWindows()) return;
Expand Down