diff --git a/src/XMakeBuildEngine/UnitTestsPublicOM/Construction/ProjectSdkImplicitImport_Tests.cs b/src/XMakeBuildEngine/UnitTestsPublicOM/Construction/ProjectSdkImplicitImport_Tests.cs index 842fe450f25..84971df7438 100644 --- a/src/XMakeBuildEngine/UnitTestsPublicOM/Construction/ProjectSdkImplicitImport_Tests.cs +++ b/src/XMakeBuildEngine/UnitTestsPublicOM/Construction/ProjectSdkImplicitImport_Tests.cs @@ -3,7 +3,9 @@ //----------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.IO; +using System.Linq; using System.Xml; using Microsoft.Build.Construction; using Microsoft.Build.Evaluation; @@ -66,18 +68,57 @@ public void SdkImportsAreInImportList() ResolvedImport finalResolvedImport = project.Imports[1]; Assert.Equal(_sdkTargetsPath, finalResolvedImport.ImportedProject.FullPath); - ProjectProperty initialImportProperty = project.GetProperty("InitialImportProperty"); - Assert.Equal(_sdkPropsPath, initialImportProperty.Xml.ContainingProject.FullPath); - Assert.True(initialImportProperty.IsImported); - - ProjectProperty finalImportProperty = project.GetProperty("FinalImportProperty"); - Assert.Equal(_sdkTargetsPath, finalImportProperty.Xml.ContainingProject.FullPath); - Assert.True(finalImportProperty.IsImported); + VerifyPropertyFromImplicitImport(project, "InitialImportProperty", _sdkPropsPath, "Hello"); + VerifyPropertyFromImplicitImport(project, "FinalImportProperty", _sdkTargetsPath, "World"); // TODO: Check the location of the import, maybe it should point to the location of the SDK attribute? } } + /// + /// Verifies that when a user specifies more than one SDK that everything works as expected + /// + [Fact] + public void SdkSupportsMultiple() + { + IList sdkNames = new List + { + "MSBuild.SDK.One", + "MSBuild.SDK.Two", + "MSBuild.SDK.Three", + }; + + foreach (string sdkName in sdkNames) + { + string testSdkDirectory = Directory.CreateDirectory(Path.Combine(_testSdkRoot, sdkName, "Sdk")).FullName; + + File.WriteAllText(Path.Combine(testSdkDirectory, "Sdk.props"), $"{sdkName}"); + File.WriteAllText(Path.Combine(testSdkDirectory, "Sdk.targets"), $"{sdkName}"); + } + + using (new Helpers.TemporaryEnvironment("MSBuildSDKsPath", _testSdkRoot)) + { + string content = $@" + + + "; + + ProjectRootElement projectRootElement = ProjectRootElement.Create(XmlReader.Create(new StringReader(content))); + + Project project = new Project(projectRootElement); + + // The XML representation of the project should indicate there are no imports + Assert.Equal(0, projectRootElement.Imports.Count); + + // The project representation should have twice as many imports as SDKs + Assert.Equal(sdkNames.Count * 2, project.Imports.Count); + + // Last imported SDK should set the value + VerifyPropertyFromImplicitImport(project, "InitialImportProperty", Path.Combine(_testSdkRoot, sdkNames.Last(), "Sdk", "Sdk.props"), sdkNames.Last()); + VerifyPropertyFromImplicitImport(project, "FinalImportProperty", Path.Combine(_testSdkRoot, sdkNames.Last(), "Sdk", "Sdk.targets"), sdkNames.Last()); + } + } + [Fact] public void ProjectWithSdkImportsIsCloneable() { @@ -88,7 +129,7 @@ public void ProjectWithSdkImportsIsCloneable() { // Based on the new-console-project CLI template (but not matching exactly // should not be a deal-breaker). - string content = @" + string content = $@" Exe netcoreapp1.0 @@ -121,7 +162,7 @@ public void ProjectWithSdkImportsIsRemoveable() { // Based on the new-console-project CLI template (but not matching exactly // should not be a deal-breaker). - string content = @" + string content = $@" Exe netcoreapp1.0 @@ -227,5 +268,18 @@ public void Dispose() FileUtilities.DeleteWithoutTrailingBackslash(_testSdkDirectory, true); } } + + private void VerifyPropertyFromImplicitImport(Project project, string propertyName, string expectedContainingProjectPath, string expectedValue) + { + ProjectProperty property = project.GetProperty(propertyName); + + Assert.NotNull(property?.Xml?.ContainingProject?.FullPath); + + Assert.Equal(expectedContainingProjectPath, property.Xml.ContainingProject.FullPath); + + Assert.True(property.IsImported); + + Assert.Equal(expectedValue, property.EvaluatedValue); + } } }