Skip to content

Commit

Permalink
Test for task ALC behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
rainersigwald committed Nov 14, 2019
1 parent 5be281e commit d775c0c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/MSBuild.UnitTests/ValidateAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if FEATURE_ASSEMBLYLOADCONTEXT

using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
using System.Runtime.Loader;

namespace Microsoft.Build.UnitTests
{
public class ValidateAssemblyLoadContext : Task
{
public override bool Execute()
{
var thisLoadContext = AssemblyLoadContext.GetLoadContext(typeof(ValidateAssemblyLoadContext).Assembly);

// Check by name because this always reports false, presumably due to ALC shenanigans:
// if (thisLoadContext is MSBuildLoadContext context)
if (thisLoadContext.GetType().FullName == typeof(MSBuildLoadContext).FullName)
{
if (!thisLoadContext.Name.EndsWith(typeof(ValidateAssemblyLoadContext).Assembly.GetName().Name + ".dll"))
{
Log.LogError($"Unexpected AssemblyLoadContext name: \"{thisLoadContext.Name}\", but the current executing assembly was {typeof(ValidateAssemblyLoadContext).Assembly.GetName().Name}");
}
else
{
Log.LogMessage(MessageImportance.High, $"Task {nameof(ValidateAssemblyLoadContext)} loaded in AssemblyLoadContext named {thisLoadContext.Name}");
}
}
else
{
Log.LogError($"Load context was a {thisLoadContext.GetType().AssemblyQualifiedName} instead of an {typeof(MSBuildLoadContext).AssemblyQualifiedName}");
}

return !Log.HasLoggedErrors;
}
}
}
#endif
29 changes: 29 additions & 0 deletions src/MSBuild.UnitTests/XMake_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Xunit.Abstractions;
using Shouldly;
using System.IO.Compression;
using System.Reflection;

namespace Microsoft.Build.UnitTests
{
Expand Down Expand Up @@ -2119,6 +2120,34 @@ public void BinaryLogContainsImportedFiles()
}
}

#if FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Ensure that tasks get loaded into their own <see cref="System.Runtime.Loader.AssemblyLoadContext"/>.
/// </summary>
/// <remarks>
/// When loading a task from a test assembly in a test within that assembly, the assembly is already loaded
/// into the default context. So put the test here and isolate the task into an MSBuild that runs in its
/// own process, causing it to newly load the task (test) assembly in a new ALC.
/// </remarks>
[Fact]
public void TasksGetAssemblyLoadContexts()
{
string customTaskPath = Assembly.GetExecutingAssembly().Location;

string projectContents = $@"<Project ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>
<UsingTask TaskName=`ValidateAssemblyLoadContext` AssemblyFile=`{customTaskPath}` />
<Target Name=`Build`>
<ValidateAssemblyLoadContext />
</Target>
</Project>";

ExecuteMSBuildExeExpectSuccess(projectContents);
}

#endif


private string CopyMSBuild()
{
string dest = null;
Expand Down

0 comments on commit d775c0c

Please sign in to comment.