Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH1669: fixed parsing of versions with prerelease #3568

Merged
merged 1 commit into from
Oct 19, 2021
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
2 changes: 2 additions & 0 deletions src/Cake.Common.Tests/Unit/ReleaseNotesAliasesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void Should_Read_Content_Of_File_And_Parse_It()

// Then
Assert.Equal("1.2.3", result[0].Version.ToString());
Assert.Equal("1.2.3", result[0].SemVersion.ToString());
}
}

Expand All @@ -82,6 +83,7 @@ public void Should_Return_The_Latest_Release_Notes()

// Then
Assert.Equal("1.2.5", result.Version.ToString());
Assert.Equal("1.2.5", result.SemVersion.ToString());
}
}
}
Expand Down
42 changes: 40 additions & 2 deletions src/Cake.Common.Tests/Unit/ReleaseNotesParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Core;
using Xunit;

namespace Cake.Common.Tests.Unit
{
public sealed class ReleaseNotesParserTests
Expand Down Expand Up @@ -67,6 +65,7 @@ public void Should_Parse_Release_Note_Version()

// Then
Assert.Equal("0.1.9", result[0].Version.ToString());
Assert.Equal("0.1.9", result[0].SemVersion.ToString());
}

[Fact]
Expand Down Expand Up @@ -129,6 +128,8 @@ public void Should_Return_Release_Notes_In_Descending_Order()
Assert.Equal(2, result.Count);
Assert.Equal("0.1.10", result[0].Version.ToString());
Assert.Equal("0.1.9", result[1].Version.ToString());
Assert.Equal("0.1.10", result[0].SemVersion.ToString());
Assert.Equal("0.1.9", result[1].SemVersion.ToString());
}

[Fact]
Expand Down Expand Up @@ -159,6 +160,40 @@ public void Should_Set_RawVersionLine_Property_To_Line_Containing_Version_Number
// Then
Assert.Equal("### New in 0.1.9-beta1 (Releases 2014/06/28)", result[0].RawVersionLine);
}

[Fact]
public void Should_Parse_Release_Note_Version_With_Prerelease()
{
// Given
var parser = new ReleaseNotesParser();
const string content = "### New in 0.1.9-beta1 (Releases 2014/06/28)\nLine 1\n \n\t\n";

// When
var result = parser.Parse(content);

// Then
Assert.Equal("0.1.9", result[0].Version.ToString());
Assert.Equal("0.1.9-beta1", result[0].SemVersion.ToString());
}

[Fact]
public void Should_Return_Multiple_Release_Notes_With_Prerelease()
{
// Given
var parser = new ReleaseNotesParser();
const string content = "### New in 0.1.9-alpha1 (Releases 2014/06/28)\n* Line 1\n" +
"###New in 0.1.10-gamma3\n* Line 2\n Line 3";

// When
var result = parser.Parse(content);

// Then
Assert.Equal(2, result.Count);
Assert.Equal("0.1.10", result[0].Version.ToString());
Assert.Equal("0.1.9", result[1].Version.ToString());
Assert.Equal("0.1.10-gamma3", result[0].SemVersion.ToString());
Assert.Equal("0.1.9-alpha1", result[1].SemVersion.ToString());
}
}

public sealed class SimpleFormat
Expand Down Expand Up @@ -190,6 +225,7 @@ public void Should_Parse_Release_Note_Version()

// Then
Assert.Equal("0.1.9", result[0].Version.ToString());
Assert.Equal("0.1.9", result[0].SemVersion.ToString());
}

[Fact]
Expand Down Expand Up @@ -249,6 +285,8 @@ public void Should_Return_Release_Notes_In_Descending_Order()
Assert.Equal(2, result.Count);
Assert.Equal("0.1.10", result[0].Version.ToString());
Assert.Equal("0.1.9", result[1].Version.ToString());
Assert.Equal("0.1.10", result[0].SemVersion.ToString());
Assert.Equal("0.1.9", result[1].SemVersion.ToString());
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/Cake.Common.Tests/Unit/ReleaseNotesTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using Xunit;

Expand All @@ -15,11 +15,23 @@ public sealed class TheConstructor
public void Should_Throw_If_Version_Is_Null()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false

{
// Given, When
var result = Record.Exception(() => new ReleaseNotes(null, Enumerable.Empty<string>(), null));
Version version = null;
var result = Record.Exception(() => new ReleaseNotes(version, Enumerable.Empty<string>(), null));

// Then
AssertEx.IsArgumentNullException(result, "version");
}

