From 350edb97639798119c63eaf911a0a9212c3ab9bb Mon Sep 17 00:00:00 2001 From: Matthias Langhard Date: Thu, 4 Nov 2021 10:53:30 +0100 Subject: [PATCH] fix: fixes bug in identifying neweset version. Adds a test for it --- src/Cli/Cli.csproj | 2 +- .../Queries/GetVersionInformationFromRepo.cs | 9 +++-- .../GetVersionInformationFromRepoTests.cs | 39 +++++++++++++++++++ .../update-tag.tests/update-tag.tests.csproj | 1 + 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tests/update-tag.tests/GetVersionInformationFromRepoTests.cs diff --git a/src/Cli/Cli.csproj b/src/Cli/Cli.csproj index 60dd655..188e877 100644 --- a/src/Cli/Cli.csproj +++ b/src/Cli/Cli.csproj @@ -9,7 +9,7 @@ Novaloop.UpdateTag Updates the tag of a repo to the next chosen version according the semver symantic. semver;update-tag;tag;git - 0.1.7 + 0.2.1 Matthias Langhard Novaloop AG https://gitlab.com/novaloop-oss/novaloop.update-tag diff --git a/src/Core/Queries/GetVersionInformationFromRepo.cs b/src/Core/Queries/GetVersionInformationFromRepo.cs index beb5d95..74821b6 100644 --- a/src/Core/Queries/GetVersionInformationFromRepo.cs +++ b/src/Core/Queries/GetVersionInformationFromRepo.cs @@ -1,12 +1,14 @@ using System; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Application.Interfaces; using Application.Models; using MediatR; namespace Application.Queries { - public class GetVersionInformationFromRepo : RequestHandler + public class GetVersionInformationFromRepo : IRequestHandler { public class Query : IRequest { @@ -27,7 +29,7 @@ namespace Application.Queries _gitRepoReadService = gitRepoReadService; } - protected override VersionInformation Handle(Query request) + public async Task Handle(Query request, CancellationToken cancellationToken) { var versions = _gitRepoReadService .GetAllVersions(request.RepositoryPath); @@ -42,10 +44,11 @@ namespace Application.Queries .OrderByDescending(v => v.Major) .ThenByDescending(v => v.Minor) .ThenByDescending(v => v.Patch) + .ThenByDescending(v => v.Rc == null) .ThenByDescending(v => v.Rc) .FirstOrDefault(); - return currentVersion == null ? null : new VersionInformation(currentVersion); + return await Task.FromResult(currentVersion == null ? null : new VersionInformation(currentVersion)); } } } \ No newline at end of file diff --git a/tests/update-tag.tests/GetVersionInformationFromRepoTests.cs b/tests/update-tag.tests/GetVersionInformationFromRepoTests.cs new file mode 100644 index 0000000..c2e661e --- /dev/null +++ b/tests/update-tag.tests/GetVersionInformationFromRepoTests.cs @@ -0,0 +1,39 @@ +using System.Collections.Generic; +using System.Threading; +using Application.Interfaces; +using Application.Models; +using Application.Queries; +using Moq; +using Xunit; + +namespace UpdateTag.Tests +{ + public class GetVersionInformationFromRepoTests + { + [Fact] + public async void DoesReadCurrentVersionCorrectly() + { + // Arrange + var mockedVersionList = new List + { + new Version(0, 1, 5), + new Version(0, 1, 7), + new Version(0, 2, 0), + new Version(0, 2, 0, 0), + new Version(0, 2, 0, 1), + new Version(0, 2, 0, 2) + }; + var gitRepoMock = new Mock(); + gitRepoMock.Setup(m => m.GetAllVersions(It.IsAny())) + .Returns(mockedVersionList); + var handler = new GetVersionInformationFromRepo(gitRepoMock.Object); + var query = new GetVersionInformationFromRepo.Query(""); + + // Act + var versionInformation = await handler.Handle(query, CancellationToken.None); + + // Assert + Assert.Equal("0.2.0", versionInformation.CurrentVersion.ToString()); + } + } +} \ No newline at end of file diff --git a/tests/update-tag.tests/update-tag.tests.csproj b/tests/update-tag.tests/update-tag.tests.csproj index ff64426..55071d9 100644 --- a/tests/update-tag.tests/update-tag.tests.csproj +++ b/tests/update-tag.tests/update-tag.tests.csproj @@ -9,6 +9,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive