Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,89 @@
Assert.IsFalse(content.Contains("Merge pull request"), "Should filter PR merge");
}

private void SetupTaggedHistory(IReadOnlyList<string> tagsNewestFirst, Dictionary<string, IReadOnlyList<CommitInfo>> commitsByRange)
{
_gitService.GetTagsAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(tagsNewestFirst));
foreach (string tag in tagsNewestFirst)
{
_gitService.GetTagCommitHashAsync(Arg.Any<string>(), tag, Arg.Any<CancellationToken>())
.Returns(Task.FromResult<string?>($"sha-{tag}"));
}

_gitService.GetCommitsAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(call => Task.FromResult(commitsByRange.GetValueOrDefault(call.ArgAt<string>(1)) ?? []));
}

private static string GetSection(string changelog, string tag)
{
int start = changelog.IndexOf($"## {tag} ", StringComparison.Ordinal);
Assert.IsTrue(start >= 0, $"Changelog should have an entry for {tag}");

Check warning on line 145 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.IsGreaterThanOrEqualTo' instead of 'Assert.IsTrue'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSq&open=AaDnzzMigRgWWbZF5tSq&pullRequest=180
int end = changelog.IndexOf("## ", start + 3, StringComparison.Ordinal);
return end < 0 ? changelog[start..] : changelog[start..end];
}

[TestMethod]
public async Task GenerateAsync_FirstPrereleaseListsCommitsSinceThePreviousTagOnceItsStableTagExists()
{
// Arrange: v1.0.0, then "Add feature A" tagged v1.0.1-pre.1, then "Fix bug B" tagged v1.0.1.
SetupTaggedHistory(
["v1.0.1", "v1.0.1-pre.1", "v1.0.0"],
new Dictionary<string, IReadOnlyList<CommitInfo>>
{
["sha-v1.0.0..sha-v1.0.1-pre.1"] = [new CommitInfo { Hash = "aaa", Subject = "Add feature A", Author = "developer" }],
["sha-v1.0.1-pre.1..sha-v1.0.1"] = [new CommitInfo { Hash = "bbb", Subject = "Fix bug B", Author = "developer" }],
["sha-v1.0.0..sha-v1.0.1"] =
[
new CommitInfo { Hash = "bbb", Subject = "Fix bug B", Author = "developer" },
new CommitInfo { Hash = "aaa", Subject = "Add feature A", Author = "developer" },
],
});

// Act
await _generator.GenerateAsync(
version: "1.0.2",
commitHash: "head",
workingDirectory: "/repo",
outputPath: _tempDir,
lineEnding: "\n").ConfigureAwait(false);

Check warning on line 173 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSr&open=AaDnzzMigRgWWbZF5tSr&pullRequest=180

// Assert
string content = await File.ReadAllTextAsync(Path.Combine(_tempDir, "CHANGELOG.md")).ConfigureAwait(false);

Check warning on line 176 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSs&open=AaDnzzMigRgWWbZF5tSs&pullRequest=180
string prerelease = GetSection(content, "v1.0.1-pre.1");
Assert.IsTrue(prerelease.Contains("Changes since v1.0.0:"), prerelease);

Check warning on line 178 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'Assert.IsTrue'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSt&open=AaDnzzMigRgWWbZF5tSt&pullRequest=180
Assert.IsTrue(prerelease.Contains("Add feature A"), prerelease);

Check warning on line 179 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'Assert.IsTrue'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSu&open=AaDnzzMigRgWWbZF5tSu&pullRequest=180
Assert.IsFalse(prerelease.Contains("No significant changes"), prerelease);

Check warning on line 180 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.DoesNotContain' instead of 'Assert.IsFalse'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSv&open=AaDnzzMigRgWWbZF5tSv&pullRequest=180
}

[TestMethod]
public async Task GenerateAsync_LaterPrereleaseDiffsAgainstThePreviousPrerelease()
{
// Arrange
SetupTaggedHistory(
["v1.0.1-pre.2", "v1.0.1-pre.1", "v1.0.0"],
new Dictionary<string, IReadOnlyList<CommitInfo>>
{
["sha-v1.0.0..sha-v1.0.1-pre.1"] = [new CommitInfo { Hash = "aaa", Subject = "Add feature A", Author = "developer" }],
["sha-v1.0.1-pre.1..sha-v1.0.1-pre.2"] = [new CommitInfo { Hash = "bbb", Subject = "Add feature B", Author = "developer" }],
});

// Act
await _generator.GenerateAsync(
version: "1.0.1-pre.3",
commitHash: "head",
workingDirectory: "/repo",
outputPath: _tempDir,
lineEnding: "\n").ConfigureAwait(false);

Check warning on line 201 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSw&open=AaDnzzMigRgWWbZF5tSw&pullRequest=180

// Assert
string content = await File.ReadAllTextAsync(Path.Combine(_tempDir, "CHANGELOG.md")).ConfigureAwait(false);

Check warning on line 204 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSx&open=AaDnzzMigRgWWbZF5tSx&pullRequest=180
string second = GetSection(content, "v1.0.1-pre.2");
Assert.IsTrue(second.Contains("Changes since v1.0.1-pre.1:"), second);

Check warning on line 206 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'Assert.IsTrue'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSy&open=AaDnzzMigRgWWbZF5tSy&pullRequest=180
Assert.IsTrue(second.Contains("Add feature B"), second);

Check warning on line 207 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'Assert.IsTrue'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tSz&open=AaDnzzMigRgWWbZF5tSz&pullRequest=180
Assert.IsFalse(second.Contains("Add feature A"), second);

Check warning on line 208 in KtsuBuild.Tests/Metadata/ChangelogGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.DoesNotContain' instead of 'Assert.IsFalse'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_KtsuBuild&issues=AaDnzzMigRgWWbZF5tS0&open=AaDnzzMigRgWWbZF5tS0&pullRequest=180
}

[TestMethod]
public async Task GenerateAsync_FormatsEntriesCorrectly()
{
Expand Down
9 changes: 8 additions & 1 deletion KtsuBuild/Metadata/ChangelogGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,14 @@ private static string FindSearchTag(IReadOnlyList<string> allTags, string fromTa
int searchPatch;
int searchPrerelease;

if (toPrerelease != 0)
if (toPrerelease == 1)
{
// The first prerelease of a series has no earlier prerelease to diff against. Searching for
// "prerelease 0" would match the stable X.Y.Z tag, which comes after it, and produce an empty
// range, so diff against the adjacent earlier tag instead.
return fromTag;
}
else if (toPrerelease != 0)
{
// Prerelease: look for previous prerelease in same series
searchMajor = toMajor;
Expand Down
Loading