Skip to content

Commit

Permalink
Fix JENKINS-29816
Browse files Browse the repository at this point in the history
Add extra $ automatically before calling the replaceMacro and environment variable expander.
  • Loading branch information
slide committed Nov 1, 2015
1 parent 82f329f commit 17a2d83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Expand Up @@ -234,9 +234,12 @@ public static String expandAll(AbstractBuild<?,?> context, TaskListener listener
public static String expandAll(AbstractBuild<?,?> context, TaskListener listener, String stringWithMacro, boolean throwException, List<TokenMacro> privateTokens) throws MacroEvaluationException, IOException, InterruptedException {
// Do nothing for an empty String
if (stringWithMacro==null || stringWithMacro.length()==0) return stringWithMacro;

// Expand environment variables
stringWithMacro = stringWithMacro.replaceAll("\\$\\$", "\\$\\$\\$\\$");
String s = context.getEnvironment(listener).expand(stringWithMacro);
// Expand build variables
s = s.replaceAll("\\$\\$", "\\$\\$\\$\\$");
s = Util.replaceMacro(s,context.getBuildVariableResolver());
// Expand Macros
s = expand(context,listener,s,throwException,privateTokens);
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/jenkinsci/plugins/tokenmacro/TokenMacroTest.java
Expand Up @@ -4,6 +4,7 @@
import hudson.model.*;
import hudson.util.StreamTaskListener;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.TestExtension;

import java.io.IOException;
Expand Down Expand Up @@ -90,6 +91,20 @@ public void testEscaped() throws Exception {
assertEquals("{abc=[def, ghi], jkl=[true]}$TEST_NESTED",TokenMacro.expand(b,TaskListener.NULL,"$TEST_NESTED$$TEST_NESTED"));
assertEquals("{abc=[def, ghi], jkl=[true]}${TEST_NESTED}",TokenMacro.expand(b,TaskListener.NULL,"$TEST_NESTED$${TEST_NESTED}"));
}

@Test
@Issue("JENKINS-29816")
public void testEscapedExpandAll() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("foo");
FreeStyleBuild b = p.scheduleBuild2(0).get();

assertEquals("${TEST_NESTED}",TokenMacro.expandAll(b, TaskListener.NULL, "$${TEST_NESTED}"));
assertEquals("$TEST_NESTED",TokenMacro.expandAll(b, TaskListener.NULL, "$$TEST_NESTED"));
assertEquals("$TEST_NESTED{abc=[def, ghi], jkl=[true]}",TokenMacro.expandAll(b, TaskListener.NULL, "$$TEST_NESTED$TEST_NESTED"));
assertEquals("${TEST_NESTED}{abc=[def, ghi], jkl=[true]}",TokenMacro.expandAll(b, TaskListener.NULL, "$${TEST_NESTED}$TEST_NESTED"));
assertEquals("{abc=[def, ghi], jkl=[true]}$TEST_NESTED",TokenMacro.expandAll(b, TaskListener.NULL, "$TEST_NESTED$$TEST_NESTED"));
assertEquals("{abc=[def, ghi], jkl=[true]}${TEST_NESTED}",TokenMacro.expandAll(b, TaskListener.NULL, "$TEST_NESTED$${TEST_NESTED}"));
}

@Test
public void testInvalidNoException() throws Exception {
Expand Down

0 comments on commit 17a2d83

Please sign in to comment.