using Microsoft.Win32; namespace Chronos.App.Tests.Setup; /// /// Scratch keys under HKCU\Software\Chronos.Tests for the setup tests, which run in parallel: a key is created, and the /// shared parent removed once empty, under one lock, so one test's cleanup never deletes the parent while another test /// is creating its key under it. /// internal static class ScratchRegistry { private const string Root = @"Software\Chronos.Tests"; private static readonly Lock Gate = new(); /// Creates a new empty scratch key and returns its path under HKCU. public static string Create() { var keyPath = Root + @"\" + Guid.NewGuid().ToString("N"); lock (Gate) Registry.CurrentUser.CreateSubKey(keyPath, writable: true).Dispose(); return keyPath; } /// Deletes a scratch key made by , and the shared parent too once it is empty. public static void Delete(string keyPath) { lock (Gate) { Registry.CurrentUser.DeleteSubKeyTree(keyPath, throwOnMissingSubKey: false); bool empty; using (var tests = Registry.CurrentUser.OpenSubKey(Root)) empty = tests is { SubKeyCount: 0, ValueCount: 0 }; if (empty) Registry.CurrentUser.DeleteSubKey(Root, throwOnMissingSubKey: false); } } }