Skip to content

Commit

Permalink
test: Improve testing of file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 2, 2024
1 parent 191fa25 commit 23a19e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 9 additions & 7 deletions AzureLiquid.Preview/PreviewProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public PreviewProcess()
/// Gets or sets a value indicating whether the process should watch for changes to template or content files.
/// </summary>
/// <value>
/// <c>true</c> if should watch; otherwise, <c>false</c>.
/// <c>true</c> if the process should watch for changes; otherwise, <c>false</c>.
/// </value>
[ExcludeFromCodeCoverage]
private bool ShouldWatch { get; set; }
Expand Down Expand Up @@ -228,21 +228,23 @@ public string Render()
/// Reads the file content.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <param name="retry">The operation is being retried.</param>
/// <returns>The file content.</returns>
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;
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
Expand Down
18 changes: 18 additions & 0 deletions AzureLiquid.Tests/PreviewProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ public void EnsureWatcher()
instance.Log.Should().NotBeEmpty("A log should have been created");
}

/// <summary>
/// Ensure the preview process can read a file.
/// </summary>
[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");
}

/// <summary>
/// Contains arranged values used for testing, containing mock instances and expected return values.
/// </summary>
Expand Down

0 comments on commit 23a19e0

Please sign in to comment.