From 3d64abbe0e1b37074e0bce32fd2d9297f1bf8a24 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 4 Apr 2024 20:30:20 +0200 Subject: [PATCH] [ERR] TestNetCore: Message: Initialization method starskytest.starsky.foundation.native.OpenApplicationNative.Helpers.WindowsSetFileAssociationsTests.TestInitialize threw exception. System.IO.IOException: The specified registry key does not exist.. --- .../WindowsSetFileAssociationsTests.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/starsky/starskytest/starsky.foundation.native/OpenApplicationNative/Helpers/WindowsSetFileAssociationsTests.cs b/starsky/starskytest/starsky.foundation.native/OpenApplicationNative/Helpers/WindowsSetFileAssociationsTests.cs index 4d6f625180..23bfe1f4a4 100644 --- a/starsky/starskytest/starsky.foundation.native/OpenApplicationNative/Helpers/WindowsSetFileAssociationsTests.cs +++ b/starsky/starskytest/starsky.foundation.native/OpenApplicationNative/Helpers/WindowsSetFileAssociationsTests.cs @@ -1,3 +1,4 @@ +using System.IO; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.Win32; using starsky.foundation.native.OpenApplicationNative.Helpers; @@ -39,13 +40,21 @@ private static void CleanSetup() } // Ensure no keys exist before the test starts - Registry.CurrentUser.DeleteSubKeyTree($"Software\\Classes\\{Extension}", false); - Registry.CurrentUser.DeleteSubKeyTree($"Software\\Classes\\{ProgId}", false); + try + { + Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{Extension}", false); + Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{ProgId}", false); + } + catch ( IOException ) + { + // Ignore if the key does not exist + } } [TestMethod] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", - "CA1416:Validate platform compatibility", Justification = "Check if test for windows only")] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", + "CA1416:Validate platform compatibility", + Justification = "Check if test for windows only")] public void EnsureAssociationsSet() { if ( !new AppSettings().IsWindows ) @@ -53,7 +62,7 @@ public void EnsureAssociationsSet() Assert.Inconclusive("This test if for Windows Only"); return; } - + var filePath = new CreateFakeStarskyWindowsExe().FullFilePath; WindowsSetFileAssociations.EnsureAssociationsSet( new FileAssociation @@ -67,10 +76,10 @@ public void EnsureAssociationsSet() var registryKeyPath = $@"Software\Classes\{ProgId}\shell\open\command"; using var key = Registry.CurrentUser.OpenSubKey(registryKeyPath); - + var valueKey = key?.GetValue(string.Empty)?.ToString(); var pattern = "\"([^\"]*)\""; - Assert.IsNotNull( valueKey ); + Assert.IsNotNull(valueKey); var match = Regex.Match(valueKey, pattern); var value = match.Groups[1].Value;