Skip to content

Commit c6a2782

Browse files
authored
fix: return correct Parent of DirectoryInfo with trailing directory separator (#846)
This PR fixes a bug in the `DirectoryInfo.Parent` property when the directory path contains a trailing directory separator. The issue was that the parent directory calculation incorrectly included the trailing separator, leading to wrong parent resolution. ### Key Changes - Fixed the `GetParent()` method in `InMemoryLocation` to trim trailing directory separators before calculating the parent path - Added a test case to verify the correct parent is returned when a directory path has a trailing separator
1 parent e2cffb6 commit c6a2782

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Source/Testably.Abstractions.Testing/Storage/InMemoryLocation.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,12 @@ public override int GetHashCode()
9090
/// <inheritdoc cref="IStorageLocation.GetParent()" />
9191
public IStorageLocation? GetParent()
9292
{
93-
string? parentPath = _fileSystem.Execute.Path.GetDirectoryName(FullPath);
93+
string fullPathWithoutTrailingSeparator = FullPath
94+
.TrimEnd(_fileSystem.Path.DirectorySeparatorChar);
95+
string? parentPath =
96+
_fileSystem.Execute.Path.GetDirectoryName(fullPathWithoutTrailingSeparator);
9497
if (string.Equals(
95-
_fileSystem.Execute.Path.GetPathRoot(FullPath),
98+
_fileSystem.Execute.Path.GetPathRoot(fullPathWithoutTrailingSeparator),
9699
FullPath,
97100
_fileSystem.Execute.StringComparisonMode)
98101
|| parentPath == null)

Tests/Testably.Abstractions.Tests/FileSystem/DirectoryInfo/Tests.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,8 @@ public async Task Parent_ArbitraryPaths_ShouldNotBeNull(string path1,
279279
IDirectoryInfo sut = FileSystem.DirectoryInfo.New(path);
280280

281281
await That(sut.Parent).IsNotNull();
282-
await That(sut?.Exists).IsFalse();
283-
await That(sut?.Parent).IsNotNull();
284-
await That(sut?.Parent?.Exists).IsFalse();
282+
await That(sut.Exists).IsFalse();
283+
await That(sut.Parent!.Exists).IsFalse();
285284
}
286285

287286
[Fact]
@@ -343,6 +342,21 @@ public async Task Parent_ToString_ShouldBeDirectoryNameOnNetFramework(
343342
}
344343
}
345344

345+
[Theory]
346+
[AutoData]
347+
public async Task Parent_WithTrailingDirectorySeparator_ShouldReturnCorrectParent(string path1,
348+
string path2)
349+
{
350+
string path = FileSystem.Path.Combine(path1, path2);
351+
string expectedParent = FileSystem.Path.GetFullPath(path1);
352+
353+
IDirectoryInfo sut =
354+
FileSystem.DirectoryInfo.New(path + FileSystem.Path.DirectorySeparatorChar);
355+
356+
await That(sut.Parent).IsNotNull();
357+
await That(sut.Parent!.FullName).IsEqualTo(expectedParent);
358+
}
359+
346360
[Fact]
347361
public async Task Root_Name_ShouldBeCorrect()
348362
{

0 commit comments

Comments
 (0)