Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-466: Fixing that TestOutput.log not included in failure dump when run in GitHub Actions #230

Merged
merged 8 commits into from
Nov 13, 2022
Merged
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI/OrchardCoreUITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected virtual async Task ExecuteTestAsync(

var originalTestOutputHelper = _testOutputHelper;
(_testOutputHelper, var afterTest) =
GitHubActionsGroupingTestOutputHelper.CreateWrapper(_testOutputHelper, testManifest);
GitHubActionsGroupingTestOutputHelper.CreateDecorator(_testOutputHelper, testManifest);

var configuration = new OrchardCoreUITestExecutorConfiguration
{
Expand Down
21 changes: 11 additions & 10 deletions Lombiq.Tests.UI/Services/GitHubActionsGroupingTestOutputHelper.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
using Lombiq.Tests.UI.Models;
using Lombiq.Tests.UI.Models;
using System;
using Xunit.Abstractions;

namespace Lombiq.Tests.UI.Services;

internal sealed class GitHubActionsGroupingTestOutputHelper : ITestOutputHelper
internal sealed class GitHubActionsGroupingTestOutputHelper : ITestOutputHelperDecorator
{
public static Lazy<bool> IsGitHubEnvironment { get; } = new(() =>
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ENV")));

private readonly ITestOutputHelper _inner;
private readonly string _groupName;

private bool _isStarted;

public GitHubActionsGroupingTestOutputHelper(ITestOutputHelper inner, string groupName)
public ITestOutputHelper Decorated { get; private set; }

public GitHubActionsGroupingTestOutputHelper(ITestOutputHelper decorated, string groupName)
{
_inner = inner;
Decorated = decorated;
_groupName = groupName;
}

public void WriteLine(string message)
{
Start();
_inner.WriteLine(message);
Decorated.WriteLine(message);
}

public void WriteLine(string format, params object[] args)
{
Start();
_inner.WriteLine(format, args);
Decorated.WriteLine(format, args);
}

private void Start()
{
if (_isStarted) return;

_inner.WriteLine($"::group::{_groupName}");
Decorated.WriteLine($"::group::{_groupName}");
_isStarted = true;
}

private void EndGroup()
{
if (_isStarted) _inner.WriteLine("::endgroup::");
if (_isStarted) Decorated.WriteLine("::endgroup::");
}

public static (ITestOutputHelper WrappedOutputHelper, Action AfterTest) CreateWrapper(
public static (ITestOutputHelper DecoratedOutputHelper, Action AfterTest) CreateDecorator(
ITestOutputHelper testOutputHelper,
UITestManifest testManifest)
{
Expand Down
14 changes: 14 additions & 0 deletions Lombiq.Tests.UI/Services/ITestOutputHelperDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Xunit.Abstractions;

namespace Lombiq.Tests.UI.Services;

/// <summary>
/// Defines an <see cref="ITestOutputHelper"/> that decorates another <see cref="ITestOutputHelper"/>.
/// </summary>
public interface ITestOutputHelperDecorator : ITestOutputHelper
{
/// <summary>
/// Gets the decorated <see cref="ITestOutputHelper"/> instance.
/// </summary>
ITestOutputHelper Decorated { get; }
}
5 changes: 4 additions & 1 deletion Lombiq.Tests.UI/Services/UITestExecutionSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ private async Task SaveTestOutputAsync(string debugInformationPath)
{
try
{
if (_testOutputHelper is TestOutputHelper concreteTestOutputHelper)
var concreteTestOutputHelper = _testOutputHelper as TestOutputHelper;
concreteTestOutputHelper ??= (_testOutputHelper as ITestOutputHelperDecorator)?.Decorated as TestOutputHelper;

if (concreteTestOutputHelper != null)
{
// While this depends on the directory creation in the above try block it needs to come after the catch
// otherwise the message saved there wouldn't be included.
Expand Down