Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions TestHelpers.Tests/MockFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,56 @@ public void MockFile_AppendAllText_ShouldPersistNewText()
file.ReadAllText(path));
}

[Test]
public void MockFile_AppendAllText_ShouldCreateIfNotExist()
{
// Arrange
string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra file seems extraneous for the test?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, never mind. It's because you need the folder...

});

// Act
var path2 = @"c:\something\demo2.txt";
fileSystem.File.AppendAllText(path2, "some text");
var path3 = @"c:\something\demo3.txt";
fileSystem.File.AppendAllText(path3, "some text", Encoding.Unicode);

// Assert
Assert.AreEqual(
"some text",
fileSystem.File.ReadAllText(path2));
Assert.AreEqual(
"some text",
fileSystem.File.ReadAllText(path3, Encoding.Unicode));
}

[Test]
public void MockFile_AppendAllText_ShouldFailIfNotExistButDirectoryAlsoNotExist()
{
// Arrange
string path = @"c:\something\demo.txt";
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("Demo text content") }
});

var file = new MockFile(fileSystem);


// Act
path = @"c:\something2\demo.txt";

// Assert
Exception ex;
ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text"));
Assert.That(ex.Message, Is.EqualTo(String.Format("Could not find a part of the path '{0}'.", path)));

ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text", Encoding.Unicode));
Assert.That(ex.Message, Is.EqualTo(String.Format("Could not find a part of the path '{0}'.", path)));
}

[Test]
public void MockFile_AppendAllText_ShouldPersistNewTextWithCustomEncoding()
{
Expand Down
38 changes: 31 additions & 7 deletions TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,41 @@ public MockFile(IMockFileDataAccessor mockFileDataAccessor)

public override void AppendAllText(string path, string contents)
{
mockFileDataAccessor
.GetFile(path)
.TextContents += contents;
if (!mockFileDataAccessor.FileExists(path))
{
var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
if (!mockFileDataAccessor.Directory.Exists(dir))
{
throw new DirectoryNotFoundException(String.Format("Could not find a part of the path '{0}'.", path));
}
mockFileDataAccessor.AddFile(path, new MockFileData(contents));
}
else
{
mockFileDataAccessor
.GetFile(path)
.TextContents += contents;
}
}

public override void AppendAllText(string path, string contents, Encoding encoding)
{
var file = mockFileDataAccessor.GetFile(path);
var originalText = encoding.GetString(file.Contents);
var newText = originalText + contents;
file.Contents = encoding.GetBytes(newText);
if (!mockFileDataAccessor.FileExists(path))
{
var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
if (!mockFileDataAccessor.Directory.Exists(dir))
{
throw new DirectoryNotFoundException(String.Format("Could not find a part of the path '{0}'.", path));
}
mockFileDataAccessor.AddFile(path, new MockFileData(encoding.GetBytes(contents)));
}
else
{
var file = mockFileDataAccessor.GetFile(path);
var originalText = encoding.GetString(file.Contents);
var newText = originalText + contents;
file.Contents = encoding.GetBytes(newText);
}
}

public override StreamWriter AppendText(string path)
Expand Down