diff --git a/Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs b/Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs index 8123aa94..8095bf91 100644 --- a/Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs +++ b/Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs @@ -90,9 +90,12 @@ public override int GetHashCode() /// public IStorageLocation? GetParent() { - string? parentPath = _fileSystem.Execute.Path.GetDirectoryName(FullPath); + string fullPathWithoutTrailingSeparator = FullPath + .TrimEnd(_fileSystem.Path.DirectorySeparatorChar); + string? parentPath = + _fileSystem.Execute.Path.GetDirectoryName(fullPathWithoutTrailingSeparator); if (string.Equals( - _fileSystem.Execute.Path.GetPathRoot(FullPath), + _fileSystem.Execute.Path.GetPathRoot(fullPathWithoutTrailingSeparator), FullPath, _fileSystem.Execute.StringComparisonMode) || parentPath == null) diff --git a/Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfo/Tests.cs b/Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfo/Tests.cs index ba73a9b2..3b67316b 100644 --- a/Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfo/Tests.cs +++ b/Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfo/Tests.cs @@ -279,9 +279,8 @@ public async Task Parent_ArbitraryPaths_ShouldNotBeNull(string path1, IDirectoryInfo sut = FileSystem.DirectoryInfo.New(path); await That(sut.Parent).IsNotNull(); - await That(sut?.Exists).IsFalse(); - await That(sut?.Parent).IsNotNull(); - await That(sut?.Parent?.Exists).IsFalse(); + await That(sut.Exists).IsFalse(); + await That(sut.Parent!.Exists).IsFalse(); } [Fact] @@ -343,6 +342,21 @@ public async Task Parent_ToString_ShouldBeDirectoryNameOnNetFramework( } } + [Theory] + [AutoData] + public async Task Parent_WithTrailingDirectorySeparator_ShouldReturnCorrectParent(string path1, + string path2) + { + string path = FileSystem.Path.Combine(path1, path2); + string expectedParent = FileSystem.Path.GetFullPath(path1); + + IDirectoryInfo sut = + FileSystem.DirectoryInfo.New(path + FileSystem.Path.DirectorySeparatorChar); + + await That(sut.Parent).IsNotNull(); + await That(sut.Parent!.FullName).IsEqualTo(expectedParent); + } + [Fact] public async Task Root_Name_ShouldBeCorrect() {