Skip to content

Commit 93344cc

Browse files
authored
Relax version directive argument, allow for major.minor format (#89)
1 parent 32636df commit 93344cc

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Elastic.Markdown/Myst/Directives/VersionBlock.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using Elastic.Markdown.Helpers;
6+
using static System.StringSplitOptions;
67

78
namespace Elastic.Markdown.Myst.Directives;
89

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

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

2728
if (!SemVersion.TryParse(tokens[0], out var version))
2829
{
29-
EmitError(context, $"{tokens[0]} is not a valid version");
30-
return;
30+
var numbers = tokens[0].Split('.', RemoveEmptyEntries);
31+
if (numbers.Length != 2 || !SemVersion.TryParse($"{numbers[0]}.{numbers[1]}.0", out version))
32+
EmitError(context, $"'{tokens[0]}' is not a valid version");
3133
}
3234

3335
Version = version;

tests/Elastic.Markdown.Tests/Directives/VersionTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,35 @@ public class VersionDeprectatedTests(ITestOutputHelper output) : VersionTests(ou
5050
[Fact]
5151
public void SetsTitle() => Block!.Title.Should().Be("Deprecated (1.0.1-beta1): more information");
5252
}
53+
54+
public abstract class VersionValidationTests(ITestOutputHelper output, string version) : DirectiveTest<VersionBlock>(output,
55+
$$"""
56+
```{versionchanged} {{version}} more information
57+
Version brief summary
58+
```
59+
A regular paragraph.
60+
"""
61+
);
62+
63+
public class SimpleVersion(ITestOutputHelper output) : VersionValidationTests(output, "7.17")
64+
{
65+
[Fact]
66+
public void SetsVersion() => Block!.Version.Should().Be(new SemVersion(7, 17, 0));
67+
68+
[Fact]
69+
public void HasNoError() => Collector.Diagnostics.Should().BeEmpty();
70+
}
71+
72+
public class MajorVersionOnly(ITestOutputHelper output) : VersionValidationTests(output, "8")
73+
{
74+
[Fact]
75+
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
76+
.And.Contain(d => d.Message.Contains("'8' is not a valid version"));
77+
}
78+
79+
public class BranchVersion(ITestOutputHelper output) : VersionValidationTests(output, "8.x")
80+
{
81+
[Fact]
82+
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
83+
.And.Contain(d => d.Message.Contains("'8.x' is not a valid version"));
84+
}

0 commit comments

Comments
 (0)