-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ProjectReferences Negotiate SetPlatform Metadata (#6655)
* GetPlatforms target added * Add _GetProjectReferencePlatformProperties * Add GetNearestPlatformTask v1 * Changes to GetNearestPlatformTask, Platform items flow into TargetFramework logic.
- Loading branch information
1 parent
9128adb
commit b6e7d60
Showing
24 changed files
with
1,150 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,203 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Build.UnitTests; | ||
using Microsoft.Build.Utilities; | ||
using Shouldly; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Build.Tasks.UnitTests | ||
{ | ||
sealed public class GetCompatiblePlatform_Tests | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public GetCompatiblePlatform_Tests(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void ResolvesViaPlatformLookupTable() | ||
{ | ||
// PlatformLookupTable always takes priority. It is typically user-defined. | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", "x64;x86;AnyCPU"); | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "win32", | ||
PlatformLookupTable = "win32=x64", | ||
AnnotatedProjects = new TaskItem[] { projectReference } | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
|
||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe("x64"); | ||
} | ||
|
||
[Fact] | ||
public void ResolvesViaProjectReferencesPlatformLookupTable() | ||
{ | ||
// A ProjectReference's PlatformLookupTable takes priority over the current project's table. | ||
// This allows overrides on a per-ProjectItem basis. | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", "x64;x86;AnyCPU"); | ||
|
||
// ProjectReference will be assigned x86 because its table takes priority | ||
projectReference.SetMetadata("PlatformLookupTable", "win32=x86"); | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "win32", | ||
PlatformLookupTable = "win32=x64", | ||
AnnotatedProjects = new TaskItem[] { projectReference } | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
|
||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe("x86"); | ||
} | ||
|
||
[Fact] | ||
public void ResolvesViaAnyCPUDefault() | ||
{ | ||
// No valid mapping via the lookup table, should default to AnyCPU when the current project | ||
// and ProjectReference platforms don't match. | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", "x64;AnyCPU"); | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "x86", | ||
PlatformLookupTable = "AnyCPU=x64", | ||
AnnotatedProjects = new TaskItem[] { projectReference } | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
|
||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe("AnyCPU"); | ||
} | ||
|
||
[Fact] | ||
public void ResolvesViaSamePlatform() | ||
{ | ||
// No valid mapping via the lookup table. If the ProjectReference's platform | ||
// matches the current project's platform, it takes priority over AnyCPU default. | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", "x86;x64;AnyCPU"); | ||
projectReference.SetMetadata("PlatformLookupTable", "x86=AnyCPU"); // matching platform takes priority over lookup tables | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "x86", | ||
PlatformLookupTable = "x86=AnyCPU", | ||
AnnotatedProjects = new TaskItem[] { projectReference } | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
|
||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe("x86"); | ||
} | ||
|
||
[Fact] | ||
public void FailsToResolve() | ||
{ | ||
// No valid mapping via the lookup table, ProjectReference can't default to AnyCPU, | ||
// it also can't match with current project, log a warning. | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", "x64"); | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "x86", | ||
PlatformLookupTable = "AnyCPU=x64", | ||
AnnotatedProjects = new TaskItem[] { projectReference }, | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
// When the task logs a warning, it does not set NearestPlatform | ||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe(string.Empty); | ||
((MockEngine)task.BuildEngine).AssertLogContains("MSB3981"); | ||
} | ||
|
||
[Fact] | ||
public void WarnsWhenProjectReferenceHasNoPlatformOptions() | ||
{ | ||
// Task should log a warning when a ProjectReference has no options to build as. | ||
// It will continue and have no NearestPlatform metadata. | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", string.Empty); | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "x86", | ||
PlatformLookupTable = "AnyCPU=x64", | ||
AnnotatedProjects = new TaskItem[] { projectReference }, | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
// When the task logs a warning, it does not set NearestPlatform | ||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe(string.Empty); | ||
((MockEngine)task.BuildEngine).AssertLogContains("MSB3982"); | ||
} | ||
|
||
/// <summary> | ||
/// Invalid format on PlatformLookupTable results in an exception being thrown. | ||
/// </summary> | ||
[Fact] | ||
public void WarnsOnInvalidFormatLookupTable() | ||
{ | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", "x64"); | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "AnyCPU", | ||
PlatformLookupTable = "AnyCPU=;A=B", // invalid format | ||
AnnotatedProjects = new TaskItem[] { projectReference }, | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
// When the platformlookuptable is in an invalid format, it is discarded. | ||
// There shouldn't have been a translation found from AnyCPU to anything. | ||
// Meaning the projectreference would not have NearestPlatform set. | ||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe(string.Empty); | ||
((MockEngine)task.BuildEngine).AssertLogContains("MSB3983"); | ||
} | ||
|
||
/// <summary> | ||
/// Invalid format on PlatformLookupTable from the projectreference results in an exception being thrown. | ||
/// </summary> | ||
[Fact] | ||
public void WarnsOnInvalidFormatProjectReferenceLookupTable() | ||
{ | ||
TaskItem projectReference = new TaskItem("foo.bar"); | ||
projectReference.SetMetadata("Platforms", "x64;x86"); | ||
projectReference.SetMetadata("PlatformLookupTable", "x86=;b=d"); | ||
|
||
GetCompatiblePlatform task = new GetCompatiblePlatform() | ||
{ | ||
BuildEngine = new MockEngine(_output), | ||
CurrentProjectPlatform = "AnyCPU", | ||
PlatformLookupTable = "AnyCPU=x86;A=B", // invalid format | ||
AnnotatedProjects = new TaskItem[] { projectReference }, | ||
}; | ||
|
||
task.Execute().ShouldBeTrue(); | ||
|
||
// A ProjectReference PlatformLookupTable should take priority, but is thrown away when | ||
// it has an invalid format. The current project's PLT should be the next priority. | ||
task.AssignedProjectsWithPlatform[0].GetMetadata("NearestPlatform").ShouldBe("x86"); | ||
((MockEngine)task.BuildEngine).AssertLogContains("MSB3983"); | ||
} | ||
} | ||
} |
Oops, something went wrong.