Skip to content
Permalink
Browse files

JENKINS-41801: Allow empty build parameters to be replaced in ant pro…

…perties.

By using EnvVars.overrideAll() to add all build variable to EnvVars, any blank ("") build parameters are not added (or if they existed would be removed) from EnvVars.    This means any reference to a blank variable is not replaced instead of being set to empty.

The tests check the log line where the ant command line is printed.  It's needed to check here for reliable tests, because on unix the $parameter will be replaced by the shell, but not in windows.   By testing the command line params we ensure the replacements occur before going to the OS to run ant.

The tests try to be resilient to the command line and param formatting on windows.
  • Loading branch information
mattdoran committed Feb 7, 2017
1 parent e9815c3 commit c57e18f6ac9c01ccce927930aed0221853d38ff6
Showing with 58 additions and 2 deletions.
  1. +12 −2 src/main/java/hudson/tasks/Ant.java
  2. +46 −0 src/test/java/hudson/tasks/AntTest.java
@@ -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;

@@ -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");
@@ -31,13 +31,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;
@@ -48,6 +51,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.*;
@@ -322,6 +326,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)

0 comments on commit c57e18f

Please sign in to comment.
You can’t perform that action at this time.