diff --git a/src/ModuleCore/Git/GitManager.cs b/src/ModuleCore/Git/GitManager.cs index 0d5af37..25a5b41 100644 --- a/src/ModuleCore/Git/GitManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -138,8 +138,15 @@ public class GitManager // we add it to the relevant dictionary if (directorySegmentsFromName.Count == 0) { - FullRepositoryPath = absoluteRepositoryLocation; - return this; + // If this is a new registration, set the FullRepositoryPath and return, otherwise we've got a duplicate + // entry and we throw + if (FullRepositoryPath == null) + { + FullRepositoryPath = absoluteRepositoryLocation; + return this; + } + + throw new Exception($"Registration already exists for {InternalPath}"); } var nextSegment = directorySegmentsFromName.Peek(); diff --git a/tests/ModuleTests/Git/AddRegistrationTests.cs b/tests/ModuleTests/Git/AddRegistrationTests.cs index 6de31d6..e04cfc3 100644 --- a/tests/ModuleTests/Git/AddRegistrationTests.cs +++ b/tests/ModuleTests/Git/AddRegistrationTests.cs @@ -74,4 +74,23 @@ public class AddRegistrationTests Assert.Equal("repo", whitespaceName); } + + [Fact] + public void DuplicateRepoRegistrationShouldFail() + { + Settings.UseFileName(nameof(RepoRegistrationWithWhitespaceName)); + + var gitManager = GitManager.InternalFreshInstance; + 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)); + + var firstRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, names.NormalSeparator); + // TODO: make nested registrations fail in both directions and test + var secondRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, Path.Combine(paths[..1])); + + Assert.Equal(Path.Combine(paths), firstRegistration); + + Assert.Throws(() => gitManager.RegisterRepo(testRepoAbsolutePath, names.AltSeparator)); + } } \ No newline at end of file