From 74c98e48b935d90714daa8f12971e9cbbcd40bf3 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Wed, 20 Nov 2024 09:34:33 +0100 Subject: [PATCH] Fix DiagnosticsCollector API to be a bit cleaner --- .../Diagnostics/DiagnosticsChannel.cs | 43 ++++++++++++------- .../DocumentationGenerator.cs | 5 +-- .../DiagnosticLinkInlineParser.cs | 4 ++ .../Directives/AdmonitionTests.cs | 6 +++ .../Directives/AdmonitionUnsupportedTests.cs | 1 - .../Directives/DirectiveBaseTests.cs | 4 +- .../Inline/InlneBaseTests.cs | 4 +- 7 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs b/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs index a9944ea..9a01560 100644 --- a/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs +++ b/src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs @@ -41,7 +41,6 @@ public void Write(Diagnostic diagnostic) } } - public enum Severity { Error, Warning } public readonly record struct Diagnostic @@ -70,7 +69,6 @@ public void Write(Diagnostic diagnostic) } } - public class DiagnosticsCollector(ILoggerFactory loggerFactory, IReadOnlyCollection outputs) : IHostedService { @@ -84,24 +82,32 @@ public class DiagnosticsCollector(ILoggerFactory loggerFactory, IReadOnlyCollect public long Warnings => _warnings; public long Errors => _errors; + private Task? _started; + public HashSet OffendingFiles { get; } = new(); - public async Task StartAsync(Cancel ctx) + public Task StartAsync(Cancel ctx) { - await Channel.WaitToWrite(); - while (!Channel.CancellationToken.IsCancellationRequested) + if (_started is not null) return _started; + _started = Task.Run(async () => { - try - { - while (await Channel.Reader.WaitToReadAsync(Channel.CancellationToken)) - Drain(); - } - catch + await Channel.WaitToWrite(); + while (!Channel.CancellationToken.IsCancellationRequested) { - //ignore + try + { + while (await Channel.Reader.WaitToReadAsync(Channel.CancellationToken)) + Drain(); + } + catch + { + //ignore + } } - } - Drain(); + + Drain(); + }, ctx); + return _started; void Drain() { @@ -124,7 +130,12 @@ private void IncrementSeverityCount(Diagnostic item) Interlocked.Increment(ref _warnings); } - protected virtual void HandleItem(Diagnostic diagnostic) {} + protected virtual void HandleItem(Diagnostic diagnostic) { } - public virtual Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public virtual async Task StopAsync(CancellationToken cancellationToken) + { + if (_started is not null) + await _started; + await Channel.Reader.Completion; + } } diff --git a/src/Elastic.Markdown/DocumentationGenerator.cs b/src/Elastic.Markdown/DocumentationGenerator.cs index bc6e74e..2b0a02e 100644 --- a/src/Elastic.Markdown/DocumentationGenerator.cs +++ b/src/Elastic.Markdown/DocumentationGenerator.cs @@ -97,7 +97,7 @@ public async Task GenerateAll(Cancel ctx) var handledItems = 0; - var collectTask = Task.Run(async () => await Context.Collector.StartAsync(ctx), ctx); + _ = Context.Collector.StartAsync(ctx); await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) => { @@ -126,11 +126,8 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) => await GenerateDocumentationState(ctx); - await collectTask; - await Context.Collector.Channel.Reader.Completion; await Context.Collector.StopAsync(ctx); - IFileInfo OutputFile(string relativePath) { var outputFile = _writeFileSystem.FileInfo.New(Path.Combine(DocumentationSet.OutputPath.FullName, relativePath)); diff --git a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs index a2cebb1..e01ba40 100644 --- a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs +++ b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs @@ -76,6 +76,10 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice) var pathOnDisk = Path.Combine(includeFrom, url.TrimStart('/')); if (!context.Build.ReadFileSystem.File.Exists(pathOnDisk)) processor.EmitError(line, column, length, $"`{url}` does not exist. resolved to `{pathOnDisk}"); + else + { + + } } else link.Url = ""; diff --git a/tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs b/tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs index 814a840..baed992 100644 --- a/tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs +++ b/tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs @@ -41,6 +41,12 @@ public class TipTests(ITestOutputHelper output) : AdmonitionTests(output, "tip") public void SetsTitle() => Block!.Title.Should().Be("Tip"); } +public class AttentionTests(ITestOutputHelper output) : AdmonitionTests(output, "attention") +{ + [Fact] + public void SetsTitle() => Block!.Title.Should().Be("Attention"); +} + public class NoteTitleTests(ITestOutputHelper output) : DirectiveTest(output, """ ```{note} This is my custom note diff --git a/tests/Elastic.Markdown.Tests/Directives/AdmonitionUnsupportedTests.cs b/tests/Elastic.Markdown.Tests/Directives/AdmonitionUnsupportedTests.cs index 66e24a5..80223d3 100644 --- a/tests/Elastic.Markdown.Tests/Directives/AdmonitionUnsupportedTests.cs +++ b/tests/Elastic.Markdown.Tests/Directives/AdmonitionUnsupportedTests.cs @@ -26,7 +26,6 @@ A regular paragraph. } // ReSharper disable UnusedType.Global -public class AttentionTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "attention"); public class DangerTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "danger"); public class ErrorTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "error"); public class HintTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "hint"); diff --git a/tests/Elastic.Markdown.Tests/Directives/DirectiveBaseTests.cs b/tests/Elastic.Markdown.Tests/Directives/DirectiveBaseTests.cs index 7fcd321..03ec0f7 100644 --- a/tests/Elastic.Markdown.Tests/Directives/DirectiveBaseTests.cs +++ b/tests/Elastic.Markdown.Tests/Directives/DirectiveBaseTests.cs @@ -86,14 +86,12 @@ protected virtual void AddToFileSystem(MockFileSystem fileSystem) { } public virtual async Task InitializeAsync() { - var collectTask = Task.Run(async () => await Collector.StartAsync(default), default); + _ = Collector.StartAsync(default); Document = await File.ParseFullAsync(default); Html = File.CreateHtml(Document); Collector.Channel.TryComplete(); - await collectTask; - await Collector.Channel.Reader.Completion; await Collector.StopAsync(default); } diff --git a/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs b/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs index acd7af3..b85428c 100644 --- a/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs @@ -96,7 +96,7 @@ protected virtual void AddToFileSystem(MockFileSystem fileSystem) { } public virtual async Task InitializeAsync() { - var collectTask = Task.Run(async () => await Collector.StartAsync(default), default); + _ = Collector.StartAsync(default); await Set.ResolveDirectoryTree(default); @@ -104,8 +104,6 @@ public virtual async Task InitializeAsync() Html = File.CreateHtml(Document); Collector.Channel.TryComplete(); - await collectTask; - await Collector.Channel.Reader.Completion; await Collector.StopAsync(default); }