From 23a19e04f836aeeefb6fe379033590e3502d4144 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 2 Dec 2024 14:16:53 +0000 Subject: [PATCH] test: Improve testing of file parsing --- AzureLiquid.Preview/PreviewProcess.cs | 16 +++++++++------- AzureLiquid.Tests/PreviewProcessTests.cs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/AzureLiquid.Preview/PreviewProcess.cs b/AzureLiquid.Preview/PreviewProcess.cs index 70b6321..6bfc0b3 100644 --- a/AzureLiquid.Preview/PreviewProcess.cs +++ b/AzureLiquid.Preview/PreviewProcess.cs @@ -77,7 +77,7 @@ public PreviewProcess() /// Gets or sets a value indicating whether the process should watch for changes to template or content files. /// /// - /// true if should watch; otherwise, false. + /// true if the process should watch for changes; otherwise, false. /// [ExcludeFromCodeCoverage] private bool ShouldWatch { get; set; } @@ -228,21 +228,23 @@ public string Render() /// Reads the file content. /// /// The file path. + /// The operation is being retried. /// The file content. - internal string ReadFileContent(string filePath) + internal string ReadFileContent(string filePath, bool retry = false) { try { return File.ReadAllText(filePath); } - catch (IOException) + catch { // Lock issue, wait and retry Thread.Sleep(TimeSpan.FromSeconds(1)); - return ReadFileContent(filePath); - } - catch - { + if (!retry) + { + return ReadFileContent(filePath, true); + } + LogWarning($"Unable to read file: {filePath}"); return string.Empty; } diff --git a/AzureLiquid.Tests/PreviewProcessTests.cs b/AzureLiquid.Tests/PreviewProcessTests.cs index 9356741..ad2ebc5 100644 --- a/AzureLiquid.Tests/PreviewProcessTests.cs +++ b/AzureLiquid.Tests/PreviewProcessTests.cs @@ -143,6 +143,24 @@ public void EnsureWatcher() instance.Log.Should().NotBeEmpty("A log should have been created"); } + /// + /// Ensure the preview process can read a file. + /// + [Fact] + public void EnsureFileReadExceptionHandling() + { + // Arrange + var instance = new PreviewProcess(); + const string file = "notfound.liquid"; + + // Act + var result = instance.ReadFileContent(file); + + // Assert + result.Should().BeEmpty("A result should not have been created"); + instance.Log.Should().NotBeEmpty("A log should not have been created"); + } + /// /// Contains arranged values used for testing, containing mock instances and expected return values. ///