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
35 changes: 35 additions & 0 deletions TestHelpers.Tests/MockFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,5 +1154,40 @@ public void MockFile_Open_OverwritesExistingFileOnCreate()
Assert.That(stream.Length, Is.EqualTo(0));
Assert.That(file.Contents.Length, Is.EqualTo(0));
}
[Test]
public void MockFile_AppendText_AppendTextToanExistingFile()
{
const string filepath = @"c:\something\does\exist.txt";
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filepath, new MockFileData("I'm here. ") }
});

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

stream.Write("Me too!");
stream.Flush();
stream.Close();

var file = filesystem.GetFile(filepath);
Assert.That(file.TextContents, Is.EqualTo("I'm here. Me too!"));
}

[Test]
public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile()
{
const string filepath = @"c:\something\doesnt\exist.txt";
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());

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

stream.Write("New too!");
stream.Flush();
stream.Close();

var file = filesystem.GetFile(filepath);
Assert.That(file.TextContents, Is.EqualTo("New too!"));
Assert.That(filesystem.FileExists(filepath));
}
}
}
3 changes: 3 additions & 0 deletions TestHelpers.Tests/TestHelpers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<None Include="packages.config" />
<Compile Include="StringExtensionsTests.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
12 changes: 11 additions & 1 deletion TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ public override void AppendAllText(string path, string contents, Encoding encodi

public override StreamWriter AppendText(string path)
{
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.");
if (mockFileDataAccessor.FileExists(path))
{
StreamWriter sw = new StreamWriter(OpenWrite(path));
sw.BaseStream.Seek(0, SeekOrigin.End); //push the stream pointer at the end for append.
return sw;

}
else
{
return new StreamWriter(Create(path));
}
}

public override void Copy(string sourceFileName, string destFileName)
Expand Down