-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathInProcessEnvIsolation.cs
More file actions
111 lines (94 loc) · 4.78 KB
/
Copy pathInProcessEnvIsolation.cs
File metadata and controls
111 lines (94 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit.Sdk;
namespace GitHub.Copilot.Test.Harness;
// Because many of the tests mutate global environment variables, we have to snapshot the original
// state and restore it after each test. Otherwise tests influence each other depending on run order.
// This is especially important for the in-process transport because the runtime is inside the test
// host process and will be reading/writing its environment variables directly.
internal static class InProcessEnvIsolation
{
// Unset because CI sets them but the replay snapshots expect Bearer/OAuth.
private static readonly string[] SuppressEnvVars = ["COPILOT_HMAC_KEY", "CAPI_HMAC_KEY"];
// Captured at load, before any fixture/test mutates env.
private static readonly Dictionary<string, string?> s_ambient = CaptureEnvironment();
// The process working directory captured at load, restored after each test so an
// in-process test that repoints the cwd (the FFI worker inherits it at spawn)
// can't leak that change into the next test.
private static readonly string s_ambientCwd = Directory.GetCurrentDirectory();
// Runs at assembly load so the ambient env is snapshotted before the shared
// fixture mirrors per-test env onto the process. Justifies suppressing CA2255.
#pragma warning disable CA2255 // ModuleInitializer discouraged in libraries; intentional in this test harness.
[ModuleInitializer]
internal static void CaptureAtLoad() => _ = s_ambient;
#pragma warning restore CA2255
[DllImport("libc", EntryPoint = "setenv", CharSet = CharSet.Ansi,
BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern int NativeSetEnv(string name, string value, int overwrite);
[DllImport("libc", EntryPoint = "unsetenv", CharSet = CharSet.Ansi,
BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern int NativeUnsetEnv(string name);
// Sets/unsets on the managed cache and, on Unix, the libc block so native
// readers in the loaded cdylib observe it.
public static void Apply(string name, string? value)
{
Environment.SetEnvironmentVariable(name, value);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_ = value is null ? NativeUnsetEnv(name) : NativeSetEnv(name, value, 1);
}
}
public static void NeutralizeAmbientCredentials()
{
foreach (var name in SuppressEnvVars)
{
Apply(name, null);
}
}
// Points the process working directory at the given path so the in-process FFI
// worker inherits it at spawn (the native host has no per-client cwd parameter).
// RestoreAmbient() returns the process to its load-time cwd after the test.
public static void SetWorkingDirectory(string path) =>
Directory.SetCurrentDirectory(path);
public static void RestoreAmbient()
{
// Unconditionally repoint the process cwd at its load-time value. We must
// not read Directory.GetCurrentDirectory() first: an in-process test can
// chdir into a temp work dir that the harness then deletes, so getcwd()
// would throw FileNotFoundException. SetCurrentDirectory to an absolute
// path succeeds regardless of whether the old cwd still exists.
Directory.SetCurrentDirectory(s_ambientCwd);
foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
{
var name = (string)entry.Key;
if (!s_ambient.ContainsKey(name))
{
Apply(name, null);
}
}
foreach (var (name, value) in s_ambient)
{
if (!string.Equals(Environment.GetEnvironmentVariable(name), value, StringComparison.Ordinal))
{
Apply(name, value);
}
}
}
private static Dictionary<string, string?> CaptureEnvironment() =>
Environment.GetEnvironmentVariables()
.Cast<DictionaryEntry>()
.ToDictionary(e => (string)e.Key, e => e.Value?.ToString(), StringComparer.Ordinal);
}
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class InProcessEnvIsolationAttribute : BeforeAfterTestAttribute
{
public override void Before(MethodInfo methodUnderTest) =>
InProcessEnvIsolation.NeutralizeAmbientCredentials();
public override void After(MethodInfo methodUnderTest) =>
InProcessEnvIsolation.RestoreAmbient();
}