From d315078a801314a2f835d7dad0a084f3c9417e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Climent?= Date: Fri, 30 Aug 2024 18:26:27 +0200 Subject: [PATCH] Fixed not passing tests, extended test coverage for the UpdateEnvironmentVariables method --- src/UniGetUI.Core.Tools.Tests/ToolsTests.cs | 27 ++++++++++++++++++++- src/UniGetUI.Core.Tools/Tools.cs | 11 ++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/UniGetUI.Core.Tools.Tests/ToolsTests.cs b/src/UniGetUI.Core.Tools.Tests/ToolsTests.cs index 965e2fc3a..7f66f0697 100644 --- a/src/UniGetUI.Core.Tools.Tests/ToolsTests.cs +++ b/src/UniGetUI.Core.Tools.Tests/ToolsTests.cs @@ -139,7 +139,7 @@ public void TestSafeQueryString(string query, string expected) } [Fact] - public void TestEnvVariables() + public void TestEnvVariableCreation() { const string ENV1 = "NONEXISTENTENVVARIABLE"; Environment.SetEnvironmentVariable(ENV1, null, EnvironmentVariableTarget.Process); @@ -160,5 +160,30 @@ public void TestEnvVariables() newInfo2.Environment.TryGetValue(ENV1, out string? result3); Assert.Null(result3); } + + [Fact] + public void TestEnvVariableReplacement() + { + const string ENV = "TMP"; + + var expected = Environment.GetEnvironmentVariable(ENV, EnvironmentVariableTarget.User); + + ProcessStartInfo info = CoreTools.UpdateEnvironmentVariables(); + info.Environment.TryGetValue(ENV, out string? result); + Assert.Equal(expected, result); + } + + [Fact] + public void TestEnvVariableYuxtaposition() + { + const string ENV = "PATH"; + + var oldpath = Environment.GetEnvironmentVariable(ENV, EnvironmentVariableTarget.Machine) + ";" + + Environment.GetEnvironmentVariable(ENV, EnvironmentVariableTarget.User); + + ProcessStartInfo info = CoreTools.UpdateEnvironmentVariables(); + info.Environment.TryGetValue(ENV, out string? result); + Assert.Equal(oldpath, result); + } } } diff --git a/src/UniGetUI.Core.Tools/Tools.cs b/src/UniGetUI.Core.Tools/Tools.cs index 7f14277fb..43d5cc785 100644 --- a/src/UniGetUI.Core.Tools/Tools.cs +++ b/src/UniGetUI.Core.Tools/Tools.cs @@ -498,7 +498,16 @@ public static ProcessStartInfo UpdateEnvironmentVariables(ProcessStartInfo info) } foreach (DictionaryEntry env in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)) { - info.Environment[env.Key.ToString()] = env.Value?.ToString(); + string key = env.Key.ToString() ?? ""; + string newValue = env.Value?.ToString() ?? ""; + if (info.Environment.TryGetValue(key, out string? oldValue) && oldValue is not null && oldValue.Contains(';') && newValue != "") + { + info.Environment[key] = oldValue + ";" + newValue; + } + else + { + info.Environment[key] = newValue; + } } return info; }