Skip to content

Commit

Permalink
Merge pull request #1240 from bUnit-dev/release/v1.24
Browse files Browse the repository at this point in the history
Release of new minor version v1.24
  • Loading branch information
egil authored Oct 14, 2023
2 parents 3142159 + 9da72c5 commit 2891c81
Show file tree
Hide file tree
Showing 74 changed files with 719 additions and 2,757 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- name: ⚙️ Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v5
uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.BUNIT_BOT_GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.BUNIT_BOT_GPG_KEY_PASSPHRASE }}
Expand Down Expand Up @@ -88,7 +88,7 @@ jobs:
- name: 🛠️ Deploy to GitHub Pages
if: success()
uses: crazy-max/ghaction-github-pages@v3
uses: crazy-max/ghaction-github-pages@v4
with:
build_dir: docs/site/_site
fqdn: bunit.dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/prepare-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
exit 1
- name: ⚙️ Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v5
uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.BUNIT_BOT_GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.BUNIT_BOT_GPG_KEY_PASSPHRASE }}
Expand Down
8 changes: 0 additions & 8 deletions .github/workflows/release-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ on:
types: [completed]
branches: [main, v2]

workflow_dispatch:
inputs:
nugetRelease:
description: 'Release to NuGet? Set to "true" to release to NuGet.org as well as GPR.'
required: true
default: 'true'

