Skip to content

Commit

Permalink
Fix JENKINS-42698
Browse files Browse the repository at this point in the history
  • Loading branch information
slide committed Apr 4, 2017
1 parent 582865c commit 8fe0f66
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Expand Up @@ -9,6 +9,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

Expand All @@ -23,6 +25,9 @@ public class EnvironmentVariableMacro extends DataBoundTokenMacro {
@Parameter(required=true)
public String var = "";

@Parameter(alias="default")
public String def = "";

@Override
public List<String> getAcceptedMacroNames() {
return Collections.singletonList(MACRO_NAME);
Expand All @@ -40,6 +45,8 @@ public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener,
Map<String, String> env = run.getEnvironment(listener);
if(env.containsKey(var)){
return env.get(var);
} else if(StringUtils.isNotBlank(def)) {
return def;
}
return "";
}
Expand Down
Expand Up @@ -24,4 +24,18 @@ public void testEnvironmentVariableExpansion() throws Exception {
FreeStyleBuild b = p.scheduleBuild2(0).get();
assertEquals("foo",TokenMacro.expand(b, StreamTaskListener.fromStdout(),"${ENV,var=\"JOB_NAME\"}"));
}

@Test
public void testEnvironmentVariableExpansionDefault() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("foo");
FreeStyleBuild b = p.scheduleBuild2(0).get();
assertEquals("You got the default",TokenMacro.expand(b, StreamTaskListener.fromStdout(),"${ENV,var=\"JORB_NAME\", default=\"You got the default\"}"));
}

@Test
public void testEnvironmentVariableExpansionMissingNoDefault() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("foo");
FreeStyleBuild b = p.scheduleBuild2(0).get();
assertEquals("",TokenMacro.expand(b, StreamTaskListener.fromStdout(),"${ENV,var=\"JORB_NAME\"}"));
}
}

0 comments on commit 8fe0f66

Please sign in to comment.