Skip to content

Commit

Permalink
Thow an exception if the SDK attribute contains an empty value in the…
Browse files Browse the repository at this point in the history
… list like "One; ;Two"
  • Loading branch information
jeffkl committed Jan 4, 2017
1 parent f7793f2 commit 0f234f6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/XMakeBuildEngine/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,13 @@ element is ProjectOtherwiseElement
{
// SDK imports are added implicitly where they are evaluated at the top and bottom as if they are in the XML
//
foreach (string sdk in currentProjectOrImport.Sdk.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(i => i.Trim())
.Where(i => !String.IsNullOrWhiteSpace(i)))
foreach (string sdk in currentProjectOrImport.Sdk.Split(';').Select(i => i.Trim()))
{
if (String.IsNullOrWhiteSpace(sdk))
{
ProjectErrorUtilities.ThrowInvalidProject(currentProjectOrImport.SdkLocation, "InvalidSdkFormat", currentProjectOrImport.Sdk);
}

int slashIndex = sdk.LastIndexOf("/", StringComparison.Ordinal);
string sdkName = slashIndex > 0 ? sdk.Substring(0, slashIndex) : sdk;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class ProjectSdkImplicitImport_Tests : IDisposable
private readonly string _sdkPropsPath;
private readonly string _sdkTargetsPath;


public ProjectSdkImplicitImport_Tests()
{
_testSdkRoot = Path.Combine(ObjectModelHelpers.TempProjectDir, Guid.NewGuid().ToString("N"));
Expand All @@ -44,7 +43,7 @@ public void SdkImportsAreInImportList()
using (new Helpers.TemporaryEnvironment("MSBuildSDKsPath", _testSdkRoot))
{
string content = $@"
<Project Sdk='{SdkName}'>
<Project Sdk=""{SdkName}"">
<PropertyGroup>
<UsedToTestIfImplicitImportsAreInTheCorrectLocation>null</UsedToTestIfImplicitImportsAreInTheCorrectLocation>
</PropertyGroup>
Expand Down Expand Up @@ -148,6 +147,9 @@ public void ProjectWithSdkImportsIsRemoveable()
}
}

/// <summary>
/// Verifies that an error occurs when an SDK name is not in the correct format.
/// </summary>
[Fact]
public void ProjectWithInvalidSdkName()
{
Expand All @@ -158,7 +160,7 @@ public void ProjectWithInvalidSdkName()
using (new Helpers.TemporaryEnvironment("MSBuildSDKsPath", _testSdkRoot))
{
string content = $@"
<Project Sdk='{invalidSdkName}'>
<Project Sdk=""{invalidSdkName}"">
<PropertyGroup>
<UsedToTestIfImplicitImportsAreInTheCorrectLocation>null</UsedToTestIfImplicitImportsAreInTheCorrectLocation>
</PropertyGroup>
Expand All @@ -171,22 +173,53 @@ public void ProjectWithInvalidSdkName()
Assert.Equal("MSB4229", exception.ErrorCode);
}

/// <summary>
/// Verifies that an empty SDK attribute works and nothing is imported.
/// </summary>
[Fact]
public void ProjectWithEmptySdkName()
{
using (new Helpers.TemporaryEnvironment("MSBuildSDKsPath", _testSdkRoot))
{
string content = @"
<Project Sdk=''>
<Project Sdk="""">
<PropertyGroup>
<UsedToTestIfImplicitImportsAreInTheCorrectLocation>null</UsedToTestIfImplicitImportsAreInTheCorrectLocation>
</PropertyGroup>
</Project>";

Project project = new Project(ProjectRootElement.Create(XmlReader.Create(new StringReader(content))));

Assert.Equal(0, project.Imports.Count);
}
}

/// <summary>
/// Verifies that an error occurs when one or more SDK names are empty.
/// </summary>
[Fact]
public void ProjectWithEmptySdkNameInValidList()
{
const string invalidSdkName = "foo; ;bar";

InvalidProjectFileException exception = Assert.Throws<InvalidProjectFileException>(() =>
{
using (new Helpers.TemporaryEnvironment("MSBuildSDKsPath", _testSdkRoot))
{
string content = $@"
<Project Sdk=""{invalidSdkName}"">
<PropertyGroup>
<UsedToTestIfImplicitImportsAreInTheCorrectLocation>null</UsedToTestIfImplicitImportsAreInTheCorrectLocation>
</PropertyGroup>
</Project>";

Project project = new Project(ProjectRootElement.Create(XmlReader.Create(new StringReader(content))));
}
});

Assert.Equal("MSB4229", exception.ErrorCode);
}

public void Dispose()
{
if (Directory.Exists(_testSdkDirectory))
Expand Down

0 comments on commit 0f234f6

Please sign in to comment.