Skip to content

Commit

Permalink
[FIXED JENKINS-24870] Use placeholder for executable when parsing par…
Browse files Browse the repository at this point in the history
…ameters
  • Loading branch information
vjuranek committed Sep 29, 2014
1 parent dfb64b9 commit a551555
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/main/java/hudson/plugins/groovy/Groovy.java
Expand Up @@ -316,12 +316,13 @@ protected List<String> buildCommandLine(AbstractBuild build, BuildListener liste
*
*/
private String[] parseParams(String line) {
CommandLine cmdLine = CommandLine.parse(line);
//JENKINS-24870 CommandLine.getExecutable tries to fix file separators, so if the first param contains slashes, it can cause problems
//Adding some placeholder instead of executable
CommandLine cmdLine = CommandLine.parse("executable_placeholder " + line);
String[] parsedArgs = cmdLine.getArguments();
String[] args = new String[parsedArgs.length + 1];
args[0] = cmdLine.getExecutable(); //as we pass only arguments, this is actually the first argument
String[] args = new String[parsedArgs.length];
if(parsedArgs.length > 0) {
System.arraycopy(parsedArgs, 0, args, 1, parsedArgs.length);
System.arraycopy(parsedArgs, 0, args, 0, parsedArgs.length);
}
return args;
}
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/hudson/plugins/groovy/PluginTest.java
@@ -1,14 +1,13 @@
package hudson.plugins.groovy;

import static org.junit.Assert.assertTrue;

import hudson.model.FreeStyleProject;
import hudson.tasks.Builder;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

Expand Down Expand Up @@ -62,6 +61,20 @@ public void roundtripTestGroovyStringScript() throws Exception {
j.assertEqualBeans(before, after, "groovyName,parameters,scriptParameters,properties,javaOpts,classPath");
j.assertEqualBeans(before.getScriptSource(), after.getScriptSource(), "command");
}

@Test
public void roundtripTestGroovyParams() throws Exception {
Groovy before = new Groovy(new StringScriptSource("println 'Test'"),"(Default)", "some 'param with spaces' and other params", "some other 'param with spaces' and other params", "some.property=true", "-Xmx1024m", "test.jar");
Groovy after = doRoundtrip(before, Groovy.class);
j.assertEqualBeans(before, after, "parameters,scriptParameters");
}

@Test
public void roundtripTestGroovyParamsSlashes() throws Exception {
Groovy before = new Groovy(new StringScriptSource("println 'Test'"),"(Default)", "some 'param with spaces' and http://slashes/and c:\\backslashes", "some other 'param with spaces' and http://slashes/and c:\\backslashes", "some.property=true", "-Xmx1024m", "test.jar");
Groovy after = doRoundtrip(before, Groovy.class);
j.assertEqualBeans(before, after, "parameters,scriptParameters");
}

private <T extends Builder> T doRoundtrip(T before, Class<T> clazz) throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
Expand Down

0 comments on commit a551555

Please sign in to comment.