Skip to content

Commit de8dd8e

Browse files
authored
fix: MockFile - clone the bytes in read/write allbytes methods (#856)
1 parent ae9fb3a commit de8dd8e

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ public override byte[] ReadAllBytes(string path)
538538
throw CommonExceptions.FileNotFound(path);
539539
}
540540
mockFileDataAccessor.GetFile(path).CheckFileAccess(path, FileAccess.Read);
541-
return mockFileDataAccessor.GetFile(path).Contents;
541+
return mockFileDataAccessor.GetFile(path).Contents.ToArray();
542542
}
543543

544544
/// <inheritdoc />
@@ -784,7 +784,7 @@ public override void WriteAllBytes(string path, byte[] bytes)
784784
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
785785
VerifyDirectoryExists(path);
786786

787-
mockFileDataAccessor.AddFile(path, new MockFileData(bytes));
787+
mockFileDataAccessor.AddFile(path, new MockFileData(bytes.ToArray()));
788788
}
789789

790790
/// <summary>

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ public void MockFile_ReadAllBytes_ShouldTolerateAltDirectorySeparatorInPath()
6262

6363
Assert.AreEqual(data, fileSystem.File.ReadAllBytes(altPath));
6464
}
65+
66+
[Test]
67+
public void MockFile_ReadAllBytes_ShouldReturnANewCopyOfTheFileContents()
68+
{
69+
var path = XFS.Path(@"c:\something\demo.bin");
70+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
71+
{
72+
{ path, new MockFileData(new byte[] { 1, 2, 3, 4 }) }
73+
});
74+
75+
var firstRead = fileSystem.File.ReadAllBytes(path);
76+
77+
var secondRead = fileSystem.File.ReadAllBytes(path);
78+
79+
for (int i = 0; i < firstRead.Length; i++)
80+
{
81+
firstRead[i] += 1;
82+
}
83+
84+
Assert.AreNotEqual(firstRead, secondRead);
85+
}
86+
6587
#if FEATURE_ASYNC_FILE
6688
[Test]
6789
public async Task MockFile_ReadAllBytesAsync_ShouldReturnOriginalByteData()

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ public void MockFile_WriteAllBytes_ShouldThrowAnArgumentNullExceptionIfBytesAreN
8383
Assert.That(exception.ParamName, Is.EqualTo("bytes"));
8484
}
8585

86+
[Test]
87+
public void MockFile_WriteAllBytes_ShouldWriteASeparateCopyToTheFileSystem()
88+
{
89+
var fileSystem = new MockFileSystem();
90+
string path = XFS.Path(@"c:\something\file.bin");
91+
fileSystem.AddDirectory(XFS.Path(@"c:\something"));
92+
var fileContent = new byte[] { 1, 2, 3, 4 };
93+
94+
fileSystem.File.WriteAllBytes(path, fileContent);
95+
96+
for(int i = 0; i < fileContent.Length; i++)
97+
{
98+
fileContent[i] += 1;
99+
}
100+
101+
var readAgain = fileSystem.File.ReadAllBytes(path);
102+
103+
Assert.AreNotEqual(fileContent, readAgain);
104+
}
105+
106+
86107
#if FEATURE_ASYNC_FILE
87108
[Test]
88109
public void MockFile_WriteAllBytesAsync_ShouldThrowDirectoryNotFoundExceptionIfPathDoesNotExists()

0 commit comments

Comments
 (0)