19 Commits

Author SHA1 Message Date
Markus Huggler
3ddf81bd1d chore: updates LibGit2Sharp to the preview version because OpenSSL 1.0 is removed from a lot of distros 2021-12-01 13:52:57 +01:00
Matthias Langhard
b6a4a8f8ec chore: bumps version number 2021-11-04 21:23:06 +01:00
Matthias Langhard
5e1dd98ecd deploy: runs tests only on tags 2021-11-04 21:22:49 +01:00
Matthias Langhard
7b7fafbce9 feat: implements spinner which runs when pushing tags / commits to remote 2021-11-04 21:20:41 +01:00
Matthias Langhard
8a8b16c655 feat: implements better commandline args parsing with '--version' and '--help' outputs 2021-11-04 21:13:40 +01:00
Matthias Langhard
c45ba92f41 feat: adds a 'v' prefix to the new version based on wether the newest last version had one 2021-11-04 17:00:49 +01:00
Matthias Langhard
5d2c1d8f63 feat: bumps version number 2021-11-04 13:39:10 +01:00
Matthias Langhard
aeae68b5c4 feat: allows pipeline to run with 'v' prefix 2021-11-04 13:38:51 +01:00
Matthias Langhard
3acdb3e38c fix: fixes missing 'v' prefix. Sorry mäge, force of habit :P 2021-11-04 11:50:02 +01:00
Matthias Langhard
350edb9763 fix: fixes bug in identifying neweset version. Adds a test for it 2021-11-04 10:53:30 +01:00
Matthias Langhard
9c313526e7 feat: implements option to add a service-name when initializing first tag 2021-11-04 08:22:09 +01:00
Matthias Langhard
bbf09be431 feat: improving push-functionality 2021-11-03 08:35:48 +01:00
Matthias Langhard
808f0e4564 chore: improves next version selection 2021-11-03 08:24:17 +01:00
Matthias Langhard
0ca934cfa8 chore: fixing package project-url 2021-11-03 08:11:31 +01:00
Matthias Langhard
189cbe0ae8 chore: updating tool to 0.1.4 2021-11-03 08:03:48 +01:00
Matthias Langhard
0cdd2aa4be chore: adds update command to README.md 2021-11-03 08:02:44 +01:00
Matthias Langhard
f4cfce101d chore: optimizing next version handling 2021-11-03 07:58:20 +01:00
Matthias Langhard
38c94e315f chore: small refactorings 2021-11-02 20:25:02 +01:00
Matthias Langhard
0ede69ccc4 chore: improvements 2021-11-02 12:21:27 +01:00
21 changed files with 552 additions and 103 deletions

View File

@@ -3,6 +3,8 @@ stages:
- publish
running tests for tag:
only:
- tags
image: mcr.microsoft.com/dotnet/sdk:5.0
stage: test
script:
@@ -10,7 +12,7 @@ running tests for tag:
publish to nuget:
only:
- /^\d*.\d*.\d*$/ # gets triggered if the commit tag is in the form n.n.n where n is any number
- /^v+\d*.\d*.\d*$/ # gets triggered if the commit tag is in the form n.n.n where n is any number
image: mcr.microsoft.com/dotnet/sdk:5.0
stage: publish
script:

View File

