Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysparry committed Mar 17, 2024
0 parents commit 39accfa
Show file tree
Hide file tree
Showing 18 changed files with 817 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "nuget" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
assignees:
- "rhysparry"
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
assignees:
- "rhysparry"
31 changes: 31 additions & 0 deletions .github/workflows/dotnet-ci-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: .NET CI Build

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

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

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build

- name: Test
run: dotnet test --no-build --verbosity normal
52 changes: 52 additions & 0 deletions .github/workflows/nuget-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: NuGet Publish

on:
push:
tags:
- 'v*.*.*'

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

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

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --configuration Release

- name: Test
run: dotnet test --configuration Release --no-build --verbosity normal

- name: Pack
run: dotnet pack --configuration Release
if: startsWith(github.ref, 'refs/tags/v')

- name: Create a release with the NuGet Package
uses: softprops/action-gh-release@v2
with:
files: '**/*.nupkg'
if: startsWith(github.ref, 'refs/tags/v')

- name: Load NuGet API Key
uses: 1password/load-secrets-action@v1
with:
export-env: true
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
NUGET_API_KEY: "op://GitHub Actions/NuGet API Dirt/credential"
if: startsWith(github.ref, 'refs/tags/v')

- name: Push NuGet package
run: dotnet nuget push **/*.nupkg --source https://api.nuget.org/v3/index.json --api-key $NUGET_API_KEY
if: startsWith(github.ref, 'refs/tags/v')

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
/.idea
*.user
30 changes: 30 additions & 0 deletions Dirt.Env.Tests/Dirt.Env.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>Dirt.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dirt.Env\Dirt.Env.csproj" />
</ItemGroup>

</Project>
233 changes: 233 additions & 0 deletions Dirt.Env.Tests/EnvBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
using Dirt.Environment;

namespace Dirt.Tests;

public class EnvBuilderTests
{
[Fact]
public void NewEnvBuilderCreatesEmptyEnvironment()
{
var builder = new EnvBuilder();
var env = builder.Build();
Assert.Empty(env);
}

[Fact]
public void EnvBuilderAddsVariable()
{
var builder = new EnvBuilder()
.SetVariable("key", "value");
var env = builder.Build();
Assert.Equal("value", env["key"]);
}

[Fact]
public void EnvBuilderOverwritesExistingVariablesByDefault()
{
var builder = new EnvBuilder()
.SetVariable("key", "value1")
.SetVariable("key", "value2");
var env = builder.Build();
Assert.Equal("value2", env["key"]);
}

[Fact]
public void EnvBuilderDoesNotOverwriteExistingVariablesWhenSkipDuplicate()
{
var builder = new EnvBuilder
{
DefaultDuplicateKeyStrategy = IfDuplicate.Skip
}
.SetVariable("key", "value1")
.SetVariable("key", "value2");
var env = builder.Build();
Assert.Equal("value1", env["key"]);
}

[Fact]
public void EnvBuilderClearsVariables()
{
var builder = new EnvBuilder()
.SetVariable("key", "value1")
.Clear();
var env = builder.Build();
Assert.Empty(env);
}

[Fact]
public void EnvBuilderCanUnsetPreviouslySetVariables()
{
var builder = new EnvBuilder()
.SetVariable("key", "value")
.SetVariable("key2", "value2")
.UnsetVariable("key");

var env = builder.Build();
Assert.False(env.ContainsKey("key"));
Assert.True(env.ContainsKey("key2"));
}

private static readonly string[] TwoValues = ["value1", "value2"];

[Fact]
public void EnvBuilderAutomaticallyHandlesSettingPathVariable()
{
var builder = new EnvBuilder();
builder
.SetVariable(builder.PathVariable, TwoValues[0])
.AppendPath(TwoValues[1]);
var env = builder.Build();
Assert.Equal(TwoValues, env.Path);
}

[Fact]
public void CanAppendPathEvenWhenPathVariableIsNotSet()
{
var builder = new EnvBuilder()
.AppendPath(TwoValues[0]);
var env = builder.Build();
Assert.Equal(TwoValues[0], env.Path[0]);
}

[Fact]
public void CanPrependPath()
{
var builder = new EnvBuilder()
.AppendPath(TwoValues[1])
.PrependPath(TwoValues[0]);
var env = builder.Build();
Assert.Equal(TwoValues, env.Path);
}

[Fact]
public void CanPrependPathEventWhenPathVariableIsNotSet()
{
var builder = new EnvBuilder()
.PrependPath(TwoValues[0]);
var env = builder.Build();
Assert.Equal(TwoValues[0], env.Path[0]);
}

[Fact]
public void SkippingWhenAppendingPathPreservesOriginal()
{
var builder = new EnvBuilder
{
DefaultDuplicatePathStrategy = IfDuplicatePath.Skip
}
.AppendPath(TwoValues[0])
.AppendPath(TwoValues[1])
.AppendPath(TwoValues[0]);
var env = builder.Build();
Assert.Equal(TwoValues, env.Path);
}

[Fact]
public void SupersedingWhenAppendingPathRemovesOriginal()
{
var builder = new EnvBuilder
{
DefaultDuplicatePathStrategy = IfDuplicatePath.Supersede
}
.AppendPath(TwoValues[0])
.AppendPath(TwoValues[1])
.AppendPath(TwoValues[0]);
var env = builder.Build();
Assert.Equal(TwoValues[1], env.Path[0]);
Assert.Equal(TwoValues[0], env.Path[1]);
}

[Fact]
public void IgnoringWhenAppendingPathPreservesOriginal()
{
var builder = new EnvBuilder
{
DefaultDuplicatePathStrategy = IfDuplicatePath.Ignore
}
.AppendPath(TwoValues[0])
.AppendPath(TwoValues[1])
.AppendPath(TwoValues[0]);
var env = builder.Build();
Assert.Equal(TwoValues.Append(TwoValues[0]), env.Path);
}

[Fact]
public void UsingWindowsPathSetsPathAsExpected()
{
var builder = new EnvBuilder()
.UsingWindowsPath()
.AppendPath(TwoValues[0])
.AppendPath(TwoValues[1]);

var env = builder.Build();
Assert.True(env.ContainsKey("Path"));
Assert.Equal($"{TwoValues[0]};{TwoValues[1]}", env["Path"]);
}

[Fact]
public void UsingNixPathSetsPathAsExpected()
{
var builder = new EnvBuilder()
.UsingNixPath()
.AppendPath(TwoValues[0])
.AppendPath(TwoValues[1]);

var env = builder.Build();
Assert.True(env.ContainsKey("PATH"));
Assert.Equal($"{TwoValues[0]}:{TwoValues[1]}", env["PATH"]);
}

[Fact]
public void CanInstantiateUsingSystemEnvironment()
{
var builder = new EnvBuilder()
.WithSystemEnvironment();
var env = builder.Build();
Assert.NotEmpty(env);
}

[Fact]
public void CanBuildWithCustomEnvironment()
{
var builder = new EnvBuilder();
builder
.WithEnvironment(new Dictionary<string, string>
{
["key"] = "value",
[builder.PathVariable] = TwoValues[0]
})
.AppendPath(TwoValues[1]);
var env = builder.Build();
Assert.Equal("value", env["key"]);
Assert.Equal(TwoValues, env.Path);
}

[Fact]
public void TryGetValueReturnsFalseWhenKeyIsNotPresent()
{
var builder = new EnvBuilder();
var env = builder.Build();
Assert.False(env.TryGetValue("key", out _));
}

[Fact]
public void TryGetValueReturnsTrueWhenKeyIsPresent()
{
var builder = new EnvBuilder()
.SetVariable("key", "value");
var env = builder.Build();
Assert.True(env.TryGetValue("key", out var value));
Assert.Equal("value", value);
}

[Fact]
public void ValueContainsExpectedValues()
{
var builder = new EnvBuilder()
.SetVariable("key", "value");
var env = builder.Build();
Assert.Contains("key", env.Keys);
Assert.Contains("value", env.Values);
Assert.Single(env.Values);
}
}
Loading

0 comments on commit 39accfa

Please sign in to comment.