process/src/process.tests/_fixture/TestEnvironment.cs

32 lines
741 B
C#
Raw Normal View History

2026-01-20 22:41:16 +01:00
// Copyright (c) The Geekeey Authors
// SPDX-License-Identifier: EUPL-1.2
namespace Geekeey.Process.Tests;
internal sealed class TestEnvironment : IDisposable
{
private readonly Action _action;
private TestEnvironment(Action action)
{
_action = action;
}
public static TestEnvironment Create(string name, string? value)
{
var lastValue = Environment.GetEnvironmentVariable(name);
Environment.SetEnvironmentVariable(name, value);
return new TestEnvironment(() => Environment.SetEnvironmentVariable(name, lastValue));
}
public static TestEnvironment ExtendPath(string path)
{
return Create("PATH", Environment.GetEnvironmentVariable("PATH") + Path.PathSeparator + path);
}
public void Dispose()
{
_action();
}
}