Avoid setting current process environment variables

pull/584/head
NextTurn 2019-01-02 00:00:00 +08:00 committed by Next Turn
parent c454dcf85c
commit 700f45cb84
3 changed files with 9 additions and 70 deletions

View File

@ -161,11 +161,15 @@ namespace winsw.Util
if (envVars != null) if (envVars != null)
{ {
foreach (string key in envVars.Keys) var newEnvironment =
#if NETCOREAPP
ps.Environment;
#else
ps.EnvironmentVariables;
#endif
foreach (KeyValuePair<string, string> pair in envVars)
{ {
Environment.SetEnvironmentVariable(key, envVars[key]); newEnvironment[pair.Key] = pair.Value;
// DONTDO: ps.EnvironmentVariables[key] = envs[key];
// bugged (lower cases all variable names due to StringDictionary being used, see http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=326163)
} }
} }

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using NUnit.Framework;
namespace winswTests.Util namespace winswTests.Util
{ {
@ -18,30 +16,5 @@ namespace winswTests.Util
Console.Out.WriteLine("Created the temporary directory: {0}", tempDirectory); Console.Out.WriteLine("Created the temporary directory: {0}", tempDirectory);
return tempDirectory; return tempDirectory;
} }
/// <summary>
/// Parses output of the "set" command from the file
/// </summary>
/// <param name="filePath">File path</param>
/// <returns>Dictionary of the strings.</returns>
public static Dictionary<string, string> parseSetOutput(string filePath)
{
Dictionary<string, string> res = new Dictionary<string, string>();
var lines = File.ReadAllLines(filePath);
foreach (var line in lines)
{
var parsed = line.Split("=".ToCharArray(), 2);
if (parsed.Length == 2)
{
res.Add(parsed[0], parsed[1]);
}
else
{
Assert.Fail("Wrong line in the parsed Set output file: " + line);
}
}
return res;
}
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System.Diagnostics;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using NUnit.Framework; using NUnit.Framework;
@ -10,43 +9,6 @@ namespace winswTests.Util
[TestFixture] [TestFixture]
class ProcessHelperTest class ProcessHelperTest
{ {
/// <summary>
/// Also reported as <a href="https://issues.jenkins-ci.org/browse/JENKINS-42744">JENKINS-42744</a>
/// </summary>
[Test]
public void ShouldPropagateVariablesInUppercase()
{
Assert.Ignore();
Environment.SetEnvironmentVariable("TEST_KEY", "TEST_VALUE");
var tmpDir = FilesystemTestHelper.CreateTmpDirectory();
string envFile = Path.Combine(tmpDir, "env.properties");
string scriptFile = Path.Combine(tmpDir, "printenv.bat");
File.WriteAllText(scriptFile, "set > " + envFile);
Process proc = new Process();
var ps = proc.StartInfo;
ps.FileName = scriptFile;
ProcessHelper.StartProcessAndCallbackForExit(proc);
var exited = proc.WaitForExit(5000);
if (!exited)
{
Assert.Fail("Process " + proc + " didn't exit after 5 seconds");
}
// Check several veriables, which are expected to be in Uppercase
var envVars = FilesystemTestHelper.parseSetOutput(envFile);
string[] keys = new string[envVars.Count];
envVars.Keys.CopyTo(keys, 0);
string availableVars = "[" + string.Join(",", keys) + "]";
Assert.That(envVars.ContainsKey("TEST_KEY"), "No TEST_KEY in the injected vars: " + availableVars);
// And just ensure that the parsing logic is case-sensitive
Assert.That(!envVars.ContainsKey("test_key"), "Test error: the environment parsing logic is case-insensitive");
}
[Test] [Test]
public void ShouldNotHangWhenWritingLargeStringToStdOut() public void ShouldNotHangWhenWritingLargeStringToStdOut()
{ {