Skip to content

Commit

Permalink
Merge pull request #25 from mattdoran/master
Browse files Browse the repository at this point in the history
JENKINS-41801: Allow empty build parameters to be replaced in ant properties
  • Loading branch information
armfergom committed May 9, 2017
2 parents d1c5e13 + c57e18f commit 7025e65
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/hudson/tasks/Ant.java
Expand Up @@ -65,6 +65,7 @@
import java.util.Arrays;
import java.util.Properties;
import java.util.List;
import java.util.Map;
import java.util.Collections;
import java.util.Set;

Expand Down Expand Up @@ -146,8 +147,17 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
ArgumentListBuilder args = new ArgumentListBuilder();

EnvVars env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables());


// Allow empty build parameters to be used in property replacements.
// The env.override/overrideAll methods remove the propery if it's an empty string.
for (Map.Entry<String, String> e : build.getBuildVariables().entrySet()) {
if (e.getValue() != null && e.getValue().length() == 0) {
env.put(e.getKey(), e.getValue());
} else {
env.override(e.getKey(), e.getValue());
}
}

AntInstallation ai = getAnt();
if(ai==null) {
args.add(launcher.isUnix() ? "ant" : "ant.bat");
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/hudson/tasks/AntTest.java
Expand Up @@ -32,13 +32,16 @@
import hudson.matrix.AxisList;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.Cause;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.PasswordParameterDefinition;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.tasks.Ant.AntInstallation;
import hudson.tasks.Ant.AntInstallation.DescriptorImpl;
import hudson.tasks.Ant.AntInstaller;
Expand All @@ -49,6 +52,7 @@
import hudson.util.VersionNumber;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import jenkins.model.Jenkins;
import org.apache.commons.lang.SystemUtils;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -329,6 +333,48 @@ public void emptyParameterTest() throws Exception {
assertEquals(Result.SUCCESS, build.getResult());
}

@Test
public void propertyReplacedByVariable() throws Exception {
testVariableReplaced("x");
}

@Test
@Issue("JENKINS-41801")
public void propertyReplacedByEmptyBuildParameter() throws Exception {
testVariableReplaced("");
}

private void testVariableReplaced(String variableValue) throws Exception {
FreeStyleProject project = createSimpleAntProject("", null, "build-properties.xml", "testProperty=$variable");

FreeStyleBuild build = project.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("variable", variableValue))).get();

assertEquals(Result.SUCCESS, build.getResult());
List<String> logs = build.getLog(Integer.MAX_VALUE);

// Find ant command line in logs
String commandLineLine = null;
for (int i = 0; i < logs.size(); i++) {
String line = logs.get(i);

if (line.contains("ant") && line.contains("build-properties.xml")
&& line.contains("-DtestProperty=")) {
commandLineLine = line;
break;
}
}

assertNotNull("Unable to find ant command line", commandLineLine);

// space so we can look for empty value
commandLineLine += " ";

// Check value is expected (with and without quotes)
assertTrue("-DtestProperty param not '" + variableValue + "': " + commandLineLine,
commandLineLine.contains("-DtestProperty=\"" + variableValue + "\" ") || commandLineLine.contains("-DtestProperty=" + variableValue + " "));
}

/**
* Creates a FreeStyleProject with an Ant build step with parameters passed as parameter.
* The Project to use is always the same (sample-helloworld-ant.zip)
Expand Down

0 comments on commit 7025e65

Please sign in to comment.