Skip to content

Commit

Permalink
feat: add Null to MockFileStream to shadow the static Stream.Null
Browse files Browse the repository at this point in the history
… field (#1025)

Fixes #1009 

This is a suggestion based on the [workaround by @sheila-stewart](#1009 (comment)).
  • Loading branch information
vbreuss authored Aug 22, 2023
1 parent 4c5b0c1 commit aebb626
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ namespace System.IO.Abstractions.TestingHelpers
[Serializable]
public class MockFileStream : FileSystemStream, IFileSystemAclSupport
{
/// <summary>
/// Wrapper around a <see cref="Stream" /> with no backing store, which
/// is used as a replacement for a <see cref="FileSystemStream" />. As such
/// it implements the same properties and methods as a <see cref="FileSystemStream" />.
/// </summary>
public new static FileSystemStream Null { get; } = new NullFileSystemStream();

private class NullFileSystemStream : FileSystemStream
{
/// <summary>
/// Initializes a new instance of <see cref="NullFileSystemStream" />.
/// </summary>
public NullFileSystemStream() : base(Null, ".", true)
{

}
}

private readonly IMockFileDataAccessor mockFileDataAccessor;
private readonly string path;
private readonly FileAccess access = FileAccess.ReadWrite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,24 @@ public void MockFileStream_FlushBool_ShouldNotChangePosition([Values] bool flush
Assert.AreEqual(200, stream.Position);
}
}

[Test]
public void MockFileStream_Null_ShouldReturnSingletonObject()
{
var result1 = MockFileStream.Null;
var result2 = MockFileStream.Null;

Assert.AreSame(result1, result2);
}

[Test]
public void MockFileStream_Null_ShouldHaveExpectedProperties()
{
var result = MockFileStream.Null;

Assert.AreEqual(result.Name, ".");
Assert.AreEqual(result.Length, 0);
Assert.AreEqual(result.IsAsync, true);
}
}
}

0 comments on commit aebb626

Please sign in to comment.