@@ -1,3 +1,15 @@
# update-tag
Updates the tag of a repo to the next chosen version according the semver symantic.
Updates the tag of a repo to the next chosen version according the semver symantic.
## Install
```
dotnet tool install --global Novaloop.UpdateTag
```
## Update
```
dotnet tool update --global Novaloop.UpdateTag
```

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Commands;
using Application.Queries;
@@ -19,13 +20,45 @@ namespace Cli
_mediator = mediator;
}
public async Task Run(string repoPath)
public async Task Run(string workingDir)
{
var repoBasePath = await GetRepoBasePath(workingDir);
var chosenService = await ChooseService(repoBasePath);
var selection = await SelectVersion(repoBasePath, chosenService);
await AnsiConsole.Status()
.StartAsync("pushing to remote...", async _ =>
{
await AddVersionTagToRepo(repoBasePath, selection.Version.ToString());
await PushTagsToRemote(repoBasePath);
await PushCommitsToRemote(repoBasePath);
}
);
}
private async Task<string> GetRepoBasePath(string workingDir)
{
var repoBasePath = "";
try
{
repoBasePath = await _mediator.Send(new GetRepoBasePath.Query(workingDir));
}
catch (CliWrap.Exceptions.CommandExecutionException)
{
AnsiConsole.Markup("[red]Error:[/] Unable to extract Versions. Are we running inside a git repository?\n\n");
Environment.Exit(1);
}
return repoBasePath;
}
private async Task<string> ChooseService(string repoBasePath)
{
// Check if git dir
var services = new List<string>();
try
{
services = await _mediator.Send(new GetServicesFromGitRepo.Query(repoPath));
services = await _mediator.Send(new GetServicesFromGitRepo.Query(repoBasePath));
}
catch (LibGit2Sharp.RepositoryNotFoundException)
{
@@ -44,8 +77,12 @@ namespace Cli
.AddChoices(services));
}
return chosenService;
}
var versionInfo = await _mediator.Send(new GetVersionInformationFromRepo.Query(repoPath, chosenService));
private async Task<Selection> SelectVersion(string repoBasePath, string chosenService)
{
var versionInfo = await _mediator.Send(new GetVersionInformationFromRepo.Query(repoBasePath, chosenService));
Selection selection;
if (versionInfo != null)
{
@@ -54,10 +91,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()
)
);
}
@@ -68,11 +105,20 @@ namespace Cli
.Title("[red]Error evaluating version from newest tag.[/]\nAdd new version tag **AND** push to origin?)")
.PageSize(10)
.AddChoices(
new Selection("yes", new Version(0, 1, 0)),
new Selection("yes", new Version(0, 1, 0, 1)),
new Selection("yes", new Version(0, 1, 0, true)),
new Selection("yes", new Version(0, 1, 0, 1, true)),
new Selection("no", null)
)
);
var serviceName = AnsiConsole.Prompt(
new TextPrompt<string>("[grey][[Optional]][/] Enter [green]service name[/]:")
.AllowEmpty()
);
if (!string.IsNullOrWhiteSpace(serviceName))
{
selection.Version.SetService(serviceName.Trim());
}
}
if (selection.Version == null)
@@ -80,20 +126,15 @@ namespace Cli
Environment.Exit(0);
}
try
{
await _mediator.Send(new AddTagToGitRepo.Command(repoPath, selection.Version.ToString()));
}
catch (Exception ex)
{
AnsiConsole.Markup("[red]Error:[/] Unable to write Tag to repository\n\n");
AnsiConsole.WriteException(ex);
Environment.Exit(1);
}
return selection;
}
private async Task PushTagsToRemote(string workingDir)
{
try
{
await _mediator.Send(new PushCommitsToRemote.Command(repoPath));
var output = await _mediator.Send(new PushTagsToRemote.Command(workingDir));
AnsiConsole.Write(output);
}
catch (Exception ex)
{
@@ -102,5 +143,34 @@ namespace Cli
Environment.Exit(1);
}
}
private async Task PushCommitsToRemote(string workingDir)
{
try
{
var output = await _mediator.Send(new PushCommitsToRemote.Command(workingDir));
AnsiConsole.Write(output);
}
catch (Exception ex)
{
AnsiConsole.Markup("[red]Error:[/] Tag was written but unable to push to remote\n\n");
AnsiConsole.WriteException(ex);
Environment.Exit(1);
}
}
private async Task AddVersionTagToRepo(string workingDir, string tag)
{
try
{
await _mediator.Send(new AddTagToGitRepo.Command(workingDir, tag));
}
catch (Exception ex)
{
AnsiConsole.Markup("[red]Error:[/] Unable to write Tag to repository\n\n");
AnsiConsole.WriteException(ex);
Environment.Exit(1);
}
}
}
}

View File

@@ -6,18 +6,20 @@
<RootNamespace>Cli</RootNamespace>
<PackAsTool>true</PackAsTool>
<ToolCommandName>update-tag</ToolCommandName>
<AssemblyTitle>update-tag</AssemblyTitle>
<PackageId>Novaloop.UpdateTag</PackageId>
<title>Updates the tag of a repo to the next chosen version according the semver symantic.</title>
<PackageTags>semver;update-tag;tag;git</PackageTags>
<Version>0.1.2</Version>
<Version>0.6.1</Version>
<Authors>Matthias Langhard</Authors>
<Company>Novaloop AG</Company>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.updatetag</PackageProjectUrl>
<PackageProjectUrl>https://gitlab.com/novaloop-oss/novaloop.update-tag</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0"/>
<PackageReference Include="Spectre.Console" Version="0.42.0"/>

View File

@@ -0,0 +1,10 @@
using CommandLine;
namespace Cli.Models
{
public class CliParams
{
[Option('r', "repository-path", Required = false, HelpText = "Run update-tag on a git repository other than the current directory.")]
public string RepositoryPath { get; set; }
}
}

