-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to disable the detection of a CI/CD environment to over…
…ride the disabling of output coloring
- Loading branch information
Showing
6 changed files
with
118 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,74 @@ | ||
using Xunit; | ||
namespace Pastel.Tests | ||
{ | ||
using Xunit; | ||
|
||
using System; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Pastel.Tests | ||
{ | ||
public class EnvironmentTests | ||
{ | ||
private static readonly object _lock = new object(); | ||
|
||
[Theory] | ||
[InlineData("BITBUCKET_SOMEKEY", "somevalue", true)] | ||
[InlineData("BITbucket_SOMEKEY", "somevalue", true)] | ||
[InlineData("TEAMCITY_SOMEKEY", "somevalue", true)] | ||
[InlineData("TEAMcity_SOMEKEY", "somevalue", true)] | ||
[InlineData("NO_COLOR", "true", true)] | ||
[InlineData("no_color", "true", true)] | ||
[InlineData("GITHUB_ACTION", "somevalue", true)] | ||
[InlineData("GitHuB_action", "somevalue", true)] | ||
[InlineData("CI", "true", true)] | ||
[InlineData("ci", "true", true)] | ||
[InlineData("CI", "1", true)] | ||
[InlineData("ci", "1", true)] | ||
[InlineData("CI", "false", false)] | ||
[InlineData("ci", "false", false)] | ||
[InlineData("CI", "0", false)] | ||
[InlineData("ci", "0", false)] | ||
[InlineData("JENKINS_URL", "http://localhost:8080", true)] | ||
[InlineData("jenkins_URL", "http://localhost:8080", true)] | ||
[InlineData("TEAMCITY_VERSION", "2023.1.1", true)] | ||
[InlineData("teamcity_VERSION", "2023.1.1", true)] | ||
[InlineData("SOME_OTHER_KEY", "somevalue", false)] | ||
[InlineData("some_other_KEY", "somevalue", false)] | ||
private const string DisableEnvironmentDetectionEnvironmentVariableName = "PASTEL_DISABLE_ENVIRONMENT_DETECTION"; | ||
|
||
public void TestEnvironmentVariables(string key, string value, bool expected) | ||
[Theory, CombinatorialData] | ||
public void TestEnvironmentVariables([CombinatorialMemberData(nameof(GetEnvironmentVariables))] (string Key, string Value, bool ExpectedOutcome) environmentVariable, [CombinatorialMemberData(nameof(GetEnvironmentDetectionDisabledEnvironmentVariables))] string environmentDetectionDisabledEnvironmentVariable) | ||
{ | ||
try | ||
{ | ||
// Arrange | ||
Environment.SetEnvironmentVariable(key, value, EnvironmentVariableTarget.Process); | ||
if (environmentDetectionDisabledEnvironmentVariable != null) | ||
{ | ||
Environment.SetEnvironmentVariable(DisableEnvironmentDetectionEnvironmentVariableName, environmentDetectionDisabledEnvironmentVariable, EnvironmentVariableTarget.Process); | ||
} | ||
Environment.SetEnvironmentVariable(environmentVariable.Key, environmentVariable.Value, EnvironmentVariableTarget.Process); | ||
|
||
// Act | ||
var result = EnvironmentDetector.ColorsDisabled; | ||
var result = EnvironmentDetector.ColorsEnabled(); | ||
|
||
// Assert | ||
Assert.Equal(expected, result); | ||
Assert.Equal(environmentDetectionDisabledEnvironmentVariable != null || environmentVariable.ExpectedOutcome, result); | ||
} | ||
finally | ||
{ | ||
// Cleanup | ||
Environment.SetEnvironmentVariable(key, null, EnvironmentVariableTarget.Process); | ||
Environment.SetEnvironmentVariable(DisableEnvironmentDetectionEnvironmentVariableName, null, EnvironmentVariableTarget.Process); | ||
Environment.SetEnvironmentVariable(environmentVariable.Key, null, EnvironmentVariableTarget.Process); | ||
} | ||
} | ||
|
||
private static IEnumerable<(string Key, string Value, bool ExpectedOutcome)> GetEnvironmentVariables() | ||
{ | ||
yield return ("BITBUCKET_SOMEKEY", "somevalue", false); | ||
yield return ("BITbucket_SOMEKEY", "somevalue", false); | ||
yield return ("TEAMCITY_SOMEKEY", "somevalue", false); | ||
yield return ("TEAMcity_SOMEKEY", "somevalue", false); | ||
yield return ("NO_COLOR", "true", false); | ||
yield return ("no_color", "true", false); | ||
yield return ("GITHUB_ACTION", "somevalue", false); | ||
yield return ("GitHuB_action", "somevalue", false); | ||
yield return ("CI", "true", false); | ||
yield return ("ci", "true", false); | ||
yield return ("CI", "1", false); | ||
yield return ("ci", "1", false); | ||
yield return ("CI", "false", true); | ||
yield return ("ci", "false", true); | ||
yield return ("CI", "0", true); | ||
yield return ("ci", "0", true); | ||
yield return ("JENKINS_URL", "http://localhost:8080", false); | ||
yield return ("jenkins_URL", "http://localhost:8080", false); | ||
yield return ("TEAMCITY_VERSION", "2023.1.1", false); | ||
yield return ("teamcity_VERSION", "2023.1.1", false); | ||
yield return ("SOME_OTHER_KEY", "somevalue", true); | ||
yield return ("some_other_KEY", "somevalue", true); | ||
} | ||
|
||
private static IEnumerable<string> GetEnvironmentDetectionDisabledEnvironmentVariables() | ||
{ | ||
yield return null; | ||
yield return "1"; | ||
yield return "true"; | ||
yield return "TRUE"; | ||
yield return "trUe"; | ||
yield return "something_random"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters