From ae996f7cd5f69f14ed830eb73045b1b815e167e7 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Tue, 25 Oct 2022 13:23:48 -0500 Subject: [PATCH] More exhaustive future-facing checks for supported SDK versions (#211) * more exhaustive future-facing checks for supported SDK versions --- .../TargetsTests.cs | 36 ++++++++++++++++--- .../Microsoft.NET.Build.Containers.targets | 20 ++++++++--- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Test.Microsoft.NET.Build.Containers.Filesystem/TargetsTests.cs b/Test.Microsoft.NET.Build.Containers.Filesystem/TargetsTests.cs index f226ad5d..e170fb45 100644 --- a/Test.Microsoft.NET.Build.Containers.Filesystem/TargetsTests.cs +++ b/Test.Microsoft.NET.Build.Containers.Filesystem/TargetsTests.cs @@ -37,7 +37,7 @@ public static void Cleanup() if (CombinedTargetsLocation != null) File.Delete(CombinedTargetsLocation); } - private Project InitProject(Dictionary bonusProps) + private (Project, IDisposable) InitProject(Dictionary bonusProps, string logFileName = "log") { var props = new Dictionary(); // required parameters @@ -46,6 +46,8 @@ private Project InitProject(Dictionary bonusProps) props["_TargetFrameworkVersionWithoutV"] = "7.0"; props["_NativeExecutableExtension"] = ".exe"; //TODO: windows/unix split here props["Version"] = "1.0.0"; // TODO: need to test non-compliant version strings here + props["NETCoreSdkVersion"] = "7.0.100"; // we manipulate this value during evaluation, so we need a good default. + // tests that rely on checking this value can override it with bonusProps. // test setup parameters so that we can load the props/targets/tasks props["CustomTasksAssembly"] = Path.GetFullPath(Path.Combine(".", "Microsoft.NET.Build.Containers.dll")); @@ -53,7 +55,7 @@ private Project InitProject(Dictionary bonusProps) var loggers = new List { - // new Microsoft.Build.Logging.BinaryLogger() {CollectProjectImports = Microsoft.Build.Logging.BinaryLogger.ProjectImportsCollectionMode.Embed, Verbosity = LoggerVerbosity.Diagnostic, Parameters = "LogFile=blah.binlog" }, + // new global::Microsoft.Build.Logging.BinaryLogger() {CollectProjectImports = global::Microsoft.Build.Logging.BinaryLogger.ProjectImportsCollectionMode.Embed, Verbosity = LoggerVerbosity.Diagnostic, Parameters = $"LogFile={logFileName}.binlog" }, new global::Microsoft.Build.Logging.ConsoleLogger(LoggerVerbosity.Detailed) }; var collection = new ProjectCollection(null, loggers, ToolsetDefinitionLocations.Default); @@ -61,7 +63,9 @@ private Project InitProject(Dictionary bonusProps) { props[kvp.Key] = kvp.Value; } - return collection.LoadProject(CombinedTargetsLocation, props, null); + var p = collection.LoadProject(CombinedTargetsLocation, props, null); + + return (p, collection); } [DataRow(true, "/app/foo.exe")] @@ -69,10 +73,11 @@ private Project InitProject(Dictionary bonusProps) [TestMethod] public void CanSetEntrypointArgsToUseAppHost(bool useAppHost, params string[] entrypointArgs) { - var project = InitProject(new() + var (project, dispose) = InitProject(new() { ["UseAppHost"] = useAppHost.ToString() }); + using var _ = dispose; Assert.IsTrue(project.Build("ComputeContainerConfig")); var computedEntrypointArgs = project.GetItems("ContainerEntrypoint").Select(i => i.EvaluatedInclude).ToArray(); foreach (var (First, Second) in entrypointArgs.Zip(computedEntrypointArgs)) @@ -89,12 +94,33 @@ public void CanSetEntrypointArgsToUseAppHost(bool useAppHost, params string[] en [TestMethod] public void CanNormalizeInputContainerNames(string projectName, string expectedContainerImageName, bool shouldPass) { - var project = InitProject(new() + var (project, dispose) = InitProject(new() { ["AssemblyName"] = projectName }); + using var _ = dispose; var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None); Assert.AreEqual(shouldPass, instance.Build(new[]{"ComputeContainerConfig"}, null, null, out var outputs), "Build should have succeeded"); Assert.AreEqual(expectedContainerImageName, instance.GetPropertyValue("ContainerImageName")); } + + [DataRow("7.0.100", true)] + [DataRow("8.0.100", true)] + [DataRow("7.0.100-preview.7", true)] + [DataRow("7.0.100-rc.1", true)] + [DataRow("6.0.100", false)] + [DataRow("7.0.100-preview.1", false)] + [TestMethod] + public void CanWarnOnInvalidSDKVersions(string sdkVersion, bool isAllowed) { + var (project, dispose) = InitProject(new() + { + ["NETCoreSdkVersion"] = sdkVersion, + ["PublishProfile"] = "DefaultContainer" + }, $"version-test-{sdkVersion}"); + using var _ = dispose; + var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None); + var derivedIsAllowed = Boolean.Parse(project.GetProperty("_IsSDKContainerAllowedVersion").EvaluatedValue); + // var buildResult = instance.Build(new[]{"_ContainerVerifySDKVersion"}, null, null, out var outputs); + Assert.AreEqual(isAllowed, derivedIsAllowed, $"SDK version {(isAllowed ? "should" : "should not")} have been allowed "); + } } \ No newline at end of file diff --git a/packaging/build/Microsoft.NET.Build.Containers.targets b/packaging/build/Microsoft.NET.Build.Containers.targets index 329eda8e..b6912549 100644 --- a/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/packaging/build/Microsoft.NET.Build.Containers.targets @@ -1,4 +1,18 @@ + + <_IsSDKContainerAllowedVersion>false + + <_IsSDKContainerAllowedVersion + Condition="$([MSBuild]::VersionGreaterThan($(NetCoreSdkVersion), 7.0.100)) + OR ( $([MSBuild]::VersionEquals($(NetCoreSdkVersion), 7.0.100)) + AND ( + $(NETCoreSdkVersion.Contains('-preview.7')) + OR $(NETCoreSdkVersion.Contains('-rc')) + OR $(NETCoreSdkVersion.Contains('-')) == false + ) + )">true + + @@ -6,11 +20,7 @@ via the default Profile (PublishProfile = DefaultContainer), make sure they're on a supported SDK version. We do the explicit profile name check here because for preview6 for example the profile didn't exist, so we can't rely only on the WebPublishMethod. --> - - - <_IsAllowedVersion Condition="$(NETCoreSdkVersion.StartsWith('7.0.100-preview.7')) or $(NETCoreSdkVersion.StartsWith('7.0.100-rc')) or ($(NETCoreSdkVersion.StartsWith('7.0.10')) and $(NETCoreSdkVersion.Contains('-')) == false)">true - - +