Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-14754] Shebang fix.
This fixes an exception when using shebangs, and also adds a test for
this functionality.
  • Loading branch information
jorgenpt committed Aug 9, 2012
1 parent af3bf32 commit de70895
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/lookout/jenkins/EnvironmentScript.java
Expand Up @@ -20,6 +20,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -123,7 +124,7 @@ private Environment generateEnvironment(AbstractBuild<?, ?> build,
try {
properties.load(propertiesInput);
} catch (IOException e) {
Util.displayIOException(e,listener);
Util.displayIOException(e, listener);
e.printStackTrace(listener.fatalError(Messages.EnvironmentScriptWrapper_UnableToParseScriptOutput()));
return null;
}
Expand Down Expand Up @@ -166,7 +167,7 @@ public String[] buildCommandLine(FilePath scriptFile) {
String shell = script.substring(0, end).trim();
shell = shell.substring(2);

List<String> args = Arrays.asList(Util.tokenize(shell));
List<String> args = new ArrayList<String>(Arrays.asList(Util.tokenize(shell)));
args.add(scriptFile.getRemote());

return args.toArray(new String[args.size()]);
Expand Down
44 changes: 31 additions & 13 deletions src/test/java/com/lookout/jenkins/EnvironmentScriptTest.java
@@ -1,10 +1,13 @@
package com.lookout.jenkins;

import javax.annotation.RegEx;

import hudson.EnvVars;
import hudson.model.Build;
import hudson.model.Project;

import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.For;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.SingleFileSCM;

Expand All @@ -21,15 +24,31 @@ public TestJob (String script) throws Exception {
}
}

final static String SCRIPT_SIMPLE_VARIABLES =
"echo var1=one\n"
+ "echo var2=two\n"
+ "echo var3=three";

final static String SCRIPT_DEPENDENT_VARIABLES =
"echo var1=one\n"
+ "echo var2='$var1 two'\n"
+ "echo var3='yo $var4'\n"
+ "echo var4='three ${var2}'";

final static String SCRIPT_OVERRIDDEN_VARIABLES =
"echo var1=one\n"
+ "echo var1+something='not one'\n"
+ "echo var2+something='two'";

final static String SCRIPT_SHEBANG =
"#!/bin/cat\n"
+ "hello=world";

public void testWithEmptyScript () throws Exception {
TestJob job = new TestJob("");
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
}

final static String SCRIPT_SIMPLE_VARIABLES =
"echo var1=one\n"
+ "echo var2=two\n"
+ "echo var3=three";
public void testWithSimpleVariables () throws Exception {
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES);
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
Expand All @@ -40,11 +59,6 @@ public void testWithSimpleVariables () throws Exception {
assertEquals("three", vars.get("var3"));
}

final static String SCRIPT_DEPENDENT_VARIABLES =
"echo var1=one\n"
+ "echo var2='$var1 two'\n"
+ "echo var3='yo $var4'\n"
+ "echo var4='three ${var2}'";
public void testWithDependentVariables () throws Exception {
TestJob job = new TestJob(SCRIPT_DEPENDENT_VARIABLES);
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
Expand All @@ -56,10 +70,6 @@ public void testWithDependentVariables () throws Exception {
assertEquals("three one two", vars.get("var4"));
}

final static String SCRIPT_OVERRIDDEN_VARIABLES =
"echo var1=one\n"
+ "echo var1+something='not one'\n"
+ "echo var2+something='two'";
public void testWithOverridenVariables () throws Exception {
TestJob job = new TestJob(SCRIPT_OVERRIDDEN_VARIABLES);
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());
Expand All @@ -84,4 +94,12 @@ public void testWorkingDirectory () throws Exception {
// Make sure that the $PWD of the script is $WORKSPACE.
assertTrue(build.getWorkspace().child("was_run").exists());
}

public void testWithShebang () throws Exception {
TestJob job = new TestJob(SCRIPT_SHEBANG);
assertBuildStatusSuccess(job.project.scheduleBuild2(0).get());

EnvVars vars = job.builder.getEnvVars();
assertEquals("world", vars.get("hello"));
}
}

0 comments on commit de70895

Please sign in to comment.