-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Migrate GetAssemblyIdentity to multithreaded execution model #13588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jankratochvilcz
merged 4 commits into
main
from
jankratochvilcz/multithreaded/get-assembly-identity-migration
May 1, 2026
+205
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
731caaf
Migrate GetAssemblyIdentity to multithreaded execution model
jankratochvilcz 3a757f8
Changes behavior to throw on null args
jankratochvilcz ffaee98
Fixes the contract to be the same as before
jankratochvilcz d12b539
Address review feedback on PR #13588
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Reflection; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Tasks; | ||
| using Microsoft.Build.Utilities; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| #nullable disable | ||
|
|
||
| namespace Microsoft.Build.UnitTests | ||
| { | ||
| public sealed class GetAssemblyIdentity_Tests | ||
| { | ||
| private readonly ITestOutputHelper _output; | ||
|
|
||
| public GetAssemblyIdentity_Tests(ITestOutputHelper output) | ||
| { | ||
| _output = output; | ||
| } | ||
|
|
||
| private GetAssemblyIdentity CreateTaskUnderTest(MockEngine engine = null) | ||
| { | ||
| return new GetAssemblyIdentity | ||
| { | ||
| BuildEngine = engine ?? new MockEngine(_output), | ||
| }; | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AbsolutePathToExistingAssembly_ProducesCorrectIdentity() | ||
| { | ||
| // Use this test assembly as the input — it's guaranteed to exist. | ||
| // Use the compile-time assembly name as the baseline rather than | ||
| // AssemblyName.GetAssemblyName(path), to avoid asserting against the same | ||
| // reflection API the production code uses internally. | ||
| string assemblyPath = typeof(GetAssemblyIdentity_Tests).Assembly.Location; | ||
| AssemblyName expectedName = typeof(GetAssemblyIdentity_Tests).Assembly.GetName(); | ||
|
|
||
| GetAssemblyIdentity task = CreateTaskUnderTest(); | ||
| task.AssemblyFiles = [new TaskItem(assemblyPath)]; | ||
|
|
||
| task.Execute().ShouldBeTrue(); | ||
|
|
||
| task.Assemblies.Length.ShouldBe(1); | ||
| task.Assemblies[0].ItemSpec.ShouldBe(expectedName.FullName); | ||
| task.Assemblies[0].GetMetadata("Name").ShouldBe(expectedName.Name); | ||
| task.Assemblies[0].GetMetadata("Version").ShouldBe(expectedName.Version.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NonExistentFile_LogsErrorAndReturnsFalse() | ||
| { | ||
| var engine = new MockEngine(_output); | ||
| GetAssemblyIdentity task = CreateTaskUnderTest(engine); | ||
| task.AssemblyFiles = [new TaskItem("does-not-exist.dll")]; | ||
|
|
||
| task.Execute().ShouldBeFalse(); | ||
|
|
||
| AssertCouldNotGetAssemblyName(engine); | ||
| engine.Log.ShouldContain("does-not-exist.dll"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NonAssemblyFile_LogsErrorAndReturnsFalse() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| TransientTestFile textFile = env.CreateFile("notanassembly.txt", "This is not an assembly."); | ||
|
|
||
| var engine = new MockEngine(_output); | ||
| GetAssemblyIdentity task = CreateTaskUnderTest(engine); | ||
| task.AssemblyFiles = [new TaskItem(textFile.Path)]; | ||
|
|
||
| task.Execute().ShouldBeFalse(); | ||
|
|
||
| AssertCouldNotGetAssemblyName(engine); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MixedBatch_GoodAndBadItems() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| TransientTestFile textFile = env.CreateFile("bad.txt", "not an assembly"); | ||
|
|
||
| string goodAssemblyPath = typeof(GetAssemblyIdentity_Tests).Assembly.Location; | ||
| AssemblyName expectedName = typeof(GetAssemblyIdentity_Tests).Assembly.GetName(); | ||
|
|
||
| var engine = new MockEngine(_output); | ||
| GetAssemblyIdentity task = CreateTaskUnderTest(engine); | ||
| task.AssemblyFiles = | ||
| [ | ||
| new TaskItem(goodAssemblyPath), | ||
| new TaskItem(textFile.Path), | ||
| ]; | ||
|
|
||
| // Execute returns false because one item failed. | ||
| task.Execute().ShouldBeFalse(); | ||
|
|
||
| // The good item should still appear in the output. | ||
| task.Assemblies.Length.ShouldBe(1); | ||
| task.Assemblies[0].ItemSpec.ShouldBe(expectedName.FullName); | ||
|
|
||
| // The bad item should have produced an error. | ||
| engine.Errors.ShouldBe(1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EmptyItemSpec_LogsErrorAndReturnsFalse() | ||
| { | ||
| var engine = new MockEngine(_output); | ||
| GetAssemblyIdentity task = CreateTaskUnderTest(engine); | ||
| task.AssemblyFiles = [new TaskItem("")]; | ||
|
|
||
| task.Execute().ShouldBeFalse(); | ||
|
|
||
| AssertCouldNotGetAssemblyName(engine); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NullItemSpec_LogsErrorAndReturnsFalse() | ||
| { | ||
| var engine = new MockEngine(_output); | ||
| GetAssemblyIdentity task = CreateTaskUnderTest(engine); | ||
| Assert.Throws<ArgumentNullException>(() => task.AssemblyFiles = [new TaskItem((ITaskItem)null)]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RelativePath_ResolvesAgainstProjectDirectory() | ||
| { | ||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| TransientTestFolder projectDir = env.CreateFolder(); | ||
|
|
||
| string sourceAssembly = typeof(GetAssemblyIdentity_Tests).Assembly.Location; | ||
| AssemblyName expectedName = typeof(GetAssemblyIdentity_Tests).Assembly.GetName(); | ||
|
|
||
| string relativeFileName = "TestAssembly.dll"; | ||
| File.Copy(sourceAssembly, Path.Combine(projectDir.Path, relativeFileName)); | ||
|
|
||
| var engine = new MockEngine(_output); | ||
| GetAssemblyIdentity task = CreateTaskUnderTest(engine); | ||
| task.TaskEnvironment = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(projectDir.Path); | ||
|
jankratochvilcz marked this conversation as resolved.
|
||
| task.AssemblyFiles = [new TaskItem(relativeFileName)]; | ||
|
|
||
| task.Execute().ShouldBeTrue(); | ||
|
|
||
| task.Assemblies.Length.ShouldBe(1); | ||
| task.Assemblies[0].ItemSpec.ShouldBe(expectedName.FullName); | ||
| } | ||
|
jankratochvilcz marked this conversation as resolved.
|
||
|
|
||
| [Fact] | ||
| public void OutputItem_DoesNotContainAbsolutizedPaths() | ||
| { | ||
| string assemblyPath = typeof(GetAssemblyIdentity_Tests).Assembly.Location; | ||
| AssemblyName expectedName = typeof(GetAssemblyIdentity_Tests).Assembly.GetName(); | ||
|
|
||
| GetAssemblyIdentity task = CreateTaskUnderTest(); | ||
| task.AssemblyFiles = [new TaskItem(assemblyPath)]; | ||
|
|
||
| task.Execute().ShouldBeTrue(); | ||
|
|
||
| task.Assemblies.Length.ShouldBe(1); | ||
|
|
||
| // ItemSpec should be the assembly identity string, not a path. | ||
| task.Assemblies[0].ItemSpec.ShouldBe(expectedName.FullName); | ||
|
jankratochvilcz marked this conversation as resolved.
|
||
| } | ||
|
|
||
| [Fact] | ||
| public void OutputItem_CopiesInputMetadataVerbatim() | ||
| { | ||
| string assemblyPath = typeof(GetAssemblyIdentity_Tests).Assembly.Location; | ||
|
|
||
| var inputItem = new TaskItem(assemblyPath); | ||
| inputItem.SetMetadata("CustomMeta", "CustomValue"); | ||
|
|
||
| GetAssemblyIdentity task = CreateTaskUnderTest(); | ||
| task.AssemblyFiles = [inputItem]; | ||
|
|
||
| task.Execute().ShouldBeTrue(); | ||
|
|
||
| task.Assemblies.ShouldHaveSingleItem() | ||
| .GetMetadata("CustomMeta").ShouldBe("CustomValue"); | ||
| } | ||
|
|
||
| private void AssertCouldNotGetAssemblyName(MockEngine engine) | ||
| { | ||
| engine.Errors.ShouldBe(1); | ||
| engine.Log.ShouldContain("MSB3441"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.