Skip to content

Commit ba96069

Browse files
authored
fix: pass on FileAccess to open function when FileMode is Create or CreateNew. (#714)
1 parent e5ee326 commit ba96069

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/System.IO.Abstractions.TestingHelpers/MockFile.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public override Stream Create(string path, int bufferSize) =>
126126
Create(path, bufferSize, FileOptions.None);
127127

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

131-
private Stream CreateInternal(string path, FileOptions options)
131+
private Stream CreateInternal(string path, FileAccess access, FileOptions options)
132132
{
133133
if (path == null)
134134
{
@@ -140,7 +140,7 @@ private Stream CreateInternal(string path, FileOptions options)
140140

141141
var mockFileData = new MockFileData(new byte[0]);
142142
mockFileDataAccessor.AddFile(path, mockFileData);
143-
return OpenWriteInternal(path, options);
143+
return OpenInternal(path, FileMode.Open, access, options);
144144
}
145145

146146
public override StreamWriter CreateText(string path)
@@ -442,13 +442,13 @@ private Stream OpenInternal(
442442

443443
if (!exists || mode == FileMode.CreateNew)
444444
{
445-
return Create(path);
445+
return CreateInternal(path, access, options);
446446
}
447447

448448
if (mode == FileMode.Create || mode == FileMode.Truncate)
449449
{
450450
Delete(path);
451-
return Create(path);
451+
return CreateInternal(path, access, options);
452452
}
453453

454454
var mockFileData = mockFileDataAccessor.GetFile(path);

tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileOpenTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ public void MockFile_Open_CreatesNewFileFileOnCreate()
5151
Assert.That(stream.Length, Is.EqualTo(0));
5252
}
5353

54+
[Test]
55+
public void MockFile_Open_AllowsReadWriteOnCreate()
56+
{
57+
string filepath = XFS.Path(@"c:\something\doesnt\exist.txt");
58+
var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>());
59+
filesystem.AddDirectory(XFS.Path(@"c:\something\doesnt"));
60+
61+
var stream = filesystem.File.Open(filepath, FileMode.Create);
62+
63+
Assert.True(stream.CanRead);
64+
Assert.True(stream.CanWrite);
65+
}
66+
5467
[Test]
5568
public void MockFile_Open_CreatesNewFileFileOnCreateNew()
5669
{

0 commit comments

Comments
 (0)