From 377be903d62b3b7ff87bc05fb163fa4173634067 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 12 Jul 2024 22:42:54 +0200 Subject: [PATCH] fixed mock file system exception when using root path as a parameter --- .../MockFileSystem.cs | 6 ++++++ .../MockFileSystemTests.cs | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index cac9b0648..1909e97ec 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -243,6 +243,12 @@ public void AddFile(string path, MockFileData mockFile) } var directoryPath = Path.GetDirectoryName(fixedPath); + if (directoryPath == null) + { + AddDrive(fixedPath, new MockDriveData()); + SetEntry(fixedPath, mockFile); + return; + } if (!DirectoryExistsWithoutFixingPath(directoryPath)) { diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index 28fed6d61..4c5073f44 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -435,6 +435,26 @@ public void MockFileSystem_Constructor_ThrowsForNonRootedCurrentDirectory() ); Assert.That(ae.ParamName, Is.EqualTo("currentDirectory")); } + + [Test] + public void MockFileSystem_Constructor_ShouldSupportDifferentRootDrives() + { + var fileSystem = new MockFileSystem(new Dictionary + { + [@"c:\"] = new MockDirectoryData(), + [@"z:\"] = new MockDirectoryData(), + [@"d:\"] = new MockDirectoryData(), + }); + + var cExists = fileSystem.Directory.Exists(@"c:\"); + var zExists = fileSystem.Directory.Exists(@"z:\"); + var dExists = fileSystem.Directory.Exists(@"d:\"); + + Assert.That(fileSystem, Is.Not.Null); + Assert.That(cExists, Is.True); + Assert.That(zExists, Is.True); + Assert.That(dExists, Is.True); + } [Test] public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()