Skip to content

Commit

Permalink
Merge pull request #12 from synopsys-arc-oss/additional_variables_fix
Browse files Browse the repository at this point in the history
[FIXED JENKINS-19889] - Additional variables fix
  • Loading branch information
oleg-nenashev committed Nov 13, 2013
2 parents 99eaa76 + ed6e1e6 commit dcf9ac7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -8,6 +8,7 @@

<groupId>com.cloudbees.jenkins.plugins</groupId>
<artifactId>custom-tools-plugin</artifactId>
<name>Jenkins Custom Tools Plugin</name>
<version>0.5-SNAPSHOT</version>
<packaging>hpi</packaging>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Custom+Tools+Plugin</url>
Expand Down
Expand Up @@ -64,14 +64,16 @@ public class CustomTool extends ToolInstallation implements
private static final LabelSpecifics[] EMPTY_LABELS = new LabelSpecifics[0];
private transient String correctedHome;
private final ToolVersionConfig toolVersion;
private final String additionalVariables;

@DataBoundConstructor
public CustomTool(String name, String home, List properties,
String exportedPaths, LabelSpecifics[] labelSpecifics, ToolVersionConfig toolVersion) {
String exportedPaths, LabelSpecifics[] labelSpecifics, ToolVersionConfig toolVersion, String additionalVariables) {
super(name, home, properties);
this.exportedPaths = exportedPaths;
this.labelSpecifics = labelSpecifics;
this.toolVersion = toolVersion;
this.additionalVariables = additionalVariables;
}

public String getExportedPaths() {
Expand All @@ -98,27 +100,41 @@ public void correctHome(PathsList pathList) {
public LabelSpecifics[] getLabelSpecifics() {
return (labelSpecifics!=null) ? labelSpecifics : EMPTY_LABELS;
}

/**Check if the tool has additional variables set*/
public boolean hasAdditionalVariables() {
return additionalVariables != null;
}

public String getAdditionalVariables() {
return additionalVariables;
}

@Override
public CustomTool forEnvironment(EnvVars environment) {
return new CustomTool(getName(), environment.expand(getHome()),
getProperties().toList(), environment.expand(exportedPaths),
LabelSpecifics.substitute(getLabelSpecifics(), environment), toolVersion);
LabelSpecifics.substitute(getLabelSpecifics(), environment),
toolVersion, environment.expand(additionalVariables));
}

@Override
public CustomTool forNode(Node node, TaskListener log) throws IOException,
InterruptedException {
String substitutedPath = EnvStringParseHelper.resolveExportedPath(exportedPaths, node);
String substitutedHomeDir = EnvStringParseHelper.resolveExportedPath(translateFor(node, log), node);
String substitutedAdditionalVariables = EnvStringParseHelper.resolveExportedPath(additionalVariables, node);

return new CustomTool(getName(), substitutedHomeDir, getProperties().toList(),
substitutedPath, LabelSpecifics.substitute(getLabelSpecifics(), node), toolVersion);
substitutedPath, LabelSpecifics.substitute(getLabelSpecifics(), node),
toolVersion, substitutedAdditionalVariables);
}

//FIXME: just a stub
public CustomTool forBuildProperties(Map<JobPropertyDescriptor,JobProperty> properties) {
return new CustomTool(getName(), getHome(), getProperties().toList(), getExportedPaths(), getLabelSpecifics(), toolVersion);
return new CustomTool(getName(), getHome(), getProperties().toList(),
getExportedPaths(), getLabelSpecifics(),
toolVersion, getAdditionalVariables());
}

