Skip to content

Commit

Permalink
Do not clear env variables by default (microsoft#3433)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd authored Apr 8, 2022
1 parent ac3553c commit 9b6ed1a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ public ConsoleParameters(IFileHelper fileHelper)
}

/// <summary>
/// Environment variables to be set for the process
/// Environment variables to be set for the process. This will merge the specified entries to the environment variables
/// inherited from the current process. If you wish to provide a full set of environment variables yourself set <see cref="InheritEnvironmentVariables"/> to false.
/// </summary>
public Dictionary<string, string> EnvironmentVariables { get; set; }
public Dictionary<string, string> EnvironmentVariables { get; set; } = new Dictionary<string, string>();

/// <summary>
/// When set to true (default), all environment variables are inherited from the current process and the entries provided in <see cref="EnvironmentVariables"/> are merged with that set.
/// When set to false, only the values you provide in <see cref="EnvironmentVariables"/> are used. Giving you full control of the environment vstest.console is started with.
/// This is only rarely useful and can lead to vstest.console not being able to start at all.
/// You most likely want to use <see cref="System.Environment.GetEnvironmentVariables(System.EnvironmentVariableTarget)"/> and combine
/// <see cref="System.EnvironmentVariableTarget.Machine"/> and <see cref="System.EnvironmentVariableTarget.User"/> responses.
/// </summary>
public bool InheritEnvironmentVariables { get; set; } = true;

/// <summary>
/// Trace level for logs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,5 @@ static Microsoft.VisualStudio.TestPlatform.VsTestConsole.TranslationLayer.Resour
virtual Microsoft.TestPlatform.VsTestConsole.TranslationLayer.TestSession.Dispose(bool disposing) -> void
Microsoft.TestPlatform.VsTestConsole.TranslationLayer.ConsoleParameters.EnvironmentVariables.get -> System.Collections.Generic.Dictionary<string, string>
Microsoft.TestPlatform.VsTestConsole.TranslationLayer.ConsoleParameters.EnvironmentVariables.set -> void
Microsoft.TestPlatform.VsTestConsole.TranslationLayer.ConsoleParameters.InheritEnvironmentVariables.get -> bool
Microsoft.TestPlatform.VsTestConsole.TranslationLayer.ConsoleParameters.InheritEnvironmentVariables.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,25 @@ public void StartProcess(ConsoleParameters consoleParameters)
RedirectStandardError = true
};

EqtTrace.Verbose("VsTestCommandLineWrapper: Process Start Info {0} {1}", info.FileName, info.Arguments);
EqtTrace.Verbose("VsTestCommandLineWrapper.StartProcess: Process Start Info {0} {1}", info.FileName, info.Arguments);

if (consoleParameters.EnvironmentVariables != null)
if (!consoleParameters.InheritEnvironmentVariables)
{
EqtTrace.Verbose("VsTestCommandLineWrapper.StartProcess: Clearing all environment variables.");

info.EnvironmentVariables.Clear();
}

if (consoleParameters.EnvironmentVariables != null)
{
foreach (var envVariable in consoleParameters.EnvironmentVariables)
{
if (envVariable.Key != null)
{
info.EnvironmentVariables.Add(envVariable.Key, envVariable.Value?.ToString());
// Not printing the value on purpose, env variables can contain secrets and we don't need to know the values
// most of the time.
EqtTrace.Verbose("VsTestCommandLineWrapper.StartProcess: Setting environment variable: {0}", envVariable.Key);
info.EnvironmentVariables[envVariable.Key] = envVariable.Value?.ToString();
}
}
}
Expand Down

0 comments on commit 9b6ed1a

Please sign in to comment.