EnvironmentAbstractions 1.0.0-preview

This is a prerelease version of EnvironmentAbstractions.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package EnvironmentAbstractions --version 1.0.0-preview
NuGet\Install-Package EnvironmentAbstractions -Version 1.0.0-preview
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EnvironmentAbstractions" Version="1.0.0-preview" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnvironmentAbstractions --version 1.0.0-preview
#r "nuget: EnvironmentAbstractions, 1.0.0-preview"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EnvironmentAbstractions as a Cake Addin
#addin nuget:?package=EnvironmentAbstractions&version=1.0.0-preview&prerelease

// Install EnvironmentAbstractions as a Cake Tool
#tool nuget:?package=EnvironmentAbstractions&version=1.0.0-preview&prerelease

EnvironmentAbstractions

The EnvironmentAbstractions package provides an interface, IEnvironmentVariableProvider, that can be used as a layer of abstraction for code to be easier to mock for unit testing.

IEnvironmentVariableProvider

The IEnvironmentVariableProvider interface supports the following methods:

public interface IEnvironmentVariableProvider
{
    string? ExpandEnvironmentVariables(string name);

    string? GetEnvironmentVariable(string name, EnvironmentVariableTarget target);

    string? GetEnvironmentVariable(string name);

    IReadOnlyDictionary<string, string> GetEnvironmentVariables();

    IReadOnlyDictionary<string, string> GetEnvironmentVariables(EnvironmentVariableTarget target);

    void SetEnvironmentVariable(string name, string? value);

    void SetEnvironmentVariable(string name, string? value, EnvironmentVariableTarget target);
}

Examples

The example below shows how to use IEnvironmentVariableProvider in a class to read environment variables. You should have two constructors: one that does default logic and another that accepts an IEnvironmentVariableProvider instance. The constructor that accepts an IEnvironmentVariableProvider can be internal so it can only be called by unit tests.

public class MyClass
{
    private readonly IEnvironmentVariableProvider environmentVariableProvider;

    /// <summary>
    /// Internal constructor only for unit tests which reads environment variables from the specified IEnvironmentVariableProvider.
    /// </summary>
    internal MyClass(IEnvironmentVariableProvider environmentVariableProvider)
    {
        this.environmentVariableProvider = environmentVariableProvider
    }

    /// <summary>
    /// Initializes a new instance of the MyClass class which reads environment variables directly from the system.
    /// </summary>
    public MyClass()
      : this(SystemEnvironmentVariableProvider.Instance)
    {
    }

    public CustomConfig GetConfiguration()
    {
        // Check if the user has set an environment variable to override the default location
        string customConfigLocation = environmentVariableProvider.GetEnvironmentVariable("CUSTOM_CONFIG");

        if (!string.IsNullOrWhitespace(environmentVariableProvider))
        {
            return LoadConfigFromLocation(customConfigLocation);
        }

        // Load the configuration from the default location, %UserProfile%\configuration.xml
        return LoadConfiguationFromLocation(Path.Combine(environmentVariableProvider.GetEnvironmentVariable("USERPROFILE"), "configuration.xml"));
    }
}

Unit tests can then use the MockEnvironmentVariableProvider class from the EnvironmentAbstractions.TestHelpers package, an existing mocking framework like Moq, or any custom implementation.

[Fact]
public void MethodReturnsExpectedValue()
{
    IEnvironmentVariableProvider environmentVariableProvider = new MockEnvironmentVariableProvider
    {
        ["Variable1"] = "Value1";
    };

    MyClass instance = new MyClass(environmentVariableProvider);

    string value = instance.Method();
}

Preventing usage of System.Environment

If you want to use IEnvironmentVariableProvider exclusively in your repository, you can reference the EnvironmentAbstractions.BannedApiAnalyzer package which uses the Microsoft.CodeAnalysis.BannedApiAnalyzers Roslyn analyzer to prevent code from accessing the System.Environment APIs.

Sample project

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="EnvironmentAbstractions.BannedApiAnalyzer" Version="1.0.0" />
  </ItemGroup>
</Project>

Sample source code

public static void Main(string[] args)
{
    Console.WriteLine("Hello, {0}!", System.Environment.GetEnvironmentVariable("USERNAME"));
}

Sample error

warning RS0030: The symbol 'Environment.GetEnvironmentVariable(string)' is banned in this project: Use IEnvironmentVariableProvider.GetEnvironmentVariable(string) instead.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on EnvironmentAbstractions:

Package Downloads
EnvironmentAbstractions.TestHelpers

Provides implementations of IEnvironmentVariableProvider so that unit tests can mock calls that retrieve environment variable.

EnvironmentAbstractions.BannedApiAnalyzer

Adds rules for Microsoft.CodeAnalysis.BannedApiAnalyzers to ensure projects don't use the System.Environment class to manipulate environment variables..

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EnvironmentAbstractions:

Repository Stars
microsoft/slngen
Visual Studio solution generator
Version Downloads Last updated
3.0.2 2,936 11/15/2023
2.1.0 647 9/26/2023