Skip to content

Commit ae996f7

Browse files
authored
More exhaustive future-facing checks for supported SDK versions (#211)
* more exhaustive future-facing checks for supported SDK versions
1 parent 8fd8936 commit ae996f7

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

Test.Microsoft.NET.Build.Containers.Filesystem/TargetsTests.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void Cleanup()
3737
if (CombinedTargetsLocation != null) File.Delete(CombinedTargetsLocation);
3838
}
3939

40-
private Project InitProject(Dictionary<string, string> bonusProps)
40+
private (Project, IDisposable) InitProject(Dictionary<string, string> bonusProps, string logFileName = "log")
4141
{
4242
var props = new Dictionary<string, string>();
4343
// required parameters
@@ -46,33 +46,38 @@ private Project InitProject(Dictionary<string, string> bonusProps)
4646
props["_TargetFrameworkVersionWithoutV"] = "7.0";
4747
props["_NativeExecutableExtension"] = ".exe"; //TODO: windows/unix split here
4848
props["Version"] = "1.0.0"; // TODO: need to test non-compliant version strings here
49+
props["NETCoreSdkVersion"] = "7.0.100"; // we manipulate this value during evaluation, so we need a good default.
50+
// tests that rely on checking this value can override it with bonusProps.
4951

5052
// test setup parameters so that we can load the props/targets/tasks
5153
props["CustomTasksAssembly"] = Path.GetFullPath(Path.Combine(".", "Microsoft.NET.Build.Containers.dll"));
5254
props["_IsTest"] = "true";
5355

5456
var loggers = new List<ILogger>
5557
{
56-
// new Microsoft.Build.Logging.BinaryLogger() {CollectProjectImports = Microsoft.Build.Logging.BinaryLogger.ProjectImportsCollectionMode.Embed, Verbosity = LoggerVerbosity.Diagnostic, Parameters = "LogFile=blah.binlog" },
58+
// new global::Microsoft.Build.Logging.BinaryLogger() {CollectProjectImports = global::Microsoft.Build.Logging.BinaryLogger.ProjectImportsCollectionMode.Embed, Verbosity = LoggerVerbosity.Diagnostic, Parameters = $"LogFile={logFileName}.binlog" },
5759
new global::Microsoft.Build.Logging.ConsoleLogger(LoggerVerbosity.Detailed)
5860
};
5961
var collection = new ProjectCollection(null, loggers, ToolsetDefinitionLocations.Default);
6062
foreach (var kvp in bonusProps)
6163
{
6264
props[kvp.Key] = kvp.Value;
6365
}
64-
return collection.LoadProject(CombinedTargetsLocation, props, null);
66+
var p = collection.LoadProject(CombinedTargetsLocation, props, null);
67+
68+
return (p, collection);
6569
}
6670

