Skip to content

Commit

Permalink
Fix regression of JENKINS-40624
Browse files Browse the repository at this point in the history
  • Loading branch information
nfalco79 committed Feb 12, 2017
1 parent 652e9f4 commit 8a3cfc8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
@@ -1,5 +1,6 @@
package jenkins.plugins.nodejs;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

Expand Down Expand Up @@ -31,6 +32,7 @@
import jenkins.plugins.nodejs.configfiles.VerifyConfigProviderException;
import jenkins.plugins.nodejs.tools.NodeJSInstallation;
import jenkins.plugins.nodejs.tools.Platform;
import jenkins.security.MasterToSlaveCallable;

/**
* This class executes a JavaScript file using node. The file should contain
Expand All @@ -40,6 +42,26 @@
* @author Nikolas Falco
*/
public class NodeJSCommandInterpreter extends CommandInterpreter {

/*
* This class must be kept simple and serialisable because has to run on
* slave.
*/
private static class ExecutableVerifier extends MasterToSlaveCallable<Boolean, IOException> {
private static final long serialVersionUID = -8960093360218932530L;

private final String executable;

public ExecutableVerifier(String executable) {
this.executable = executable;
}

@Override
public Boolean call() throws IOException {
return new File(executable).exists();
}
}

private final String nodeJSInstallationName;
private final String configId;
private transient String nodeExec; // NOSONAR
Expand Down Expand Up @@ -92,11 +114,16 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
if (node == null) {
throw new AbortException(Messages.NodeJSBuilders_nodeOffline());
}

ni = ni.forNode(node, listener);
ni = ni.forEnvironment(env);
ni.buildEnvVars(env);

nodeExec = ni.getExecutable();
if (nodeExec == null) {

// ensure that executable exists on target node
Boolean exists = launcher.getChannel().call(new ExecutableVerifier(nodeExec));
if (exists == null || !exists) {
throw new AbortException(Messages.NodeJSBuilders_noExecutableFound(ni.getHome()));
}
}
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/jenkins/plugins/nodejs/tools/NodeJSInstallation.java
@@ -1,3 +1,26 @@
/*
* The MIT License
*
* Copyright (c) 2009-2010, Sun Microsystems, Inc., CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.plugins.nodejs.tools;

import java.io.File;
Expand Down Expand Up @@ -79,7 +102,7 @@ public void buildEnvVars(EnvVars env) {
return;
}
env.put("NODEJS_HOME", home);
env.override("PATH+NODEJS", getBin());
env.override("PATH+NODEJS", getBin().getAbsolutePath());
}

/**
Expand All @@ -91,21 +114,16 @@ public void buildEnvVars(EnvVars env) {
* if something goes wrong
*/
public String getExecutable() throws IOException {
File exe = getExeFile(getPlatform());
if (exe.exists()) {
return exe.getPath();
}
return null;
return getExeFile(getPlatform()).getAbsolutePath();
}

private File getExeFile(@Nonnull Platform platform) {
File bin = new File(getHome(), platform.binFolder);
return new File(bin, platform.nodeFileName);
return new File(getBin(), platform.nodeFileName);
}

private String getBin() {
private File getBin() {
try {
return new File(getHome(), getPlatform().binFolder).getPath();
return new File(getHome(), getPlatform().binFolder);
} catch (DetectionFailedException e) {
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ public class NodeJSInstaller extends DownloadFromUrlInstaller {
* Define the elapse time before perform a new npm install for defined
* global packages.
*/
public static final int NPM_PACKAGES_REFRESH_HOURS = 72;
public static final int DEFAULT_NPM_PACKAGES_REFRESH_HOURS = 72;

private final String npmPackages;
private final Long npmPackagesRefreshHours;
Expand Down
Expand Up @@ -20,7 +20,7 @@ public class NodeJSInstallerTest {

@Test
public void test_skip_install_global_packages_when_empty() throws Exception {
MockNodeJSInstaller mock = new MockNodeJSInstaller("test-id", " ", NodeJSInstaller.NPM_PACKAGES_REFRESH_HOURS);
MockNodeJSInstaller mock = new MockNodeJSInstaller("test-id", " ", NodeJSInstaller.DEFAULT_NPM_PACKAGES_REFRESH_HOURS);
mock.performInstallation(null, null, null);
}

Expand Down

0 comments on commit 8a3cfc8

Please sign in to comment.