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
10 changes: 5 additions & 5 deletions src/System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public override Stream Create(string path, int bufferSize) =>
Create(path, bufferSize, FileOptions.None);

public override Stream Create(string path, int bufferSize, FileOptions options) =>
CreateInternal(path, options);
CreateInternal(path, FileAccess.Write, options);

private Stream CreateInternal(string path, FileOptions options)
private Stream CreateInternal(string path, FileAccess access, FileOptions options)
{
if (path == null)
{
Expand All @@ -140,7 +140,7 @@ private Stream CreateInternal(string path, FileOptions options)

var mockFileData = new MockFileData(new byte[0]);
mockFileDataAccessor.AddFile(path, mockFileData);
return OpenWriteInternal(path, options);
return OpenInternal(path, FileMode.Open, access, options);
}

public override StreamWriter CreateText(string path)
Expand Down Expand Up @@ -442,13 +442,13 @@ private Stream OpenInternal(

if (!exists || mode == FileMode.CreateNew)
{
return Create(path);
return CreateInternal(path, access, options);
}

if (mode == FileMode.Create || mode == FileMode.Truncate)
{
Delete(path);
return Create(path);
return CreateInternal(path, access, options);
}

var mockFileData = mockFileDataAccessor.GetFile(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ public void MockFile_Open_CreatesNewFileFileOnCreate()
Assert.That(stream.Length, Is.EqualTo(0));
}

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

var stream = filesystem.File.Open(filepath, FileMode.Create);

Assert.True(stream.CanRead);
Assert.True(stream.CanWrite);
}

[Test]
public void MockFile_Open_CreatesNewFileFileOnCreateNew()
{
Expand Down