Skip to content

Commit

Permalink
Merge pull request #7 from tyrius02/master
Browse files Browse the repository at this point in the history
[FIXED JENKINS-17876] Use QuotedStringTokenizer when constructing msbuild command-line arguments
  • Loading branch information
gboissinot committed Jun 25, 2013
2 parents dbf9ee7 + 9a35332 commit 0ae0e87
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.410</version>
<version>1.420</version>
</parent>

<artifactId>msbuild</artifactId>
Expand Down Expand Up @@ -52,7 +52,6 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

</project>


23 changes: 15 additions & 8 deletions src/main/java/hudson/plugins/msbuild/MsBuildBuilder.java
Expand Up @@ -6,6 +6,7 @@
import hudson.tasks.Builder;
import hudson.tools.ToolInstallation;
import hudson.util.ArgumentListBuilder;
import hudson.util.QuotedStringTokenizer;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
Expand Down Expand Up @@ -72,7 +73,8 @@ public boolean getContinueOnBuildFailure() {
}

public MsBuildInstallation getMsBuild() {
for (MsBuildInstallation i : DESCRIPTOR.getInstallations()) {
DescriptorImpl descriptor = (DescriptorImpl)getDescriptor();
for (MsBuildInstallation i : descriptor.getInstallations()) {
if (msBuildName != null && i.getName().equals(msBuildName))
return i;
}
Expand Down Expand Up @@ -110,7 +112,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
args.add(pathToMsBuild);

if (ai.getDefaultArgs() != null) {
args.addTokenized(ai.getDefaultArgs());
args.add(tokenizeArgs(ai.getDefaultArgs()));
}
}

Expand All @@ -120,7 +122,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
normalizedArgs = Util.replaceMacro(normalizedArgs, build.getBuildVariables());

if (normalizedArgs.trim().length() > 0)
args.addTokenized(normalizedArgs);
args.add(tokenizeArgs(normalizedArgs));

//Build /P:key1=value1;key2=value2 ...
Map<String, String> variables = build.getBuildVariables();
Expand Down Expand Up @@ -174,20 +176,25 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

@Override
public Descriptor<Builder> getDescriptor() {
return DESCRIPTOR;
return (DescriptorImpl)super.getDescriptor();
}

/**
* Descriptor should be singleton.
* Tokenize a set of arguments, preserving quotes.
* @param args
* @return
*/
@Extension
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
static String[] tokenizeArgs(String args) {
QuotedStringTokenizer tokenizer = new QuotedStringTokenizer(args, " \t\n\r\f", false, true);
return tokenizer.toArray();
}

@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
@CopyOnWrite
private volatile MsBuildInstallation[] installations = new MsBuildInstallation[0];

DescriptorImpl() {
public DescriptorImpl() {
super(MsBuildBuilder.class);
load();
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/hudson/plugins/msbuild/MsBuildBuilderTest.java
@@ -0,0 +1,32 @@
package hudson.plugins.msbuild;

import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author Jonathan Zimmerman
*/
public class MsBuildBuilderTest {

@Test
public void shouldRetainQuotedArguments() {
final String platform = "/p:Platform=\"Any CPU\"";

String[] tokenizedArgs = MsBuildBuilder.tokenizeArgs(platform);
assertNotNull(tokenizedArgs);
assertEquals(1, tokenizedArgs.length);
assertEquals(platform, tokenizedArgs[0]);
}

@Test
public void shouldSplitArguments() {
final String arguments = "/t:Build /p:Configuration=Debug";

String[] tokenizedArgs = MsBuildBuilder.tokenizeArgs(arguments);
assertNotNull(tokenizedArgs);
assertEquals(2, tokenizedArgs.length);
assertEquals("/t:Build", tokenizedArgs[0]);
assertEquals("/p:Configuration=Debug", tokenizedArgs[1]);
}
}

0 comments on commit 0ae0e87

Please sign in to comment.