From 70ae488e5c46ba8073a2259739a2157b0bb29df0 Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 26 May 2025 10:58:28 +0200 Subject: [PATCH 1/3] fix: ignore assemblies where `GetTypes()` throws an exception E.g. `PresentationCore` --- .../Initialization/AweXpectInitialization.cs | 29 ++++++++++++++----- .../AweXpectInitializationTests.cs | 7 ++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs b/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs index 93cf7053c..6e95f366e 100644 --- a/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs +++ b/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs @@ -24,7 +24,7 @@ internal static void EnsureInitialized() _ = State.Value; } - internal static ITestFrameworkAdapter DetectFramework(IEnumerable types) + internal static ITestFrameworkAdapter? DetectFramework(IEnumerable types) { Type frameworkInterface = typeof(ITestFrameworkAdapter); foreach (Type frameworkType in types @@ -48,18 +48,33 @@ internal static ITestFrameworkAdapter DetectFramework(IEnumerable types) } } - return new FallbackTestFramework(); + return null; } private static InitializationState Initialize() { ExecuteCustomInitializers(); - ITestFrameworkAdapter testFramework = DetectFramework(AppDomain.CurrentDomain.GetAssemblies() - .Where(assembly => Customize.aweXpect.Reflection().ExcludedAssemblyPrefixes.Get() - .All(excludedAssemblyPrefix => !assembly.FullName!.StartsWith(excludedAssemblyPrefix))) - .SelectMany(assembly => assembly.GetTypes().Where(x => !x.IsNestedPrivate))); - return new InitializationState(testFramework); + foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies() + .Where(assembly => Customize.aweXpect.Reflection().ExcludedAssemblyPrefixes.Get() + .All(excludedAssemblyPrefix => !assembly.FullName!.StartsWith(excludedAssemblyPrefix)))) + { + try + { + ITestFrameworkAdapter? testFrameworkAdapter = DetectFramework( + assembly.GetTypes().Where(x => !x.IsNestedPrivate)); + if (testFrameworkAdapter is not null) + { + return new InitializationState(testFrameworkAdapter); + } + } + catch (ReflectionTypeLoadException) + { + // Ignore any ReflectionTypeLoadException and continue with the next assembly. + } + } + + return new InitializationState(new FallbackTestFramework()); } private static void ExecuteCustomInitializers() diff --git a/Tests/aweXpect.Core.Tests/Core/Initialization/AweXpectInitializationTests.cs b/Tests/aweXpect.Core.Tests/Core/Initialization/AweXpectInitializationTests.cs index 2a11f9bf5..e79f7b556 100644 --- a/Tests/aweXpect.Core.Tests/Core/Initialization/AweXpectInitializationTests.cs +++ b/Tests/aweXpect.Core.Tests/Core/Initialization/AweXpectInitializationTests.cs @@ -8,12 +8,11 @@ namespace aweXpect.Core.Tests.Core.Initialization; public sealed class AweXpectInitializationTests { [Fact] - public async Task DetectFramework_WhenAllFrameworksAreNotAvailable_ShouldUseFallbackAdapter() + public async Task DetectFramework_WhenAllFrameworksAreNotAvailable_ShouldReturnNull() { - ITestFrameworkAdapter result = AweXpectInitialization.DetectFramework([typeof(UnavailableFrameworkAdapter),]); + ITestFrameworkAdapter? result = AweXpectInitialization.DetectFramework([typeof(UnavailableFrameworkAdapter),]); - await That(result.IsAvailable).IsFalse(); - await That(result.GetType().Name).IsEqualTo("FallbackTestFramework"); + await That(result).IsNull(); } [Fact] From a8c334fa991d89133617e6b0e68dd22bc20b9bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Mon, 26 May 2025 11:22:04 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../Core/Initialization/AweXpectInitialization.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs b/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs index 6e95f366e..6ba07e828 100644 --- a/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs +++ b/Source/aweXpect.Core/Core/Initialization/AweXpectInitialization.cs @@ -24,6 +24,13 @@ internal static void EnsureInitialized() _ = State.Value; } + /// + /// Detects a test framework adapter from the provided types. + /// + /// + /// An instance of if a matching framework is found; + /// otherwise . + /// internal static ITestFrameworkAdapter? DetectFramework(IEnumerable types) { Type frameworkInterface = typeof(ITestFrameworkAdapter); @@ -68,9 +75,11 @@ private static InitializationState Initialize() return new InitializationState(testFrameworkAdapter); } } - catch (ReflectionTypeLoadException) + catch (ReflectionTypeLoadException ex) { // Ignore any ReflectionTypeLoadException and continue with the next assembly. + Debug.WriteLine($"ReflectionTypeLoadException caught: {ex.Message}"); + Debug.WriteLine(ex.StackTrace); } } From a8d12744258a75f17ff58c63ef3b4d160b3a7ed4 Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 26 May 2025 11:29:41 +0200 Subject: [PATCH 3/3] Make `TraceWriterTests` more resilient --- Tests/aweXpect.Core.Tests/TraceWriterTests.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Tests/aweXpect.Core.Tests/TraceWriterTests.cs b/Tests/aweXpect.Core.Tests/TraceWriterTests.cs index 272388e76..881fbde39 100644 --- a/Tests/aweXpect.Core.Tests/TraceWriterTests.cs +++ b/Tests/aweXpect.Core.Tests/TraceWriterTests.cs @@ -107,10 +107,9 @@ public async Task ForSuccessfulDelegateWithReturnValue_ShouldTraceSuccessfulVeri await That(callback).ExecutesWithin(500.Milliseconds()); } - await That(traceWriter.Messages).IsEqualTo([ - "Checking expectation for callback delegate returning int 4 in 0:00", - " Successfully verified that callback executes within 0:00.500", - ]); + await That(traceWriter.Messages).HasCount(2); + await That(traceWriter.Messages[0]).IsEqualTo("Checking expectation for callback delegate returning int 4 in 0:00").AsPrefix(); + await That(traceWriter.Messages[1]).IsEqualTo(" Successfully verified that callback executes within 0:00.500"); } [Fact]