View File

@@ -1,7 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Application;
using Cli.Models;
using CommandLine;
using Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -21,9 +22,14 @@ namespace Cli
.ServiceProvider
.GetRequiredService<AppRunner>();
await appRunner.Run(args.FirstOrDefault() ?? Environment.CurrentDirectory);
await Parser.Default
.ParseArguments<CliParams>(args)
.WithParsedAsync(
async options => { await appRunner.Run(options.RepositoryPath ?? Environment.CurrentDirectory); }
);
}
private static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()

View File

@@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Application.Models;
namespace Application.Interfaces
{
public interface IGitRepoReadService
{
public IEnumerable<Version> GetAllVersions(string repoPath);
IEnumerable<Version> GetAllVersions(string repoPath);
Task<string> GetRepoBasePath(string workingDir);
}
}

View File

@@ -1,8 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
namespace Application.Interfaces
{
public interface IGitRepoWriteService
{
void AddTag(string repoPath, string tag);
void Push(string repoPath);
Task<string> PushTags(string repoPath, CancellationToken cancellationToken);
Task<string> Push(string repoPath, CancellationToken ct);
}
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Text;
using System.Text.RegularExpressions;
@@ -12,28 +13,41 @@ namespace Application.Models
/// </summary>
public class Version
{
public Version(int major, int minor, int patch)
public Version(int major, int minor, int patch, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, int rc)
public Version(int major, int minor, int patch, int? rc, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = rc;
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, string rc, string service)
public Version(int major, int minor, int patch, string rc, string service, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = ExtractNumberFromRcString(rc);
Rc = rc == null ? null : ExtractNumberFromRcString(rc);
Service = service ?? "";
_hasVPrefix = hasVPrefix;
}
public Version(int major, int minor, int patch, int? rc, string service, bool hasVPrefix)
{
Major = major;
Minor = minor;
Patch = patch;
Rc = rc;
Service = service ?? "";
_hasVPrefix = hasVPrefix;
}
private static int? ExtractNumberFromRcString(string rc)
@@ -47,12 +61,17 @@ namespace Application.Models
public int Minor { get; private set; }
public int Patch { get; private set; }
public int? Rc { get; private set; }
public string Service { get; }
public string Service { get; private set; }
private readonly bool _hasVPrefix;
public override string ToString()
{
var sb = new StringBuilder();
if (_hasVPrefix)
{
sb.Append('v');
}
sb.Append(Major);
sb.Append('.');
sb.Append(Minor);
@@ -75,62 +94,123 @@ namespace Application.Models
return sb.ToString();
}
private Version Copy()
{
return new Version(Major, Minor, Patch, Rc, Service, _hasVPrefix);
}
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 CreatePatchRc()
{
Major++;
Minor = 0;
Patch = 0;
Rc = null;
if (Rc != null)
{
throw new ArgumentException("Cannot create RC. Already an RC.");
}
var nextVersion = Copy();
nextVersion.Patch++;
nextVersion.Rc = 0;
return nextVersion;
}
public void BumpMinor()
public Version CreateMinorRc()
{
Minor++;
Patch = 0;
Rc = null;
if (Rc != null)
{
throw new ArgumentException("Cannot create RC. Already an RC.");
}
var nextVersion = Copy();
nextVersion.Minor++;
nextVersion.Patch = 0;
nextVersion.Rc = 0;
return nextVersion;
}
public void BumpPatch()
public Version CreateMajorRc()
{
Patch++;
Rc = null;
if (Rc != null)
{
throw new ArgumentException("Cannot create RC. Already an RC.");
}
var nextVersion = Copy();
nextVersion.Major++;
nextVersion.Minor = 0;
nextVersion.Patch = 0;
nextVersion.Rc = 0;
return nextVersion;
}
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, _hasVPrefix);
return nextVersion;
}
public bool IsRc()
{
return Rc != null;
}
public void SetService(string service)
{
Service = service;
}
}
}

View File

@@ -1,3 +1,5 @@
using System.Collections.Generic;
namespace Application.Models
{
public class VersionInformation
@@ -5,16 +7,36 @@ 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("patch-rc", currentVersion.CreatePatchRc()));
NextVersions.Add(new NextVersion("minor-rc", currentVersion.CreateMinorRc()));
NextVersions.Add(new NextVersion("minor-rc", currentVersion.CreateMajorRc()));
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<NextVersion> NextVersions = new List<NextVersion>();
}
public class NextVersion
{
public NextVersion(string title, Version version)
{
Title = title;
Version = version;
}
public string Title { get; }
public Version Version { get; }
}
}

