From f6f1d1a3b92795a0e79021b2cb703015e3bf894a Mon Sep 17 00:00:00 2001 From: Caio Proiete Date: Sat, 29 Oct 2016 15:59:20 -0300 Subject: [PATCH 1/7] Add FileStreamFactory and support for mocking FileStreams --- System.IO.Abstractions/FileStreamFactory.cs | 88 +++++++++++++++ System.IO.Abstractions/FileSystem.cs | 6 ++ System.IO.Abstractions/IFileStreamFactory.cs | 33 ++++++ System.IO.Abstractions/IFileSystem.cs | 1 + .../System.IO.Abstractions.csproj | 2 + .../MockFileStreamFactoryTests.cs | 51 +++++++++ TestHelpers.Tests/TestHelpers.Tests.csproj | 1 + TestingHelpers/MockFileStreamFactory.cs | 100 ++++++++++++++++++ TestingHelpers/MockFileSystem.cs | 7 ++ TestingHelpers/TestingHelpers.csproj | 1 + 10 files changed, 290 insertions(+) create mode 100644 System.IO.Abstractions/FileStreamFactory.cs create mode 100644 System.IO.Abstractions/IFileStreamFactory.cs create mode 100644 TestHelpers.Tests/MockFileStreamFactoryTests.cs create mode 100644 TestingHelpers/MockFileStreamFactory.cs diff --git a/System.IO.Abstractions/FileStreamFactory.cs b/System.IO.Abstractions/FileStreamFactory.cs new file mode 100644 index 000000000..ec99ce68e --- /dev/null +++ b/System.IO.Abstractions/FileStreamFactory.cs @@ -0,0 +1,88 @@ +using System.Security.AccessControl; +using Microsoft.Win32.SafeHandles; + +namespace System.IO.Abstractions +{ + [Serializable] + internal class FileStreamFactory : IFileStreamFactory + { + public Stream Create(string path, FileMode mode) + { + return new FileStream(path, mode); + } + + public Stream Create(string path, FileMode mode, FileAccess access) + { + return new FileStream(path, mode, access); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share) + { + return new FileStream(path, mode, access, share); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) + { + return new FileStream(path, mode, access, share, bufferSize); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) + { + return new FileStream(path, mode, access, share, bufferSize, options); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) + { + return new FileStream(path, mode, access, share, bufferSize, useAsync); + } + + public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity) + { + return new FileStream(path, mode, rights, share, bufferSize, options, fileSecurity); + } + + public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options) + { + return new FileStream(path, mode, rights, share, bufferSize, options); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access) + { + return new FileStream(handle, access); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle) + { + return new FileStream(handle, access, ownsHandle); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize) + { + return new FileStream(handle, access, ownsHandle, bufferSize); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync) + { + return new FileStream(handle, access, ownsHandle, bufferSize, isAsync); + } + + public Stream Create(SafeFileHandle handle, FileAccess access) + { + return new FileStream(handle, access); + } + + public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize) + { + return new FileStream(handle, access, bufferSize); + } + + public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) + { + return new FileStream(handle, access, bufferSize, isAsync); + } + } +} \ No newline at end of file diff --git a/System.IO.Abstractions/FileSystem.cs b/System.IO.Abstractions/FileSystem.cs index 28f3a77c5..cb7cdfd5d 100644 --- a/System.IO.Abstractions/FileSystem.cs +++ b/System.IO.Abstractions/FileSystem.cs @@ -21,6 +21,12 @@ public IFileInfoFactory FileInfo get { return fileInfoFactory ?? (fileInfoFactory = new FileInfoFactory()); } } + FileStreamFactory fileStreamFactory; + public IFileStreamFactory FileStream + { + get { return fileStreamFactory ?? (fileStreamFactory = new FileStreamFactory()); } + } + PathBase path; public PathBase Path { diff --git a/System.IO.Abstractions/IFileStreamFactory.cs b/System.IO.Abstractions/IFileStreamFactory.cs new file mode 100644 index 000000000..6b5cd04c8 --- /dev/null +++ b/System.IO.Abstractions/IFileStreamFactory.cs @@ -0,0 +1,33 @@ +using System.Security.AccessControl; +using Microsoft.Win32.SafeHandles; + +namespace System.IO.Abstractions +{ + public interface IFileStreamFactory + { + Stream Create(string path, FileMode mode); + Stream Create(string path, FileMode mode, FileAccess access); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync); + Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity); + Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options); + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] + Stream Create(IntPtr handle, FileAccess access); + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + Stream Create(IntPtr handle, FileAccess access, bool ownsHandle); + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize); + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync); + + Stream Create(SafeFileHandle handle, FileAccess access); + Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize); + Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync); + } +} \ No newline at end of file diff --git a/System.IO.Abstractions/IFileSystem.cs b/System.IO.Abstractions/IFileSystem.cs index 3d875e1cc..0dd6a1129 100644 --- a/System.IO.Abstractions/IFileSystem.cs +++ b/System.IO.Abstractions/IFileSystem.cs @@ -5,6 +5,7 @@ public interface IFileSystem FileBase File { get; } DirectoryBase Directory { get; } IFileInfoFactory FileInfo { get; } + IFileStreamFactory FileStream { get; } PathBase Path { get; } IDirectoryInfoFactory DirectoryInfo { get; } IDriveInfoFactory DriveInfo { get; } diff --git a/System.IO.Abstractions/System.IO.Abstractions.csproj b/System.IO.Abstractions/System.IO.Abstractions.csproj index 730e3540b..0376e2deb 100644 --- a/System.IO.Abstractions/System.IO.Abstractions.csproj +++ b/System.IO.Abstractions/System.IO.Abstractions.csproj @@ -77,6 +77,7 @@ + @@ -88,6 +89,7 @@ + diff --git a/TestHelpers.Tests/MockFileStreamFactoryTests.cs b/TestHelpers.Tests/MockFileStreamFactoryTests.cs new file mode 100644 index 000000000..649f6887b --- /dev/null +++ b/TestHelpers.Tests/MockFileStreamFactoryTests.cs @@ -0,0 +1,51 @@ +using System.Collections.Generic; +using NUnit.Framework; + +namespace System.IO.Abstractions.TestingHelpers.Tests +{ + [TestFixture] + public class MockFileStreamFactoryTests + { + [Test] + [TestCase(FileMode.Create)] + [TestCase(FileMode.Append)] + public void MockFileStreamFactory_Create_string_FileMode__ShouldReturnStreamForExistingFile(FileMode fileMode) + { + // Arrange + var fileSystem = new MockFileSystem(new Dictionary + { + { @"c:\a.txt", new MockFileData("Demo text content") }, + { @"c:\a\b\c.txt", new MockFileData("Demo text content") }, + }); + + var fileStreamFactory = new MockFileStreamFactory(fileSystem); + + // Act + var result = fileStreamFactory.Create(@"c:\a.txt", fileMode); + + // Assert + Assert.IsNotNull(result); + } + + [Test] + [TestCase(FileMode.Create)] + [TestCase(FileMode.Append)] + public void MockFileStreamFactory_Create_string_FileMode__ShouldReturnStreamForNonExistingFile(FileMode fileMode) + { + // Arrange + var fileSystem = new MockFileSystem(new Dictionary + { + { @"c:\a.txt", new MockFileData("Demo text content") }, + { @"c:\a\b\c.txt", new MockFileData("Demo text content") }, + }); + + var fileStreamFactory = new MockFileStreamFactory(fileSystem); + + // Act + var result = fileStreamFactory.Create(@"c:\foo.txt", fileMode); + + // Assert + Assert.IsNotNull(result); + } + } +} \ No newline at end of file diff --git a/TestHelpers.Tests/TestHelpers.Tests.csproj b/TestHelpers.Tests/TestHelpers.Tests.csproj index 6b559259e..127888221 100644 --- a/TestHelpers.Tests/TestHelpers.Tests.csproj +++ b/TestHelpers.Tests/TestHelpers.Tests.csproj @@ -98,6 +98,7 @@ + diff --git a/TestingHelpers/MockFileStreamFactory.cs b/TestingHelpers/MockFileStreamFactory.cs new file mode 100644 index 000000000..89161e954 --- /dev/null +++ b/TestingHelpers/MockFileStreamFactory.cs @@ -0,0 +1,100 @@ +using System.Security.AccessControl; +using Microsoft.Win32.SafeHandles; + +namespace System.IO.Abstractions.TestingHelpers +{ + [Serializable] + public class MockFileStreamFactory : IFileStreamFactory + { + private readonly IMockFileDataAccessor mockFileSystem; + + public MockFileStreamFactory(IMockFileDataAccessor mockFileSystem) + { + if (mockFileSystem == null) + { + throw new ArgumentNullException("mockFileSystem"); + } + + this.mockFileSystem = mockFileSystem; + } + + public Stream Create(string path, FileMode mode) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + public Stream Create(string path, FileMode mode, FileAccess access) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options) + { + return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access) + { + return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle) + { + return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize) + { + return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + } + + [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] + public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync) + { + return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + } + + public Stream Create(SafeFileHandle handle, FileAccess access) + { + return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + } + + public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize) + { + return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + } + + public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) + { + return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + } + } +} \ No newline at end of file diff --git a/TestingHelpers/MockFileSystem.cs b/TestingHelpers/MockFileSystem.cs index bddec18e1..b99ff3fe5 100644 --- a/TestingHelpers/MockFileSystem.cs +++ b/TestingHelpers/MockFileSystem.cs @@ -13,6 +13,7 @@ public class MockFileSystem : IFileSystem, IMockFileDataAccessor private readonly FileBase file; private readonly DirectoryBase directory; private readonly IFileInfoFactory fileInfoFactory; + private readonly IFileStreamFactory fileStreamFactory; private readonly PathBase pathField; private readonly IDirectoryInfoFactory directoryInfoFactory; private readonly IDriveInfoFactory driveInfoFactory; @@ -34,6 +35,7 @@ public MockFileSystem(IDictionary files, string currentDir file = new MockFile(this); directory = new MockDirectory(this, file, currentDirectory); fileInfoFactory = new MockFileInfoFactory(this); + fileStreamFactory = new MockFileStreamFactory(this); directoryInfoFactory = new MockDirectoryInfoFactory(this); driveInfoFactory = new MockDriveInfoFactory(this); @@ -61,6 +63,11 @@ public IFileInfoFactory FileInfo get { return fileInfoFactory; } } + public IFileStreamFactory FileStream + { + get { return fileStreamFactory; } + } + public PathBase Path { get { return pathField; } diff --git a/TestingHelpers/TestingHelpers.csproj b/TestingHelpers/TestingHelpers.csproj index 4ef1904a7..bb1dfccf7 100644 --- a/TestingHelpers/TestingHelpers.csproj +++ b/TestingHelpers/TestingHelpers.csproj @@ -76,6 +76,7 @@ + From 3d50fcb0923cf52b066019a988ca08e1e1a1906a Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Tue, 10 Jul 2018 22:21:30 +0200 Subject: [PATCH 2/7] Fix .NET Standard compile --- System.IO.Abstractions/FileStreamFactory.cs | 82 +++++++------------- System.IO.Abstractions/IFileStreamFactory.cs | 21 ++++- 2 files changed, 45 insertions(+), 58 deletions(-) diff --git a/System.IO.Abstractions/FileStreamFactory.cs b/System.IO.Abstractions/FileStreamFactory.cs index ec99ce68e..060c90eac 100644 --- a/System.IO.Abstractions/FileStreamFactory.cs +++ b/System.IO.Abstractions/FileStreamFactory.cs @@ -4,85 +4,59 @@ namespace System.IO.Abstractions { [Serializable] - internal class FileStreamFactory : IFileStreamFactory + internal sealed class FileStreamFactory : IFileStreamFactory { public Stream Create(string path, FileMode mode) - { - return new FileStream(path, mode); - } - + => new FileStream(path, mode); + public Stream Create(string path, FileMode mode, FileAccess access) - { - return new FileStream(path, mode, access); - } + => new FileStream(path, mode, access); public Stream Create(string path, FileMode mode, FileAccess access, FileShare share) - { - return new FileStream(path, mode, access, share); - } + => new FileStream(path, mode, access, share); public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) - { - return new FileStream(path, mode, access, share, bufferSize); - } - + => new FileStream(path, mode, access, share, bufferSize); + public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) - { - return new FileStream(path, mode, access, share, bufferSize, options); - } + => new FileStream(path, mode, access, share, bufferSize, options); public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) - { - return new FileStream(path, mode, access, share, bufferSize, useAsync); - } + => new FileStream(path, mode, access, share, bufferSize, useAsync); + + public Stream Create(SafeFileHandle handle, FileAccess access) + => new FileStream(handle, access); + + public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize) + => new FileStream(handle, access, bufferSize); + + public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) + => new FileStream(handle, access, bufferSize, isAsync); +#if NET40 public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity) - { - return new FileStream(path, mode, rights, share, bufferSize, options, fileSecurity); - } + => new FileStream(path, mode, rights, share, bufferSize, options, fileSecurity); public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options) - { - return new FileStream(path, mode, rights, share, bufferSize, options); - } + => new FileStream(path, mode, rights, share, bufferSize, options); +#endif +#if NET40 || NETSTANDARD_20 [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access) - { - return new FileStream(handle, access); - } + => new FileStream(handle, access); [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle) - { - return new FileStream(handle, access, ownsHandle); - } + => new FileStream(handle, access, ownsHandle); [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize) - { - return new FileStream(handle, access, ownsHandle, bufferSize); - } + => new FileStream(handle, access, ownsHandle, bufferSize); [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync) - { - return new FileStream(handle, access, ownsHandle, bufferSize, isAsync); - } - - public Stream Create(SafeFileHandle handle, FileAccess access) - { - return new FileStream(handle, access); - } - - public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize) - { - return new FileStream(handle, access, bufferSize); - } - - public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) - { - return new FileStream(handle, access, bufferSize, isAsync); - } + => new FileStream(handle, access, ownsHandle, bufferSize, isAsync); +#endif } } \ No newline at end of file diff --git a/System.IO.Abstractions/IFileStreamFactory.cs b/System.IO.Abstractions/IFileStreamFactory.cs index 6b5cd04c8..3114c83d8 100644 --- a/System.IO.Abstractions/IFileStreamFactory.cs +++ b/System.IO.Abstractions/IFileStreamFactory.cs @@ -6,14 +6,30 @@ namespace System.IO.Abstractions public interface IFileStreamFactory { Stream Create(string path, FileMode mode); + Stream Create(string path, FileMode mode, FileAccess access); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options); + Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync); + + Stream Create(SafeFileHandle handle, FileAccess access); + + Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize); + + Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync); + +#if NET40 Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity); + Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options); +#endif +#if NET40 || NETSTANDARD_20 [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] Stream Create(IntPtr handle, FileAccess access); @@ -25,9 +41,6 @@ public interface IFileStreamFactory [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync); - - Stream Create(SafeFileHandle handle, FileAccess access); - Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize); - Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync); +#endif } } \ No newline at end of file From 46ba25d34a458a6dafbadf48d247d6dd59ad6430 Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Tue, 10 Jul 2018 22:45:37 +0200 Subject: [PATCH 3/7] Adapt MockFileStreamFactory to MockFileStream changes --- TestingHelpers/MockFileStreamFactory.cs | 46 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/TestingHelpers/MockFileStreamFactory.cs b/TestingHelpers/MockFileStreamFactory.cs index 89161e954..67c4ecb52 100644 --- a/TestingHelpers/MockFileStreamFactory.cs +++ b/TestingHelpers/MockFileStreamFactory.cs @@ -20,81 +20,97 @@ public MockFileStreamFactory(IMockFileDataAccessor mockFileSystem) public Stream Create(string path, FileMode mode) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); } public Stream Create(string path, FileMode mode, FileAccess access) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); } public Stream Create(string path, FileMode mode, FileAccess access, FileShare share) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); } public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); } public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); } public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); } public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); } public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options) { - return new MockFileStream(mockFileSystem, path, mode == FileMode.Append); + return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); } [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access) { - return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); } [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle) { - return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); } [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize) { - return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); } [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync) { - return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); } public Stream Create(SafeFileHandle handle, FileAccess access) { - return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); } public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize) { - return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); } public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) { - return new MockFileStream(mockFileSystem, handle.ToString(), forAppend: true); + return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); + } + + private static MockFileStream.StreamType GetStreamType(FileMode mode, FileAccess access = FileAccess.ReadWrite) + { + if (access == FileAccess.Read) + { + return MockFileStream.StreamType.READ; + } + else if (mode == FileMode.Append) + { + return MockFileStream.StreamType.APPEND; + } + else + { + return MockFileStream.StreamType.WRITE; + } } } } \ No newline at end of file From 520db27e14f7d52cf25f8bd14f77284ce7e863dd Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Fri, 13 Jul 2018 23:30:11 +0200 Subject: [PATCH 4/7] Add missing ifdefs for MockFileStreamFactory --- TestingHelpers/MockFileStreamFactory.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TestingHelpers/MockFileStreamFactory.cs b/TestingHelpers/MockFileStreamFactory.cs index 67c4ecb52..9e114177a 100644 --- a/TestingHelpers/MockFileStreamFactory.cs +++ b/TestingHelpers/MockFileStreamFactory.cs @@ -48,6 +48,7 @@ public Stream Create(string path, FileMode mode, FileAccess access, FileShare sh return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); } +#if NET40 public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity) { return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); @@ -57,7 +58,9 @@ public Stream Create(string path, FileMode mode, FileSystemRights rights, FileSh { return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); } +#endif +#if NET40 || NETSTANDARD_20 [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access) { @@ -81,6 +84,7 @@ public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int buff { return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); } +#endif public Stream Create(SafeFileHandle handle, FileAccess access) { From 84574a86f5de0009cac6ed38558cafa065bf5c01 Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Fri, 13 Jul 2018 23:50:52 +0200 Subject: [PATCH 5/7] Use expression bodies --- TestingHelpers/MockFileStreamFactory.cs | 69 ++++++------------------- 1 file changed, 16 insertions(+), 53 deletions(-) diff --git a/TestingHelpers/MockFileStreamFactory.cs b/TestingHelpers/MockFileStreamFactory.cs index 9e114177a..d6da9a855 100644 --- a/TestingHelpers/MockFileStreamFactory.cs +++ b/TestingHelpers/MockFileStreamFactory.cs @@ -9,97 +9,60 @@ public class MockFileStreamFactory : IFileStreamFactory private readonly IMockFileDataAccessor mockFileSystem; public MockFileStreamFactory(IMockFileDataAccessor mockFileSystem) - { - if (mockFileSystem == null) - { - throw new ArgumentNullException("mockFileSystem"); - } - - this.mockFileSystem = mockFileSystem; - } + => this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem)); public Stream Create(string path, FileMode mode) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode)); public Stream Create(string path, FileMode mode, FileAccess access) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); public Stream Create(string path, FileMode mode, FileAccess access, FileShare share) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); public Stream Create(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode, access)); #if NET40 public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode)); public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options) - { - return new MockFileStream(mockFileSystem, path, GetStreamType(mode)); - } + => new MockFileStream(mockFileSystem, path, GetStreamType(mode)); #endif #if NET40 || NETSTANDARD_20 [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access) - { - return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); - } + => new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle) - { - return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); - } + => new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize) - { - return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); - } + => new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); [Obsolete("This method has been deprecated. Please use new Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202")] public Stream Create(IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync) - { - return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); - } + => new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); #endif public Stream Create(SafeFileHandle handle, FileAccess access) - { - return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); - } + => new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize) - { - return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); - } + => new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); public Stream Create(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) - { - return new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); - } + => new MockFileStream(mockFileSystem, handle.ToString(), GetStreamType(FileMode.Append, access)); private static MockFileStream.StreamType GetStreamType(FileMode mode, FileAccess access = FileAccess.ReadWrite) { From 2ed93bc7873fac9354ab1fbaa9f55281749590af Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sat, 14 Jul 2018 00:03:08 +0200 Subject: [PATCH 6/7] Delete unnecessary nuspec file --- .../System.IO.Abstractions.nuspec | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 System.IO.Abstractions/System.IO.Abstractions.nuspec diff --git a/System.IO.Abstractions/System.IO.Abstractions.nuspec b/System.IO.Abstractions/System.IO.Abstractions.nuspec deleted file mode 100644 index 306cece32..000000000 --- a/System.IO.Abstractions/System.IO.Abstractions.nuspec +++ /dev/null @@ -1,14 +0,0 @@ - - - - System.IO.Abstractions - $version$ - Tatham Oddie & friends - Tatham Oddie & friends - https://github.com/System-IO-Abstractions/System.IO.Abstractions/blob/master/License.txt - https://github.com/System-IO-Abstractions/System.IO.Abstractions - false - Just like System.Web.Abstractions, but for System.IO. Yay for testable IO access! Be sure to check out the System.IO.Abstractions.TestingHelpers package too. - testing - - \ No newline at end of file From a6dc64dd1b602fddf91ab7bc1961e27ab7138e57 Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sat, 14 Jul 2018 23:41:35 +0200 Subject: [PATCH 7/7] Simplify MockFileStreamFactory tests --- .../MockFileStreamFactoryTests.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/TestHelpers.Tests/MockFileStreamFactoryTests.cs b/TestHelpers.Tests/MockFileStreamFactoryTests.cs index 649f6887b..e11b6277d 100644 --- a/TestHelpers.Tests/MockFileStreamFactoryTests.cs +++ b/TestHelpers.Tests/MockFileStreamFactoryTests.cs @@ -9,19 +9,18 @@ public class MockFileStreamFactoryTests [Test] [TestCase(FileMode.Create)] [TestCase(FileMode.Append)] - public void MockFileStreamFactory_Create_string_FileMode__ShouldReturnStreamForExistingFile(FileMode fileMode) + public void MockFileStreamFactory_CreateForExistingFile_ShouldReturnStream(FileMode fileMode) { // Arrange var fileSystem = new MockFileSystem(new Dictionary { - { @"c:\a.txt", new MockFileData("Demo text content") }, - { @"c:\a\b\c.txt", new MockFileData("Demo text content") }, + { @"c:\existing.txt", MockFileData.NullObject } }); var fileStreamFactory = new MockFileStreamFactory(fileSystem); // Act - var result = fileStreamFactory.Create(@"c:\a.txt", fileMode); + var result = fileStreamFactory.Create(@"c:\existing.txt", fileMode); // Assert Assert.IsNotNull(result); @@ -30,19 +29,14 @@ public void MockFileStreamFactory_Create_string_FileMode__ShouldReturnStreamForE [Test] [TestCase(FileMode.Create)] [TestCase(FileMode.Append)] - public void MockFileStreamFactory_Create_string_FileMode__ShouldReturnStreamForNonExistingFile(FileMode fileMode) + public void MockFileStreamFactory_CreateForNonExistingFile_ShouldReturnStream(FileMode fileMode) { // Arrange - var fileSystem = new MockFileSystem(new Dictionary - { - { @"c:\a.txt", new MockFileData("Demo text content") }, - { @"c:\a\b\c.txt", new MockFileData("Demo text content") }, - }); - + var fileSystem = new MockFileSystem(); var fileStreamFactory = new MockFileStreamFactory(fileSystem); // Act - var result = fileStreamFactory.Create(@"c:\foo.txt", fileMode); + var result = fileStreamFactory.Create(@"c:\not_existing.txt", fileMode); // Assert Assert.IsNotNull(result);