Skip to content

Commit 4a1d5a3

Browse files
authored
Merge pull request #286 from wleader/master
Updates the Mock File System to throw DirectoryNotFound if creating a…
2 parents 0ae3ed8 + 9e788bc commit 4a1d5a3

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

TestHelpers.Tests/MockFileCreateTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public void Mockfile_Create_ShouldCreateNewStream()
1818
{
1919
string fullPath = XFS.Path(@"c:\something\demo.txt");
2020
var fileSystem = new MockFileSystem();
21+
fileSystem.AddDirectory(@"c:\something");
2122

2223
var sut = new MockFile(fileSystem);
2324

@@ -33,6 +34,7 @@ public void Mockfile_Create_CanWriteToNewStream()
3334
{
3435
string fullPath = XFS.Path(@"c:\something\demo.txt");
3536
var fileSystem = new MockFileSystem();
37+
fileSystem.AddDirectory(@"c:\something");
3638
var data = new UTF8Encoding(false).GetBytes("Test string");
3739

3840
var sut = new MockFile(fileSystem);
@@ -52,6 +54,7 @@ public void Mockfile_Create_OverwritesExistingFile()
5254
{
5355
string path = XFS.Path(@"c:\some\file.txt");
5456
var fileSystem = new MockFileSystem();
57+
fileSystem.AddDirectory(@"c:\some");
5558

5659
var mockFile = new MockFile(fileSystem);
5760

@@ -146,5 +149,20 @@ public void MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull()
146149
var exception = Assert.Throws<ArgumentNullException>(action);
147150
Assert.That(exception.Message, Does.StartWith("Path cannot be null."));
148151
}
152+
153+
[Test]
154+
public void MockFile_Create_ShouldThrowDirectoryNotFoundExceptionIfCreatingAndParentPathDoesNotExist()
155+
{
156+
// Arrange
157+
var fileSystem = new MockFileSystem();
158+
159+
// Act
160+
TestDelegate action = () => fileSystem.File.Create("C:\\Path\\NotFound.ext");
161+
162+
// Assert
163+
Assert.IsFalse(fileSystem.Directory.Exists("C:\\path"));
164+
var exception = Assert.Throws<DirectoryNotFoundException>(action);
165+
Assert.That(exception.Message, Does.StartWith("Could not find a part of the path"));
166+
}
149167
}
150168
}

TestHelpers.Tests/MockFileOpenTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void MockFile_Open_CreatesNewFileFileOnCreate()
4242
{
4343
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
4444
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
45+
filesystem.AddDirectory(@"c:\something\doesnt");
4546

4647
var stream = filesystem.File.Open(filepath, FileMode.Create);
4748

@@ -55,6 +56,7 @@ public void MockFile_Open_CreatesNewFileFileOnCreateNew()
5556
{
5657
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
5758
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
59+
filesystem.AddDirectory(@"c:\something\doesnt");
5860

5961
var stream = filesystem.File.Open(filepath, FileMode.CreateNew);
6062

@@ -153,6 +155,7 @@ public void MockFile_Open_CreatesNewFileOnOpenOrCreate()
153155
{
154156
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
155157
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
158+
filesystem.AddDirectory(@"c:\something\doesnt");
156159

157160
var stream = filesystem.File.Open(filepath, FileMode.OpenOrCreate);
158161

@@ -240,5 +243,20 @@ public void MockFile_OpenText_ShouldRetainCreationTime()
240243
// Assert
241244
Assert.AreEqual(creationTime, fs.FileInfo.FromFileName(filepath).CreationTime);
242245
}
246+
247+
[Test]
248+
public void MockFile_Open_ShouldThrowDirectoryNotFoundExceptionIfFileModeCreateAndParentPathDoesNotExist()
249+
{
250+
// Arrange
251+
var fileSystem = new MockFileSystem();
252+
253+
// Act
254+
TestDelegate action = () => fileSystem.File.Open("C:\\Path\\NotFound.ext", FileMode.Create);
255+
256+
// Assert
257+
Assert.IsFalse(fileSystem.Directory.Exists("C:\\path"));
258+
var exception = Assert.Throws<DirectoryNotFoundException>(action);
259+
Assert.That(exception.Message, Does.StartWith("Could not find a part of the path"));
260+
}
243261
}
244262
}

TestHelpers.Tests/MockFileTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile()
540540
{
541541
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
542542
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
543+
filesystem.AddDirectory(@"c:\something\doesnt");
543544

544545
var stream = filesystem.File.AppendText(filepath);
545546

TestingHelpers/MockFile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public override Stream Create(string path)
141141
}
142142
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
143143

144+
if (!mockFileDataAccessor.Directory.Exists(Path.GetDirectoryName(path)))
145+
throw new DirectoryNotFoundException($"Could not find a part of the path '{path}'.");
146+
144147
mockFileDataAccessor.AddFile(path, new MockFileData(new byte[0]));
145148
var stream = OpenWrite(path);
146149
return stream;

0 commit comments

Comments
 (0)