From f4cfce101de74d65b385370df6525e40151774fc Mon Sep 17 00:00:00 2001 From: Matthias Langhard Date: Tue, 2 Nov 2021 20:54:23 +0100 Subject: [PATCH] chore: optimizing next version handling --- src/Cli/AppRunner.cs | 9 +- src/Core/Models/Version.cs | 98 +++++++++++++------ src/Core/Models/VersionInformation.cs | 36 +++++-- .../VersionInformationTests.cs | 46 +++++++++ tests/update-tag.tests/VersionTests.cs | 38 ++++--- 5 files changed, 163 insertions(+), 64 deletions(-) create mode 100644 tests/update-tag.tests/VersionInformationTests.cs diff --git a/src/Cli/AppRunner.cs b/src/Cli/AppRunner.cs index 3cb4d8a..ad8e7de 100644 --- a/src/Cli/AppRunner.cs +++ b/src/Cli/AppRunner.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Application.Commands; using Application.Queries; @@ -82,10 +83,10 @@ namespace Cli .Title($"Select new version. (Current version is [green]{versionInfo.CurrentVersion}[/])") .PageSize(10) .AddChoices( - new Selection("rc ", versionInfo.NextMinorRcVersion), - new Selection("patch", versionInfo.NextPatchVersion), - new Selection("minor", versionInfo.NextMinorVersion), - new Selection("major", versionInfo.NextMajorVersion) + versionInfo + .NextVersions + .Select(nv => new Selection(nv.Title, nv.Version)) + .ToList() ) ); } diff --git a/src/Core/Models/Version.cs b/src/Core/Models/Version.cs index 4ff53f5..d7aa238 100644 --- a/src/Core/Models/Version.cs +++ b/src/Core/Models/Version.cs @@ -1,3 +1,4 @@ +using System; using System.Text; using System.Text.RegularExpressions; @@ -19,7 +20,7 @@ namespace Application.Models Patch = patch; } - public Version(int major, int minor, int patch, int rc) + public Version(int major, int minor, int patch, int? rc) { Major = major; Minor = minor; @@ -32,7 +33,16 @@ namespace Application.Models Major = major; Minor = minor; Patch = patch; - Rc = ExtractNumberFromRcString(rc); + Rc = rc == null ? null : ExtractNumberFromRcString(rc); + Service = service ?? ""; + } + + public Version(int major, int minor, int patch, int? rc, string service) + { + Major = major; + Minor = minor; + Patch = patch; + Rc = rc; Service = service ?? ""; } @@ -75,62 +85,88 @@ namespace Application.Models return sb.ToString(); } + private Version Copy() + { + return new Version(Major, Minor, Patch, Rc, Service); + } + public Version NextMajor() { - var nextVersion = new Version(Major, Minor, Patch, Rc.ToString(), Service); - nextVersion.BumpMajor(); + if (Rc != null) + { + throw new ArgumentException("Cannot create next Major. Release RC first."); + } + + var nextVersion = Copy(); + nextVersion.Major++; + nextVersion.Minor = 0; + nextVersion.Patch = 0; return nextVersion; } public Version NextMinor() { - var nextVersion = new Version(Major, Minor, Patch, Rc.ToString(), Service); - nextVersion.BumpMinor(); + if (Rc != null) + { + throw new ArgumentException("Cannot create next Minor. Release RC first."); + } + + var nextVersion = Copy(); + nextVersion.Minor++; + nextVersion.Patch = 0; return nextVersion; } public Version NextPatch() { - var nextVersion = new Version(Major, Minor, Patch, Rc.ToString(), Service); - nextVersion.BumpPatch(); + if (Rc != null) + { + throw new ArgumentException("Cannot create next Patch. Release RC first."); + } + + var nextVersion = Copy(); + nextVersion.Patch++; return nextVersion; } public Version NextRc() { - var nextVersion = new Version(Major, Minor, Patch, Rc.ToString(), Service); - nextVersion.BumpRc(); + if (Rc == null) + { + throw new ArgumentException("Cannot create next RC. Not an RC."); + } + + var nextVersion = Copy(); + nextVersion.Rc++; return nextVersion; } - public void BumpMajor() + public Version CreateRc() { - Major++; - Minor = 0; - Patch = 0; - Rc = null; + if (Rc != null) + { + throw new ArgumentException("Cannot create RC. Already an RC."); + } + + var nextVersion = Copy(); + nextVersion.Rc = 0; + return nextVersion; } - public void BumpMinor() - { - Minor++; - Patch = 0; - Rc = null; - } - - public void BumpPatch() - { - Patch++; - Rc = null; - } - - public void BumpRc() + public Version ReleaseRc() { if (Rc == null) { - BumpMinor(); + throw new ArgumentException("Cannot release RC. Not an RC."); } - Rc = Rc == null ? 0 : Rc + 1; + + var nextVersion = new Version(Major, Minor, Patch, (string)null, Service); + return nextVersion; + } + + public bool IsRc() + { + return Rc != null; } } } \ No newline at end of file diff --git a/src/Core/Models/VersionInformation.cs b/src/Core/Models/VersionInformation.cs index 2296d33..d8cc7b7 100644 --- a/src/Core/Models/VersionInformation.cs +++ b/src/Core/Models/VersionInformation.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + namespace Application.Models { public class VersionInformation @@ -5,16 +7,34 @@ namespace Application.Models public VersionInformation(Version currentVersion) { CurrentVersion = currentVersion; - NextMajorVersion = currentVersion.NextMajor(); - NextMinorVersion = currentVersion.NextMinor(); - NextPatchVersion = currentVersion.NextPatch(); - NextMinorRcVersion = currentVersion.NextRc(); + if (currentVersion.IsRc()) + { + NextVersions.Add(new NextVersion("next rc", currentVersion.NextRc())); + NextVersions.Add(new NextVersion("release", currentVersion.ReleaseRc())); + } + else + { + NextVersions.Add(new NextVersion("rc", currentVersion.CreateRc())); + NextVersions.Add(new NextVersion("patch", currentVersion.NextPatch())); + NextVersions.Add(new NextVersion("minor", currentVersion.NextMinor())); + NextVersions.Add(new NextVersion("major", currentVersion.NextMajor())); + } } public Version CurrentVersion { get; } - public Version NextMajorVersion { get; } - public Version NextMinorVersion { get; } - public Version NextPatchVersion { get; } - public Version NextMinorRcVersion { get; } + + public List NextVersions = new List(); + } + + public class NextVersion + { + public NextVersion(string title, Version version) + { + Title = title; + Version = version; + } + + public string Title { get; } + public Version Version { get; } } } \ No newline at end of file diff --git a/tests/update-tag.tests/VersionInformationTests.cs b/tests/update-tag.tests/VersionInformationTests.cs new file mode 100644 index 0000000..af32a1a --- /dev/null +++ b/tests/update-tag.tests/VersionInformationTests.cs @@ -0,0 +1,46 @@ +using System.Linq; +using Application.Models; +using Xunit; + +namespace UpdateTag.Tests +{ + public class VersionInformationTests + { + [Fact] + public void CorrectNextVersionsFromRc() + + { + // Arrange + var version = new Version(1, 0, 1, 1); + + // Act + var versionInformation = new VersionInformation(version); + var versions = versionInformation.NextVersions.Select(nv => nv.Version.ToString()).ToList(); + + // Assert + Assert.Contains("1.0.1-RC.2", versions); + Assert.Contains("1.0.1", versions); + Assert.Equal(2, versions.Count); + } + + [Fact] + public void CorrectNextVersionsFromMinor() + + { + // Arrange + var version = new Version(1, 0, 1); + + // Act + var versionInformation = new VersionInformation(version); + var versions = versionInformation.NextVersions.Select(nv => nv.Version.ToString()).ToList(); + + + // Assert + Assert.Contains("1.0.1-RC.0", versions); + Assert.Contains("1.0.2", versions); + Assert.Contains("1.1.0", versions); + Assert.Contains("2.0.0", versions); + Assert.Equal(4, versions.Count); + } + } +} \ No newline at end of file diff --git a/tests/update-tag.tests/VersionTests.cs b/tests/update-tag.tests/VersionTests.cs index 95c5ae3..135f605 100644 --- a/tests/update-tag.tests/VersionTests.cs +++ b/tests/update-tag.tests/VersionTests.cs @@ -9,13 +9,10 @@ namespace UpdateTag.Tests [InlineData(1, 0, 0, "", "", "2.0.0")] [InlineData(1, 1, 0, "", "", "2.0.0")] [InlineData(1, 1, 1, "", "", "2.0.0")] - [InlineData(1, 1, 1, "RC.4", "", "2.0.0")] - [InlineData(1, 1, 1, "RC.4", "ErpNext", "2.0.0+ErpNext")] - public void BumpMajor(int major, int minor, int patch, string rc, string service, string expected) + public void NextMajor(int major, int minor, int patch, string rc, string service, string expected) { - var version = new Version(major, minor, patch, rc, service); - version.BumpMajor(); + var version = new Version(major, minor, patch, rc, service).NextMajor(); Assert.Equal(expected, version.ToString()); } @@ -24,13 +21,10 @@ namespace UpdateTag.Tests [InlineData(1, 0, 0, "", "", "1.1.0")] [InlineData(1, 1, 0, "", "", "1.2.0")] [InlineData(1, 1, 1, "", "", "1.2.0")] - [InlineData(1, 1, 1, "RC.4", "", "1.2.0")] - [InlineData(1, 1, 1, "RC.4", "ErpNext", "1.2.0+ErpNext")] - public void BumpMinor(int major, int minor, int patch, string rc, string service, string expected) + public void NextMinor(int major, int minor, int patch, string rc, string service, string expected) { - var version = new Version(major, minor, patch, rc, service); - version.BumpMinor(); + var version = new Version(major, minor, patch, rc, service).NextMinor(); Assert.Equal(expected, version.ToString()); } @@ -38,27 +32,29 @@ namespace UpdateTag.Tests [InlineData(1, 0, 0, "", "", "1.0.1")] [InlineData(1, 1, 0, "", "", "1.1.1")] [InlineData(1, 1, 1, "", "", "1.1.2")] - [InlineData(1, 1, 1, "RC.4", "", "1.1.2")] - [InlineData(1, 1, 1, "RC.4", "ErpNext", "1.1.2+ErpNext")] - public void BumpPatch(int major, int minor, int patch, string rc, string service, string expected) + public void NextPatch(int major, int minor, int patch, string rc, string service, string expected) { - var version = new Version(major, minor, patch, rc, service); - version.BumpPatch(); + var version = new Version(major, minor, patch, rc, service).NextPatch(); Assert.Equal(expected, version.ToString()); } [Theory] - [InlineData(1, 0, 0, "", "", "1.1.0-RC.0")] - [InlineData(1, 1, 0, "", "", "1.2.0-RC.0")] - [InlineData(1, 1, 1, "", "", "1.2.0-RC.0")] [InlineData(1, 1, 1, "RC.4", "", "1.1.1-RC.5")] [InlineData(1, 1, 1, "RC.4", "ErpNext", "1.1.1-RC.5+ErpNext")] - public void BumpRc(int major, int minor, int patch, string rc, string service, string expected) + public void NextRc(int major, int minor, int patch, string rc, string service, string expected) { - var version = new Version(major, minor, patch, rc, service); - version.BumpRc(); + var version = new Version(major, minor, patch, rc, service).NextRc(); + Assert.Equal(expected, version.ToString()); + } + + [Theory] + [InlineData(1, 1, 1, "RC.4", "", "1.1.1")] + [InlineData(1, 1, 1, "RC.4", "ErpNext", "1.1.1+ErpNext")] + public void ReleaseRc(int major, int minor, int patch, string rc, string service, string expected) + { + var version = new Version(major, minor, patch, rc, service).ReleaseRc(); Assert.Equal(expected, version.ToString()); } }