Skip to content

Commit 326b17b

Browse files
authored
Add basic tests (#2)
Adds some simple tests and code coverage to GH actions
1 parent 84ab6bb commit 326b17b

15 files changed

+471
-25
lines changed

.github/workflows/build.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,23 @@ jobs:
1616
uses: actions/checkout@v2
1717
with:
1818
fetch-depth: 0
19-
- name: .NET Build
19+
- name: Build
2020
run: dotnet build Build.csproj -c Release --nologo /p:CI=true
21-
- name: .NET Test
21+
- name: Test
2222
run: dotnet test Build.csproj -c Release --no-build --nologo /p:CI=true
23+
- name: Tool Restore
24+
working-directory: tests
25+
run: dotnet tool restore
26+
- name: Code coverage
27+
run: dotnet test Build.csproj -c Release --no-build --nologo /p:CI=true --collect:"XPlat Code Coverage"
28+
- name: Code coverage report generation
29+
working-directory: tests
30+
run: dotnet reportgenerator -reports:NFig.AspNetCore.Tests/TestResults/*/coverage.cobertura.xml -targetdir:$GITHUB_WORKSPACE/.coverage -reporttypes:HtmlInline
31+
- name: Archive code coverage results
32+
uses: actions/upload-artifact@v2
33+
with:
34+
name: code-coverage-report
35+
path: .coverage/*.html
2336
build-windows:
2437
runs-on: windows-latest
2538
if: "!contains(github.event.head_commit.message, 'ci skip')"
@@ -28,7 +41,7 @@ jobs:
2841
uses: actions/checkout@v2
2942
with:
3043
fetch-depth: 0
31-
- name: .NET Build
44+
- name: Build
3245
run: dotnet build Build.csproj -c Release --nologo /p:CI=true
33-
- name: .NET Test
46+
- name: Test
3447
run: dotnet test Build.csproj -c Release --no-build --nologo /p:CI=true

.github/workflows/packages.yml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,23 @@ jobs:
1616
uses: actions/checkout@v2
1717
with:
1818
fetch-depth: 0
19-
- name: .NET Build
19+
- name: Build
2020
run: dotnet build Build.csproj -c Release --nologo /p:CI=true
21-
- name: .NET Test
21+
- name: Test
2222
run: dotnet test Build.csproj -c Release --no-build --nologo /p:CI=true
23+
- name: Tool Restore
24+
working-directory: tests
25+
run: dotnet tool restore
26+
- name: Code coverage
27+
run: dotnet test Build.csproj -c Release --no-build --nologo /p:CI=true --collect:"XPlat Code Coverage"
28+
- name: Code coverage report generation
29+
working-directory: tests
30+
run: dotnet reportgenerator -reports:NFig.AspNetCore.Tests/TestResults/*/coverage.cobertura.xml -targetdir:$GITHUB_WORKSPACE/.coverage -reporttypes:HtmlInline
31+
- name: Archive code coverage results
32+
uses: actions/upload-artifact@v2
33+
with:
34+
name: code-coverage-report
35+
path: .coverage/*.html
2336
build-windows:
2437
needs: build-ubuntu
2538
runs-on: windows-latest
@@ -29,11 +42,11 @@ jobs:
2942
uses: actions/checkout@v2
3043
with:
3144
fetch-depth: 0
32-
- name: .NET Build
45+
- name: Build
3346
run: dotnet build Build.csproj -c Release --nologo /p:CI=true
34-
- name: .NET Test
47+
- name: Test
3548
run: dotnet test Build.csproj -c Release --no-build --nologo /p:CI=true
36-
- name: .NET Pack
49+
- name: Pack
3750
run: dotnet pack Build.csproj --no-build -c Release --nologo /p:PackageOutputPath=${env:GITHUB_WORKSPACE}\.nupkgs /p:CI=true
3851
- name: Push to MyGet
3952
run: dotnet nuget push ${env:GITHUB_WORKSPACE}\.nupkgs\*.nupkg -s https://www.myget.org/F/stackoverflow/api/v2/package -k ${{ secrets.MYGET_API_KEY }}

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ node_modules/
99
.publish/
1010
.idea/
1111
*.user
12-
.DS_Store
12+
.DS_Store
13+
.coverage
14+
TestResults/

src/NFig.AspNetCore/Attributes.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("NFig.AspNetCore.Tests")]

src/NFig.AspNetCore/NFigOptionsManager`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public NFigOptionsManager(IOptionsMonitor<TOptions> monitor)
1616
_monitor = monitor;
1717
}
1818

19-
public TOptions Value => _monitor.Get(Options.DefaultName);
19+
public TOptions Value => Get(Options.DefaultName);
2020

2121
public TOptions Get(string name) => _monitor.Get(name);
2222
}

tests/.config/dotnet-tools.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": false,
4+
"tools": {
5+
"dotnet-reportgenerator-globaltool": {
6+
"version": "4.6.1",
7+
"commands": [
8+
"reportgenerator"
9+
]
10+
}
11+
}
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Builder.Internal;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Xunit;
7+
8+
namespace NFig.AspNetCore.Tests
9+
{
10+
public class ArgumentTests
11+
{
12+
[Fact]
13+
public void NullApplicationThrows()
14+
{
15+
Assert.Throws<ArgumentNullException>(
16+
"applicationName",
17+
() =>
18+
{
19+
var configuration = new ConfigurationBuilder().Build();
20+
var services = new ServiceCollection()
21+
.AddSingleton<IConfiguration>(configuration)
22+
.AddNFig<Settings, Tier, DataCenter>()
23+
.BuildServiceProvider();
24+
25+
var appBuilder = new ApplicationBuilder(services);
26+
27+
appBuilder.UseNFig<Settings, Tier, DataCenter>(
28+
(configuration, builder) =>
29+
{
30+
builder.UseRedis(null, Tier.Local, DataCenter.Local, null);
31+
}
32+
);
33+
}
34+
);
35+
}
36+
}
37+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Options;
4+
using Xunit;
5+
6+
namespace NFig.AspNetCore.Tests
7+
{
8+
public class BasicFunctionalityTests : IClassFixture<NFigFixture<Startup>>
9+
{
10+
private readonly NFigFixture<Startup> _fixture;
11+
12+
public BasicFunctionalityTests(NFigFixture<Startup> fixture)
13+
{
14+
_fixture = fixture;
15+
}
16+
17+
[Fact]
18+
public void OptionsAreConfiguredCorrectly()
19+
{
20+
var services = _fixture.Host.Services;
21+
var settings = services.GetService<IOptions<Settings>>();
22+
Assert.NotNull(settings);
23+
var featureFlags = services.GetService<IOptions<Settings.FeatureFlagSettings>>();
24+
Assert.NotNull(featureFlags);
25+
Assert.Same(settings.Value.FeatureFlags, featureFlags.Value);
26+
}
27+
28+
[Fact]
29+
public void OverridesArePropagated()
30+
{
31+
var services = _fixture.Host.Services;
32+
var featureFlags = services.GetService<IOptions<Settings.FeatureFlagSettings>>();
33+
34+
Assert.NotNull(featureFlags);
35+
Assert.False(featureFlags.Value.FoobarEnabled);
36+
37+
Assert.True(NFigSettingsCache.TryGet<Settings, Tier, DataCenter>(out var storeWithSettings));
38+
if (!(storeWithSettings.Store is NFigMemoryStore<Settings, Tier, DataCenter> memoryStore))
39+
{
40+
throw new Exception("Expected an NFigMemoryStore");
41+
}
42+
43+
var settingName = nameof(Settings.FeatureFlags) + "." + nameof(Settings.FeatureFlagSettings.FoobarEnabled);
44+
memoryStore.SetOverride(Settings.ApplicationName, settingName, bool.TrueString, Settings.Tier, Settings.DataCenter);
45+
46+
featureFlags = services.GetService<IOptions<Settings.FeatureFlagSettings>>();
47+
Assert.NotNull(featureFlags);
48+
Assert.True(featureFlags.Value.FoobarEnabled);
49+
}
50+
51+
[Fact]
52+
public void RemovalsArePropagated()
53+
{
54+
var services = _fixture.Host.Services;
55+
56+
Assert.True(NFigSettingsCache.TryGet<Settings, Tier, DataCenter>(out var storeWithSettings));
57+
if (!(storeWithSettings.Store is NFigMemoryStore<Settings, Tier, DataCenter> memoryStore))
58+
{
59+
throw new Exception("Expected an NFigMemoryStore");
60+
}
61+
62+
var settingName = nameof(Settings.FeatureFlags) + "." + nameof(Settings.FeatureFlagSettings.FoobarEnabled);
63+
memoryStore.SetOverride(Settings.ApplicationName, settingName, bool.TrueString, Settings.Tier, Settings.DataCenter);
64+
65+
var featureFlags = services.GetService<IOptions<Settings.FeatureFlagSettings>>();
66+
Assert.NotNull(featureFlags);
67+
Assert.True(featureFlags.Value.FoobarEnabled);
68+
69+
memoryStore.ClearOverride(Settings.ApplicationName, settingName, Settings.Tier, Settings.DataCenter);
70+
featureFlags = services.GetService<IOptions<Settings.FeatureFlagSettings>>();
71+
Assert.NotNull(featureFlags);
72+
Assert.False(featureFlags.Value.FoobarEnabled);
73+
}
74+
}
75+
}

tests/NFig.AspNetCore.Tests/NFig.AspNetCore.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
10+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
911
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
12+
1013
<PackageReference Include="xunit" Version="2.4.1" />
14+
<PackageReference Include="coverlet.collector" Version="1.3.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers" />
1115
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" />
16+
<ProjectReference Include="..\..\src\NFig.AspNetCore\NFig.AspNetCore.csproj" />
1217
</ItemGroup>
1318

1419
</Project>

tests/NFig.AspNetCore.Tests/NFigConfigurationProviderTests.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)