diff --git a/src/core-sdk-tasks/CalculateTemplateVersions.cs b/src/core-sdk-tasks/CalculateTemplateVersions.cs index abbc987fe..55d58fd40 100644 --- a/src/core-sdk-tasks/CalculateTemplateVersions.cs +++ b/src/core-sdk-tasks/CalculateTemplateVersions.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System; -using System.Text.RegularExpressions; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using NuGet.Versioning; @@ -49,11 +47,7 @@ namespace Microsoft.DotNet.Cli.Build { var aspNetCoreTemplate = NuGetVersion.Parse(aspNetCorePackageVersionTemplate); - // due to historical bug https://github.com/dotnet/core-sdk/issues/6243 - // we need to increase patch version by one in order to "reset" existing install ComponentId - // more in the above bug's detail - var baseMajorMinorPatch = new NuGetVersion(aspNetCoreTemplate.Major, aspNetCoreTemplate.Minor, - aspNetCoreTemplate.Patch + _patchVersionResetOffset); + NuGetVersion baseMajorMinorPatch = GetBaseMajorMinorPatch(aspNetCoreTemplate); string bundledTemplateInstallPath = aspNetCoreTemplate.IsPrerelease ? $"{baseMajorMinorPatch.Major}.{baseMajorMinorPatch.Minor}.{baseMajorMinorPatch.Patch}-{versionSuffix}" @@ -64,5 +58,23 @@ namespace Microsoft.DotNet.Cli.Build bundledTemplateInstallPath, $"{baseMajorMinorPatch.Major}.{baseMajorMinorPatch.Minor}"); } + + private static NuGetVersion GetBaseMajorMinorPatch(NuGetVersion aspNetCoreTemplate) + { + // due to historical bug https://github.com/dotnet/core-sdk/issues/6243 + // we need to increase patch version by one in order to "reset" existing install ComponentId + // more in the above bug's detail. + // There is no non-deterministic existing ComponentId under Major version 5. + // so only apply the patch bump when below 5 + + int basePatch = + aspNetCoreTemplate.Major < 5 + ? aspNetCoreTemplate.Patch + _patchVersionResetOffset + : aspNetCoreTemplate.Patch; + + var baseMajorMinorPatch = new NuGetVersion(aspNetCoreTemplate.Major, aspNetCoreTemplate.Minor, + basePatch); + return baseMajorMinorPatch; + } } } diff --git a/test/core-sdk-tasks.Tests/CalculateTemplateVerionsTests.cs b/test/core-sdk-tasks.Tests/CalculateTemplateVerionsTests.cs index 041337b45..8f25cc34e 100644 --- a/test/core-sdk-tasks.Tests/CalculateTemplateVerionsTests.cs +++ b/test/core-sdk-tasks.Tests/CalculateTemplateVerionsTests.cs @@ -7,22 +7,43 @@ namespace EndToEnd public class CalculateTemplateVersionsTests { [Fact] - public void ItCanCalculateTemplateVersionsInStableBuilds() + public void WhenAspNetCoreTemplateMajorVersionLowerthan3ItCanCalculateTemplateVersionsInStableBuilds() { var result = CalculateTemplateVersions.Calculate("3.1.0", "014885", "dev"); - result.BundledTemplateInstallPath.Should().Be("3.1.1", - "the patch is 1 higher than aspnetTemplateVersion due to https://github.com/dotnet/core-sdk/issues/6243"); - result.BundledTemplateMsiVersion.Should().Be("3.1.1.014885"); - result.BundledTemplateMajorMinorVersion.Should().Be("3.1"); + + result.Should() + .Be(("3.1.1.014885", "3.1.1", "3.1"), + "the patch is 1 higher than aspnetTemplateVersion " + + "due to https://github.com/dotnet/core-sdk/issues/6243"); } [Fact] - public void ItCanCalculateTemplateVersionsInNonStableBuilds() + public void WhenAspNetCoreTemplateMajorVersionLowerthan3ItCanCalculateTemplateVersionsInNonStableBuilds() + { + var result = CalculateTemplateVersions.Calculate("3.0.0-alpha.1.20071.6", "014885", "dev"); + + result.Should() + .Be(("3.0.1.014885", "3.0.1-dev", "3.0")); + } + + [Fact] + public void WhenAspNetCoreTemplateMajorVersionHigherthan3ItCanCalculateTemplateVersionsInStableBuilds() + { + var result = CalculateTemplateVersions.Calculate("5.1.0", "014885", "dev"); + + result.Should() + .Be(("5.1.0.014885", "5.1.0", "5.1"), + "the patch align with AspNetCoreTemplateMajorVersion again, " + + "since there is no non-deterministic existing ComponentId under Major version 5."); + } + + [Fact] + public void WhenAspNetCoreTemplateMajorVersionHigherthan3ItCanCalculateTemplateVersionsInNonStableBuilds() { var result = CalculateTemplateVersions.Calculate("5.0.0-alpha.1.20071.6", "014885", "dev"); - result.BundledTemplateInstallPath.Should().Be("5.0.1-dev"); - result.BundledTemplateMsiVersion.Should().Be("5.0.1.014885"); - result.BundledTemplateMajorMinorVersion.Should().Be("5.0"); + + result.Should() + .Be(("5.0.0.014885", "5.0.0-dev", "5.0")); } } }