Skip to content

Commit

Permalink
Fixed not passing tests, extended test coverage for the UpdateEnviron…
Browse files Browse the repository at this point in the history
…mentVariables method
  • Loading branch information
marticliment committed Aug 30, 2024
1 parent 656c3ff commit d315078
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/UniGetUI.Core.Tools.Tests/ToolsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
11 changes: 10 additions & 1 deletion src/UniGetUI.Core.Tools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d315078

Please sign in to comment.