From e57c3880eaa969c2a697d02d247f187d3eb36684 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Sat, 3 Sep 2022 21:13:57 -0300 Subject: [PATCH] When include in readme fails, log a warning This makes it easier to troubleshoot include authoring mistakes. Fixes #232 --- src/NuGetizer.Tasks/CreatePackage.cs | 3 ++- src/NuGetizer.Tasks/IncludesResolver.cs | 10 ++++++++-- src/NuGetizer.Tests/IncludesResolverTests.cs | 17 ++++++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/NuGetizer.Tasks/CreatePackage.cs b/src/NuGetizer.Tasks/CreatePackage.cs index c2df6c8c..4d4b9d9a 100644 --- a/src/NuGetizer.Tasks/CreatePackage.cs +++ b/src/NuGetizer.Tasks/CreatePackage.cs @@ -203,8 +203,9 @@ void GeneratePackage(Stream output = null) File.Exists(readmeFile.Source)) { // replace readme with includes replaced. + var replaced = IncludesResolver.Process(readmeFile.Source, message => Log.LogWarningCode("NG001", message)); var temp = Path.GetTempFileName(); - File.WriteAllText(temp, IncludesResolver.Process(readmeFile.Source)); + File.WriteAllText(temp, replaced); readmeFile.Source = temp; } diff --git a/src/NuGetizer.Tasks/IncludesResolver.cs b/src/NuGetizer.Tasks/IncludesResolver.cs index bb21998c..81d2d785 100644 --- a/src/NuGetizer.Tasks/IncludesResolver.cs +++ b/src/NuGetizer.Tasks/IncludesResolver.cs @@ -13,7 +13,7 @@ public class IncludesResolver static readonly Regex IncludeRegex = new Regex(@"", RegexOptions.Compiled); static readonly HttpClient http = new(); - public static string Process(string filePath) + public static string Process(string filePath, Action logWarning = default) { string content = null; @@ -74,8 +74,10 @@ public static string Process(string filePath) var anchor = $""; var start = includedContent.IndexOf(anchor); if (start == -1) - // Warn/error? + { + logWarning?.Invoke($"Failed to resolve anchor {fragment} in {includedPath}."); continue; + } includedContent = includedContent.Substring(start); var end = includedContent.IndexOf(anchor, anchor.Length); @@ -91,6 +93,10 @@ public static string Process(string filePath) else replacements[new Regex(@$"")] = replacement; } + else + { + logWarning?.Invoke($"Failed to resolve include: {includedPath}{fragment}. File not found at expected location {includedFullPath}."); + } } if (replacements.Count > 0) diff --git a/src/NuGetizer.Tests/IncludesResolverTests.cs b/src/NuGetizer.Tests/IncludesResolverTests.cs index 81dc1ab1..51e54ec2 100644 --- a/src/NuGetizer.Tests/IncludesResolverTests.cs +++ b/src/NuGetizer.Tests/IncludesResolverTests.cs @@ -1,4 +1,5 @@ -using Xunit; +using System.IO; +using Xunit; namespace NuGetizer; @@ -25,4 +26,18 @@ public void ResolveUrlInclude() Assert.Contains("Daniel Cazzulino", content); } + + [Fact] + public void ResolveNonExistingInclude() + { + var path = Path.GetTempFileName(); + var include = ""; + File.WriteAllText(path, include); + + string? failed = default; + var content = IncludesResolver.Process(path, s => failed = s); + + Assert.NotNull(failed); + Assert.Contains("foo.md#bar", failed); + } }