Skip to content

Commit

Permalink
Host file contention will no longer cause contents to be deleted. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
richardszalay committed Jun 24, 2017
1 parent f7159c0 commit a8c8a51
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
45 changes: 45 additions & 0 deletions RichardSzalay.Hosts.Tests/FileInfoResourceSpec.cs
@@ -0,0 +1,45 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace RichardSzalay.Hosts.Tests
{
public class FileInfoResourceSpec
{
[Subject(typeof(FileInfoResource), "OpenWrite")]
public class When_writing_to_the_resource
{
Establish context = () =>
{
tempFile = new FileInfo(Path.GetTempFileName());
File.WriteAllText(tempFile.FullName, "Test original longer text");
sut = new FileInfoResource(tempFile);
};

Because of = () =>
{
using (Stream stream = sut.OpenWrite())
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write("Test 2");
stream.Flush();
}
result = File.ReadAllText(tempFile.FullName);
};

It should_overwrite_existing_contents = () =>
result.ShouldEqual("Test 2");

static FileInfoResource sut;
static FileInfo tempFile;
static string result;
}
}
}
1 change: 1 addition & 0 deletions RichardSzalay.Hosts.Tests/RichardSzalay.Hosts.Tests.csproj
Expand Up @@ -47,6 +47,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileInfoResourceSpec.cs" />
<Compile Include="HostEntrySpec.cs" />
<Compile Include="HostsFileSpec.cs" />
<Compile Include="Infrastructure\StringResource.cs" />
Expand Down
4 changes: 1 addition & 3 deletions RichardSzalay.Hosts/FileInfoResource.cs
Expand Up @@ -26,9 +26,7 @@ public Stream OpenRead()

public Stream OpenWrite()
{
this.file.Delete();

return this.file.OpenWrite();
return File.Create(this.file.FullName);
}

#endregion
Expand Down

0 comments on commit a8c8a51

Please sign in to comment.