jobs:
release-preview:
if: github.event_name == 'workflow_dispatch' || (github.ref == 'refs/heads/main' && ${{ github.event.workflow_run.conclusion == 'success' }}) || (github.ref == 'refs/heads/v2' && ${{ github.event.workflow_run.conclusion == 'success' }})
Expand Down Expand Up @@ -58,5 +51,4 @@ jobs:
run: dotnet nuget push ${GITHUB_WORKSPACE}/packages/*.nupkg -k ${{ secrets.GITHUB_TOKEN }} -s https://nuget.pkg.github.com/bunit-dev/index.json --skip-duplicate --no-symbols

- name: 🛠️ Upload library to NuGet.org repository
if: github.event.inputs.nugetRelease == 'true'
run: dotnet nuget push ${GITHUB_WORKSPACE}/packages/*.nupkg -k ${{ secrets.NUGET_KEY }} -s https://api.nuget.org/v3/index.json --skip-duplicate --no-symbols
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: ⚙️ Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v5
uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.BUNIT_BOT_GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.BUNIT_BOT_GPG_KEY_PASSPHRASE }}
Expand Down
417 changes: 214 additions & 203 deletions CHANGELOG.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<PropertyGroup Label="Compile settings">
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>
<LangVersion>preview</LangVersion>
<AccelerateBuildsInVisualStudio>false</AccelerateBuildsInVisualStudio>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>CA1014,NU5104,NETSDK1138,SYSLIB0051</NoWarn>
Expand Down Expand Up @@ -51,7 +51,7 @@
<!-- Shared code analyzers used for all projects in the solution -->
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.9.0.77355" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.12.0.78982" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Label="Implicit usings"
Expand Down
2 changes: 1 addition & 1 deletion benchmark/bunit.benchmarks/bunit.benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.9" />
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 0 additions & 21 deletions bunit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.web.tests", "tests\bu
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.testassets", "tests\bunit.testassets\bunit.testassets.csproj", "{7972A80F-30DC-4EF4-9294-7D4DD2965882}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.web.testcomponents", "src\bunit.web.testcomponents\bunit.web.testcomponents.csproj", "{F61B8A55-3B5B-45FB-8E38-DBF9296B35C7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.web.testcomponents.tests", "tests\bunit.web.testcomponents.tests\bunit.web.testcomponents.tests.csproj", "{4CAB561E-0AFF-4516-AB9B-981F94EF1E86}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".workflows", ".workflows", "{3B2F3419-5336-4147-A212-E19091195203}"
ProjectSection(SolutionItems) = preProject
.github\workflows\docs-deploy.yml = .github\workflows\docs-deploy.yml
Expand All @@ -70,8 +66,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bunit.benchmarks.assets", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharpWrappers", "src\AngleSharpWrappers\AngleSharpWrappers.csproj", "{ABB3F046-0297-491A-9106-7948403BE0DE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharpWrappers.Tests", "tests\AngleSharpWrappers.Tests\AngleSharpWrappers.Tests.csproj", "{BE6B6206-E32F-4AFC-BAB4-AEC5E8A7F16B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -102,14 +96,6 @@ Global
{7972A80F-30DC-4EF4-9294-7D4DD2965882}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7972A80F-30DC-4EF4-9294-7D4DD2965882}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7972A80F-30DC-4EF4-9294-7D4DD2965882}.Release|Any CPU.Build.0 = Release|Any CPU
{F61B8A55-3B5B-45FB-8E38-DBF9296B35C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F61B8A55-3B5B-45FB-8E38-DBF9296B35C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F61B8A55-3B5B-45FB-8E38-DBF9296B35C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F61B8A55-3B5B-45FB-8E38-DBF9296B35C7}.Release|Any CPU.Build.0 = Release|Any CPU
{4CAB561E-0AFF-4516-AB9B-981F94EF1E86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CAB561E-0AFF-4516-AB9B-981F94EF1E86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CAB561E-0AFF-4516-AB9B-981F94EF1E86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CAB561E-0AFF-4516-AB9B-981F94EF1E86}.Release|Any CPU.Build.0 = Release|Any CPU
{9F7A0623-8294-4A5D-946F-70C481732AA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F7A0623-8294-4A5D-946F-70C481732AA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F7A0623-8294-4A5D-946F-70C481732AA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -122,10 +108,6 @@ Global
{ABB3F046-0297-491A-9106-7948403BE0DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABB3F046-0297-491A-9106-7948403BE0DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABB3F046-0297-491A-9106-7948403BE0DE}.Release|Any CPU.Build.0 = Release|Any CPU
{BE6B6206-E32F-4AFC-BAB4-AEC5E8A7F16B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE6B6206-E32F-4AFC-BAB4-AEC5E8A7F16B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE6B6206-E32F-4AFC-BAB4-AEC5E8A7F16B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE6B6206-E32F-4AFC-BAB4-AEC5E8A7F16B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -138,12 +120,9 @@ Global
{6127D121-9387-451B-B15D-8350A32D3001} = {9A2B3B34-D41C-43E8-BC7D-246BEBE48D59}
{FC122F63-8B22-4BAE-B96A-7AF3194CD204} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
{7972A80F-30DC-4EF4-9294-7D4DD2965882} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
{F61B8A55-3B5B-45FB-8E38-DBF9296B35C7} = {9A2B3B34-D41C-43E8-BC7D-246BEBE48D59}
{4CAB561E-0AFF-4516-AB9B-981F94EF1E86} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
{9F7A0623-8294-4A5D-946F-70C481732AA5} = {F6084D31-2A92-4794-A47E-A8F2254E6970}
{3619481F-DF6F-4399-9FED-450EE545A19E} = {F6084D31-2A92-4794-A47E-A8F2254E6970}
{ABB3F046-0297-491A-9106-7948403BE0DE} = {9A2B3B34-D41C-43E8-BC7D-246BEBE48D59}
{BE6B6206-E32F-4AFC-BAB4-AEC5E8A7F16B} = {6EA09ED4-B714-4E6F-B0E1-4D987F8AE520}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {24106918-1C86-4769-BDA6-9C80E64CD260}
Expand Down
49 changes: 49 additions & 0 deletions docs/samples/tests/xunit/CustomServiceProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Bunit.Docs.Samples;

public sealed class CustomServiceProvider : IServiceProvider, IServiceScopeFactory, IServiceScope
{
private readonly IServiceProvider _serviceProvider;

public CustomServiceProvider(IServiceCollection serviceDescriptors)
=> _serviceProvider = serviceDescriptors.BuildServiceProvider();

public object GetService(Type serviceType)
{
if (serviceType == typeof(IServiceScope) || serviceType == typeof(IServiceScopeFactory))
return this;

if (serviceType == typeof(DummyService))
return new DummyService();

return _serviceProvider.GetService(serviceType);
}

void IDisposable.Dispose() { }
public IServiceScope CreateScope() => this;
IServiceProvider IServiceScope.ServiceProvider => this;
}

public sealed class CustomServiceProviderFactoryContainerBuilder
{
private readonly IServiceCollection _serviceDescriptors;

public CustomServiceProviderFactoryContainerBuilder(IServiceCollection serviceDescriptors)
=> this._serviceDescriptors = serviceDescriptors;

public IServiceProvider Build()
=> new CustomServiceProvider(_serviceDescriptors);
}

public sealed class CustomServiceProviderFactory : IServiceProviderFactory<CustomServiceProviderFactoryContainerBuilder>
{
public CustomServiceProviderFactoryContainerBuilder CreateBuilder(IServiceCollection services)
=> new CustomServiceProviderFactoryContainerBuilder(services);

public IServiceProvider CreateServiceProvider(CustomServiceProviderFactoryContainerBuilder containerBuilder)
=> containerBuilder.Build();
}
89 changes: 89 additions & 0 deletions docs/samples/tests/xunit/CustomServiceProviderFactoryUsage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Bunit.Docs.Samples;
public class CustomServiceProviderFactoryUsage : TestContext
{
[Fact]
public void CustomServiceProviderViaFactoryReturns()
{
Services.UseServiceProviderFactory(new CustomServiceProviderFactory());

var dummyService = Services.GetService<DummyService>();

Assert.NotNull(dummyService);
}

[Fact]
public void CustomServiceProviderViaDelegateReturns()
{
Services.UseServiceProviderFactory(x => new CustomServiceProvider(x));

var dummyService = Services.GetService<DummyService>();

Assert.NotNull(dummyService);
}

[Fact]
public void AutofacServiceProviderViaFactoryReturns()
{
void ConfigureContainer(ContainerBuilder containerBuilder)
{
containerBuilder
.RegisterType<DummyService>()
.AsSelf();
}

Services.UseServiceProviderFactory(new AutofacServiceProviderFactory(ConfigureContainer));

//get a service which was installed in the Autofac ContainerBuilder

var dummyService = Services.GetService<DummyService>();

Assert.NotNull(dummyService);

//get a service which was installed in the bUnit ServiceCollection

var testContextBase = Services.GetService<TestContextBase>();

Assert.NotNull(testContextBase);
Assert.Equal(this, testContextBase);
}

[Fact]
public void AutofacServiceProviderViaDelegateReturns()
{
ILifetimeScope ConfigureContainer(IServiceCollection services)
{
var containerBuilder = new ContainerBuilder();

containerBuilder
.RegisterType<DummyService>()
.AsSelf();

containerBuilder.Populate(services);

return containerBuilder.Build();
}

Services.UseServiceProviderFactory(x => new AutofacServiceProvider(ConfigureContainer(x)));

//get a service which was installed in the Autofac ContainerBuilder

var dummyService = Services.GetService<DummyService>();

Assert.NotNull(dummyService);

//get a service which was installed in the bUnit ServiceCollection

var testContextBase = Services.GetService<TestContextBase>();

Assert.NotNull(testContextBase);
Assert.Equal(this, testContextBase);
}
}
2 changes: 2 additions & 0 deletions docs/samples/tests/xunit/bunit.docs.xunit.samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="7.1.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
Expand Down
29 changes: 29 additions & 0 deletions docs/site/docs/providing-input/inject-services-into-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ Here is a test where the fallback service provider is used:

In this example, the `DummyService` is provided by the fallback service provider, since it is not registered in the default service provider.

## Using a custom IServiceProvider implementation
A custom service provider factory can be registered with the built-in `TestServiceProvider`. It is used to create the underlying IServiceProvider. This enables a few interesting use cases, such as using an alternative IoC container (which should implement the `IServiceProvider` interface). This approach can be useful if the fallback service provider is not an option. For example, if you have dependencies in the fallback container, that rely on dependencies which are in the main container and vice versa.

### Registering Autofac service provider factory
The example makes use of `AutofacServiceProviderFactory` and `AutofacServiceProvider` from the package `Autofac.Extensions.DependencyInjection` and shows how to use an Autofac dependency container with bUnit.

Here is a test where the Autofac service provider factory is used:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=32&end=56)]

Here is a test where the Autofac service provider is used via delegate:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=58&end=88)]

### Registering a custom service provider factory
The examples contain dummy implementations of `IServiceProvider` and `IServiceProviderFactory<TContainerBuilder>`. Normally those implementations are supplied by the creator of your custom dependency injection solution (e.g. Autofac example above). This dummy implementations are not intended to use as is.

This is an example of how to implement and use a dummy custom service provider factory.

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactory.cs?start=8&end=49)]

Here is a test where the custom service provider factory is used:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=15&end=19)]

Here is a test where the custom service provider is used via delegate:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=25&end=29)]

## Further reading

A closely related topic is mocking. To learn more about mocking in bUnit, go to the <xref:test-doubles> page.
1 change: 1 addition & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
##########################################

dotnet_diagnostic.S1133.severity = none # S1133: Deprecated code should be removed
dotnet_diagnostic.S3011.severity = none # S3011: Reflection should not be used to increase accessibility of classes, methods, or fields
Loading

0 comments on commit 2891c81

Please sign in to comment.