Skip to content

Commit

Permalink
Relax version directive argument, allow for major.minor format (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Dec 3, 2024
1 parent 32636df commit 93344cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Elastic.Markdown/Myst/Directives/VersionBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using Elastic.Markdown.Helpers;
using static System.StringSplitOptions;

namespace Elastic.Markdown.Myst.Directives;

Expand All @@ -17,7 +18,7 @@ public class VersionBlock(DirectiveBlockParser parser, string directive, Diction

public override void FinalizeAndValidate(ParserContext context)
{
var tokens = Arguments?.Split(" ", 2, StringSplitOptions.RemoveEmptyEntries) ?? [];
var tokens = Arguments?.Split(" ", 2, RemoveEmptyEntries) ?? [];
if (tokens.Length < 1)
{
EmitError(context, $"{directive} needs exactly 2 arguments: <version> <title>");
Expand All @@ -26,8 +27,9 @@ public override void FinalizeAndValidate(ParserContext context)

if (!SemVersion.TryParse(tokens[0], out var version))
{
EmitError(context, $"{tokens[0]} is not a valid version");
return;
var numbers = tokens[0].Split('.', RemoveEmptyEntries);
if (numbers.Length != 2 || !SemVersion.TryParse($"{numbers[0]}.{numbers[1]}.0", out version))
EmitError(context, $"'{tokens[0]}' is not a valid version");
}

Version = version;
Expand Down
32 changes: 32 additions & 0 deletions tests/Elastic.Markdown.Tests/Directives/VersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,35 @@ public class VersionDeprectatedTests(ITestOutputHelper output) : VersionTests(ou
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Deprecated (1.0.1-beta1): more information");
}

public abstract class VersionValidationTests(ITestOutputHelper output, string version) : DirectiveTest<VersionBlock>(output,
$$"""
```{versionchanged} {{version}} more information
Version brief summary
```
A regular paragraph.
"""
);

public class SimpleVersion(ITestOutputHelper output) : VersionValidationTests(output, "7.17")
{
[Fact]
public void SetsVersion() => Block!.Version.Should().Be(new SemVersion(7, 17, 0));

[Fact]
public void HasNoError() => Collector.Diagnostics.Should().BeEmpty();
}

public class MajorVersionOnly(ITestOutputHelper output) : VersionValidationTests(output, "8")
{
[Fact]
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
.And.Contain(d => d.Message.Contains("'8' is not a valid version"));
}

public class BranchVersion(ITestOutputHelper output) : VersionValidationTests(output, "8.x")
{
[Fact]
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
.And.Contain(d => d.Message.Contains("'8.x' is not a valid version"));
}

0 comments on commit 93344cc

Please sign in to comment.