View File

@@ -0,0 +1,33 @@
using System.Threading;
using System.Threading.Tasks;
using Application.Interfaces;
using MediatR;
namespace Application.Queries
{
public class GetRepoBasePath : IRequestHandler<GetRepoBasePath.Query, string>
{
public class Query : IRequest<string>
{
public Query(string workingDir)
{
WorkingDir = workingDir;
}
public string WorkingDir { get; }
}
private readonly IGitRepoReadService _gitRepoReadService;
public GetRepoBasePath(IGitRepoReadService gitRepoReadService)
{
_gitRepoReadService = gitRepoReadService;
}
public async Task<string> Handle(Query request, CancellationToken cancellationToken)
{
return await _gitRepoReadService.GetRepoBasePath(request.WorkingDir);
}
}
}

View File

@@ -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<GetVersionInformationFromRepo.Query, VersionInformation>
public class GetVersionInformationFromRepo : IRequestHandler<GetVersionInformationFromRepo.Query, VersionInformation>
{
public class Query : IRequest<VersionInformation>
{
@@ -27,7 +29,7 @@ namespace Application.Queries
_gitRepoReadService = gitRepoReadService;
}
protected override VersionInformation Handle(Query request)
public async Task<VersionInformation> 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));
}
}
}

View File

@@ -1,9 +1,11 @@
using System.Threading;
using System.Threading.Tasks;
using Application.Interfaces;
using MediatR;
namespace Application.Commands
namespace Application.Queries
{
public class PushCommitsToRemote : RequestHandler<PushCommitsToRemote.Command>
public class PushCommitsToRemote : IRequestHandler<PushCommitsToRemote.Command, string>
{
private readonly IGitRepoWriteService _gitRepoWriteService;
@@ -12,7 +14,7 @@ namespace Application.Commands
_gitRepoWriteService = gitRepoWriteService;
}
public class Command : IRequest
public class Command : IRequest<string>
{
public string RepoPath { get; }
@@ -23,9 +25,9 @@ namespace Application.Commands
}
protected override void Handle(Command request)
public async Task<string> Handle(Command request, CancellationToken ct)
{
_gitRepoWriteService.Push(request.RepoPath);
return await _gitRepoWriteService.Push(request.RepoPath, ct);
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Threading;
using System.Threading.Tasks;
using Application.Interfaces;
using MediatR;
namespace Application.Queries
{
public class PushTagsToRemote : IRequestHandler<PushTagsToRemote.Command, string>
{
private readonly IGitRepoWriteService _gitRepoWriteService;
public PushTagsToRemote(IGitRepoWriteService gitRepoWriteService)
{
_gitRepoWriteService = gitRepoWriteService;
}
public class Command : IRequest<string>
{
public string RepoPath { get; }
public Command(string repoPath)
{
RepoPath = repoPath;
}
}
public async Task<string> Handle(Command request, CancellationToken ct)
{
return await _gitRepoWriteService.PushTags(request.RepoPath, ct);
}
}
}

View File

@@ -9,7 +9,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="LibGit2Sharp" Version="0.26.2" />
<PackageReference Include="CliWrap" Version="3.3.3" />
<PackageReference Include="LibGit2Sharp" Version="0.27.0-preview-0158" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="semver" Version="2.0.6" />
</ItemGroup>

View File

@@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Application.Interfaces;
using CliWrap;
using CliWrap.Buffered;
using LibGit2Sharp;
using Semver;
using Version = Application.Models.Version;
@@ -8,6 +12,15 @@ namespace Infrastructure.Services
{
public class GitRepoReadService : IGitRepoReadService
{
public async Task<string> GetRepoBasePath(string workingDir)
{
var result = await Cli.Wrap("git")
.WithArguments("rev-parse --show-toplevel")
.WithWorkingDirectory(workingDir)
.ExecuteBufferedAsync();
return result.StandardOutput.Trim();
}
public IEnumerable<Version> GetAllVersions(string repoPath)
{
using var repo = new Repository(repoPath);
@@ -15,21 +28,23 @@ namespace Infrastructure.Services
{
if (TryParse(tag.FriendlyName, out var semver))
{
yield return new Version(semver.Major, semver.Minor, semver.Patch, semver.Prerelease, semver.Build);
yield return semver;
}
}
}
private static bool TryParse(string version, out SemVersion semverVersion)
private static bool TryParse(string versionStr, out Version version)
{
try
{
semverVersion = SemVersion.Parse(version.TrimStart('v').TrimStart('V'));
var semver = SemVersion.Parse(versionStr.TrimStart('v').TrimStart('V'));
var hasVPrefix = versionStr.StartsWith("v", StringComparison.InvariantCultureIgnoreCase);
version = new Version(semver.Major, semver.Minor, semver.Patch, semver.Prerelease, semver.Build, hasVPrefix);
return true;
}
catch
{
semverVersion = null;
version = null;
return false;
}
}

View File

@@ -1,4 +1,8 @@
using System.Threading;
using System.Threading.Tasks;
using Application.Interfaces;
using CliWrap;
using CliWrap.Buffered;
using LibGit2Sharp;
namespace Infrastructure.Services
@@ -11,10 +15,23 @@ namespace Infrastructure.Services
repo.ApplyTag(tag);
}
public void Push(string repoPath)
public async Task<string> PushTags(string repoPath, CancellationToken ct)
{
using var repo = new Repository(repoPath);
repo.Network.Push(repo.Head);
var result = await Cli.Wrap("git")
.WithArguments("push --tags")
.WithWorkingDirectory(repoPath)
.ExecuteBufferedAsync(ct);
return result.StandardOutput.Trim();
}
public async Task<string> Push(string repoPath, CancellationToken ct)
{
var result = await Cli.Wrap("git")
.WithArguments("push")
.WithWorkingDirectory(repoPath)
.ExecuteBufferedAsync(ct);
return result.StandardOutput.Trim();
}
}
}

View File

@@ -0,0 +1,63 @@
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<Version>
{
new Version(0, 1, 5, false),
new Version(0, 1, 7, false),
new Version(0, 2, 0, false),
new Version(0, 2, 0, 0, false),
new Version(0, 2, 0, 1, false),
new Version(0, 2, 0, 2, false)
};
var gitRepoMock = new Mock<IGitRepoReadService>();
gitRepoMock.Setup(m => m.GetAllVersions(It.IsAny<string>()))
.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());
}
[Theory]
[InlineData(0, 2, 0, true, "v0.2.0")]
[InlineData(0, 2, 0, false, "0.2.0")]
public async void AddVPrefixToNextVersionIfCurrentVersionHasOne(int major, int minor, int patch, bool hasVPrefix,
string expectedVersionOutput)
{
// Arrange
var mockedVersionList = new List<Version>
{
new Version(major, minor, patch, hasVPrefix)
};
var gitRepoMock = new Mock<IGitRepoReadService>();
gitRepoMock.Setup(m => m.GetAllVersions(It.IsAny<string>()))
.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(expectedVersionOutput, versionInformation.CurrentVersion.ToString());
}
}
}

