Skip to content

Commit 659f7b4

Browse files
authored
fix: throw IOException when creating a directory with a conflicting file (#969)
Fixes #968: When creating a directory, check if a file with the same name already exists. If so, throw an IOException.
1 parent 941f487 commit 659f7b4

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

System.IO.Abstractions.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ EndProject
2525
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_", "_", "{BBF7AD8D-5522-48C0-A906-00CBB72308A0}"
2626
ProjectSection(SolutionItems) = preProject
2727
Directory.Build.props = Directory.Build.props
28+
global.json = global.json
2829
EndProjectSection
2930
EndProject
3031
Global

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ private IDirectoryInfo CreateDirectoryInternal(string path)
7171
path = path.TrimEnd(' ');
7272
}
7373

74-
if (!Exists(path))
74+
var existingFile = mockFileDataAccessor.GetFile(path);
75+
if (existingFile == null)
7576
{
7677
mockFileDataAccessor.AddDirectory(path);
7778
}
79+
else if (!existingFile.IsDirectory)
80+
{
81+
throw CommonExceptions.FileAlreadyExists("path");
82+
}
7883

7984
var created = new MockDirectoryInfo(mockFileDataAccessor, path);
8085

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,20 @@ public void MockDirectoryInfo_LastWriteTimeUtc_ShouldReturnDefaultTimeForNonExis
621621
Assert.That(result, Is.EqualTo(MockFileData.DefaultDateTimeOffset.UtcDateTime));
622622
}
623623

624+
[Test]
625+
public void MockDirectoryInfo_Create_WithConflictingFile_ShouldThrowIOException()
626+
{
627+
var fileSystem = new MockFileSystem();
628+
fileSystem.AddFile(XFS.Path(@"c:\foo\bar.txt"), new MockFileData("Demo text content"));
629+
var sut = fileSystem.DirectoryInfo.New(XFS.Path(@"c:\foo\bar.txt"));
630+
631+
// Act
632+
TestDelegate action = () => sut.Create();
633+
634+
// Assert
635+
Assert.Throws<IOException>(action);
636+
}
637+
624638
public void MockDirectoryInfo_CreationTime_SetterShouldThrowDirectoryNotFoundExceptionForNonExistingDirectory()
625639
{
626640
var newTime = new DateTime(2022, 04, 06);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,19 @@ public void MockDirectory_Exists_ShouldReturnFalseForIllegalPath(string path)
662662
Assert.IsFalse(result);
663663
}
664664

665+
[Test]
666+
public void MockDirectory_CreateDirectory_WithConflictingFile_ShouldThrowIOException()
667+
{
668+
var fileSystem = new MockFileSystem();
669+
fileSystem.AddFile(XFS.Path(@"c:\foo\bar.txt"), new MockFileData("Demo text content"));
670+
671+
// Act
672+
TestDelegate action = () => fileSystem.Directory.CreateDirectory(XFS.Path(@"c:\foo\bar.txt"));
673+
674+
// Assert
675+
Assert.Throws<IOException>(action);
676+
}
677+
665678
[Test]
666679
public void MockDirectory_Exists_ShouldReturnFalseForFiles()
667680
{

0 commit comments

Comments
 (0)