Skip to content

Commit

Permalink
Add filtering to SdkSupportedTargetPlatformVersion validation. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfbjarne committed Jan 23, 2024
1 parent 849d677 commit 25b360d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ Copyright (c) .NET Foundation. All rights reserved.
BeforeTargets="ProcessFrameworkReferences"
Condition="'$(TargetPlatformVersion)' != '' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), 5.0)) and ('$(Language)' != 'C++' or '$(_EnablePackageReferencesInVCProjects)' == 'true')">
<ItemGroup>
<_ValidTargetPlatformVersion Include="@(SdkSupportedTargetPlatformVersion)" Condition="'@(SdkSupportedTargetPlatformVersion)' != '' and $([MSBuild]::VersionEquals(%(Identity), $(TargetPlatformVersion)))" />
<_ApplicableTargetPlatformVersion Include="@(SdkSupportedTargetPlatformVersion)" Condition="'@(SdkSupportedTargetPlatformVersion)' != '' and '%(SdkSupportedTargetPlatformVersion.DefineConstantsOnly)' != 'true'" RemoveMetadata="DefineConstantsOnly" />
<_ValidTargetPlatformVersion Include="@(_ApplicableTargetPlatformVersion)" Condition="'@(_ApplicableTargetPlatformVersion)' != '' and $([MSBuild]::VersionEquals(%(Identity), $(TargetPlatformVersion)))" />
</ItemGroup>

<PropertyGroup>
Expand All @@ -236,8 +237,8 @@ Copyright (c) .NET Foundation. All rights reserved.
Condition="'$(TargetPlatformVersion)' != '' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), 5.0)) and ('$(Language)' != 'C++' or '$(_EnablePackageReferencesInVCProjects)' == 'true')">
<PropertyGroup>
<TargetPlatformVersionSupported Condition="'$(TargetPlatformVersionSupported)' == '' and '@(_ValidTargetPlatformVersion)' != ''" >true</TargetPlatformVersionSupported>
<_ValidTargetPlatformVersions Condition="'@(SdkSupportedTargetPlatformVersion)' != ''" >@(SdkSupportedTargetPlatformVersion, '%0a')</_ValidTargetPlatformVersions>
<_ValidTargetPlatformVersions Condition="'@(SdkSupportedTargetPlatformVersion)' == ''" >None</_ValidTargetPlatformVersions>
<_ValidTargetPlatformVersions Condition="'@(_ApplicableTargetPlatformVersion)' != ''" >@(_ApplicableTargetPlatformVersion, '%0a')</_ValidTargetPlatformVersions>
<_ValidTargetPlatformVersions Condition="'@(_ApplicableTargetPlatformVersion)' == ''" >None</_ValidTargetPlatformVersions>
</PropertyGroup>

<NetSdkError Condition="'$(TargetPlatformVersionSupported)' != 'true'"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.NET.Build.Tasks;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToBuildWithATargetPlatform : SdkTest
Expand Down Expand Up @@ -90,5 +92,82 @@ public void It_fails_on_unsupported_os()
.And
.HaveStdOutContaining("NETSDK1139");
}

[Fact]
public void It_fails_if_targetplatformversion_is_constant_only()
{
var testProject = new TestProject()
{
Name = "It_fails_if_targetplatformversion_is_constant_only",
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
};
var testAsset = _testAssetsManager.CreateTestProject(testProject);


string DirectoryBuildTargetsContent = $@"
<Project>
<ItemGroup>
<SdkSupportedTargetPlatformVersion Include=""111.0"" DefineConstantsOnly=""true"" />
<SdkSupportedTargetPlatformVersion Include=""222.0"" />
</ItemGroup>
<PropertyGroup>
<TargetPlatformVersion>111.0</TargetPlatformVersion>
<TargetPlatformIdentifier>ios</TargetPlatformIdentifier>
<TargetPlatformSupported>true</TargetPlatformSupported>
</PropertyGroup>
</Project>
";

File.WriteAllText(Path.Combine(testAsset.TestRoot, "Directory.Build.targets"), DirectoryBuildTargetsContent);

var buildCommand = new BuildCommand(testAsset);
buildCommand.Execute()
.Should()
.Fail()
.And
.HaveStdOutContaining("NETSDK1140")
.And
.HaveStdOutContaining(string.Format(Strings.InvalidTargetPlatformVersion, "111.0", "ios", "222.0").Split ('\n', '\r') [0])
.And
.HaveStdOutContaining("222.0");
}

[Fact]
public void It_fails_if_targetplatformversion_is_invalid()
{
var testProject = new TestProject()
{
Name = "It_fails_if_targetplatformversion_is_invalid",
TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
};
var testAsset = _testAssetsManager.CreateTestProject(testProject);


string DirectoryBuildTargetsContent = $@"
<Project>
<ItemGroup>
<SdkSupportedTargetPlatformVersion Include=""222.0"" />
</ItemGroup>
<PropertyGroup>
<TargetPlatformVersion>111.0</TargetPlatformVersion>
<TargetPlatformIdentifier>ios</TargetPlatformIdentifier>
<TargetPlatformSupported>true</TargetPlatformSupported>
</PropertyGroup>
</Project>
";

File.WriteAllText(Path.Combine(testAsset.TestRoot, "Directory.Build.targets"), DirectoryBuildTargetsContent);

var buildCommand = new BuildCommand(testAsset);
buildCommand.Execute()
.Should()
.Fail()
.And
.HaveStdOutContaining("NETSDK1140")
.And
.HaveStdOutContaining(string.Format(Strings.InvalidTargetPlatformVersion, "111.0", "ios", "222.0").Split ('\n', '\r') [0])
.And
.HaveStdOutContaining("222.0");
}
}
}

0 comments on commit 25b360d

Please sign in to comment.