View File

@@ -0,0 +1,48 @@
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, false);
// 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); // next rc
Assert.Contains("1.0.1", versions); // release rc
Assert.Equal(2, versions.Count);
}
[Fact]
public void CorrectNextVersionsFromNonRc()
{
// Arrange
var version = new Version(1, 0, 1, false);
// Act
var versionInformation = new VersionInformation(version);
var versions = versionInformation.NextVersions.Select(nv => nv.Version.ToString()).ToList();
// Assert
Assert.Contains("1.0.2-RC.0", versions); // patch RC
Assert.Contains("1.1.0-RC.0", versions); // minor RC
Assert.Contains("2.0.0-RC.0", versions); // major RC
Assert.Contains("1.0.2", versions); // next patch
Assert.Contains("1.1.0", versions); // next minor
Assert.Contains("2.0.0", versions); // next major
Assert.Equal(6, versions.Count);
}
}
}

View File

@@ -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, false).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, false).NextMinor();
Assert.Equal(expected, version.ToString());
}
@@ -38,27 +32,56 @@ 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, false).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, false).NextRc();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, null, "", "1.1.2-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "1.1.2-RC.0+ErpNext")]
public void CreatePatchRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service, false).CreatePatchRc();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, null, "", "1.2.0-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "1.2.0-RC.0+ErpNext")]
public void CreateMinorRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service, false).CreateMinorRc();
Assert.Equal(expected, version.ToString());
}
[Theory]
[InlineData(1, 1, 1, null, "", "2.0.0-RC.0")]
[InlineData(1, 1, 1, null, "ErpNext", "2.0.0-RC.0+ErpNext")]
public void CreateMajroRc(int major, int minor, int patch, string rc, string service, string expected)
{
var version = new Version(major, minor, patch, rc, service, false).CreateMajorRc();
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, false).ReleaseRc();
Assert.Equal(expected, version.ToString());
}
}

View File

@@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>