Skip to content

Commit

Permalink
Adjust tests with terminal logger enabled (#9995)
Browse files Browse the repository at this point in the history
* Make terminal logger print messages in the tests

* Clear the environment variables about terminal logger during instantiating TestEnvironment to avoid affecting the tests

* Reverting variants should be in reverse order to get back to original state

* Improve resetting variants in reverse order to get back to original state
  • Loading branch information
GangWang01 authored Apr 16, 2024
1 parent 4ec9575 commit 3671494
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 0 additions & 4 deletions src/Build.UnitTests/TerminalLoggerConfiguration_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public TerminalLoggerConfiguration_Tests(ITestOutputHelper output)
{
_env = TestEnvironment.Create(output);

// Ignore environment variables that may have been set by the environment where the tests are running.
_env.SetEnvironmentVariable("MSBUILDLIVELOGGER", null);
_env.SetEnvironmentVariable("MSBUILDTERMINALLOGGER", null);

TransientTestFolder logFolder = _env.CreateFolder(createFolder: true);
TransientTestFile projectFile = _env.CreateFile(logFolder, "myProj.proj", $"""
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Hello">
Expand Down
6 changes: 3 additions & 3 deletions src/MSBuild.UnitTests/MSBuildServer_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class MSBuildServer_Tests : IDisposable
<ProcessIdTask>
<Output PropertyName=""PID"" TaskParameter=""Pid"" />
</ProcessIdTask>
<Message Text=""Server ID is $(PID)"" Importance=""High"" />
<Message Text=""[Work around Github issue #9667 with --interactive]Server ID is $(PID)"" Importance=""High"" />
</Target>
</Project>";
private static string sleepingTaskContentsFormat = @$"
Expand Down Expand Up @@ -313,8 +313,8 @@ public void PropertyMSBuildStartupDirectoryOnServer()
<ProcessIdTask>
<Output PropertyName=""PID"" TaskParameter=""Pid"" />
</ProcessIdTask>
<Message Text=""Server ID is $(PID)"" Importance=""High"" />
<Message Text="":MSBuildStartupDirectory:$(MSBuildStartupDirectory):"" Importance=""high"" />
<Message Text=""[Work around Github issue #9667 with --interactive]Server ID is $(PID)"" Importance=""High"" />
<Message Text=""[Work around Github issue #9667 with --interactive]:MSBuildStartupDirectory:$(MSBuildStartupDirectory):"" Importance=""high"" />
</Target>
</Project>";

Expand Down
10 changes: 7 additions & 3 deletions src/UnitTests.Shared/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public static TestEnvironment Create(ITestOutputHelper output = null, bool ignor
env.WithInvariant(new BuildFailureLogInvariant());
}

// Clear these two environment variables first in case pre-setting affects the test.
env.SetEnvironmentVariable("MSBUILDLIVELOGGER", null);
env.SetEnvironmentVariable("MSBUILDTERMINALLOGGER", null);

return env;
}

Expand Down Expand Up @@ -83,10 +87,10 @@ private void Cleanup()
{
_disposed = true;

// Reset test variants
foreach (var variant in _variants)
// Reset test variants in reverse order to get back to original state.
for (int i = _variants.Count - 1; i >= 0; i--)
{
variant.Revert();
_variants[i].Revert();
}

// Assert invariants
Expand Down

0 comments on commit 3671494

Please sign in to comment.