From 70f0ed64df48003fef8a38ec3d9b3da4c4034ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 29 Jun 2026 16:46:30 +0200 Subject: [PATCH] refactor: simplify duplicate catch blocks and attribute-loop consistency - PackagedAppTestHostHandle: combine two identical IOException / UnauthorizedAccessException catch blocks into a single handler using an exception filter (when ex is IOException or UnauthorizedAccessException). Both handlers had identical bodies; the combined form eliminates the duplication and uses idiomatic C# exception filters. - SourceGeneratedReflectionOperations.GetSingleAttributeOrDefault: replace the 'is not T / continue' guard with a direct 'is T' positive match, making the loop consistent with the adjacent IsAttributeDefined and GetFirstAttributeOrDefault methods which use the same positive-match pattern. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../SourceGeneratedReflectionOperations.cs | 14 ++++++-------- .../PackagedAppTestHostHandle.cs | 6 +----- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs b/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs index cf6ed80411..b86bb7516c 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs @@ -319,17 +319,15 @@ public bool IsAttributeDefined(ICustomAttributeProvider attributePro TAttribute? first = null; foreach (Attribute attr in GetCustomAttributesCached(attributeProvider)) { - if (attr is not TAttribute match) + if (attr is TAttribute match) { - continue; - } + if (first is not null) + { + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Found multiple attributes of type '{0}' when only one was expected.", typeof(TAttribute))); + } - if (first is not null) - { - throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Found multiple attributes of type '{0}' when only one was expected.", typeof(TAttribute))); + first = match; } - - first = match; } return first; diff --git a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostHandle.cs b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostHandle.cs index 01debf9433..beedb00ea5 100644 --- a/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostHandle.cs +++ b/src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostHandle.cs @@ -56,11 +56,7 @@ public void Dispose() Directory.Delete(_deploymentDirectory, recursive: true); } } - catch (IOException ex) - { - Debug.WriteLine($"Best-effort cleanup of deployment directory '{_deploymentDirectory}' failed: {ex}"); - } - catch (UnauthorizedAccessException ex) + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { Debug.WriteLine($"Best-effort cleanup of deployment directory '{_deploymentDirectory}' failed: {ex}"); }