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

Add environment variable controlled skipable LocalFactAttribute #6009

Merged
merged 4 commits into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//-----------------------------------------------------------------------
// <copyright file="LocalFactAttribute.cs" company="Akka.NET Project">
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------

using System;
using Xunit;

namespace Akka.TestKit.Xunit2.Attributes
{
/// <summary>
/// <para>
/// This custom XUnit Fact attribute will skip unit tests if the environment variable
/// "XUNIT_SKIP_LOCAL_FACTS" exists and is set to the string "true"
Aaronontheweb marked this conversation as resolved.
Show resolved Hide resolved
/// </para>
/// <para>
/// Note that the original <see cref="FactAttribute.Skip"/> property takes precedence over this attribute,
/// any unit tests with <see cref="LocalFactAttribute"/> with its <see cref="FactAttribute.Skip"/> property
/// set will always be skipped, regardless of the environment variable content.
/// </para>
/// </summary>
public class LocalFactAttribute: FactAttribute
{
private const string EnvironmentVariableName = "XUNIT_SKIP_LOCAL_FACTS";

private string _skip;

/// <inheritdoc cref="FactAttribute.Skip"/>
public override string Skip
{
get
{
if (_skip != null)
return _skip;

var skipLocal = Environment.GetEnvironmentVariable(EnvironmentVariableName)?
.ToLowerInvariant();
return skipLocal is "true" ? SkipLocal ?? "Local facts are being skipped" : null;
}
set => _skip = value;
}

/// <summary>
/// The reason why this unit test is being skipped by the <see cref="LocalFactAttribute"/>.
/// Note that the original <see cref="FactAttribute.Skip"/> property takes precedence over this message.
/// </summary>
public string SkipLocal { get; set; }
}
}