Skip to content

Commit

Permalink
feat: print changelog diff during a dry-run (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
cabauman committed Feb 5, 2022
1 parent 6e98de6 commit 40a58f3
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 33 deletions.
46 changes: 43 additions & 3 deletions Versionize.Tests/Changelog/ChangelogBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,20 @@ public void ShouldGenerateAChangelogForFixFeatAndBreakingCommits()
},
ChangelogOptions.Default);

var wasChangelogWritten = File.Exists(Path.Join(_testDirectory, "CHANGELOG.md"));
Assert.True(wasChangelogWritten);
var changelogContents = File.ReadAllText(changelog.FilePath);

// TODO: Assert changelog entries
var sb = new ChangelogStringBuilder();
sb.Append(ChangelogOptions.Preamble);
sb.Append("<a name=\"1.1.0\"></a>");
sb.Append("## 1.1.0 (1-1-1)", 2);
sb.Append("### Features", 2);
sb.Append("* a breaking change feature");
sb.Append("* a feature", 2);
sb.Append("### Bug Fixes", 2);
sb.Append("* a fix", 2);
sb.Append("### Breaking Changes", 2);
sb.Append("* a breaking change feature", 2);
Assert.Equal(sb.Build(), changelogContents);
}

[Fact]
Expand Down Expand Up @@ -373,6 +383,36 @@ public void ShouldIncludeAllCommitsInChangelogWhenGiven()
changelogContents.ShouldContain("some foo bar");
}

[Fact]
public void GenerateMarkdownShouldGenerateMarkdownForFixFeatAndBreakingCommits()
{
var plainLinkBuilder = new PlainLinkBuilder();
string markdown = ChangelogBuilder.GenerateMarkdown(
new Version(1, 1, 0),
new DateTimeOffset(),
plainLinkBuilder,
new List<ConventionalCommit>
{
ConventionalCommitParser.Parse(new TestCommit("a360d6a307909c6e571b29d4a329fd786c5d4543", "fix: a fix")),
ConventionalCommitParser.Parse(new TestCommit("b360d6a307909c6e571b29d4a329fd786c5d4543", "feat: a feature")),
ConventionalCommitParser.Parse(
new TestCommit("c360d6a307909c6e571b29d4a329fd786c5d4543", "feat: a breaking change feature\nBREAKING CHANGE: this will break everything")),
},
ChangelogOptions.Default);

var sb = new ChangelogStringBuilder();
sb.Append("<a name=\"1.1.0\"></a>");
sb.Append("## 1.1.0 (1-1-1)", 2);
sb.Append("### Features", 2);
sb.Append("* a breaking change feature");
sb.Append("* a feature", 2);
sb.Append("### Bug Fixes", 2);
sb.Append("* a fix", 2);
sb.Append("### Breaking Changes", 2);
sb.Append("* a breaking change feature", 2);
Assert.Equal(sb.Build(), markdown);
}

public void Dispose()
{
Cleanup.DeleteDirectory(_testDirectory);
Expand Down
21 changes: 21 additions & 0 deletions Versionize.Tests/TestSupport/ChangelogStringBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text;

namespace Versionize.Tests.TestSupport
{
public class ChangelogStringBuilder
{
private readonly StringBuilder _sb = new();

public ChangelogStringBuilder Append(string text, int lineBreaks = 1)
{
_sb.Append(text);
for (int i = 0; i < lineBreaks; i++)
{
_sb.Append('\n');
}
return this;
}

public string Build() => _sb.ToString();
}
}
14 changes: 8 additions & 6 deletions Versionize.Tests/WorkingCopyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,23 @@ public void ShouldExitIfWorkingCopyDoesNotExist()
}

[Fact]
public void ShouldPreformADryRun()
public void ShouldPerformADryRun()
{
TempCsProject.Create(_testSetup.WorkingDirectory);

File.WriteAllText(Path.Join(_testSetup.WorkingDirectory, "hello.txt"), "First commit");
CommitAll(_testSetup.Repository);

File.WriteAllText(Path.Join(_testSetup.WorkingDirectory, "hello.txt"), "Second commit");
CommitAll(_testSetup.Repository);
CommitAll(_testSetup.Repository, "feat: first commit");

var workingCopy = WorkingCopy.Discover(_testSetup.WorkingDirectory);
workingCopy.Versionize(new VersionizeOptions { DryRun = true, SkipDirty = true });

_testPlatformAbstractions.Messages.Count.ShouldBe(4);
_testPlatformAbstractions.Messages.Count.ShouldBe(7);
_testPlatformAbstractions.Messages[0].Message.ShouldBe("Discovered 1 versionable projects");
_testPlatformAbstractions.Messages[3].Message.ShouldBe("\n---");
_testPlatformAbstractions.Messages[4].Message.ShouldContain("* first commit");
_testPlatformAbstractions.Messages[5].Message.ShouldBe("---\n");
var wasChangelogWritten = File.Exists(Path.Join(_testSetup.WorkingDirectory, "CHANGELOG.md"));
Assert.False(wasChangelogWritten);
}

