| | | 1 | | using ArturRios.Configuration.Enums; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Util.Test.Attributes; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Shared logic that decides whether a custom test attribute should skip the current test, |
| | | 7 | | /// based on the active <c>ASPNETCORE_ENVIRONMENT</c> and an optional manual condition. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class EnvironmentSkip |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Computes the skip reason for a test. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="environments"> |
| | | 15 | | /// Environments in which the test must <b>not</b> run. When the current environment is one of these, |
| | | 16 | | /// the test is skipped. <c>null</c> means the test is not restricted by environment. |
| | | 17 | | /// </param> |
| | | 18 | | /// <param name="skipCondition">When <c>true</c>, the test is skipped regardless of the environment.</param> |
| | | 19 | | /// <returns>The skip reason, or <c>null</c> when the test should run.</returns> |
| | | 20 | | public static string? GetReason(EnvironmentType[]? environments, bool skipCondition) |
| | 18 | 21 | | { |
| | 18 | 22 | | if (skipCondition) |
| | 2 | 23 | | { |
| | 2 | 24 | | return "Condition to skip matched"; |
| | | 25 | | } |
| | | 26 | | |
| | 16 | 27 | | if (environments is null || environments.Length == 0) |
| | 11 | 28 | | { |
| | 11 | 29 | | return null; |
| | | 30 | | } |
| | | 31 | | |
| | 5 | 32 | | var currentEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); |
| | | 33 | | |
| | 10 | 34 | | var blocked = environments.Any(x => x.ToString().Equals(currentEnvironment, StringComparison.OrdinalIgnoreCase)) |
| | | 35 | | |
| | 5 | 36 | | return blocked ? $"Test can't run on {currentEnvironment}" : null; |
| | 18 | 37 | | } |
| | | 38 | | } |