From 4ce151a461e733acde1707a2bdd5a4d84251d157 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Mon, 31 Jul 2023 13:47:17 +0200 Subject: [PATCH 1/3] feat: Add support for net8.0 --- .github/workflows/docs-deploy.yml | 1 + .github/workflows/release-preview.yml | 1 + .github/workflows/release.yml | 1 + .github/workflows/verification.yml | 1 + CHANGELOG.md | 5 ++++ Directory.Build.props | 3 ++- global.json | 2 +- src/bunit.core/Rendering/TestRenderer.cs | 24 ++++++++++++++++++- src/bunit.core/bunit.core.csproj | 8 ++++++- .../bunit.web.testcomponents.csproj | 4 ---- .../TestServiceProviderExtensions.cs | 5 ++++ .../BunitScrollToLocationHash.cs | 10 ++++++++ src/bunit.web/bunit.web.csproj | 15 +++++++++--- src/bunit/bunit.csproj | 2 +- .../AngleSharpWrappers.Tests.csproj | 2 +- .../Rendering/TestRendererTest.cs | 21 ++++++++++++++++ .../bunit.core.tests/bunit.core.tests.csproj | 2 +- .../bunit.testassets/bunit.testassets.csproj | 8 ++++++- tests/bunit.web.tests/bunit.web.tests.csproj | 2 +- 19 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 src/bunit.web/TestDoubles/ScrollToLocationHash/BunitScrollToLocationHash.cs diff --git a/.github/workflows/docs-deploy.yml b/.github/workflows/docs-deploy.yml index 5d610a2d5..d85e829b0 100644 --- a/.github/workflows/docs-deploy.yml +++ b/.github/workflows/docs-deploy.yml @@ -68,6 +68,7 @@ jobs: 5.0.x 6.0.x 7.0.x + 8.0.x - name: ๐ŸŽจ Setup color run: | diff --git a/.github/workflows/release-preview.yml b/.github/workflows/release-preview.yml index 53aa12abb..134b5b3d6 100644 --- a/.github/workflows/release-preview.yml +++ b/.github/workflows/release-preview.yml @@ -36,6 +36,7 @@ jobs: 5.0.x 6.0.x 7.0.x + 8.0.x - name: ๐ŸŽจ Setup color run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a21f37e05..ba276bfec 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -67,6 +67,7 @@ jobs: 5.0.x 6.0.x 7.0.x + 8.0.x - name: ๐Ÿ› ๏ธ Update changelog uses: thomaseizinger/keep-a-changelog-new-release@1.3.0 diff --git a/.github/workflows/verification.yml b/.github/workflows/verification.yml index 387adad62..5b6120f2a 100644 --- a/.github/workflows/verification.yml +++ b/.github/workflows/verification.yml @@ -48,6 +48,7 @@ jobs: 5.0.x 6.0.x 7.0.x + 8.0.x - name: ๐ŸŽจ Setup color if: matrix.os != 'windows-latest' diff --git a/CHANGELOG.md b/CHANGELOG.md index babc33a94..1c6aa6834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ All notable changes to **bUnit** will be documented in this file. The project ad ## [Unreleased] + +### Added + +- `net8.0` support + ## [1.22.19] - 2023-07-28 ### Added diff --git a/Directory.Build.props b/Directory.Build.props index 9e0490b0e..ba4338c81 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -6,6 +6,7 @@ 5.0.0 6.0.0 7.0.0 + 8.0.0-* @@ -38,7 +39,7 @@ AllEnabledByDefault true - latest + 7 true diff --git a/global.json b/global.json index 18562b58c..83105ca8e 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { "rollForward": "latestMajor", - "allowPrerelease": false + "allowPrerelease": true } } diff --git a/src/bunit.core/Rendering/TestRenderer.cs b/src/bunit.core/Rendering/TestRenderer.cs index 04b9e4987..11c1b106f 100644 --- a/src/bunit.core/Rendering/TestRenderer.cs +++ b/src/bunit.core/Rendering/TestRenderer.cs @@ -12,7 +12,9 @@ public class TestRenderer : Renderer, ITestRenderer { private static readonly Type RendererType = typeof(Renderer); private static readonly FieldInfo IsBatchInProgressField = RendererType.GetField("_isBatchInProgress", BindingFlags.Instance | BindingFlags.NonPublic)!; +#if !NET8_0_OR_GREATER private static readonly MethodInfo GetRequiredComponentStateMethod = RendererType.GetMethod("GetRequiredComponentState", BindingFlags.Instance | BindingFlags.NonPublic)!; +#endif private readonly object renderTreeUpdateLock = new(); private readonly Dictionary renderedComponents = new(); @@ -96,7 +98,11 @@ public IRenderedComponentBase RenderComponent(ComponentP EventArgs eventArgs) => DispatchEventAsync(eventHandlerId, fieldInfo, eventArgs, ignoreUnknownEventHandlers: false); /// +#if !NET8_0_OR_GREATER public Task DispatchEventAsync( +#else + public new Task DispatchEventAsync( +#endif ulong eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs, @@ -188,6 +194,17 @@ public void DisposeComponents() AssertNoUnhandledExceptions(); } } + +#if NET8_0_OR_GREATER + /// + protected override IComponent ResolveComponentForRenderMode(Type componentType, int? parentComponentId, + IComponentActivator componentActivator, IComponentRenderMode renderMode) + + { + ArgumentNullException.ThrowIfNull(componentActivator); + return componentActivator.CreateInstance(componentType); + } +#endif /// internal void SetDirectParameters(IRenderedFragmentBase renderedComponent, ParameterView parameters) @@ -207,9 +224,14 @@ internal void SetDirectParameters(IRenderedFragmentBase renderedComponent, Param try { IsBatchInProgress = true; - + +#if NET8_0_OR_GREATER + var setDirectParametersMethod = typeof(ComponentState).GetMethod("SetDirectParameters", BindingFlags.NonPublic | BindingFlags.Instance)!; + var componentState = GetComponentState(renderedComponent.ComponentId); +#else var componentState = GetRequiredComponentStateMethod.Invoke(this, new object[] { renderedComponent.ComponentId })!; var setDirectParametersMethod = componentState.GetType().GetMethod("SetDirectParameters", BindingFlags.Public | BindingFlags.Instance)!; +#endif setDirectParametersMethod.Invoke(componentState, new object[] { parameters }); } catch (TargetInvocationException ex) when (ex.InnerException is not null) diff --git a/src/bunit.core/bunit.core.csproj b/src/bunit.core/bunit.core.csproj index 49d699ac3..a65412011 100644 --- a/src/bunit.core/bunit.core.csproj +++ b/src/bunit.core/bunit.core.csproj @@ -1,7 +1,7 @@ - netstandard2.1;net5.0;net6.0;net7.0 + netstandard2.1;net5.0;net6.0;net7.0;net8.0 Bunit Bunit.Core @@ -38,4 +38,10 @@ + + + + + + \ No newline at end of file diff --git a/src/bunit.web.testcomponents/bunit.web.testcomponents.csproj b/src/bunit.web.testcomponents/bunit.web.testcomponents.csproj index d6ee50665..cc63f294e 100644 --- a/src/bunit.web.testcomponents/bunit.web.testcomponents.csproj +++ b/src/bunit.web.testcomponents/bunit.web.testcomponents.csproj @@ -33,10 +33,6 @@ - - - - diff --git a/src/bunit.web/Extensions/TestServiceProviderExtensions.cs b/src/bunit.web/Extensions/TestServiceProviderExtensions.cs index f9788565c..3eb44f69a 100644 --- a/src/bunit.web/Extensions/TestServiceProviderExtensions.cs +++ b/src/bunit.web/Extensions/TestServiceProviderExtensions.cs @@ -45,6 +45,11 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl services.AddSingleton(); services.AddSingleton(s => s.GetRequiredService()); +#if NET8_0_OR_GREATER + // bUnits fake ScrollToLocationHash + services.AddSingleton(); +#endif + // bUnit specific services services.AddSingleton(testContext); services.AddSingleton(); diff --git a/src/bunit.web/TestDoubles/ScrollToLocationHash/BunitScrollToLocationHash.cs b/src/bunit.web/TestDoubles/ScrollToLocationHash/BunitScrollToLocationHash.cs new file mode 100644 index 000000000..0db5ba9f0 --- /dev/null +++ b/src/bunit.web/TestDoubles/ScrollToLocationHash/BunitScrollToLocationHash.cs @@ -0,0 +1,10 @@ +#if NET8_0_OR_GREATER +using Microsoft.AspNetCore.Components.Routing; + +namespace Bunit.TestDoubles; + +internal sealed class BunitScrollToLocationHash : IScrollToLocationHash +{ + public Task RefreshScrollPositionForHash(string locationAbsolute) => Task.CompletedTask; +} +#endif \ No newline at end of file diff --git a/src/bunit.web/bunit.web.csproj b/src/bunit.web/bunit.web.csproj index a3651f4e9..baa52f589 100644 --- a/src/bunit.web/bunit.web.csproj +++ b/src/bunit.web/bunit.web.csproj @@ -1,7 +1,7 @@ - netstandard2.1;net5.0;net6.0;net7.0 + netstandard2.1;net5.0;net6.0;net7.0;net8.0 Bunit Bunit.Web @@ -55,6 +55,15 @@ + + + + + + + + + @@ -68,7 +77,7 @@ - <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))"/> + <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))" /> @@ -76,7 +85,7 @@ - + diff --git a/src/bunit/bunit.csproj b/src/bunit/bunit.csproj index 7026fa64d..d3241c457 100644 --- a/src/bunit/bunit.csproj +++ b/src/bunit/bunit.csproj @@ -1,6 +1,6 @@ - netstandard2.1;net5.0;net6.0;net7.0 + netstandard2.1;net5.0;net6.0;net7.0;net8.0 true false false diff --git a/tests/AngleSharpWrappers.Tests/AngleSharpWrappers.Tests.csproj b/tests/AngleSharpWrappers.Tests/AngleSharpWrappers.Tests.csproj index 3098b4549..dfa8b4417 100644 --- a/tests/AngleSharpWrappers.Tests/AngleSharpWrappers.Tests.csproj +++ b/tests/AngleSharpWrappers.Tests/AngleSharpWrappers.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1;net5.0;net6.0;net7.0 + netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 AngleSharpWrappers AngleSharpWrappers.Tests diff --git a/tests/bunit.core.tests/Rendering/TestRendererTest.cs b/tests/bunit.core.tests/Rendering/TestRendererTest.cs index 7856ca0a5..28adadae4 100644 --- a/tests/bunit.core.tests/Rendering/TestRendererTest.cs +++ b/tests/bunit.core.tests/Rendering/TestRendererTest.cs @@ -488,6 +488,16 @@ public void Test208() cut.Instance.AfterRenderCount.ShouldBe(2); cut.Instance.AfterRenderAsyncCount.ShouldBe(2); } + +#if NET8_0_OR_GREATER + [Fact(DisplayName = "Can render components that have a RenderMode attribute")] + public void Test209() + { + var cut = RenderComponent(); + + cut.Find("h3").TextContent.ShouldBe("Hello from Server"); + } +#endif internal sealed class LifeCycleMethodInvokeCounter : ComponentBase { @@ -622,4 +632,15 @@ protected override async Task OnAfterRenderAsync(bool firstRender) throw new InvalidOperationException(); } } + +#if NET8_0_OR_GREATER + [RenderModeServer] + private sealed class RenderModeServerComponent : ComponentBase + { + protected override void BuildRenderTree(RenderTreeBuilder builder) + { + builder.AddMarkupContent(0, "

