Skip to content

Commit

Permalink
JENKINS-41801: Allow empty build parameters to be replaced in ant pro…
Browse files Browse the repository at this point in the history
…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 c57e18f
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 @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c57e18f

Please sign in to comment.