From a69f22cda33784027583390edb2f2103cfab250a Mon Sep 17 00:00:00 2001 From: Mike Ciechan Date: Thu, 15 Dec 2022 23:47:50 +0000 Subject: [PATCH] Fixing UNC path GetFullPath when Current Directory is a UNC path --- .../MockPath.cs | 6 +++--- .../MockDirectoryInfoTests.cs | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs index e30fe9147..c2d110cb7 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs @@ -85,7 +85,7 @@ public override string GetFullPath(string path) // unc paths need at least two segments, the others need one segment var isUnixRooted = mockFileDataAccessor.StringOperations.StartsWith( mockFileDataAccessor.Directory.GetCurrentDirectory(), - string.Format(CultureInfo.InvariantCulture, "{0}", DirectorySeparatorChar)); + "/"); var minPathSegments = isUnc ? 2 @@ -121,7 +121,7 @@ public override string GetFullPath(string path) if (isUnixRooted && !isUnc) { - fullPath = DirectorySeparatorChar + fullPath; + fullPath = "/" + fullPath; } else if (isUnixRooted) { @@ -156,4 +156,4 @@ public override string GetTempFileName() /// public override string GetTempPath() => defaultTempDirectory; } -} \ No newline at end of file +} diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs index 1f3ab454e..f14658bd7 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs @@ -96,7 +96,28 @@ public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath() Assert.AreEqual(fileName, files[0].FullName); } + [Test] + [WindowsOnly(WindowsSpecifics.UNCPaths)] + public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath_WhenCurrentDirectoryIsUnc() + { + var fileName = XFS.Path(@"\\unc\folder\file.txt"); + var directoryName = XFS.Path(@"\\unc\folder"); + // Arrange + var fileSystem = new MockFileSystem(new Dictionary + { + {fileName, ""} + }); + + fileSystem.Directory.SetCurrentDirectory(directoryName); + + var directoryInfo = new MockDirectoryInfo(fileSystem, directoryName); + + // Act + var files = directoryInfo.GetFiles(); + // Assert + Assert.AreEqual(fileName, files[0].FullName); + } [Test] public void MockDirectoryInfo_FullName_ShouldReturnFullNameWithoutIncludingTrailingPathDelimiter()