Skip to content

Commit b133b7e

Browse files
committed
Relax version directive argument, allow for major.minor format
1 parent 32636df commit b133b7e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 5 additions & 2 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,7 +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+
var numbers = tokens[0].Split('.', RemoveEmptyEntries);
31+
if (numbers.Length != 2 || !SemVersion.TryParse($"{numbers[0]}.{numbers[1]}", out version))
32+
EmitError(context, $"'{tokens[0]}' is not a valid version");
3033
return;
3134
}
3235

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,32 @@ 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+
69+
public class MajorVersionOnly(ITestOutputHelper output) : VersionValidationTests(output, "8")
70+
{
71+
[Fact]
72+
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
73+
.And.Contain(d => d.Message.Contains("'8' is not a valid version"));
74+
}
75+
76+
public class BranchVersion(ITestOutputHelper output) : VersionValidationTests(output, "8.x")
77+
{
78+
[Fact]
79+
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
80+
.And.Contain(d => d.Message.Contains("'8.x' is not a valid version"));
81+
}

0 commit comments

Comments
 (0)