Skip to content

Commit 891c3b2

Browse files
committed
Add tests.
1 parent b1401dd commit 891c3b2

File tree

4 files changed

+81
-17
lines changed

4 files changed

+81
-17
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<classpath>
2+
<classpathentry kind="src" path="src/test/java" including="**/*.java"/>
23
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
34
<classpathentry kind="src" path="target/generated-sources/localizer" including="**/*.java"/>
45
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<version>1.0-SNAPSHOT</version>
1212
<packaging>hpi</packaging>
1313

14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
1418
<repositories>
1519
<repository>
1620
<snapshots>

src/main/java/com/lookout/jenkins/EnvironmentScript.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.lookout.jenkins;
2-
import hudson.EnvVars;
32
import hudson.Extension;
43
import hudson.FilePath;
54
import hudson.Launcher;
@@ -13,9 +12,8 @@
1312

1413
import java.io.ByteArrayOutputStream;
1514
import java.io.IOException;
16-
import java.util.AbstractMap;
17-
import java.util.ArrayList;
1815
import java.util.Arrays;
16+
import java.util.HashMap;
1917
import java.util.List;
2018
import java.util.Map;
2119

@@ -24,14 +22,13 @@
2422
import org.kohsuke.stapler.DataBoundConstructor;
2523

2624
/**
27-
*
25+
* Runs a specific chunk of code before each build, parsing output for new environment variables.
2826
*
2927
* @author Jørgen P. Tjernø
3028
*/
3129
public class EnvironmentScript extends BuildWrapper {
3230
private final String script;
3331

34-
// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
3532
@DataBoundConstructor
3633
public EnvironmentScript(String script) {
3734
this.script = script;
@@ -48,7 +45,7 @@ public String getScript() {
4845
@Override
4946
public Environment setUp(AbstractBuild build,
5047
final Launcher launcher,
51-
BuildListener listener) throws IOException, InterruptedException {
48+
final BuildListener listener) throws IOException, InterruptedException {
5249

5350
// First we create the script in a temporary directory.
5451
FilePath ws = build.getWorkspace();
@@ -77,9 +74,10 @@ public Environment setUp(AbstractBuild build,
7774
return null;
7875
}
7976

77+
8078
// Then we parse the variables out of it. We could use java.util.Properties, but it doesn't order the properties, so expanding variables with previous variables (like a shell script expects) doesn't work.
8179
String[] lines = commandOutput.toString().split("(\n|\r\n)");
82-
final List<Map.Entry<String, String>> injectedVariables = new ArrayList<Map.Entry<String, String>>(lines.length);
80+
final Map<String, String> envAdditions = new HashMap<String, String>(lines.length);
8381
for (String line : lines)
8482
{
8583
if (line.trim().isEmpty()) {
@@ -90,29 +88,23 @@ public Environment setUp(AbstractBuild build,
9088
if (keyAndValue.length < 2) {
9189
listener.error("[environment-script] Invalid line encountered, ignoring: " + line);
9290
} else {
93-
Map.Entry<String, String> entry = new AbstractMap.SimpleEntry<String, String>(keyAndValue[0], keyAndValue[1]);
94-
listener.getLogger().println("[environment-script] Adding variable '" + entry.getKey() + "' with value '" + entry.getValue() + "'");
95-
injectedVariables.add(entry);
91+
listener.getLogger().println("[environment-script] Adding variable '" + keyAndValue[0] + "' with value '" + keyAndValue[1] + "'");
92+
envAdditions.put(keyAndValue[0], keyAndValue[1]);
9693
}
9794
}
9895

9996
return new Environment() {
10097
@Override
10198
public void buildEnvVars(Map<String, String> env) {
102-
// Here we evaluate the variables we parsed above, in order.
103-
// We expand against all the environment variables we have so far.
104-
EnvVars vars = new EnvVars(env);
105-
for (Map.Entry<String, String> variable : injectedVariables) {
106-
env.put(variable.getKey(), vars.expand(variable.getValue()));
107-
}
99+
env.putAll(envAdditions);
108100
}
109101
};
110102
}
111103

112104
// Mostly stolen from hudson.tasks.Shell.buildCommandLine.
113105
public String[] buildCommandLine(FilePath scriptFile) {
114106
// Respect shebangs
115-
if(script.startsWith("#!")) {
107+
if (script.startsWith("#!")) {
116108
// Find first line, or just entire script if it's one line.
117109
int end = script.indexOf('\n');
118110
if (end < 0)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.lookout.jenkins;
2+
3+
import hudson.EnvVars;
4+
import hudson.model.Build;
5+
import hudson.model.Project;
6+
7+
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
8+
import org.jvnet.hudson.test.HudsonTestCase;
9+
import org.jvnet.hudson.test.SingleFileSCM;
10+
11+
public class EnvironmentScriptTest extends HudsonTestCase {
12+
class TestJob {
13+
public Project<?,?> project;
14+
public CaptureEnvironmentBuilder builder;
15+
16+
public TestJob (String script) throws Exception {
17+
project = createFreeStyleProject();
18+
builder = new CaptureEnvironmentBuilder();
19+
project.getBuildersList().add(builder);
20+
project.getBuildWrappersList().add(new EnvironmentScript(script));
21+
}
22+
}
23+
24+
public void testWithEmptyScript () throws Exception {
25+
TestJob job = new TestJob("");
26+
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
27+
}
28+
29+
final String SCRIPT_SIMPLE_VARIABLES =
30+
"echo var1=one\n"
31+
+ "echo var2=two\n"
32+
+ "echo var3=three";
33+
public void testWithSimpleVariables () throws Exception {
34+
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES);
35+
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
36+
37+
EnvVars vars = job.builder.getEnvVars();
38+
assertEquals("one", vars.get("var1"));
39+
assertEquals("two", vars.get("var2"));
40+
assertEquals("three", vars.get("var3"));
41+
}
42+
43+
final String SCRIPT_DEPENDENT_VARIABLES =
44+
"echo var1=one\n"
45+
+ "echo var2='$var1 two'\n"
46+
+ "echo var3='yo $var4'\n"
47+
+ "echo var4='three ${var2}'";
48+
public void testWithDependentVariables () throws Exception {
49+
TestJob job = new TestJob(SCRIPT_DEPENDENT_VARIABLES);
50+
Build<?, ?> b = assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
51+
System.out.println(b.getLog());
52+
53+
EnvVars vars = job.builder.getEnvVars();
54+
assertEquals("one", vars.get("var1"));
55+
assertEquals("one two", vars.get("var2"));
56+
assertEquals("yo three ${var2}", vars.get("var3"));
57+
assertEquals("three one two", vars.get("var4"));
58+
}
59+
60+
public void testReadingFileFromSCM () throws Exception {
61+
TestJob job = new TestJob("cat envs");
62+
job.project.setScm(new SingleFileSCM("envs", "foo_var=bar"));
63+
64+
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
65+
assertEquals("bar", job.builder.getEnvVars().get("foo_var"));
66+
}
67+
}

0 commit comments

Comments
 (0)