[Fact]
public void Should_Throw_If_SemVersion_Is_Null()
{
// Given, When
SemVersion semVersion = null;
var result = Record.Exception(() => new ReleaseNotes(semVersion, Enumerable.Empty<string>(), null));

// Then
AssertEx.IsArgumentNullException(result, "semVersion");
}
}
}
}
37 changes: 32 additions & 5 deletions src/Cake.Common/ReleaseNotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public sealed class ReleaseNotes
{
private readonly List<string> _notes;

/// <summary>
/// Gets the version.
/// </summary>
/// <value>The version.</value>
public SemVersion SemVersion { get; }

/// <summary>
/// Gets the version.
/// </summary>
Expand All @@ -33,19 +39,40 @@ public sealed class ReleaseNotes
/// <value>The raw text of the Version line.</value>
public string RawVersionLine { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ReleaseNotes"/> class.
/// </summary>
/// <param name="semVersion">The semantic version.</param>
/// <param name="notes">The notes.</param>
/// <param name="rawVersionLine">The raw text of the version line.</param>
public ReleaseNotes(SemVersion semVersion, IEnumerable<string> notes, string rawVersionLine)
: this(
semVersion?.AssemblyVersion ?? throw new ArgumentNullException(nameof(semVersion)),
semVersion,
notes,
rawVersionLine)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ReleaseNotes"/> class.
/// </summary>
/// <param name="version">The version.</param>
/// <param name="notes">The notes.</param>
/// <param name="rawVersionLine">The raw text of the version line.</param>
public ReleaseNotes(Version version, IEnumerable<string> notes, string rawVersionLine)
: this(
version ?? throw new ArgumentNullException(nameof(version)),
new SemVersion(version.Major, version.Minor, version.Build),
notes,
rawVersionLine)
{
}

private ReleaseNotes(Version version, SemVersion semVersion, IEnumerable<string> notes, string rawVersionLine)
{
if (version == null)
{
throw new ArgumentNullException(nameof(version));
}
Version = version;
Version = version ?? throw new ArgumentNullException(nameof(version));
SemVersion = semVersion ?? throw new ArgumentNullException(nameof(semVersion));
RawVersionLine = rawVersionLine;
_notes = new List<string>(notes ?? Enumerable.Empty<string>());
}
Expand Down
1 change: 1 addition & 0 deletions src/Cake.Common/ReleaseNotesAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Cake.Common
/// Contains functionality related to release notes.
/// </summary>
[CakeAliasCategory("Release Notes")]

public static class ReleaseNotesAliases
{
private static readonly ReleaseNotesParser _parser;
Expand Down
30 changes: 13 additions & 17 deletions src/Cake.Common/ReleaseNotesParser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -22,7 +21,7 @@ public sealed class ReleaseNotesParser
/// </summary>
public ReleaseNotesParser()
{
_versionRegex = new Regex(@"([0-9]+\.)+[0-9]+");
_versionRegex = new Regex(@"(?<Version>\d+(\s*\.\s*\d+){0,3})(?<Release>-[a-z][0-9a-z-]*)?");
}

/// <summary>
Expand Down Expand Up @@ -68,15 +67,14 @@ private IReadOnlyList<ReleaseNotes> ParseComplexFormat(string[] lines)
break;
}

// Parse header.
var versionResult = _versionRegex.Match(lines[lineIndex]);
if (!versionResult.Success)
// Create release notes.
var semVer = SemVersion.Zero;
var version = SemVersion.TryParse(lines[lineIndex], out semVer);
if (!version)
{
throw new CakeException("Could not parse version from release notes header.");
}

// Create release notes.
var version = Version.Parse(versionResult.Value);
var rawVersionLine = lines[lineIndex];

// Increase the line index.
Expand Down Expand Up @@ -106,10 +104,10 @@ private IReadOnlyList<ReleaseNotes> ParseComplexFormat(string[] lines)
lineIndex++;
}

result.Add(new ReleaseNotes(version, notes, rawVersionLine));
result.Add(new ReleaseNotes(semVer, notes, rawVersionLine));
}

return result.OrderByDescending(x => x.Version).ToArray();
return result.OrderByDescending(x => x.SemVersion).ToArray();
}

private IReadOnlyList<ReleaseNotes> ParseSimpleFormat(string[] lines)
Expand All @@ -133,24 +131,22 @@ private IReadOnlyList<ReleaseNotes> ParseSimpleFormat(string[] lines)
}

// Parse header.
var versionResult = _versionRegex.Match(line);
if (!versionResult.Success)
var semVer = SemVersion.Zero;
var version = SemVersion.TryParse(lines[lineIndex], out semVer);
if (!version)
{
throw new CakeException("Could not parse version from release notes header.");
}

var version = Version.Parse(versionResult.Value);

// Parse the description.
line = line.Substring(versionResult.Length).Trim('-', ' ');
line = line.Substring(semVer.ToString().Length).Trim('-', ' ');

// Add the release notes to the result.
result.Add(new ReleaseNotes(version, new[] { line }, line));
result.Add(new ReleaseNotes(semVer, new[] { line }, line));

lineIndex++;
}

return result.OrderByDescending(x => x.Version).ToArray();
return result.OrderByDescending(x => x.SemVersion).ToArray();
}
}
}
Loading