-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include heading level overried for deployment applicability (#104)
- Loading branch information
Showing
7 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Markdown.Myst.FrontMatter; | ||
using Markdig.Syntax; | ||
|
||
namespace Elastic.Markdown.Myst.Directives; | ||
|
||
public class AppliesBlock(DirectiveBlockParser parser, Dictionary<string, string> properties) | ||
: DirectiveBlock(parser, properties) | ||
{ | ||
public override string Directive => "mermaid"; | ||
|
||
public Deployment? Deployment { get; private set; } | ||
|
||
public override void FinalizeAndValidate(ParserContext context) | ||
{ | ||
if (TryGetAvailability("stack", out var version)) | ||
{ | ||
Deployment ??= new Deployment(); | ||
Deployment.SelfManaged ??= new SelfManagedDeployment(); | ||
Deployment.SelfManaged.Stack = version; | ||
} | ||
if (TryGetAvailability("ece", out version)) | ||
{ | ||
Deployment ??= new Deployment(); | ||
Deployment.SelfManaged ??= new SelfManagedDeployment(); | ||
Deployment.SelfManaged.Ece = version; | ||
} | ||
if (TryGetAvailability("eck", out version)) | ||
{ | ||
Deployment ??= new Deployment(); | ||
Deployment.SelfManaged ??= new SelfManagedDeployment(); | ||
Deployment.SelfManaged.Eck = version; | ||
} | ||
if (TryGetAvailability("hosted", out version)) | ||
{ | ||
Deployment ??= new Deployment(); | ||
Deployment.Cloud ??= new CloudManagedDeployment(); | ||
Deployment.Cloud.Hosted = version; | ||
} | ||
if (TryGetAvailability("serverless", out version)) | ||
{ | ||
Deployment ??= new Deployment(); | ||
Deployment.Cloud ??= new CloudManagedDeployment(); | ||
Deployment.Cloud.Serverless = version; | ||
} | ||
|
||
if (Deployment is null) | ||
EmitError(context, "{applies} block with no product availability specified"); | ||
|
||
var index = Parent?.IndexOf(this); | ||
if (Parent is not null && index > 0) | ||
{ | ||
var i = index - 1 ?? 0; | ||
var prevSib = Parent[i]; | ||
if (prevSib is not HeadingBlock) | ||
EmitError(context, "{applies} should follow a heading"); | ||
} | ||
|
||
bool TryGetAvailability(string key, out ProductAvailability? semVersion) | ||
{ | ||
semVersion = null; | ||
return Prop(key) is {} v && ProductAvailability.TryParse(v, out semVersion); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/Elastic.Markdown.Tests/Directives/AppliesBlockTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Markdown.Diagnostics; | ||
using Elastic.Markdown.Myst.Directives; | ||
using FluentAssertions; | ||
using Xunit.Abstractions; | ||
|
||
namespace Elastic.Markdown.Tests.Directives; | ||
|
||
public class AppliesBlockTests(ITestOutputHelper output) : DirectiveTest<AppliesBlock>(output, | ||
""" | ||
# heading | ||
```{applies} | ||
:eck: unavailable | ||
``` | ||
""" | ||
) | ||
{ | ||
[Fact] | ||
public void ParsesBlock() => Block.Should().NotBeNull(); | ||
|
||
[Fact] | ||
public void IncludesProductAvailability() => | ||
Html.Should().Contain("Unavailable</span>") | ||
.And.Contain("Elastic Cloud Kubernetes") | ||
.And.Contain("Applies To:"); | ||
|
||
|
||
[Fact] | ||
public void NoErrors() => Collector.Diagnostics.Should().BeEmpty(); | ||
} | ||
|
||
public class EmptyAppliesBlock(ITestOutputHelper output) : DirectiveTest<AppliesBlock>(output, | ||
""" | ||
```{applies} | ||
``` | ||
""" | ||
) | ||
{ | ||
[Fact] | ||
public void ParsesBlock() => Block.Should().NotBeNull(); | ||
|
||
[Fact] | ||
public void DoesNotRender() => | ||
Html.Should().BeNullOrWhiteSpace(); | ||
|
||
[Fact] | ||
public void EmitErrorOnEmptyBlock() | ||
{ | ||
Collector.Diagnostics.Should().NotBeNullOrEmpty().And.HaveCount(2); | ||
Collector.Diagnostics.Should().OnlyContain(d => d.Severity == Severity.Error); | ||
Collector.Diagnostics.Should() | ||
.Contain(d => d.Message.Contains("{applies} block with no product availability specified")); | ||
|
||
Collector.Diagnostics.Should() | ||
.Contain(d => d.Message.Contains("{applies} should follow a heading")); | ||
} | ||
} | ||
|
||
// ensures we allow for empty lines between heading and applies block | ||
public class AppliesHeadingTests(ITestOutputHelper output) : DirectiveTest<AppliesBlock>(output, | ||
""" | ||
# heading | ||
```{applies} | ||
:eck: unavailable | ||
``` | ||
""" | ||
) | ||
{ | ||
[Fact] | ||
public void NoErrors() => Collector.Diagnostics.Should().BeEmpty(); | ||
} | ||
|