Skip to content

Commit

Permalink
Merge pull request bUnit-dev#1583 from bUnit-dev/release/v1.33
Browse files Browse the repository at this point in the history
Release of new minor version v1.33
  • Loading branch information
linkdotnet authored Oct 11, 2024
2 parents e095b07 + 932c8ac commit 76793a1
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"rollForward": false
},
"dotnet-dump": {
"version": "8.0.532401",
"version": "8.0.547301",
"commands": [
"dotnet-dump"
],
"rollForward": false
}
}
}
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to **bUnit** will be documented in this file. The project ad

## [Unreleased]

### Added

- `bunit.generators` respect parameters from the base class.
- Supports components using constructor injection in `net9.0`.

### Fixed

- Use latest `System.Text.Json` due to CVE in `8.0.4`.

## [1.32.7] - 2024-10-04

### Fixed
Expand Down
28 changes: 8 additions & 20 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ItemGroup Condition="!$(MSBuildProjectName.EndsWith('samples'))">
<GlobalPackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" IncludeAssets="Runtime;Build;Native;contentFiles;Analyzers"/>
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167" PrivateAssets="All" IncludeAssets="Runtime;Build;Native;contentFiles;Analyzers"/>
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Meziantou.Polyfill" Version="1.0.39" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />
<PackageVersion Include="Meziantou.Polyfill" Version="1.0.40" />
</ItemGroup>

<ItemGroup Label="Shared">
Expand All @@ -20,14 +20,19 @@
<PackageVersion Include="AngleSharp.Diffing" Version="0.18.2"/>

<PackageVersion Include="AngleSharp" Version="1.1.2"/>
<PackageVersion Include="AngleSharp.Css" Version="1.0.0-beta.139"/>
<PackageVersion Include="AngleSharp.Css" Version="1.0.0-beta.144"/>
</ItemGroup>

<ItemGroup Label="Package Versioning">
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.6.143" />
</ItemGroup>

<ItemGroup Label="System.Text.Json Vulnerability">
<!-- Due to a CVE in System.Text.Json we explicitly reference the latest version of System.Text.Json -->
<PackageVersion Include="System.Text.Json" Version="8.0.5"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageVersion Include="Microsoft.Extensions.Logging" Version="3.1.32"/>
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.32"/>
Expand All @@ -40,9 +45,6 @@
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="3.1.32"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="3.2.1"/>

<!-- Due to a CVE in System.Text.Json we explicitly reference the latest version of System.Text.Json -->
<PackageVersion Include="System.Text.Json" Version="6.0.9"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand All @@ -57,9 +59,6 @@
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.17"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="5.0.17"/>

<!-- Due to a CVE in System.Text.Json we explicitly reference the latest version of System.Text.Json -->
<PackageVersion Include="System.Text.Json" Version="6.0.9"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand All @@ -73,9 +72,6 @@
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="6.0.33"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.33"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.33"/>

<!-- Due to a CVE in System.Text.Json we explicitly reference the latest version of System.Text.Json -->
<PackageVersion Include="System.Text.Json" Version="6.0.9"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
Expand All @@ -89,9 +85,6 @@
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="7.0.20"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.20"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.20"/>

<!-- Due to a CVE in System.Text.Json we explicitly reference the latest version of System.Text.Json -->
<PackageVersion Include="System.Text.Json" Version="8.0.4"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
Expand All @@ -105,9 +98,6 @@
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="8.0.8"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.8"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.8"/>

<!-- Due to a CVE in System.Text.Json we explicitly reference the latest version of System.Text.Json -->
<PackageVersion Include="System.Text.Json" Version="8.0.4"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
Expand All @@ -121,8 +111,6 @@
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="9.0.0-rc.1.24452.1"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0-rc.1.24452.1"/>
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.0-rc.1.24452.1"/>

<PackageVersion Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup Label="Test Dependencies">
Expand Down
18 changes: 13 additions & 5 deletions src/bunit.core/Rendering/BunitComponentActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ internal class BunitComponentActivator : IComponentActivator
private readonly ComponentFactoryCollection factories;
private readonly IComponentActivator componentActivator;

public BunitComponentActivator(ComponentFactoryCollection factories, IComponentActivator? externalComponentActivator)
public BunitComponentActivator(
IServiceProvider serviceProvider,
ComponentFactoryCollection factories,
IComponentActivator? externalComponentActivator)
{
this.factories = factories ?? throw new ArgumentNullException(nameof(factories));
this.componentActivator = externalComponentActivator ?? DefaultComponentActivator.Instance;
this.componentActivator = externalComponentActivator ?? new DefaultComponentActivator(serviceProvider);
}

public IComponent CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type componentType)
Expand Down Expand Up @@ -43,12 +46,17 @@ public IComponent CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessed

private sealed class DefaultComponentActivator : IComponentActivator
{
public static IComponentActivator Instance { get; } = new DefaultComponentActivator();
private readonly IServiceProvider serviceProvider;

public DefaultComponentActivator(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}

/// <inheritdoc />
public IComponent CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type componentType)
{
return (IComponent)Activator.CreateInstance(componentType)!;
{
return (IComponent)ActivatorUtilities.CreateInstance(serviceProvider, componentType);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bunit.core/Rendering/TestRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public TestRenderer(IRenderedComponentActivator renderedComponentActivator, Test
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), null))
: base(services, loggerFactory, new BunitComponentActivator(services, services.GetRequiredService<ComponentFactoryCollection>(), null))
{
logger = loggerFactory.CreateLogger<TestRenderer>();
this.activator = renderedComponentActivator;
Expand All @@ -89,7 +89,7 @@ public TestRenderer(IRenderedComponentActivator renderedComponentActivator, Test
/// Initializes a new instance of the <see cref="TestRenderer"/> class.
/// </summary>
public TestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
: base(services, loggerFactory, new BunitComponentActivator(services, services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
{
logger = loggerFactory.CreateLogger<TestRenderer>();
this.activator = renderedComponentActivator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

private static AddStubClassInfo? GetStubClassInfo(GeneratorSyntaxContext context)
{
var invocation = context.Node as InvocationExpressionSyntax;
if (invocation is null || !IsComponentFactoryStubMethod(invocation, context.SemanticModel))
if (context.Node is not InvocationExpressionSyntax invocation || !IsComponentFactoryStubMethod(invocation, context.SemanticModel))
{
return null;
}
Expand All @@ -56,7 +55,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var line = lineSpan.StartLinePosition.Line + 1;
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddStub", StringComparison.Ordinal) + 1;

var properties = symbol.GetMembers()
var properties = symbol.GetAllMembersRecursively()
.OfType<IPropertySymbol>()
.Where(IsParameterOrCascadingParameter)
.Select(CreateFromProperty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static bool IsClassWithComponentStubAttribute(SyntaxNode s) =>
}

var parameter = attribute.AttributeClass!.TypeArguments
.SelectMany(s => s.GetMembers())
.SelectMany(s => s.GetAllMembersRecursively())
.OfType<IPropertySymbol>()
.Where(IsParameterOrCascadingParameter)
.Select(CreateFromProperty)
Expand Down
19 changes: 19 additions & 0 deletions src/bunit.generators/Web.Stubs/MemberRetriever.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.CodeAnalysis;

namespace Bunit.Web.Stubs;

internal static class MemberRetriever
{
public static IEnumerable<ISymbol> GetAllMembersRecursively(this ITypeSymbol type)
{
var currentType = type;
while (currentType is not null)
{
foreach (var member in currentType.GetMembers())
{
yield return member;
}
currentType = currentType.BaseType;
}
}
}
37 changes: 36 additions & 1 deletion tests/bunit.generators.tests/Web.Stub/AddStubGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,42 @@ public void Generated_stub_via_attribute_has_same_parameters()
var stub = cut.FindComponent<ButtonComponentStub>();
Assert.Equal("test", stub.Instance.Text);
}

[Fact]
public void Generated_Stub_respects_base_class_parameters()
{
ComponentFactories.AddStub<DerivedComponent>();

var cut = RenderComponent<ContainerComponent>(c => c.AddChildContent<DerivedComponent>());

var stub = cut.FindComponent<DerivedComponentStub>();
Assert.Equal(0, stub.Instance.BaseCount);
}

[Fact]
public void Generated_stub_via_attribute_respects_base_class_parameters()
{
ComponentFactories.Add<DerivedComponent, DerivedComponentStubViaAttributeAnnotation>();

var cut = RenderComponent<ContainerComponent>(c => c.AddChildContent<DerivedComponent>());

var stub = cut.FindComponent<DerivedComponentStubViaAttributeAnnotation>();
Assert.Equal(0, stub.Instance.BaseCount);
}
}

[ComponentStub<ButtonComponent>]
public partial class ButtonComponentStub;
public partial class ButtonComponentStub;

public abstract class BaseComponent : ComponentBase
{
[Parameter] public int BaseCount { get; set; }
}

public class DerivedComponent : BaseComponent
{
[Parameter] public int DerivedCount { get; set; }
}

[ComponentStub<DerivedComponent>]
public partial class DerivedComponentStubViaAttributeAnnotation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Bunit.Web.Stub.Components;

public class ContainerComponent : ComponentBase
{
[Parameter]
public RenderFragment ChildContent { get; set; }

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "div");
builder.AddContent(1, ChildContent);
builder.CloseElement();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.JSInterop;

namespace Bunit.TestAssets.SampleComponents;

public class ConstructorInjectionComponent : ComponentBase
{
public IJSRuntime JSRuntime { get; }

public ConstructorInjectionComponent(IJSRuntime jsRuntime)
{
JSRuntime = jsRuntime;
}
}
10 changes: 10 additions & 0 deletions tests/bunit.web.tests/Rendering/RenderedComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ public void Test020()

Should.Throw<ComponentDisposedException>(() => target.Instance);
}
#if NET9_0_OR_GREATER

[Fact(DisplayName = "Component with constructor dependencies is resolved when rendered")]
public void Test021()
{
var cut = RenderComponent<ConstructorInjectionComponent>();

cut.Instance.JSRuntime.ShouldNotBeNull();
}
#endif
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.32",
"version": "1.33",
"assemblyVersion": {
"precision": "revision"
},
Expand Down

0 comments on commit 76793a1

Please sign in to comment.