[Fact]
Expand Down
54 changes: 33 additions & 21 deletions Versionize/Changelog/ChangelogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ private ChangelogBuilder(string file)
IChangelogLinkBuilder linkBuilder,
IEnumerable<ConventionalCommit> commits,
ChangelogOptions changelogOptions)
{
string markdown = GenerateMarkdown(version, versionTime, linkBuilder, commits, changelogOptions);

if (File.Exists(FilePath))
{
var contents = File.ReadAllText(FilePath);

var firstReleaseHeadlineIdx = contents.IndexOf("<a name=\"", StringComparison.Ordinal);

if (firstReleaseHeadlineIdx >= 0)
{
markdown = contents.Insert(firstReleaseHeadlineIdx, markdown);
}
else
{
markdown = contents + "\n\n" + markdown;
}

File.WriteAllText(FilePath, markdown);
}
else
{
File.WriteAllText(FilePath, changelogOptions.Header + "\n" + markdown);
}
}

public static string GenerateMarkdown(
Version version,
DateTimeOffset versionTime,
IChangelogLinkBuilder linkBuilder,
IEnumerable<ConventionalCommit> commits,
ChangelogOptions changelogOptions)
{
var versionTagLink = string.IsNullOrWhiteSpace(linkBuilder.BuildVersionTagLink(version))
? version.ToString()
Expand Down Expand Up @@ -68,27 +100,7 @@ private ChangelogBuilder(string file)
}
}

if (File.Exists(FilePath))
{
var contents = File.ReadAllText(FilePath);

var firstReleaseHeadlineIdx = contents.IndexOf("<a name=\"", StringComparison.Ordinal);

if (firstReleaseHeadlineIdx >= 0)
{
markdown = contents.Insert(firstReleaseHeadlineIdx, markdown);
}
else
{
markdown = contents + "\n\n" + markdown;
}

File.WriteAllText(FilePath, markdown);
}
else
{
File.WriteAllText(FilePath, changelogOptions.Header + "\n" + markdown);
}
return markdown;
}

public static string BuildBlock(string header, IChangelogLinkBuilder linkBuilder, IEnumerable<ConventionalCommit> commits)
Expand Down
2 changes: 1 addition & 1 deletion Versionize/ChangelogOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Versionize
// TODO: Consider creating a ChangelogConfigurationContract as a layer of abstraction.
public record class ChangelogOptions
{
private const string Preamble = "# Change Log\n\nAll notable changes to this project will be documented in this file. See [versionize](https://github.com/saintedlama/versionize) for commit guidelines.\n";
public const string Preamble = "# Change Log\n\nAll notable changes to this project will be documented in this file. See [versionize](https://github.com/saintedlama/versionize) for commit guidelines.\n";
public static readonly ChangelogOptions Default = new ChangelogOptions
{
Header = Preamble,
Expand Down
11 changes: 9 additions & 2 deletions Versionize/WorkingCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ public Version Versionize(VersionizeOptions options)
Step($"bumping version from {projects.Version} to {nextVersion} in projects");

var changelog = ChangelogBuilder.CreateForPath(workingDirectory);
var changelogLinkBuilder = LinkBuilderFactory.CreateFor(repo);

if (!options.DryRun)
if (options.DryRun)
{
string markdown = ChangelogBuilder.GenerateMarkdown(nextVersion, versionTime, changelogLinkBuilder, conventionalCommits, options.Changelog);
Information("\n---");
Information(markdown.TrimEnd('\n'));
Information("---\n");
}
else
{
var changelogLinkBuilder = LinkBuilderFactory.CreateFor(repo);
changelog.Write(nextVersion, versionTime, changelogLinkBuilder, conventionalCommits, options.Changelog);
}

Expand Down

0 comments on commit 40a58f3

Please sign in to comment.