Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Build, Test & Coverage

on:
push:
branches: [ master ]
pull_request:
workflow_dispatch:

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup .NET 10 SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: '10.x'

- name: Restore dependencies
run: dotnet restore
working-directory: ./src

- name: Build all projects
run: dotnet build --configuration Release --no-restore
working-directory: ./src

- name: Run tests with code coverage
run: dotnet test --configuration Release --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=../../coverage/coverage.cobertura.xml
working-directory: ./src

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./coverage/
fail_ci_if_error: true
33 changes: 0 additions & 33 deletions .github/workflows/build.yml

This file was deleted.

70 changes: 35 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI - Build, Test & Coverage](https://github.com/bolorundurowb/dotenv.net/actions/workflows/build.yml/badge.svg)](https://github.com/bolorundurowb/dotenv.net/actions/workflows/build.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![Coverage Status](https://coveralls.io/repos/github/bolorundurowb/dotenv.net/badge.svg?branch=master)](https://coveralls.io/github/bolorundurowb/dotenv.net?branch=master)
[![codecov](https://codecov.io/gh/bolorundurowb/dotenv.net/graph/badge.svg?token=Qw8xSiEHNp)](https://codecov.io/gh/bolorundurowb/dotenv.net)
![NuGet Version](https://img.shields.io/nuget/v/dotenv.net)


Expand All @@ -19,7 +19,7 @@ Whether you're building a small project or a large-scale application, **dotenv.n
- **Simple and Pain-Free** 🎯: Easily load and read `.env` files with minimal setup.
- **Flexible Configuration** 🔧: Customize how environment variables are loaded with a variety of options.
- **Dependency Injection Support** 🧩: Works seamlessly with popular DI frameworks.
- **Cross-Platform** 🌍: Fully compatible with .NET Core, .NET 5, and beyond.
- **Cross-Platform** 🌍: Fully compatible with .NET Core, .NET 5, .NET 6, .NET 9 and beyond.
- **Open Source** 💡: Actively maintained and supported by the community.

---
Expand All @@ -42,7 +42,7 @@ You can install **dotenv.net** via NuGet:

- **Manual Installation** (via `.csproj`):
```xml
<PackageReference Include="dotenv.net" Version="4.0.0"/>
<PackageReference Include="dotenv.net" Version="4.x.x"/>
```

---
Expand All @@ -61,13 +61,43 @@ You can install **dotenv.net** via NuGet:
DotEnv.Load();
```

This will automatically locate and load the `.env` file in the same directory as your application.
This will automatically locate and load the `.env` file in the same directory as your application's executable.

---

### Fluent API 🎨

For a more expressive syntax, **dotenv.net** provides a fluent API:

```csharp
// Load environment variables with custom options
DotEnv.Fluent()
.WithExceptions()
.WithEnvFiles("./path/to/env")
.WithTrimValues()
.WithEncoding(Encoding.ASCII)
.WithOverwriteExistingVars()
.WithProbeForEnv(probeLevelsToSearch: 6)
.WithSupportExportSyntax()
.Load();

// Read environment variables
var envVars = DotEnv.Fluent()
.WithoutExceptions()
.WithEnvFiles() // Defaults to .env
.WithoutTrimValues()
.WithEncoding(Encoding.UTF8)
.WithoutOverwriteExistingVars()
.WithoutProbeForEnv()
.WithoutSupportExportSyntax()
.Read();
```

---

### Advanced Configuration ⚙️

**dotenv.net** offers a wide range of configuration options to tailor the loading process to your needs:
**dotenv.net** offers a wide range of configuration options to tailor the loading process to your needs using the `DotEnvOptions` class:

- **Specify Custom `.env` File Paths**:
```csharp
Expand Down Expand Up @@ -112,36 +142,6 @@ Console.WriteLine(envVars["KEY"]); // Outputs the value associated with 'KEY'

---

### Fluent API 🎨

For a more expressive syntax, **dotenv.net** provides a fluent API:

```csharp
// Load environment variables with custom options
DotEnv.Fluent()
.WithExceptions()
.WithEnvFiles("./path/to/env")
.WithTrimValues()
.WithEncoding(Encoding.ASCII)
.WithOverwriteExistingVars()
.WithProbeForEnv(probeLevelsToSearch: 6)
.WithSupportExportSyntax()
.Load();

// Read environment variables
var envVars = DotEnv.Fluent()
.WithoutExceptions()
.WithEnvFiles() // Defaults to .env
.WithoutTrimValues()
.WithDefaultEncoding()
.WithoutOverwriteExistingVars()
.WithoutProbeForEnv()
.WithoutSupportExportSyntax()
.Read();
```

---

### Environment Variable Helpers 🛠️

The `Utilities` namespace provides additional methods for reading environment variables in a typed manner:
Expand Down
2 changes: 0 additions & 2 deletions src/dotenv.net.Tests/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public void ReadFileLines_InvalidPathAndIgnoreExceptionsFalse_ShouldThrowArgumentException(string? path,
bool ignoreExceptions)
{
Action act = () => Reader.ReadFileLines(path, ignoreExceptions, null);

Check warning on line 45 in src/dotenv.net.Tests/ReaderTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'envFilePath' in 'ReadOnlySpan<string> Reader.ReadFileLines(string envFilePath, bool ignoreExceptions, Encoding? encoding)'.
act.ShouldThrow<ArgumentException>().Message
.ShouldContain("The file path cannot be null, empty or whitespace.");
}
Expand All @@ -54,7 +54,7 @@
public void ReadFileLines_InvalidPathAndIgnoreExceptionsTrue_ShouldReturnEmptySpan(string? path,
bool ignoreExceptions)
{
var result = Reader.ReadFileLines(path, ignoreExceptions, null).ToArray();

Check warning on line 57 in src/dotenv.net.Tests/ReaderTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'envFilePath' in 'ReadOnlySpan<string> Reader.ReadFileLines(string envFilePath, bool ignoreExceptions, Encoding? encoding)'.
result.ShouldBeEmpty();
}

Expand Down Expand Up @@ -201,8 +201,6 @@
exception.Message.ShouldContain($"after searching {levelsToSearch} directory level(s) upwards.");
exception.Message.ShouldContain("Searched paths:");
exception.Message.ShouldContain(_startPath);
exception.Message.ShouldContain("net9.0");
exception.Message.ShouldContain("Debug");
}

[Fact]
Expand Down
10 changes: 5 additions & 5 deletions src/dotenv.net.Tests/dotenv.net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PackageReference Include="coverlet.collector" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
<PackageReference Include="coverlet.msbuild" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.9.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions dotenv.net.sln → src/dotenv.net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Visual Studio 2013
VisualStudioVersion = 12.0.0.0
MinimumVisualStudioVersion = 10.0.0.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotenv.net", "src\dotenv.net\dotenv.net.csproj", "{ABC30C21-62C1-4DF5-B352-4D33BCDD9845}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotenv.net", "dotenv.net\dotenv.net.csproj", "{ABC30C21-62C1-4DF5-B352-4D33BCDD9845}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotenv.net.Tests", "src\dotenv.net.Tests\dotenv.net.Tests.csproj", "{FC5ED4B3-DFDC-45E9-889C-847433F08E0E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotenv.net.Tests", "dotenv.net.Tests\dotenv.net.Tests.csproj", "{FC5ED4B3-DFDC-45E9-889C-847433F08E0E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
16 changes: 10 additions & 6 deletions src/dotenv.net/DotEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

namespace dotenv.net;

/// <summary>
/// Provides static methods to configure, read, and load environment variables from .env files.
/// </summary>
public static class DotEnv
{
/// <summary>
/// Initialize the fluent configuration API
/// Initialises the fluent configuration API.
/// </summary>
/// <returns>A new instance of <see cref="DotEnvOptions" />.</returns>
public static DotEnvOptions Fluent() => new();

/// <summary>
/// Read and return the values in the provided env files
/// Reads the values from the provided env files based on the specified options.
/// </summary>
/// <param name="options">The options required to configure the env loader</param>
/// <returns>The key value pairs read from the env files</returns>
/// <param name="options">The options required to configure the env loader. If null, default options are used.</param>
/// <returns>A dictionary containing the key-value pairs read from the env files.</returns>
public static IDictionary<string, string> Read(DotEnvOptions? options = null)
{
options ??= new DotEnvOptions();
Expand All @@ -35,9 +39,9 @@ public static IDictionary<string, string> Read(DotEnvOptions? options = null)
}

/// <summary>
/// Load the values in the provided env files into the environment variables
/// Loads the values from the provided env files into the system environment variables.
/// </summary>
/// <param name="options">The options required to configure the env loader</param>
/// <param name="options">The options required to configure the env loader. If null, default options are used.</param>
public static void Load(DotEnvOptions? options = null)
{
options ??= new DotEnvOptions();
Expand Down
Loading
Loading