From e7030f17275967890a3b53c98ca2a63dbad744a6 Mon Sep 17 00:00:00 2001 From: Florian Ramillien Date: Thu, 28 May 2020 11:58:21 +0200 Subject: [PATCH] [JENKINS-62484] Fix env vars handling On Windows some trailling '\r' was added to env vars. This cause issues with some tools like CMake findPython task --- .../pyenvpipeline/jenkins/VirtualenvManager.java | 4 ++-- .../jenkins/VirtualenvManagerTest.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/pyenvpipeline/jenkins/VirtualenvManager.java b/src/main/java/com/github/pyenvpipeline/jenkins/VirtualenvManager.java index 1fd6040..bcbca13 100644 --- a/src/main/java/com/github/pyenvpipeline/jenkins/VirtualenvManager.java +++ b/src/main/java/com/github/pyenvpipeline/jenkins/VirtualenvManager.java @@ -183,7 +183,7 @@ private BourneShellScript getUnixPreVirtualenvTask() { return new BourneShellScript("env"); } - private EnvVars fromEnvOutput(String commandOutput) { + protected EnvVars fromEnvOutput(String commandOutput) { EnvVars result = new EnvVars(); for (String line: commandOutput.split(Pattern.quote("\n"))) { @@ -199,7 +199,7 @@ private EnvVars fromEnvOutput(String commandOutput) { } if (!skip) { - result.addLine(line); + result.addLine(line.trim()); } } diff --git a/src/test/java/com/github/pyenvpipeline/jenkins/VirtualenvManagerTest.java b/src/test/java/com/github/pyenvpipeline/jenkins/VirtualenvManagerTest.java index f092317..db3b802 100644 --- a/src/test/java/com/github/pyenvpipeline/jenkins/VirtualenvManagerTest.java +++ b/src/test/java/com/github/pyenvpipeline/jenkins/VirtualenvManagerTest.java @@ -1,5 +1,6 @@ package com.github.pyenvpipeline.jenkins; +import hudson.EnvVars; import hudson.Launcher; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.junit.Before; @@ -62,4 +63,16 @@ public void testUnixVirtualEnvPathHandling() throws Exception { result = manager.processPathValues(originalPath, newPath, stepContextWrapper); assertEquals(result, "/venv:/venv/bin"); } + + @Test + public void testEnvVarsHandling() throws Exception { + VirtualenvManager manager = VirtualenvManager.getInstance(); + EnvVars ref = new EnvVars("ENV_VAR_1", "something", "VIRTUAL_ENV", "a_path", "ENV_VAR_2" ,"anotherthing"); + + EnvVars env1 = manager.fromEnvOutput("ENV_VAR_1=something\n\rVIRTUAL_ENV=a_path\n\rENV_VAR_2=anotherthing\n\r"); + assertEquals(ref, env1); + + EnvVars env2 = manager.fromEnvOutput("ENV_VAR_1=something\nVIRTUAL_ENV=a_path\nENV_VAR_2=anotherthing\n"); + assertEquals(ref, env2); + } }