Skip to content

Commit 03aa8ca

Browse files
author
Sutikshan Dubey
committed
Added MockFile.AppendText Implementation with unit test.
1 parent e86ba60 commit 03aa8ca

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

TestHelpers.Tests/MockFileTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,5 +1154,40 @@ public void MockFile_Open_OverwritesExistingFileOnCreate()
11541154
Assert.That(stream.Length, Is.EqualTo(0));
11551155
Assert.That(file.Contents.Length, Is.EqualTo(0));
11561156
}
1157+
[Test]
1158+
public void MockFile_AppendText_AppendTextToanExistingFile()
1159+
{
1160+
const string filepath = @"c:\something\does\exist.txt";
1161+
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
1162+
{
1163+
{ filepath, new MockFileData("I'm here. ") }
1164+
});
1165+
1166+
var stream = filesystem.File.AppendText(filepath);
1167+
1168+
stream.Write("Me too!");
1169+
stream.Flush();
1170+
stream.Close();
1171+
1172+
var file = filesystem.GetFile(filepath);
1173+
Assert.That(file.TextContents, Is.EqualTo("I'm here. Me too!"));
1174+
}
1175+
1176+
[Test]
1177+
public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile()
1178+
{
1179+
const string filepath = @"c:\something\doesnt\exist.txt";
1180+
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
1181+
1182+
var stream = filesystem.File.AppendText(filepath);
1183+
1184+
stream.Write("New too!");
1185+
stream.Flush();
1186+
stream.Close();
1187+
1188+
var file = filesystem.GetFile(filepath);
1189+
Assert.That(file.TextContents, Is.EqualTo("New too!"));
1190+
Assert.That(filesystem.FileExists(filepath));
1191+
}
11571192
}
11581193
}

TestHelpers.Tests/TestHelpers.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<None Include="packages.config" />
118118
<Compile Include="StringExtensionsTests.cs" />
119119
</ItemGroup>
120+
<ItemGroup>
121+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
122+
</ItemGroup>
120123
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
121124
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
122125
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

TestingHelpers/MockFile.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ public override void AppendAllText(string path, string contents, Encoding encodi
3333

3434
public override StreamWriter AppendText(string path)
3535
{
36-
throw new NotImplementedException("This test helper hasn't been implemented yet. They are implemented on an as-needed basis. As it seems like you need it, now would be a great time to send us a pull request over at https://github.com/tathamoddie/System.IO.Abstractions. You know, because it's open source and all.");
36+
if (mockFileDataAccessor.FileExists(path))
37+
{
38+
StreamWriter sw = new StreamWriter(OpenWrite(path));
39+
sw.BaseStream.Seek(0, SeekOrigin.End); //push the stream pointer at the end for append.
40+
return sw;
41+
42+
}
43+
else
44+
{
45+
return new StreamWriter(Create(path));
46+
}
3747
}
3848

3949
public override void Copy(string sourceFileName, string destFileName)

0 commit comments

Comments
 (0)