From 3d517f419d2832298121dd05188012a395840dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Mon, 22 Jan 2024 10:50:43 +0100 Subject: [PATCH] fix: use correct path for wrapped `FileInfo` and `DirectoryInfo` (#1079) Use the full path when wrapping a `FileInfo` or `DirectoryInfo` to preserve the relevant information. *Fixes #1049* --- .../MockDirectoryInfoFactory.cs | 2 +- .../MockFileInfoFactory.cs | 2 +- .../MockDirectoryInfoFactoryTests.cs | 11 +++++++++++ .../MockFileInfoFactoryTests.cs | 11 +++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfoFactory.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfoFactory.cs index 14c3e655c..7067a231d 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfoFactory.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectoryInfoFactory.cs @@ -40,7 +40,7 @@ public IDirectoryInfo Wrap(DirectoryInfo directoryInfo) return null; } - return new MockDirectoryInfo(mockFileSystem, directoryInfo.Name); + return new MockDirectoryInfo(mockFileSystem, directoryInfo.FullName); } } } \ No newline at end of file diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfoFactory.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfoFactory.cs index d3bae8c9a..7c75e9014 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfoFactory.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileInfoFactory.cs @@ -39,7 +39,7 @@ public IFileInfo Wrap(FileInfo fileInfo) return null; } - return new MockFileInfo(mockFileSystem, fileInfo.Name); + return new MockFileInfo(mockFileSystem, fileInfo.FullName); } } } \ No newline at end of file diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoFactoryTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoFactoryTests.cs index a5430e5e9..2c4a4f590 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoFactoryTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoFactoryTests.cs @@ -14,5 +14,16 @@ public void MockDirectoryInfoFactory_Wrap_WithNull_ShouldReturnNull() Assert.That(result, Is.Null); } + + [Test] + public void MockDirectoryInfoFactory_Wrap_ShouldKeepNameAndFullName() + { + var fs = new MockFileSystem(); + var directoryInfo = new DirectoryInfo(@"C:\subfolder\file"); + var wrappedDirectoryInfo = fs.DirectoryInfo.Wrap(directoryInfo); + + Assert.That(wrappedDirectoryInfo.FullName, Is.EqualTo(directoryInfo.FullName)); + Assert.That(wrappedDirectoryInfo.Name, Is.EqualTo(directoryInfo.Name)); + } } } diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoFactoryTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoFactoryTests.cs index 291083f48..fae4700e6 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoFactoryTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoFactoryTests.cs @@ -51,5 +51,16 @@ public void MockFileInfoFactory_Wrap_WithNull_ShouldReturnNull() Assert.That(result, Is.Null); } + + [Test] + public void MockFileInfoFactory_Wrap_ShouldKeepNameAndFullName() + { + var fs = new MockFileSystem(); + var fileInfo = new FileInfo(@"C:\subfolder\file"); + var wrappedFileInfo = fs.FileInfo.Wrap(fileInfo); + + Assert.That(wrappedFileInfo.FullName, Is.EqualTo(fileInfo.FullName)); + Assert.That(wrappedFileInfo.Name, Is.EqualTo(fileInfo.Name)); + } } } \ No newline at end of file