Skip to content

Commit

Permalink
Update tests to check for AppDomain/AssemblyLoadContext in log message
Browse files Browse the repository at this point in the history
  • Loading branch information
bording committed Oct 23, 2023
1 parent 95f0f64 commit 19f5cb9
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions src/Build.UnitTests/BinaryLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ public void UnusedEnvironmentVariablesDoNotAppearInBinaryLog()
}
}

[Fact]
public void AssemblyLoadsDuringTaskRunLogged()
[WindowsFullFrameworkOnlyFact(additionalMessage: "Tests if the AppDomain used to load the task is included in the log text for the event, which is true only on Framework.")]
public void AssemblyLoadsDuringTaskRunLoggedWithAppDomain()
{
using (TestEnvironment env = TestEnvironment.Create())
{
Expand All @@ -201,7 +201,7 @@ public void AssemblyLoadsDuringTaskRunLogged()
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup />
<Task>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Reflection"/>
Expand Down Expand Up @@ -233,19 +233,88 @@ public void AssemblyLoadsDuringTaskRunLogged()

string assemblyLoadedEventText =
"Assembly loaded during TaskRun (InlineCode.HelloWorld): System.Diagnostics.Debug";
string appDomainEventText = "AppDomain: [Default]";
string text = File.ReadAllText(Path.Combine(logFolder.Path, "logFile.log"));
text.ShouldContain(assemblyLoadedEventText);
text.ShouldContain(appDomainEventText);
// events should not be in logger with verbosity normal
string text2 = File.ReadAllText(Path.Combine(logFolder.Path, "logFile2.log"));
text2.ShouldNotContain(assemblyLoadedEventText);
text2.ShouldNotContain(appDomainEventText);
RunnerUtilities.ExecMSBuild($"{logger.Parameters} -flp1:logfile={Path.Combine(logFolder.Path, "logFile3.log")};verbosity=diagnostic -flp2:logfile={Path.Combine(logFolder.Path, "logFile4.log")};verbosity=normal", out success);
success.ShouldBeTrue();
text = File.ReadAllText(Path.Combine(logFolder.Path, "logFile3.log"));
text.ShouldContain(assemblyLoadedEventText);
text.ShouldContain(appDomainEventText);
// events should not be in logger with verbosity normal
text2 = File.ReadAllText(Path.Combine(logFolder.Path, "logFile4.log"));
text2.ShouldNotContain(assemblyLoadedEventText);
text2.ShouldNotContain(appDomainEventText);
}
}

[DotNetOnlyFact(additionalMessage: "Tests if the AssemblyLoadContext used to load the task is included in the log text for the event, which is true only on Core.")]
public void AssemblyLoadsDuringTaskRunLoggedWithAssemblyLoadContext()
{
using (TestEnvironment env = TestEnvironment.Create())
{
string contents = $"""
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Hello">
<!-- This simple inline task displays "Hello, world!" -->
<UsingTask
TaskName="HelloWorld"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup />
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Reflection"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
// Display "Hello, world!"
Log.LogMessage("Hello, world!");
//load assembly
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var diagAssembly = Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(assemblies[0].Location), "System.Diagnostics.Debug.dll"));
Log.LogMessage("Loaded: " + diagAssembly);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="Hello">
<HelloWorld />
</Target>
</Project>
""";
TransientTestFolder logFolder = env.CreateFolder(createFolder: true);
TransientTestFile projectFile = env.CreateFile(logFolder, "myProj.proj", contents);
BinaryLogger logger = new();
logger.Parameters = _logFile;
env.SetEnvironmentVariable("MSBUILDNOINPROCNODE", "1");
RunnerUtilities.ExecMSBuild($"{projectFile.Path} -nr:False -bl:{logger.Parameters} -flp1:logfile={Path.Combine(logFolder.Path, "logFile.log")};verbosity=diagnostic -flp2:logfile={Path.Combine(logFolder.Path, "logFile2.log")};verbosity=normal", out bool success);
success.ShouldBeTrue();

string assemblyLoadedEventText =
"Assembly loaded during TaskRun (InlineCode.HelloWorld): System.Diagnostics.Debug";
string assemblyLoadContextEventText = "AssemblyLoadContext: Default";
string text = File.ReadAllText(Path.Combine(logFolder.Path, "logFile.log"));
text.ShouldContain(assemblyLoadedEventText);
text.ShouldContain(assemblyLoadContextEventText);
// events should not be in logger with verbosity normal
string text2 = File.ReadAllText(Path.Combine(logFolder.Path, "logFile2.log"));
text2.ShouldNotContain(assemblyLoadedEventText);
text2.ShouldNotContain(assemblyLoadContextEventText);
RunnerUtilities.ExecMSBuild($"{logger.Parameters} -flp1:logfile={Path.Combine(logFolder.Path, "logFile3.log")};verbosity=diagnostic -flp2:logfile={Path.Combine(logFolder.Path, "logFile4.log")};verbosity=normal", out success);
success.ShouldBeTrue();
text = File.ReadAllText(Path.Combine(logFolder.Path, "logFile3.log"));
text.ShouldContain(assemblyLoadedEventText);
text.ShouldContain(assemblyLoadContextEventText);
// events should not be in logger with verbosity normal
text2 = File.ReadAllText(Path.Combine(logFolder.Path, "logFile4.log"));
text2.ShouldNotContain(assemblyLoadedEventText);
text2.ShouldNotContain(assemblyLoadContextEventText);
}
}

Expand Down

0 comments on commit 19f5cb9

Please sign in to comment.