Skip to content

Commit 1378f58

Browse files
MockFileInfo.Append should create a new file if missing (#570)
Fixes #567 Co-authored-by: Florian Greinacher <fgreinacher@users.noreply.github.com>
1 parent ff509cf commit 1378f58

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,26 @@ public void MockFileInfo_AppendText_ShouldAddTextToFileInMemoryFileSystem()
230230
Assert.AreEqual($"Demo text contentThis should be at the end{Environment.NewLine}", newcontents);
231231
}
232232

233+
[Test]
234+
public void MockFileInfo_AppendText_ShouldCreateFileIfMissing()
235+
{
236+
var fileSystem = new MockFileSystem();
237+
var targetFile = XFS.Path(@"c:\a.txt");
238+
var fileInfo = new MockFileInfo(fileSystem, targetFile);
239+
240+
using (var file = fileInfo.AppendText())
241+
file.WriteLine("This should be the contents");
242+
243+
string newcontents;
244+
using (var newfile = fileInfo.OpenText())
245+
{
246+
newcontents = newfile.ReadToEnd();
247+
}
248+
249+
Assert.That(fileSystem.File.Exists(targetFile), Is.True);
250+
Assert.AreEqual($"This should be the contents{Environment.NewLine}", newcontents);
251+
}
252+
233253
[Test]
234254
public void MockFileInfo_OpenWrite_ShouldAddDataToFileInMemoryFileSystem()
235255
{

System.IO.Abstractions.TestingHelpers/MockFileInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ public override string Name
152152

153153
public override StreamWriter AppendText()
154154
{
155-
if (MockFileData == null) throw CommonExceptions.FileNotFound(path);
156155
return new StreamWriter(new MockFileStream(mockFileSystem, FullName, MockFileStream.StreamType.APPEND));
157156
}
158157

0 commit comments

Comments
 (0)