From d1a4c680f0868b2a88df8ac0f79b1e370d24af30 Mon Sep 17 00:00:00 2001 From: Forrest Date: Wed, 7 Oct 2020 13:52:04 -0700 Subject: [PATCH] Microsoft.Build.Traversal: propogate well known global properties (#208) Fixes #207 --- src/Traversal.UnitTests/TraversalTests.cs | 67 +++++++++++++++++++++++ src/Traversal/Sdk/Sdk.targets | 1 + src/Traversal/version.json | 2 +- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/Traversal.UnitTests/TraversalTests.cs b/src/Traversal.UnitTests/TraversalTests.cs index 95b34d15..a36c585a 100644 --- a/src/Traversal.UnitTests/TraversalTests.cs +++ b/src/Traversal.UnitTests/TraversalTests.cs @@ -277,6 +277,73 @@ public void TraversalGlobalPropertiesPreserveAdditionalProperties(string additio item.GetMetadataValue("AdditionalProperties").ShouldBe(expected); } + [Theory] + [InlineData("Platform=x86", "x86", null, null, null, null)] + [InlineData(null, null, "Configuration=Debug", "Debug", null, null)] + [InlineData(null, null, null, null, "TargetFramework=net472", "net472")] + [InlineData("Platform=x64", "x64", "Configuration=Debug", "Debug", null, null)] + public void TraversalPreserveWellKnownProperties(string setPlatformMetadata, string expectedPlatform, string setConfigurationMetadata, string expectedConfiguration, string setTargetFrameworkMetadata, string expectedTargetFramework) + { + // Create a project that prints out its Platform, Configuration, and TargetFramework in the Build target. + string csProj = GetSkeletonCSProjWithMessageTasksPrintingWellKnownMetadata("A").FullPath; + + // Create a traversal project that invokes the csproj. + ProjectCreator subTraversalProject = ProjectCreator + .Templates + .TraversalProject( + path: GetTempFile("dirs.proj"), + customAction: creator => + { + var metadata = new Dictionary(); + if (setPlatformMetadata != null) + { + metadata["SetPlatform"] = setPlatformMetadata; + } + + if (setConfigurationMetadata != null) + { + metadata["SetConfiguration"] = setConfigurationMetadata; + } + + if (setTargetFrameworkMetadata != null) + { + metadata["SetTargetFramework"] = setTargetFrameworkMetadata; + } + + creator.ItemProjectReference(csProj, metadata: metadata); + }) + .Save() + .TryBuild("Build", out bool result, out BuildOutput buildOutput); + + ProjectCreator GetSkeletonCSProjWithMessageTasksPrintingWellKnownMetadata(string projectName) + { + return ProjectCreator.Templates.SdkCsproj(path: GetTempFile($"{projectName}.csproj"), sdk: string.Empty) + .Target("Build") + .TaskMessage("Platform: $(Platform)") + .TaskMessage("Configuration: $(Configuration)") + .TaskMessage("TargetFramework: $(TargetFramework)") + .Save(); + } + + // Verify we received three normal messages and that the csproj received the right properties from the traversal project. + buildOutput.Messages.Normal.Count().ShouldBe(3); + + if (setPlatformMetadata != null) + { + buildOutput.Messages.Normal.ShouldContain($"Platform: {expectedPlatform}"); + } + + if (setConfigurationMetadata != null) + { + buildOutput.Messages.Normal.ShouldContain($"Configuration: {expectedConfiguration}"); + } + + if (setTargetFrameworkMetadata != null) + { + buildOutput.Messages.Normal.ShouldContain($"TargetFramework: {expectedTargetFramework}"); + } + } + [Theory] [InlineData("Build")] [InlineData("Clean")] diff --git a/src/Traversal/Sdk/Sdk.targets b/src/Traversal/Sdk/Sdk.targets index f0fd2a30..8f1f1254 100644 --- a/src/Traversal/Sdk/Sdk.targets +++ b/src/Traversal/Sdk/Sdk.targets @@ -127,6 +127,7 @@