forked from redhat-developer/dotnet-bunny
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for filtering out enviornment variables in tests
Only enabled for OPENSSL_CONF for now. See: redhat-developer#51
- Loading branch information
Showing
13 changed files
with
203 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "PassingBashTestWithEnvironmentVariables", | ||
"enabled": true, | ||
"requiresSdk": true, | ||
"version": "1.0", | ||
"versionSpecific": false, | ||
"type": "bash", | ||
"cleanup": true, | ||
"ignoredRIDs":[ | ||
] | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
|
||
env | ||
|
||
if [[ -n "${OPENSSL_CONF}" ]]; then | ||
echo "error: OPENSSL_CONF should never be set" | ||
exit 1 | ||
fi |
14 changes: 14 additions & 0 deletions
14
Samples/PassingXUnitTestWithEnvironmentVariables/PassingXUnitTestWithEnvironmentVariables.cs
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Samples | ||
{ | ||
public class PassingXUnitTestWithEnvironmentVariables | ||
{ | ||
[Fact] | ||
public void Test1() | ||
{ | ||
Assert.Null(Environment.GetEnvironmentVariable("OPENSSL_CONF")); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../PassingXUnitTestWithEnvironmentVariables/PassingXUnitTestWithEnvironmentVariables.csproj
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
12 changes: 12 additions & 0 deletions
12
Samples/PassingXUnitTestWithEnvironmentVariables/test.json
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "PassingXUnitTestWithEnvironmentVariables", | ||
"enabled": true, | ||
"requiresSdk": true, | ||
"version": "1.0", | ||
"versionSpecific": false, | ||
"type": "xunit", | ||
"cleanup": true, | ||
"ignoredRIDs":[ | ||
] | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
using Xunit; | ||
|
||
namespace Turkey.Tests | ||
{ | ||
public class EnvironmentVariableSanitizerTest | ||
{ | ||
[Theory] | ||
// TODO enable these too | ||
// [InlineData("ASPNETCORE_URLS")] | ||
// [InlineData("COREHOST_TRACE")] | ||
// [InlineData("DOTNET_FOO_BAR")] | ||
// [InlineData("DOTNET_ROLL_FORWARD")] | ||
// [InlineData("DOTNET_RUNTIME_ID")] | ||
// [InlineData("DOTNET_STARTUP_HOOKS")] | ||
// [InlineData("NUGET_PACKAGES")] | ||
[InlineData("OPENSSL_CONF")] | ||
public void EnvironmentVariablesAreRemoved(string name) | ||
{ | ||
var environment = new Dictionary<string, string>() | ||
{ | ||
{ name, "foobar" }, | ||
}; | ||
|
||
var sanitizer = new EnvironmentVariableSanitizer(); | ||
var result = sanitizer.SanitizeEnvironmentVariables(environment); | ||
|
||
Assert.DoesNotContain(name, result.Keys); | ||
} | ||
|
||
[InlineData("DOTNET_ROOT")] | ||
[InlineData("DOTNET_CLI_TELEMETRY_OPTOUT")] | ||
[InlineData("PATH")] | ||
[InlineData("USER")] | ||
[InlineData("HOME")] | ||
public void EnvironmentVariablesAreKept(string name) | ||
{ | ||
var environment = new Dictionary<string, string>() | ||
{ | ||
{ name, "foobar" }, | ||
}; | ||
|
||
var sanitizer = new EnvironmentVariableSanitizer(); | ||
var result = sanitizer.SanitizeEnvironmentVariables(environment); | ||
|
||
Assert.Contains(name, result.Keys); | ||
|
||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Turkey | ||
{ | ||
public class EnvironmentVariableSanitizer | ||
{ | ||
private readonly List<string> ToFilter = new List<string>() | ||
{ | ||
"OPENSSL_CONF", | ||
}; | ||
|
||
public Dictionary<string, string> SanitizeCurrentEnvironmentVariables() | ||
{ | ||
return SanitizeEnvironmentVariables(Environment.GetEnvironmentVariables()); | ||
} | ||
|
||
public Dictionary<string, string> SanitizeEnvironmentVariables(IDictionary environmentVariables) | ||
{ | ||
var result = new Dictionary<string, string>(); | ||
|
||
foreach (DictionaryEntry entry in environmentVariables) | ||
{ | ||
if (ToFilter.Contains((string)entry.Key)) | ||
{ | ||
continue; | ||
} | ||
|
||
result.Add((string)entry.Key, (string)entry.Value); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
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