Skip to content

Commit abd7190

Browse files
PatrykOlejniczakfgreinacher
authored andcommitted
Throw DirectoryNotFoundException when try open FileStream in non-existing path.
1 parent 48fc525 commit abd7190

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamFactoryTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace System.IO.Abstractions.TestingHelpers.Tests
55
{
6+
using XFS = MockUnixSupport;
7+
68
[TestFixture]
79
public class MockFileStreamFactoryTests
810
{
@@ -41,5 +43,21 @@ public void MockFileStreamFactory_CreateForNonExistingFile_ShouldReturnStream(Fi
4143
// Assert
4244
Assert.IsNotNull(result);
4345
}
46+
47+
[Test]
48+
[TestCase(FileMode.Create)]
49+
[TestCase(FileMode.Open)]
50+
public void MockFileStreamFactory_CreateInNonExistingDirectory_ShouldThrowDirectoryNotFoundException(FileMode fileMode)
51+
{
52+
// Arrange
53+
var fileSystem = new MockFileSystem();
54+
fileSystem.AddDirectory(XFS.Path(@"C:\Test"));
55+
56+
// Act
57+
var fileStreamFactory = new MockFileStreamFactory(fileSystem);
58+
59+
// Assert
60+
Assert.Throws<DirectoryNotFoundException>(() => fileStreamFactory.Create(@"C:\Test\NonExistingDirectory\some_random_file.txt", fileMode));
61+
}
4462
}
4563
}

System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ public class MockFileStreamTests
1313
public void MockFileStream_Flush_WritesByteToFile()
1414
{
1515
// Arrange
16-
var filepath = XFS.Path(@"c:\something\foo.txt");
17-
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
18-
var cut = new MockFileStream(filesystem, filepath, MockFileStream.StreamType.WRITE);
16+
var filepath = XFS.Path(@"C:\something\foo.txt");
17+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
18+
fileSystem.AddFile(@"C:\something\foo.txt", new MockFileData(""));
19+
20+
var cut = new MockFileStream(fileSystem, filepath, MockFileStream.StreamType.WRITE);
1921

2022
// Act
2123
cut.WriteByte(255);
2224
cut.Flush();
2325

2426
// Assert
25-
CollectionAssert.AreEqual(new byte[]{255}, filesystem.GetFile(filepath).Contents);
27+
CollectionAssert.AreEqual(new byte[]{255}, fileSystem.GetFile(filepath).Contents);
2628
}
2729

2830
[Test]
@@ -52,10 +54,11 @@ public void MockFileStream_Constructor_Reading_Nonexistent_File_Throws_Exception
5254
{
5355
// Arrange
5456
var nonexistentFilePath = XFS.Path(@"c:\something\foo.txt");
55-
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
57+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
58+
fileSystem.AddDirectory(XFS.Path(@"C:\something"));
5659

5760
// Act
58-
Assert.Throws<FileNotFoundException>(() => new MockFileStream(filesystem, nonexistentFilePath, MockFileStream.StreamType.READ));
61+
Assert.Throws<FileNotFoundException>(() => new MockFileStream(fileSystem, nonexistentFilePath, MockFileStream.StreamType.READ));
5962

6063
// Assert - expect an exception
6164
}

System.IO.Abstractions.TestingHelpers/MockFileStream.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ public MockFileStream(
5757
}
5858
else
5959
{
60+
if (!mockFileDataAccessor.Directory.Exists(Path.GetDirectoryName(path)))
61+
{
62+
throw CommonExceptions.CouldNotFindPartOfPath(path);
63+
}
64+
6065
if (StreamType.READ.Equals(streamType))
6166
{
6267
throw CommonExceptions.FileNotFound(path);
6368
}
69+
6470
mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
6571
}
6672

0 commit comments

Comments
 (0)