Skip to content

Commit 8dffd3c

Browse files
committed
Merge pull request #55 from wtjones/AppendAllText-create
MockFile.AppendAllText creates the file if it doesn't exist.
2 parents b060737 + e9e7f34 commit 8dffd3c

2 files changed

Lines changed: 81 additions & 7 deletions

File tree

TestHelpers.Tests/MockFileTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,56 @@ public void MockFile_AppendAllText_ShouldPersistNewText()
2929
file.ReadAllText(path));
3030
}
3131

32+
[Test]
33+
public void MockFile_AppendAllText_ShouldCreateIfNotExist()
34+
{
35+
// Arrange
36+
string path = @"c:\something\demo.txt";
37+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
38+
{
39+
{ path, new MockFileData("Demo text content") }
40+
});
41+
42+
// Act
43+
var path2 = @"c:\something\demo2.txt";
44+
fileSystem.File.AppendAllText(path2, "some text");
45+
var path3 = @"c:\something\demo3.txt";
46+
fileSystem.File.AppendAllText(path3, "some text", Encoding.Unicode);
47+
48+
// Assert
49+
Assert.AreEqual(
50+
"some text",
51+
fileSystem.File.ReadAllText(path2));
52+
Assert.AreEqual(
53+
"some text",
54+
fileSystem.File.ReadAllText(path3, Encoding.Unicode));
55+
}
56+
57+
[Test]
58+
public void MockFile_AppendAllText_ShouldFailIfNotExistButDirectoryAlsoNotExist()
59+
{
60+
// Arrange
61+
string path = @"c:\something\demo.txt";
62+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
63+
{
64+
{ path, new MockFileData("Demo text content") }
65+
});
66+
67+
var file = new MockFile(fileSystem);
68+
69+
70+
// Act
71+
path = @"c:\something2\demo.txt";
72+
73+
// Assert
74+
Exception ex;
75+
ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text"));
76+
Assert.That(ex.Message, Is.EqualTo(String.Format("Could not find a part of the path '{0}'.", path)));
77+
78+
ex = Assert.Throws<DirectoryNotFoundException>(() => fileSystem.File.AppendAllText(path, "some text", Encoding.Unicode));
79+
Assert.That(ex.Message, Is.EqualTo(String.Format("Could not find a part of the path '{0}'.", path)));
80+
}
81+
3282
[Test]
3383
public void MockFile_AppendAllText_ShouldPersistNewTextWithCustomEncoding()
3484
{

TestingHelpers/MockFile.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,41 @@ public MockFile(IMockFileDataAccessor mockFileDataAccessor)
1818

1919
public override void AppendAllText(string path, string contents)
2020
{
21-
mockFileDataAccessor
22-
.GetFile(path)
23-
.TextContents += contents;
21+
if (!mockFileDataAccessor.FileExists(path))
22+
{
23+
var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
24+
if (!mockFileDataAccessor.Directory.Exists(dir))
25+
{
26+
throw new DirectoryNotFoundException(String.Format("Could not find a part of the path '{0}'.", path));
27+
}
28+
mockFileDataAccessor.AddFile(path, new MockFileData(contents));
29+
}
30+
else
31+
{
32+
mockFileDataAccessor
33+
.GetFile(path)
34+
.TextContents += contents;
35+
}
2436
}
2537

2638
public override void AppendAllText(string path, string contents, Encoding encoding)
2739
{
28-
var file = mockFileDataAccessor.GetFile(path);
29-
var originalText = encoding.GetString(file.Contents);
30-
var newText = originalText + contents;
31-
file.Contents = encoding.GetBytes(newText);
40+
if (!mockFileDataAccessor.FileExists(path))
41+
{
42+
var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
43+
if (!mockFileDataAccessor.Directory.Exists(dir))
44+
{
45+
throw new DirectoryNotFoundException(String.Format("Could not find a part of the path '{0}'.", path));
46+
}
47+
mockFileDataAccessor.AddFile(path, new MockFileData(encoding.GetBytes(contents)));
48+
}
49+
else
50+
{
51+
var file = mockFileDataAccessor.GetFile(path);
52+
var originalText = encoding.GetString(file.Contents);
53+
var newText = originalText + contents;
54+
file.Contents = encoding.GetBytes(newText);
55+
}
3256
}
3357

3458
public override StreamWriter AppendText(string path)

0 commit comments

Comments
 (0)