Skip to content

Commit 05486f7

Browse files
authored
feat: allow overriding properties of (Mock)FileSystem (#782)
BREAKING CHANGES: - (Mock)FileSystem are now derived from a new public class FileSystemBase for symmetry with other types - The setter for MockFileSystem.FileSystemWatcher has been removed. It is now virtual and should be overriden if required. Fixes #757, #778 and #776
1 parent 5539592 commit 05486f7

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Globalization;
32
using System.Linq;
43
using System.Reflection;
54

@@ -9,7 +8,7 @@ namespace System.IO.Abstractions.TestingHelpers
98

109
/// <inheritdoc />
1110
[Serializable]
12-
public class MockFileSystem : IFileSystem, IMockFileDataAccessor
11+
public class MockFileSystem : FileSystemBase, IMockFileDataAccessor
1312
{
1413
private const string DEFAULT_CURRENT_DIRECTORY = @"C:\";
1514
private const string TEMP_DIRECTORY = @"C:\temp";
@@ -69,21 +68,21 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
6968
/// <inheritdoc />
7069
public StringOperations StringOperations { get; }
7170
/// <inheritdoc />
72-
public IFile File { get; }
71+
public override IFile File { get; }
7372
/// <inheritdoc />
74-
public IDirectory Directory { get; }
73+
public override IDirectory Directory { get; }
7574
/// <inheritdoc />
76-
public IFileInfoFactory FileInfo { get; }
75+
public override IFileInfoFactory FileInfo { get; }
7776
/// <inheritdoc />
78-
public IFileStreamFactory FileStream { get; }
77+
public override IFileStreamFactory FileStream { get; }
7978
/// <inheritdoc />
80-
public IPath Path { get; }
79+
public override IPath Path { get; }
8180
/// <inheritdoc />
82-
public IDirectoryInfoFactory DirectoryInfo { get; }
81+
public override IDirectoryInfoFactory DirectoryInfo { get; }
8382
/// <inheritdoc />
84-
public IDriveInfoFactory DriveInfo { get; }
83+
public override IDriveInfoFactory DriveInfo { get; }
8584
/// <inheritdoc />
86-
public IFileSystemWatcherFactory FileSystemWatcher { get; set; }
85+
public override IFileSystemWatcherFactory FileSystemWatcher { get; }
8786
/// <inheritdoc />
8887
public IFileSystem FileSystem => this;
8988
/// <inheritdoc />

src/System.IO.Abstractions/FileSystem.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
/// <inheritdoc />
44
[Serializable]
5-
public class FileSystem : IFileSystem
5+
public class FileSystem : FileSystemBase
66
{
77
/// <inheritdoc />
88
public FileSystem()
@@ -18,27 +18,27 @@ public FileSystem()
1818
}
1919

2020
/// <inheritdoc />
21-
public IDirectory Directory { get; }
21+
public override IDirectory Directory { get; }
2222

2323
/// <inheritdoc />
24-
public IFile File { get; }
24+
public override IFile File { get; }
2525

2626
/// <inheritdoc />
27-
public IFileInfoFactory FileInfo { get; }
27+
public override IFileInfoFactory FileInfo { get; }
2828

2929
/// <inheritdoc />
30-
public IFileStreamFactory FileStream { get; }
30+
public override IFileStreamFactory FileStream { get; }
3131

3232
/// <inheritdoc />
33-
public IPath Path { get; }
33+
public override IPath Path { get; }
3434

3535
/// <inheritdoc />
36-
public IDirectoryInfoFactory DirectoryInfo { get; }
36+
public override IDirectoryInfoFactory DirectoryInfo { get; }
3737

3838
/// <inheritdoc />
39-
public IDriveInfoFactory DriveInfo { get; }
39+
public override IDriveInfoFactory DriveInfo { get; }
4040

4141
/// <inheritdoc />
42-
public IFileSystemWatcherFactory FileSystemWatcher { get; }
42+
public override IFileSystemWatcherFactory FileSystemWatcher { get; }
4343
}
4444
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace System.IO.Abstractions
2+
{
3+
/// <inheritdoc />
4+
[Serializable]
5+
public abstract class FileSystemBase : IFileSystem
6+
{
7+
/// <inheritdoc />
8+
public abstract IDirectory Directory { get; }
9+
10+
/// <inheritdoc />
11+
public abstract IFile File { get; }
12+
13+
/// <inheritdoc />
14+
public abstract IFileInfoFactory FileInfo { get; }
15+
16+
/// <inheritdoc />
17+
public abstract IFileStreamFactory FileStream { get; }
18+
19+
/// <inheritdoc />
20+
public abstract IPath Path { get; }
21+
22+
/// <inheritdoc />
23+
public abstract IDirectoryInfoFactory DirectoryInfo { get; }
24+
25+
/// <inheritdoc />
26+
public abstract IDriveInfoFactory DriveInfo { get; }
27+
28+
/// <inheritdoc />
29+
public abstract IFileSystemWatcherFactory FileSystemWatcher { get; }
30+
}
31+
}

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,23 +393,24 @@ public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()
393393
}
394394

395395
[Test]
396-
public void MockFileSystem_FileSystemWatcher_PathShouldBeAssignable()
396+
public void MockFileSystem_FileSystemWatcher_Can_Be_Overridden()
397397
{
398398
var path = XFS.Path(@"C:\root");
399-
var fileSystem = new MockFileSystem { FileSystemWatcher = new TestFileSystemWatcherFactory() };
399+
var fileSystem = new TestFileSystem(new TestFileSystemWatcherFactory());
400400
var watcher = fileSystem.FileSystemWatcher.CreateNew(path);
401401
Assert.AreEqual(path, watcher.Path);
402402
}
403403

404-
[Test]
405-
public void MockFileSystem_FileSystemWatcher_PathAndFilterShouldBeAssignable()
404+
private class TestFileSystem : MockFileSystem
406405
{
407-
var path = XFS.Path(@"C:\root");
408-
var filter = "*.txt";
409-
var fileSystem = new MockFileSystem { FileSystemWatcher = new TestFileSystemWatcherFactory() };
410-
var watcher = fileSystem.FileSystemWatcher.CreateNew(path, filter);
411-
Assert.AreEqual(path, watcher.Path);
412-
Assert.AreEqual(filter, watcher.Filter);
406+
private readonly IFileSystemWatcherFactory fileSystemWatcherFactory;
407+
408+
public TestFileSystem(IFileSystemWatcherFactory fileSystemWatcherFactory)
409+
{
410+
this.fileSystemWatcherFactory = fileSystemWatcherFactory;
411+
}
412+
413+
public override IFileSystemWatcherFactory FileSystemWatcher => fileSystemWatcherFactory;
413414
}
414415

415416
private class TestFileSystemWatcherFactory : IFileSystemWatcherFactory

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "15.0",
3+
"version": "16.0",
44
"assemblyVersion": {
55
"precision": "major"
66
},

0 commit comments

Comments
 (0)