From e41b55cdf90bac1da8dffc2e1e8620a44bb2ee0c Mon Sep 17 00:00:00 2001 From: mindless Date: Sun, 14 Nov 2010 02:36:52 +0000 Subject: [PATCH] [FIXED HUDSON-7657] While testing from command prompt requires escaping of ^&<>| with a ^ character, this is apparently not needed when cmd.exe /C is invoked from Hudson.. commented that part of toWindowsCommand() and updated javadoc/unit test. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@36903 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/util/ArgumentListBuilder.java | 11 +++++++---- .../java/hudson/util/ArgumentListBuilderTest.java | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/hudson/util/ArgumentListBuilder.java b/core/src/main/java/hudson/util/ArgumentListBuilder.java index 7753990318..247378cb43 100644 --- a/core/src/main/java/hudson/util/ArgumentListBuilder.java +++ b/core/src/main/java/hudson/util/ArgumentListBuilder.java @@ -286,15 +286,18 @@ public class ArgumentListBuilder implements Serializable { * This is done as follows: * Wrap arguments in double quotes if they contain any of: * space *?,;^&<>|" or % followed by a letter. - *
These characters are also prepended with a ^ character: ^&<>| + *
When testing from command prompt, these characters also need to be + * prepended with a ^ character: ^&<>| -- however, invoking cmd.exe from + * Hudson does not seem to require this extra escaping so it is not added by + * this method. *
A " is prepended with another " character. Note: Windows has issues * escaping some combinations of quotes and spaces. Quotes should be avoided. *
A % followed by a letter has that letter wrapped in double quotes, * to avoid possible variable expansion. ie, %foo% becomes "%"f"oo%". * The second % does not need special handling because it is not followed * by a letter.
- * Example: "-Dfoo=*abc?def;ghi^^jkl^&mno^stu^|vwx""yz%"e"nd" - * @return + * Example: "-Dfoo=*abc?def;ghi^jkl&mnostu|vwx""yz%"e"nd" + * @return new ArgumentListBuilder that runs given command through cmd.exe /C */ public ArgumentListBuilder toWindowsCommand() { StringBuilder quotedArgs = new StringBuilder(); @@ -308,7 +311,7 @@ public class ArgumentListBuilder implements Serializable { } else if (c == '^' || c == '&' || c == '<' || c == '>' || c == '|') { if (!quoted) quoted = startQuoting(quotedArgs, arg, i); - quotedArgs.append('^'); + // quotedArgs.append('^'); See note in javadoc above } else if (c == '"') { if (!quoted) quoted = startQuoting(quotedArgs, arg, i); diff --git a/core/src/test/java/hudson/util/ArgumentListBuilderTest.java b/core/src/test/java/hudson/util/ArgumentListBuilderTest.java index c586b2e914..0a6254af47 100644 --- a/core/src/test/java/hudson/util/ArgumentListBuilderTest.java +++ b/core/src/test/java/hudson/util/ArgumentListBuilderTest.java @@ -113,16 +113,16 @@ public class ArgumentListBuilderTest extends Assert { public void testToWindowsCommand() { ArgumentListBuilder builder = new ArgumentListBuilder( "ant.bat", "-Dfoo1=abc", // nothing special, no quotes - "-Dfoo2=foo bar", "-Dfoo3=/u*r", "-Dfoo4=/us?", // add quotes, no other escaping + "-Dfoo2=foo bar", "-Dfoo3=/u*r", "-Dfoo4=/us?", // add quotes "-Dfoo10=bar,baz", - "-Dfoo5=foo;bar^baz", "-Dfoo6=&here;", // add quotes and ^ escaping - "-Dfoo7=foo|bar\"baz", // add quotes, ^| for | and "" for " + "-Dfoo5=foo;bar^baz", "-Dfoo6=&here;", // add quotes + "-Dfoo7=foo|bar\"baz", // add quotes and "" for " "-Dfoo8=% %QED% %comspec% %-%(%.%", // add quotes, and extra quotes for %Q and %c "-Dfoo9=%'''%%@%"); // no quotes as none of the % are followed by a letter assertArrayEquals(new String[] { "cmd.exe", "/C", "\"ant.bat -Dfoo1=abc \"-Dfoo2=foo bar\"" - + " \"-Dfoo3=/u*r\" \"-Dfoo4=/us?\" \"-Dfoo10=bar,baz\" \"-Dfoo5=foo;bar^^baz\"" - + " \"-Dfoo6=^^&here;^\" \"-Dfoo7=foo^|bar\"\"baz\"" + + " \"-Dfoo3=/u*r\" \"-Dfoo4=/us?\" \"-Dfoo10=bar,baz\" \"-Dfoo5=foo;bar^baz\"" + + " \"-Dfoo6=&here;\" \"-Dfoo7=foo|bar\"\"baz\"" + " \"-Dfoo8=% %\"Q\"ED% %\"c\"omspec% %-%(%.%\"" + " -Dfoo9=%'''%%@% && exit %%ERRORLEVEL%%\"" }, builder.toWindowsCommand().toCommandArray());