Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions TUnit.Engine/Extensions/TestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,10 @@ private static T[] SortAndFilter<T>(List<T>? receivers) where T : class, IEventR
}

// Sort by Order
receivers.Sort((a, b) => a.Order.CompareTo(b.Order));
receivers.Sort(static (a, b) => a.Order.CompareTo(b.Order));

// Apply scoped attribute filtering and return as array
var filtered = ScopedAttributeFilter.FilterScopedAttributes(receivers);
return filtered.ToArray();
// Apply scoped attribute filtering - FilterScopedAttributes already returns T[]
return ScopedAttributeFilter.FilterScopedAttributes(receivers);
}

public static IEnumerable<object> GetEligibleEventObjects(this TestContext testContext)
Expand Down
10 changes: 7 additions & 3 deletions TUnit.Engine/Services/EventReceiverOrchestrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,17 @@ private static async Task InvokeTestDiscoveryEventReceiversCoreAsync(ITestDiscov

public async ValueTask InvokeHookRegistrationEventReceiversAsync(HookRegisteredContext hookContext, CancellationToken cancellationToken)
{
// Filter scoped attributes to ensure only the highest priority one of each type is invoked
// Pre-sort by Order before filtering so that FilterScopedAttributes (which uses TryAdd
// and keeps the first encountered item per ScopeType) retains the lowest-Order attribute.
// After filtering, sort the result in-place for final invocation order.
var filteredReceivers = ScopedAttributeFilter.FilterScopedAttributes(
hookContext.HookMethod.Attributes
.OfType<IHookRegisteredEventReceiver>()
.OrderBy(static r => r.Order));
.OrderBy(static x => x.Order));

foreach (var receiver in filteredReceivers.OrderBy(static r => r.Order))
Array.Sort(filteredReceivers, static (a, b) => a.Order.CompareTo(b.Order));

foreach (var receiver in filteredReceivers)
{
await receiver.OnHookRegistered(hookContext);
}
Expand Down
Loading