Skip to content

Commit f2bcaa9

Browse files
vladimir-shirmanovYour Name
and
Your Name
authored
fix: fixed mock file system exception when using root path as a parameter (#1132)
* fixed mock file system exception when using root path as a parameter * test: added a test to cover new drive creation with nested directories * fix: fixed code structure PR comment * test: added one more test to make sure there is no duplicate drives created * docs: added information about conventional commits repo convention * docs: revert back CONTRIBUTING.md changes * fix: fixed duplicate call to SetEntry method * docs: revert back CONTRIBUTING.md * test: mark Drives tests as windows only --------- Co-authored-by: Your Name <[email protected]>
1 parent c708b42 commit f2bcaa9

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,11 @@ public void AddFile(string path, MockFileData mockFile)
243243
}
244244

245245
var directoryPath = Path.GetDirectoryName(fixedPath);
246-
247-
if (!DirectoryExistsWithoutFixingPath(directoryPath))
246+
if (directoryPath == null)
247+
{
248+
AddDrive(fixedPath, new MockDriveData());
249+
}
250+
else if (!DirectoryExistsWithoutFixingPath(directoryPath))
248251
{
249252
AddDirectory(directoryPath);
250253
}

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs

+54
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,60 @@ public void MockFileSystem_Constructor_ThrowsForNonRootedCurrentDirectory()
435435
);
436436
Assert.That(ae.ParamName, Is.EqualTo("currentDirectory"));
437437
}
438+
439+
[Test]
440+
[WindowsOnly(WindowsSpecifics.Drives)]
441+
public void MockFileSystem_Constructor_ShouldSupportDifferentRootDrives()
442+
{
443+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
444+
{
445+
[@"c:\"] = new MockDirectoryData(),
446+
[@"z:\"] = new MockDirectoryData(),
447+
[@"d:\"] = new MockDirectoryData(),
448+
});
449+
450+
var cExists = fileSystem.Directory.Exists(@"c:\");
451+
var zExists = fileSystem.Directory.Exists(@"z:\");
452+
var dExists = fileSystem.Directory.Exists(@"d:\");
453+
454+
Assert.That(fileSystem, Is.Not.Null);
455+
Assert.That(cExists, Is.True);
456+
Assert.That(zExists, Is.True);
457+
Assert.That(dExists, Is.True);
458+
}
459+
460+
[Test]
461+
[WindowsOnly(WindowsSpecifics.Drives)]
462+
public void MockFileSystem_Constructor_ShouldAddDifferentDrivesIfNotExist()
463+
{
464+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
465+
{
466+
[@"d:\foo\bar\"] = new MockDirectoryData(),
467+
});
468+
469+
var drivesInfo = fileSystem.DriveInfo.GetDrives();
470+
var fooExists = fileSystem.Directory.Exists(@"d:\foo\");
471+
var barExists = fileSystem.Directory.Exists(@"d:\foo\bar\");
472+
473+
Assert.That(drivesInfo.Any(d => string.Equals(d.Name, @"D:\", StringComparison.InvariantCultureIgnoreCase)), Is.True);
474+
Assert.That(fooExists, Is.True);
475+
Assert.That(barExists, Is.True);
476+
}
477+
478+
[Test]
479+
[WindowsOnly(WindowsSpecifics.Drives)]
480+
public void MockFileSystem_Constructor_ShouldNotDuplicateDrives()
481+
{
482+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
483+
{
484+
[@"d:\foo\bar\"] = new MockDirectoryData(),
485+
[@"d:\"] = new MockDirectoryData()
486+
});
487+
488+
var drivesInfo = fileSystem.DriveInfo.GetDrives();
489+
490+
Assert.That(drivesInfo.Where(d => string.Equals(d.Name, @"D:\", StringComparison.InvariantCultureIgnoreCase)), Has.Exactly(1).Items);
491+
}
438492

439493
[Test]
440494
public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()

0 commit comments

Comments
 (0)