Skip to content

Commit

Permalink
Add environment variable controlled skipable LocalFactAttribute (#6009)
Browse files Browse the repository at this point in the history
* Add environment variable controlled skipable LocalFactAttribute

* Fix copyright header
  • Loading branch information
Arkatufus committed Jun 20, 2022
1 parent f46d845 commit 125a2a3
Showing 1 changed file with 51 additions and 0 deletions.
@@ -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"
/// </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; }
}
}

0 comments on commit 125a2a3

Please sign in to comment.