Search in sources :

Example 36 with EnvVars

use of hudson.EnvVars in project nodejs-plugin by jenkinsci.

the class NodeJSInstallationMockitoTest method test_installer_environment.

/**
 * Ensure the use of {@link EnvVars#put(String, String)} instead
 * {@code EnvVars#override(String, String)} to respect the
 * {@link ToolInstallation#buildEnvVars(EnvVars)) API documentation.
 * <p>
 * A lot stuff rely on that logic.
 */
@Issue("JENKINS-41947")
@Test
public void test_installer_environment() throws Exception {
    String nodeJSHome = "/home/nodejs";
    String bin = nodeJSHome + "/bin";
    NodeJSInstallation installer = PowerMockito.spy(new NodeJSInstallation("test", nodeJSHome, null));
    PowerMockito.doReturn(bin).when(installer, "getBin");
    EnvVars env = PowerMockito.spy(new EnvVars());
    installer.buildEnvVars(env);
    Mockito.verify(env, Mockito.never()).override(Mockito.anyString(), Mockito.anyString());
    Mockito.verify(env, Mockito.never()).overrideAll(Mockito.<String, String>anyMap());
    assertEquals("Unexpected value for " + ENVVAR_NODEJS_HOME, nodeJSHome, env.get(ENVVAR_NODEJS_HOME));
    assertEquals("Unexpected value for " + ENVVAR_NODEJS_PATH, bin, env.get(ENVVAR_NODEJS_PATH));
    assertNull("PATH variable should not appear in this environment", env.get("PATH"));
}
Also used : EnvVars(hudson.EnvVars) Issue(org.jvnet.hudson.test.Issue) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with EnvVars

use of hudson.EnvVars in project nodejs-plugin by jenkinsci.

the class NodeJSCommandInterpreterTest method test_inject_path_variable.

@Issue("JENKINS-41947")
@Test
public void test_inject_path_variable() throws Exception {
    NodeJSInstallation installation = mockInstaller();
    NodeJSCommandInterpreter builder = CIBuilderHelper.createMock("test_executable_value", installation, null, new CIBuilderHelper.Verifier() {

        @Override
        public void verify(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) throws Exception {
            assertFalse("No Environments", build.getEnvironments().isEmpty());
            EnvVars env = build.getEnvironment(listener);
            assertThat(env.keySet(), CoreMatchers.hasItems(NodeJSConstants.ENVVAR_NODEJS_PATH, NodeJSConstants.ENVVAR_NODEJS_HOME));
            assertEquals(getTestHome(), env.get(NodeJSConstants.ENVVAR_NODEJS_HOME));
            assertEquals(getTestBin(), env.get(NodeJSConstants.ENVVAR_NODEJS_PATH));
        }
    });
    FreeStyleProject job = j.createFreeStyleProject();
    job.getBuildersList().add(builder);
    j.assertBuildStatusSuccess(job.scheduleBuild2(0));
}
Also used : NodeJSInstallation(jenkins.plugins.nodejs.tools.NodeJSInstallation) Verifier(jenkins.plugins.nodejs.CIBuilderHelper.Verifier) EnvVars(hudson.EnvVars) CIBuilderHelper(jenkins.plugins.nodejs.CIBuilderHelper) TaskListener(hudson.model.TaskListener) Launcher(hudson.Launcher) FreeStyleProject(hudson.model.FreeStyleProject) IOException(java.io.IOException) DetectionFailedException(jenkins.plugins.nodejs.tools.DetectionFailedException) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 38 with EnvVars

use of hudson.EnvVars in project nodejs-plugin by jenkinsci.

the class NodeJSCommandInterpreterTest method test_creation_of_config.

@Test
public void test_creation_of_config() throws Exception {
    Config config = createSetting("my-config-id", "email=foo@acme.com", null);
    NodeJSInstallation installation = mockInstaller();
    NodeJSCommandInterpreter builder = CIBuilderHelper.createMock("test_creation_of_config", installation, config.id, new Verifier() {

        @Override
        public void verify(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) throws Exception {
            EnvVars env = build.getEnvironment(listener);
            String var = NodeJSConstants.NPM_USERCONFIG;
            String value = env.get(var);
            assertTrue("variable " + var + " not set", env.containsKey(var));
            assertNotNull("empty value for environment variable " + var, value);
            assertTrue("file of variable " + var + " does not exists or is not a file", new File(value).isFile());
        }
    });
    FreeStyleProject job = j.createFreeStyleProject();
    job.getBuildersList().add(builder);
    j.assertBuildStatusSuccess(job.scheduleBuild2(0));
}
Also used : Config(org.jenkinsci.lib.configprovider.model.Config) NPMConfig(jenkins.plugins.nodejs.configfiles.NPMConfig) Verifier(jenkins.plugins.nodejs.CIBuilderHelper.Verifier) FreeStyleProject(hudson.model.FreeStyleProject) IOException(java.io.IOException) DetectionFailedException(jenkins.plugins.nodejs.tools.DetectionFailedException) NodeJSInstallation(jenkins.plugins.nodejs.tools.NodeJSInstallation) EnvVars(hudson.EnvVars) TaskListener(hudson.model.TaskListener) Launcher(hudson.Launcher) File(java.io.File) Test(org.junit.Test)

