From cf782211aaec35d266de49807865982272399e22 Mon Sep 17 00:00:00 2001 From: Scott Date: Sat, 5 Sep 2026 12:57:54 +1000 Subject: [PATCH] fix(git-tests): Create per-test specific databases --- src/ModuleCore/Database/DatabaseManager.cs | 12 ++++++++++ src/ModuleCore/Git/GitManager.cs | 10 ++++++++ tests/ModuleTests/Git/AddRegistrationTests.cs | 24 ++++++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/ModuleCore/Database/DatabaseManager.cs b/src/ModuleCore/Database/DatabaseManager.cs index 18c9a30..beac4f2 100644 --- a/src/ModuleCore/Database/DatabaseManager.cs +++ b/src/ModuleCore/Database/DatabaseManager.cs @@ -51,6 +51,18 @@ public class DatabaseManager return exists ?? false; } + /// + /// Deletes the current database file. This will cause any future instance methods to fail on database action if + /// a new instance is not created. + /// + /// This method should be avoided unless calling from a test. + /// + /// + internal void DeleteDatabase() + { + _databaseLocation.Delete(); + } + /// /// Removes double dots from the filename and removes the file extension /// diff --git a/src/ModuleCore/Git/GitManager.cs b/src/ModuleCore/Git/GitManager.cs index b393215..bd7a135 100644 --- a/src/ModuleCore/Git/GitManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -17,6 +17,8 @@ public class GitManager private GitManager(string? databaseName = null) { _registrations = new ConcurrentDictionary(); + // Regular usage of this constructor will never pass a database name in. Currently only tests should be hitting + // a code path that has a different database name _db = new DatabaseManager(databaseName ?? "git.db"); InitialiseRegistrations(); @@ -29,6 +31,14 @@ public class GitManager /// internal static GitManager InternalFreshInstance(string databaseName) => new(databaseName); + /// + /// Deletes the underlying database file. + /// + /// Avoid calling this outside of tests. + /// + /// + internal void DeleteDatabase() => _db.DeleteDatabase(); + /// /// Creates up any database tables and loads all previously saved git registrations. /// diff --git a/tests/ModuleTests/Git/AddRegistrationTests.cs b/tests/ModuleTests/Git/AddRegistrationTests.cs index 6c5d2a2..ced609b 100644 --- a/tests/ModuleTests/Git/AddRegistrationTests.cs +++ b/tests/ModuleTests/Git/AddRegistrationTests.cs @@ -24,13 +24,15 @@ public class AddRegistrationTests { Settings.UseFileName($"{nameof(BasicRepoRegistration)}_{testData.testId}"); - var gitManager = GitManager.InternalFreshInstance; + var gitManager = GitManager.InternalFreshInstance(nameof(BasicRepoRegistration)); var repoRegistration = gitManager.RegisterRepo("Test:/some/test/repo", testData.path); var sb = new StringBuilder(); sb.AppendLine($"Attempted to register: {testData.path}") .AppendLine($"Registration result: {repoRegistration}"); + gitManager.DeleteDatabase(); + return Verify(sb, Settings); } @@ -39,11 +41,13 @@ public class AddRegistrationTests { Settings.UseFileName(nameof(RepoRegistrationWithEmptyName)); - var gitManager = GitManager.InternalFreshInstance; + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithEmptyName)); var testRepoAbsolutePath = "Test:/some/test/repo"; var emptyName = gitManager.RegisterRepo(testRepoAbsolutePath, ""); + gitManager.DeleteDatabase(); + Assert.Equal("repo", emptyName); } @@ -52,13 +56,15 @@ public class AddRegistrationTests { Settings.UseFileName(nameof(RepoRegistrationWithNullName)); - var gitManager = GitManager.InternalFreshInstance; + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithNullName)); var testRepoAbsolutePath = "Test:/some/test/repo"; // Name is technically not-nullable, but string is a reference type so null can be passed in so we should test // it regardless var nullName = gitManager.RegisterRepo(testRepoAbsolutePath, null!); + gitManager.DeleteDatabase(); + Assert.Equal("repo", nullName); } @@ -67,11 +73,13 @@ public class AddRegistrationTests { Settings.UseFileName(nameof(RepoRegistrationWithWhitespaceName)); - var gitManager = GitManager.InternalFreshInstance; + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); var testRepoAbsolutePath = "Test:/some/test/repo"; var whitespaceName = gitManager.RegisterRepo(testRepoAbsolutePath, " "); + gitManager.DeleteDatabase(); + Assert.Equal("repo", whitespaceName); } @@ -80,7 +88,7 @@ public class AddRegistrationTests { Settings.UseFileName(nameof(RepoRegistrationWithWhitespaceName)); - var gitManager = GitManager.InternalFreshInstance; + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); var testRepoAbsolutePath = "Test:/some/test/repo"; string[] paths = ["test", "nested", "path"]; var names = (NormalSeparator: string.Join(Path.DirectorySeparatorChar, paths), AltSeparator: string.Join(Path.AltDirectorySeparatorChar, paths)); @@ -93,6 +101,8 @@ public class AddRegistrationTests Assert.Equal(Path.Combine(paths[..1]), secondRegistration); Assert.Throws(() => gitManager.RegisterRepo(testRepoAbsolutePath, Path.Combine(paths[..1]))); + + gitManager.DeleteDatabase(); } [Fact] @@ -100,7 +110,7 @@ public class AddRegistrationTests { Settings.UseFileName(nameof(RepoRegistrationWithWhitespaceName)); - var gitManager = GitManager.InternalFreshInstance; + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); var testRepoAbsolutePath = "Test:/some/test/repo"; string[] paths = ["test", "nested", "path"]; var names = (NormalSeparator: string.Join(Path.DirectorySeparatorChar, paths), AltSeparator: string.Join(Path.AltDirectorySeparatorChar, paths)); @@ -110,6 +120,8 @@ public class AddRegistrationTests var secondRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, Path.Combine(paths[..1])); var differentPathSeparatorRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, names.AltSeparator); + gitManager.DeleteDatabase(); + Assert.Equal(Path.Combine(paths), firstRegistration); Assert.Equal(Path.Combine(paths[..1]), secondRegistration); Assert.Equal(names.AltSeparator, differentPathSeparatorRegistration);