6771
[DataRow(true, "/app/foo.exe")]
6872
[DataRow(false, "dotnet", "/app/foo.dll")]
6973
[TestMethod]
7074
public void CanSetEntrypointArgsToUseAppHost(bool useAppHost, params string[] entrypointArgs)
7175
{
72-
var project = InitProject(new()
76+
var (project, dispose) = InitProject(new()
7377
{
7478
["UseAppHost"] = useAppHost.ToString()
7579
});
80+
using var _ = dispose;
7681
Assert.IsTrue(project.Build("ComputeContainerConfig"));
7782
var computedEntrypointArgs = project.GetItems("ContainerEntrypoint").Select(i => i.EvaluatedInclude).ToArray();
7883
foreach (var (First, Second) in entrypointArgs.Zip(computedEntrypointArgs))
@@ -89,12 +94,33 @@ public void CanSetEntrypointArgsToUseAppHost(bool useAppHost, params string[] en
8994
[TestMethod]
9095
public void CanNormalizeInputContainerNames(string projectName, string expectedContainerImageName, bool shouldPass)
9196
{
92-
var project = InitProject(new()
97+
var (project, dispose) = InitProject(new()
9398
{
9499
["AssemblyName"] = projectName
95100
});
101+
using var _ = dispose;
96102
var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None);
97103
Assert.AreEqual(shouldPass, instance.Build(new[]{"ComputeContainerConfig"}, null, null, out var outputs), "Build should have succeeded");
98104
Assert.AreEqual(expectedContainerImageName, instance.GetPropertyValue("ContainerImageName"));
99105
}
106+
107+
[DataRow("7.0.100", true)]
108+
[DataRow("8.0.100", true)]
109+
[DataRow("7.0.100-preview.7", true)]
110+
[DataRow("7.0.100-rc.1", true)]
111+
[DataRow("6.0.100", false)]
112+
[DataRow("7.0.100-preview.1", false)]
113+
[TestMethod]
114+
public void CanWarnOnInvalidSDKVersions(string sdkVersion, bool isAllowed) {
115+
var (project, dispose) = InitProject(new()
116+
{
117+
["NETCoreSdkVersion"] = sdkVersion,
118+
["PublishProfile"] = "DefaultContainer"
119+
}, $"version-test-{sdkVersion}");
120+
using var _ = dispose;
121+
var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None);
122+
var derivedIsAllowed = Boolean.Parse(project.GetProperty("_IsSDKContainerAllowedVersion").EvaluatedValue);
123+
// var buildResult = instance.Build(new[]{"_ContainerVerifySDKVersion"}, null, null, out var outputs);
124+
Assert.AreEqual(isAllowed, derivedIsAllowed, $"SDK version {(isAllowed ? "should" : "should not")} have been allowed ");
125+
}
100126
}

packaging/build/Microsoft.NET.Build.Containers.targets

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
<Project>
2+
<PropertyGroup>
3+
<_IsSDKContainerAllowedVersion>false</_IsSDKContainerAllowedVersion>
4+
<!-- Anything newer than 7.0.100-preview.7 is supported -->
5+
<_IsSDKContainerAllowedVersion
6+
Condition="$([MSBuild]::VersionGreaterThan($(NetCoreSdkVersion), 7.0.100))
7+
OR ( $([MSBuild]::VersionEquals($(NetCoreSdkVersion), 7.0.100))
8+
AND (
9+
$(NETCoreSdkVersion.Contains('-preview.7'))
10+
OR $(NETCoreSdkVersion.Contains('-rc'))
11+
OR $(NETCoreSdkVersion.Contains('-')) == false
12+
)
13+
)">true</_IsSDKContainerAllowedVersion>
14+
</PropertyGroup>
15+
216
<Target Name="_ContainerVerifySDKVersion"
317
Condition="'$(WebPublishMethod)' == 'Container' or '$(PublishProfile)' == 'DefaultContainer'"
418
BeforeTargets="AfterPublish">
519
<!-- If the user has opted into container publishing via their own profile (WebPublishMethod = Container) or
620
via the default Profile (PublishProfile = DefaultContainer), make sure they're on a supported SDK version.
721
We do the explicit profile name check here because for preview6 for example the profile didn't exist, so we
822
can't rely only on the WebPublishMethod. -->
9-
<PropertyGroup>
10-
<!-- Allow preview 7, any RC, or any stable version of 7 -->
11-
<_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</_IsAllowedVersion>
12-
</PropertyGroup>
13-
<Error Condition="'$(_IsAllowedVersion)' != 'true'" Code="CONTAINER002" Text="The current .NET SDK ($(NETCoreSdkVersion)) doesn't support containerization. Please use version 7.0.100-preview.7 or higher." />
23+
<Error Condition="'$(_IsSDKContainerAllowedVersion)' != 'true'" Code="CONTAINER002" Text="The current .NET SDK ($(NETCoreSdkVersion)) doesn't support containerization. Please use version 7.0.100 or higher to enable containerization." />
1424
</Target>
1525

1626
<Target Name="ComputeContainerConfig">

0 commit comments

Comments
 (0)