Hello from Server

"); + } + } +#endif } diff --git a/tests/bunit.core.tests/bunit.core.tests.csproj b/tests/bunit.core.tests/bunit.core.tests.csproj index 0a68cc9bc..eff142c62 100644 --- a/tests/bunit.core.tests/bunit.core.tests.csproj +++ b/tests/bunit.core.tests/bunit.core.tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1;net5.0;net6.0;net7.0 + netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 Bunit Bunit.Core.Tests diff --git a/tests/bunit.testassets/bunit.testassets.csproj b/tests/bunit.testassets/bunit.testassets.csproj index 92ce4b7af..093946866 100644 --- a/tests/bunit.testassets/bunit.testassets.csproj +++ b/tests/bunit.testassets/bunit.testassets.csproj @@ -1,7 +1,7 @@ - netstandard2.1;net5.0;net6.0;net7.0 + netstandard2.1;net5.0;net6.0;net7.0;net8.0 Bunit.TestAssets Bunit.TestAssets Library @@ -42,5 +42,11 @@
+ + + + + +
\ No newline at end of file diff --git a/tests/bunit.web.tests/bunit.web.tests.csproj b/tests/bunit.web.tests/bunit.web.tests.csproj index b1c66bf2f..9fed95ee1 100644 --- a/tests/bunit.web.tests/bunit.web.tests.csproj +++ b/tests/bunit.web.tests/bunit.web.tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1;net5.0;net6.0;net7.0 + netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 Bunit Bunit.Web.Tests From d705a7dece878ec15f3480515a3f927d0d813e36 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Mon, 31 Jul 2023 14:05:03 +0200 Subject: [PATCH 2/3] Don't warn for SYSLIB0051 --- Directory.Build.props | 2 +- src/bunit.web/JSInterop/JSRuntimeInvocationDictionary.cs | 7 ++++--- src/bunit.web/Rendering/RenderedComponent.cs | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index ba4338c81..73e317ed9 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -22,7 +22,7 @@ 11.0 false enable - CA1014,NU5104,NETSDK1138 + CA1014,NU5104,NETSDK1138,SYSLIB0051 false diff --git a/src/bunit.web/JSInterop/JSRuntimeInvocationDictionary.cs b/src/bunit.web/JSInterop/JSRuntimeInvocationDictionary.cs index 5ef031652..57f26dc70 100644 --- a/src/bunit.web/JSInterop/JSRuntimeInvocationDictionary.cs +++ b/src/bunit.web/JSInterop/JSRuntimeInvocationDictionary.cs @@ -53,11 +53,12 @@ internal void RegisterInvocation(JSRuntimeInvocation invocation) { Count++; - if (!invocations.ContainsKey(invocation.Identifier)) + if (!invocations.TryGetValue(invocation.Identifier, out var value)) { - invocations.Add(invocation.Identifier, new List()); + value = new List(); + invocations.Add(invocation.Identifier, value); } - invocations[invocation.Identifier].Add(invocation); + value.Add(invocation); } } diff --git a/src/bunit.web/Rendering/RenderedComponent.cs b/src/bunit.web/Rendering/RenderedComponent.cs index cee374acb..009b7b13a 100644 --- a/src/bunit.web/Rendering/RenderedComponent.cs +++ b/src/bunit.web/Rendering/RenderedComponent.cs @@ -50,7 +50,7 @@ private void SetComponentAndID(RenderEvent renderEvent) } } - private bool TryFindComponent(RenderTreeFrameDictionary framesCollection, int parentComponentId, out int componentId, out TComponent component) + private static bool TryFindComponent(RenderTreeFrameDictionary framesCollection, int parentComponentId, out int componentId, out TComponent component) { componentId = -1; component = default!; From 22fc22cb72811745dbedd92f33d94df3b24226ef Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Mon, 31 Jul 2023 14:09:38 +0200 Subject: [PATCH 3/3] Added some fixes for .net8.0 --- src/bunit.web/Asserting/JSInvokeCountExpectedException.cs | 3 +++ src/bunit.web/Extensions/ElementNotFoundException.cs | 3 +++ .../Components/CapturedParameterView{TComponent}.cs | 2 +- .../Localization/MissingMockStringLocalizationException.cs | 5 ++++- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/bunit.web/Asserting/JSInvokeCountExpectedException.cs b/src/bunit.web/Asserting/JSInvokeCountExpectedException.cs index c8efe2e71..856cef937 100644 --- a/src/bunit.web/Asserting/JSInvokeCountExpectedException.cs +++ b/src/bunit.web/Asserting/JSInvokeCountExpectedException.cs @@ -43,6 +43,9 @@ private JSInvokeCountExpectedException(SerializationInfo serializationInfo, Stre } /// +#if NET8_0_OR_GREATER + [Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")] +#endif public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info is null) diff --git a/src/bunit.web/Extensions/ElementNotFoundException.cs b/src/bunit.web/Extensions/ElementNotFoundException.cs index 824428399..8d6d46af3 100644 --- a/src/bunit.web/Extensions/ElementNotFoundException.cs +++ b/src/bunit.web/Extensions/ElementNotFoundException.cs @@ -40,6 +40,9 @@ protected ElementNotFoundException(SerializationInfo serializationInfo, Streamin } /// +#if NET8_0_OR_GREATER + [Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")] +#endif public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info is null) diff --git a/src/bunit.web/TestDoubles/Components/CapturedParameterView{TComponent}.cs b/src/bunit.web/TestDoubles/Components/CapturedParameterView{TComponent}.cs index 11f9b72c0..d10be16cc 100644 --- a/src/bunit.web/TestDoubles/Components/CapturedParameterView{TComponent}.cs +++ b/src/bunit.web/TestDoubles/Components/CapturedParameterView{TComponent}.cs @@ -106,6 +106,6 @@ IEnumerator IEnumerable.GetEnumerator() /// An instance of . [SuppressMessage("Design", "CA1000:Do not declare static members on generic types", Justification = "Following factory pattern used in .net")] public static CapturedParameterView From(ParameterView parameters) - => new(parameters.ToDictionary()); + => new(parameters.ToDictionary()!); } #endif diff --git a/src/bunit.web/TestDoubles/Localization/MissingMockStringLocalizationException.cs b/src/bunit.web/TestDoubles/Localization/MissingMockStringLocalizationException.cs index 99e366d8c..4ce43d8b1 100644 --- a/src/bunit.web/TestDoubles/Localization/MissingMockStringLocalizationException.cs +++ b/src/bunit.web/TestDoubles/Localization/MissingMockStringLocalizationException.cs @@ -32,7 +32,10 @@ private MissingMockStringLocalizationException(SerializationInfo serializationIn Arguments = serializationInfo.GetValue(nameof(Arguments), Array.Empty().GetType()) as object?[] ?? Array.Empty(); } - /// + /// ยจ +#if NET8_0_OR_GREATER + [Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")] +#endif public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info is null)