Example 39 with EnvVars

use of hudson.EnvVars in project nodejs-plugin by jenkinsci.

the class VerifyEnvVariableBuilder method perform.

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    EnvVars env = build.getEnvironment(listener);
    verify(env);
    return true;
}
Also used : EnvVars(hudson.EnvVars)

Example 40 with EnvVars

use of hudson.EnvVars in project nodejs-plugin by jenkinsci.

the class NodeJSInstaller method refreshGlobalPackages.

/*
     * Installing npm packages if needed
     */
protected void refreshGlobalPackages(Node node, TaskListener log, FilePath expected) throws IOException, InterruptedException {
    String globalPackages = getNpmPackages();
    if (StringUtils.isNotBlank(globalPackages)) {
        // JENKINS-41876
        boolean skipNpmPackageInstallation = areNpmPackagesUpToDate(expected, globalPackages, getNpmPackagesRefreshHours());
        if (!skipNpmPackageInstallation) {
            expected.child(NPM_PACKAGES_RECORD_FILENAME).delete();
            ArgumentListBuilder npmScriptArgs = new ArgumentListBuilder();
            if (platform == Platform.WINDOWS) {
                npmScriptArgs.add("cmd");
                npmScriptArgs.add("/c");
            }
            FilePath binFolder = expected.child(platform.binFolder);
            FilePath npmExe = binFolder.child(platform.npmFileName);
            npmScriptArgs.add(npmExe);
            npmScriptArgs.add("install");
            npmScriptArgs.add("-g");
            for (String packageName : globalPackages.split("\\s")) {
                npmScriptArgs.add(packageName);
            }
            EnvVars env = new EnvVars();
            env.put(NodeJSConstants.ENVVAR_NODEJS_PATH, binFolder.getRemote());
            try {
                buildProxyEnvVars(env, log);
            } catch (URISyntaxException e) {
                log.error("Wrong proxy URL: " + e.getMessage());
            }
            hudson.Launcher launcher = node.createLauncher(log);
            int returnCode = launcher.launch().envs(env).cmds(npmScriptArgs).stdout(log).join();
            if (returnCode == 0) {
                // leave a record for the next up-to-date check
                expected.child(NPM_PACKAGES_RECORD_FILENAME).write(globalPackages, "UTF-8");
                expected.child(NPM_PACKAGES_RECORD_FILENAME).act(new ChmodRecAPlusX());
            }
        }
    }
}
Also used : FilePath(hudson.FilePath) EnvVars(hudson.EnvVars) Launcher(hudson.Launcher) ArgumentListBuilder(hudson.util.ArgumentListBuilder) URISyntaxException(java.net.URISyntaxException)

Aggregations

EnvVars (hudson.EnvVars)64 Test (org.junit.Test)23 IOException (java.io.IOException)15 FilePath (hudson.FilePath)11 ClientConfiguration (com.amazonaws.ClientConfiguration)7 ArgumentListBuilder (hudson.util.ArgumentListBuilder)7 TaskListener (hudson.model.TaskListener)6 AbortException (hudson.AbortException)5 HashMap (java.util.HashMap)5 MavenModuleSet (hudson.maven.MavenModuleSet)4 FreeStyleProject (hudson.model.FreeStyleProject)4 ParameterValue (hudson.model.ParameterValue)4 Launcher (hudson.Launcher)3 GitException (hudson.plugins.git.GitException)3 LogTaskListener (hudson.util.LogTaskListener)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NodeJSInstallation (jenkins.plugins.nodejs.tools.NodeJSInstallation)3 ConduitAPIClient (com.uber.jenkins.phabricator.conduit.ConduitAPIClient)2 ConduitAPIException (com.uber.jenkins.phabricator.conduit.ConduitAPIException)2