/**
Expand Down
Expand Up @@ -176,11 +176,17 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,
throw new AbortException(ex.getMessage());
}

// Prepare additional variables
// Handle global options of the tool
//TODO: convert to label specifics?
PathsList installedPaths = installed.getPaths(node);
installed.correctHome(installedPaths);
paths.add(installedPaths);
if (installed.hasAdditionalVariables()) {
additionalVarInjectors.add(
EnvVariablesInjector.Create(installed.getAdditionalVariables()));
}

// Handle label-specific options of the tool
for (LabelSpecifics spec : installed.getLabelSpecifics()) {
if (!spec.appliesTo(node)) {
continue;
Expand Down
Expand Up @@ -82,7 +82,7 @@ public static String resolveExportedPath(String exportedPaths, Node node) {
*/
public static String substituteNodeProperty(String macroString, NodeProperty<?> property) {
// Get environment variables
if (EnvironmentVariablesNodeProperty.class.equals(property.getClass())) {
if (property != null && property instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty prop = (EnvironmentVariablesNodeProperty)property;
return resolveExportedPath(macroString, prop.getEnvVars());
}
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.jvnet.hudson.test.HudsonTestCase;

import com.cloudbees.jenkins.plugins.customtools.CustomTool.DescriptorImpl;
import com.synopsys.arc.jenkinsci.plugins.customtools.LabelSpecifics;
import com.synopsys.arc.jenkinsci.plugins.customtools.multiconfig.MulticonfigWrapperOptions;
import com.synopsys.arc.jenkinsci.plugins.customtools.versions.ToolVersionConfig;

Expand Down Expand Up @@ -78,7 +79,26 @@ public void testBasicCase() throws Exception {
project.getBuildersList().add(b);
Future<FreeStyleBuild> build = project.scheduleBuild2(0);
assertBuildStatusSuccess(build);

}

// @Bug(19889). https://issues.jenkins-ci.org/browse/JENKINS-19889
//TODO: Just a stub for testing. Make the test automatic
public void testAdditionalVars() throws Exception {
hudson.setNumExecutors(0);
createSlave();
tools = hudson.getDescriptorByType(CustomTool.DescriptorImpl.class);
tools.setInstallations(createEnvPrinterTool("MyTrue", null, "TEST_ADD_VAR=test"));
FreeStyleProject project = createFreeStyleProject();
CustomToolInstallWrapper.SelectedTool selectedTool = new CustomToolInstallWrapper.SelectedTool("MyTrue");

CustomToolInstallWrapper wrapper = new CustomToolInstallWrapper(
new CustomToolInstallWrapper.SelectedTool[] { selectedTool }, MulticonfigWrapperOptions.DEFAULT, false);
project.getBuildWrappersList().add(wrapper);
Builder b = new Shell("env; mytrue");
project.getBuildersList().add(b);
Future<FreeStyleBuild> build = project.scheduleBuild2(0);
assertBuildStatusSuccess(build);
}

private CustomTool createTool(String name) throws IOException {
Expand All @@ -89,7 +109,19 @@ private CustomTool createTool(String name) throws IOException {
List<ToolProperty<ToolInstallation>> properties = new ArrayList<ToolProperty<ToolInstallation>>();
properties.add(new InstallSourceProperty(installers));

CustomTool installation = new CustomTool("MyTrue", null, properties, "./", null, ToolVersionConfig.DEFAULT);
CustomTool installation = new CustomTool("MyTrue", null, properties, "./", null, ToolVersionConfig.DEFAULT, null);
return installation;
}

//TODO: refactor and generalize
private CustomTool createEnvPrinterTool(String name, LabelSpecifics[] specifics, String additionalVars) throws IOException {
List<ToolInstaller> installers = new ArrayList<ToolInstaller>();
installers.add(new CommandInstaller(null, "ln -s `which true` mytrue", "./"));

List<ToolProperty<ToolInstallation>> properties = new ArrayList<ToolProperty<ToolInstallation>>();
properties.add(new InstallSourceProperty(installers));

CustomTool installation = new CustomTool(name, null, properties, "./", specifics, ToolVersionConfig.DEFAULT, additionalVars);
return installation;
}

Expand Down

0 comments on commit dcf9ac7

Please sign in to comment.