Skip to content

Commit

Permalink
Adding AssertAppLogsCanContainWarningsAndCacheFolderErrorsAsync for a…
Browse files Browse the repository at this point in the history
… common log ignore use case
  • Loading branch information
Piedone committed Jul 9, 2024
1 parent 86b53c7 commit 6e7d9d4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
24 changes: 12 additions & 12 deletions Lombiq.Tests.UI.Samples/UITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Lombiq.Tests.UI.Extensions;
using Lombiq.Tests.UI.Samples.Helpers;
using Lombiq.Tests.UI.Services;
using Shouldly;
using System;
using System.Threading.Tasks;
using Xunit.Abstractions;
Expand Down Expand Up @@ -87,17 +86,18 @@ protected override Task ExecuteTestAsync(
// assertions: By default, the Orchard logs and the browser logs (where e.g. JavaScript errors show
// up) are checked and if there are any errors, the test will fail. You can also enable the checking of
// accessibility rules as we'll see later. Maybe not all of the default checks are suitable for you.
// Then it's simple to override them; here we change which log entries cause the tests to fail. We use
// the trick of making expected error messages not look like real errors.
configuration.AssertAppLogsAsync = async webApplicationInstance =>
(await webApplicationInstance.GetLogOutputAsync())
.ReplaceOrdinalIgnoreCase(
"|Lombiq.TrainingDemo.Services.DemoBackgroundTask|ERROR|Expected non-error",
"|Lombiq.TrainingDemo.Services.DemoBackgroundTask|EXPECTED_ERROR|Expected non-error")
.ReplaceOrdinalIgnoreCase(
"|OrchardCore.Media.Core.DefaultMediaFileStoreCacheFileProvider|ERROR|Error deleting cache folder",
"|OrchardCore.Media.Core.DefaultMediaFileStoreCacheFileProvider|EXPECTED_ERROR|Error deleting cache folder")
.ShouldNotContain("|ERROR|");
// Then it's simple to override them; here we change which log entries cause the tests to fail, and
// allow warnings and certain errors.
// Note that this is just for demonstration; you could use
// OrchardCoreUITestExecutorConfiguration.AssertAppLogsCanContainWarningsAndCacheFolderErrorsAsync which
// provides this configuration built-in.
configuration.AssertAppLogsAsync = webApplicationInstance =>
webApplicationInstance.LogsShouldBeEmptyAsync(
canContainWarnings: true,
permittedErrorLines:
[
"OrchardCore.Media.Core.DefaultMediaFileStoreCacheFileProvider|ERROR|Error deleting cache folder",
]);
if (changeConfigurationAsync != null) await changeConfigurationAsync(configuration);
});
Expand Down
25 changes: 11 additions & 14 deletions Lombiq.Tests.UI/Extensions/WebApplicationInstanceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,26 @@ public static async Task LogsShouldBeEmptyAsync(
ICollection<string> permittedErrorLines = null,
CancellationToken cancellationToken = default)
{
if (cancellationToken == default) cancellationToken = CancellationToken.None;
permittedErrorLines ??= [];

var logOutput = await webApplicationInstance.GetLogOutputAsync(cancellationToken);

if (canContainWarnings)
{
logOutput.ShouldNotContain("|FATAL|");
logOutput.ShouldNotContain("|FATAL|");

var errorLines = logOutput
.SplitByNewLines()
.Where(line => line.Contains("|ERROR|"));
var lines = logOutput.SplitByNewLines();

if (permittedErrorLines.Count != 0)
{
errorLines = errorLines.Where(line => !permittedErrorLines.Any(line.ContainsOrdinalIgnoreCase));
}
var errorLines = lines.Where(line => line.Contains("|ERROR|"));

errorLines.ShouldBeEmpty();
if (permittedErrorLines.Count != 0)
{
errorLines = errorLines.Where(line => !permittedErrorLines.Any(line.ContainsOrdinalIgnoreCase));
}
else

errorLines.ShouldBeEmpty();

if (!canContainWarnings)
{
logOutput.ShouldBeEmpty();
lines.Where(line => line.Contains("|WARNING|")).ShouldBeEmpty();
}
}

Expand Down
10 changes: 10 additions & 0 deletions Lombiq.Tests.UI/Services/OrchardCoreUITestExecutorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public class OrchardCoreUITestExecutorConfiguration
public static readonly Func<IWebApplicationInstance, Task> AssertAppLogsCanContainWarningsAsync =
app => app.LogsShouldBeEmptyAsync(canContainWarnings: true);

public static readonly Func<IWebApplicationInstance, Task> AssertAppLogsCanContainWarningsAndCacheFolderErrorsAsync =
app => app.LogsShouldBeEmptyAsync(
canContainWarnings: true,
permittedErrorLines:
[
// These errors frequently happen during UI testing when using Azure Blob Storage for media storage.
// They're harmless, though.
"OrchardCore.Media.Core.DefaultMediaFileStoreCacheFileProvider|ERROR|Error deleting cache folder",
]);

public static readonly Action<IEnumerable<LogEntry>> AssertBrowserLogIsEmpty =
logEntries => logEntries.ShouldNotContain(
logEntry => IsValidBrowserLogEntry(logEntry),
Expand Down

0 comments on commit 6e7d9d4

Please sign in to comment.