From aebb6264b17a5ec094e9034561289ce803ad6c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Tue, 22 Aug 2023 16:54:54 +0200 Subject: [PATCH] feat: add `Null` to MockFileStream to shadow the static `Stream.Null` field (#1025) Fixes #1009 This is a suggestion based on the [workaround by @sheila-stewart](https://github.com/TestableIO/System.IO.Abstractions/issues/1009#issuecomment-1680946638). --- .../MockFileStream.cs | 18 ++++++++++++++++++ .../MockFileStreamTests.cs | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs index 61b96f428..197fffd43 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileStream.cs @@ -9,6 +9,24 @@ namespace System.IO.Abstractions.TestingHelpers [Serializable] public class MockFileStream : FileSystemStream, IFileSystemAclSupport { + /// + /// Wrapper around a with no backing store, which + /// is used as a replacement for a . As such + /// it implements the same properties and methods as a . + /// + public new static FileSystemStream Null { get; } = new NullFileSystemStream(); + + private class NullFileSystemStream : FileSystemStream + { + /// + /// Initializes a new instance of . + /// + public NullFileSystemStream() : base(Null, ".", true) + { + + } + } + private readonly IMockFileDataAccessor mockFileDataAccessor; private readonly string path; private readonly FileAccess access = FileAccess.ReadWrite; diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs index 34a5e620e..8a32806d4 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileStreamTests.cs @@ -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); + } } }