Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More tests!
Browse files Browse the repository at this point in the history
jeffkl committed Jan 4, 2017
1 parent 0f234f6 commit 8da92e7
Showing 1 changed file with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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?
}
}

/// <summary>
/// Verifies that when a user specifies more than one SDK that everything works as expected
/// </summary>
[Fact]
public void SdkSupportsMultiple()
{
IList<string> sdkNames = new List<string>
{
"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"), $"<Project><PropertyGroup><InitialImportProperty>{sdkName}</InitialImportProperty></PropertyGroup></Project>");
File.WriteAllText(Path.Combine(testSdkDirectory, "Sdk.targets"), $"<Project><PropertyGroup><FinalImportProperty>{sdkName}</FinalImportProperty></PropertyGroup></Project>");
}

using (new Helpers.TemporaryEnvironment("MSBuildSDKsPath", _testSdkRoot))
{
string content = $@"
<Project Sdk=""{String.Join("; ", sdkNames)}"">
</Project>";

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 = @"<Project Sdk=""Microsoft.NET.Sdk"" ToolsVersion=""15.0"">
string content = $@"<Project Sdk=""{SdkName}"" ToolsVersion=""15.0"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
@@ -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 = @"<Project Sdk=""Microsoft.NET.Sdk"" ToolsVersion=""15.0"">
string content = $@"<Project Sdk=""{SdkName}"" ToolsVersion=""15.0"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
@@ -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);
}
}
}

0 comments on commit 8da92e7

Please sign in to comment.