Skip to content

Commit

Permalink
Add support for unreleased versions
Browse files Browse the repository at this point in the history
version: Unreleased
tag: Added
  • Loading branch information
Ben Zumhagen committed Nov 5, 2019
1 parent c33c52e commit 67bf949
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
60 changes: 60 additions & 0 deletions dotnet-gitchanges.Tests/Readers/GitReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using dotnet_gitchanges.Configuration;
using LibGit2Sharp;
using Moq;
Expand Down Expand Up @@ -34,6 +35,65 @@ public void VerifyCacheIsLoadedFromRepository()
repoMock.VerifyAll();
}

[Test]
public void VerifyUnreleasedCommitsHaveUnreleasedVersion()
{
var patterns = new ParsingPatterns
{
Reference = "reference:(.*)[\n]?",
Version = "version:(.*)[\n]?",
Tag = "tag:(.*)[\n]?"
};
var expectedChanges = new List<IChange>
{
new GitChange("Unreleased", "Added", "Some Unreleased Summary", DateTimeOffset.Now),
new GitChange("0.2.0", "Added", "Some Summary", DateTimeOffset.Now.AddDays(-1)),
new GitChange("0.1.0", "Removed", "Another Summary", DateTimeOffset.Now.AddDays(-2))
};
var repoMock = new Mock<IRepository>();
var commitLog = Mock.Of<IQueryableCommitLog>(cl => cl.GetEnumerator() == MockCommitEnumerator(expectedChanges));
var reader = new GitReader(repoMock.Object, patterns);

repoMock.Setup(r => r.Commits).Returns(commitLog);

Assert.That(reader.Changes(), Is.EquivalentTo(expectedChanges));
repoMock.VerifyAll();
}

[Test]
public void VerifyReleasedCommitsWithUnreleasedInVersionHaveCorrectVersion()
{
var patterns = new ParsingPatterns
{
Reference = "reference:(.*)[\n]?",
Version = "version:(.*)[\n]?",
Tag = "tag:(.*)[\n]?"
};
var today = DateTimeOffset.Now;
var yesterday = DateTimeOffset.Now.AddDays(-1);
var twoDaysAgo = DateTimeOffset.Now.AddDays(-2);
var changes = new List<IChange>
{
new GitChange("0.2.0", "Added", "Some Summary", today),
new GitChange("Unreleased", "Added", "Some now released Summary", yesterday),
new GitChange("0.1.0", "Removed", "Another Summary", twoDaysAgo)
};
var expectedChanges = new List<IChange>
{
new GitChange("0.2.0", "Added", "Some Summary", today),
new GitChange("0.2.0", "Added", "Some now released Summary", yesterday),
new GitChange("0.1.0", "Removed", "Another Summary", twoDaysAgo)
};
var repoMock = new Mock<IRepository>();
var commitLog = Mock.Of<IQueryableCommitLog>(cl => cl.GetEnumerator() == MockCommitEnumerator(changes));
var reader = new GitReader(repoMock.Object, patterns);

repoMock.Setup(r => r.Commits).Returns(commitLog);
var actualChanges = reader.Changes().ToList();
Assert.That(actualChanges, Is.EquivalentTo(expectedChanges));
repoMock.VerifyAll();
}

private static IEnumerator<Commit> MockCommitEnumerator(IEnumerable<IChange> expectedChanges)
{
foreach (var expectedChange in expectedChanges)
Expand Down
14 changes: 13 additions & 1 deletion dotnet-gitchanges/Readers/GitReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using dotnet_gitchanges.Configuration;
Expand All @@ -8,8 +9,10 @@ namespace dotnet_gitchanges
{
public class GitReader : IRepositoryReader
{
private const string Unreleased = "Unreleased";
private readonly IRepository _repository;
private readonly ParsingPatterns _patterns;
private string _lastVersion = Unreleased;

public GitReader(IRepository repository, ParsingPatterns patterns)
{
Expand All @@ -26,6 +29,15 @@ public IEnumerable<IChange> Changes()
var reference = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Reference));
var version = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Version));
var tag = GetMatchOrDefault(Regex.Match(commit.Message, _patterns.Tag));

if (string.Equals(version, Unreleased, StringComparison.CurrentCultureIgnoreCase))
{
version = _lastVersion;
}
else
{
_lastVersion = version;
}

yield return new GitChange(version, tag, commit.MessageShort, commit.Author.When, reference);
}
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.3
0.1.4

0 comments on commit 67bf949

Please sign in to comment.