Skip to content

Commit

Permalink
Merge pull request #286 from wleader/master
Browse files Browse the repository at this point in the history
Updates the Mock File System to throw DirectoryNotFound if creating a…
  • Loading branch information
tathamoddie authored Jul 4, 2018
2 parents 0ae3ed8 + 9e788bc commit 4a1d5a3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions TestHelpers.Tests/MockFileCreateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void Mockfile_Create_ShouldCreateNewStream()
{
string fullPath = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(@"c:\something");

var sut = new MockFile(fileSystem);

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

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

var mockFile = new MockFile(fileSystem);

Expand Down Expand Up @@ -146,5 +149,20 @@ public void MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull()
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.That(exception.Message, Does.StartWith("Path cannot be null."));
}

[Test]
public void MockFile_Create_ShouldThrowDirectoryNotFoundExceptionIfCreatingAndParentPathDoesNotExist()
{
// Arrange
var fileSystem = new MockFileSystem();

// Act
TestDelegate action = () => fileSystem.File.Create("C:\\Path\\NotFound.ext");

// Assert
Assert.IsFalse(fileSystem.Directory.Exists("C:\\path"));
var exception = Assert.Throws<DirectoryNotFoundException>(action);
Assert.That(exception.Message, Does.StartWith("Could not find a part of the path"));
}
}
}
18 changes: 18 additions & 0 deletions TestHelpers.Tests/MockFileOpenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void MockFile_Open_CreatesNewFileFileOnCreate()
{
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
filesystem.AddDirectory(@"c:\something\doesnt");

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

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

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

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

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

Expand Down Expand Up @@ -240,5 +243,20 @@ public void MockFile_OpenText_ShouldRetainCreationTime()
// Assert
Assert.AreEqual(creationTime, fs.FileInfo.FromFileName(filepath).CreationTime);
}

[Test]
public void MockFile_Open_ShouldThrowDirectoryNotFoundExceptionIfFileModeCreateAndParentPathDoesNotExist()
{
// Arrange
var fileSystem = new MockFileSystem();

// Act
TestDelegate action = () => fileSystem.File.Open("C:\\Path\\NotFound.ext", FileMode.Create);

// Assert
Assert.IsFalse(fileSystem.Directory.Exists("C:\\path"));
var exception = Assert.Throws<DirectoryNotFoundException>(action);
Assert.That(exception.Message, Does.StartWith("Could not find a part of the path"));
}
}
}
1 change: 1 addition & 0 deletions TestHelpers.Tests/MockFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile()
{
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
filesystem.AddDirectory(@"c:\something\doesnt");

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

Expand Down
3 changes: 3 additions & 0 deletions TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public override Stream Create(string path)
}
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

if (!mockFileDataAccessor.Directory.Exists(Path.GetDirectoryName(path)))
throw new DirectoryNotFoundException($"Could not find a part of the path '{path}'.");

mockFileDataAccessor.AddFile(path, new MockFileData(new byte[0]));
var stream = OpenWrite(path);
return stream;
Expand Down

0 comments on commit 4a1d5a3

Please sign in to comment.