Skip to content

Commit ccc8000

Browse files
rkoeningerfgreinacher
authored andcommitted
Made FileSystemWatcher on MockFileSystem mockable (#409)
* Added set; to MockFileSystem.FileSystemWatcher * Added FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION to StringResources * Made MockFileSystemWatcherFactory throw NotImplementedException * Removed MockFileSystemWatcher * MockFileSystemWatcherFactoryTests now assert exception thrown * Added MockFileSystem test to assert FileSystemWatcher is assignable * Elaborated on FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION message * Corrected name of MockFileSystemWatcherFactory test * Update System.IO.Abstractions.TestingHelpers/Properties/Resources.resx Co-Authored-By: rkoeninger <rkoeninger@att.net>
1 parent ec1b673 commit ccc8000

File tree

6 files changed

+51
-88
lines changed

6 files changed

+51
-88
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.ComponentModel;
23
using System.Linq;
34
using System.Reflection;
45
using System.Text;
@@ -304,5 +305,39 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir
304305

305306
Assert.IsTrue(actualCurrentDirectory.Exists);
306307
}
308+
309+
[Test]
310+
public void MockFileSystem_FileSystemWatcher_ShouldBeAssignable()
311+
{
312+
var path = XFS.Path(@"C:\root");
313+
var fileSystem = new MockFileSystem {FileSystemWatcher = new TestFileSystemWatcherFactory()};
314+
var watcher = fileSystem.FileSystemWatcher.FromPath(path);
315+
Assert.AreEqual(path, watcher.Path);
316+
}
317+
318+
private class TestFileSystemWatcherFactory : IFileSystemWatcherFactory
319+
{
320+
public FileSystemWatcherBase CreateNew() => new TestFileSystemWatcher(null);
321+
public FileSystemWatcherBase FromPath(string path) => new TestFileSystemWatcher(path);
322+
}
323+
324+
private class TestFileSystemWatcher : FileSystemWatcherBase
325+
{
326+
public TestFileSystemWatcher(string path) => Path = path;
327+
public override string Path { get; set; }
328+
public override bool IncludeSubdirectories { get; set; }
329+
public override bool EnableRaisingEvents { get; set; }
330+
public override string Filter { get; set; }
331+
public override int InternalBufferSize { get; set; }
332+
public override NotifyFilters NotifyFilter { get; set; }
333+
#if NET40
334+
public override ISite Site { get; set; }
335+
public override ISynchronizeInvoke SynchronizingObject { get; set; }
336+
public override void BeginInit() {}
337+
public override void EndInit() {}
338+
#endif
339+
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) => default(WaitForChangedResult);
340+
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) => default(WaitForChangedResult);
341+
}
307342
}
308343
}
Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,24 @@
11
using NUnit.Framework;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
2+
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;
63

74
namespace System.IO.Abstractions.TestingHelpers.Tests
85
{
96
[TestFixture]
107
public class MockFileSystemWatcherFactoryTests
118
{
129
[Test]
13-
public void MockFileSystemWatcherFactory_CreateNew_ShouldReturnNonNullMockWatcher()
10+
public void MockFileSystemWatcherFactory_CreateNew_ShouldThrowNotImplementedException()
1411
{
15-
// Arrange
1612
var factory = new MockFileSystemWatcherFactory();
17-
18-
// Act
19-
var result = factory.CreateNew();
20-
21-
// Assert
22-
Assert.IsNotNull(result);
13+
Assert.Throws<NotImplementedException>(() => factory.CreateNew());
2314
}
2415

2516
[Test]
26-
public void MockFileSystemWatcherFactory_FromPath_ShouldReturnNonNullMockWatcher()
17+
public void MockFileSystemWatcherFactory_FromPath_ShouldThrowNotImplementedException()
2718
{
28-
// Arrange
19+
var path = XFS.Path(@"y:\test");
2920
var factory = new MockFileSystemWatcherFactory();
30-
31-
// Act
32-
var result = factory.FromPath(@"y:\test");
33-
34-
// Assert
35-
Assert.IsNotNull(result);
36-
}
37-
38-
[Test]
39-
public void MockFileSystemWatcherFactory_FromPath_ShouldReturnWatcherForSpecifiedPath()
40-
{
41-
// Arrange
42-
const string path = @"z:\test";
43-
var factory = new MockFileSystemWatcherFactory();
44-
45-
// Act
46-
var result = factory.FromPath(path);
47-
48-
// Assert
49-
Assert.AreEqual(path, result.Path);
21+
Assert.Throws<NotImplementedException>(() => factory.FromPath(path));
5022
}
5123
}
5224
}

System.IO.Abstractions.TestingHelpers/MockFileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
6565

6666
public IDriveInfoFactory DriveInfo { get; }
6767

68-
public IFileSystemWatcherFactory FileSystemWatcher { get; }
68+
public IFileSystemWatcherFactory FileSystemWatcher { get; set; }
6969

7070
public IFileSystem FileSystem => this;
7171

System.IO.Abstractions.TestingHelpers/MockFileSystemWatcher.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
6-
namespace System.IO.Abstractions.TestingHelpers
1+
namespace System.IO.Abstractions.TestingHelpers
72
{
83
[Serializable]
94
public class MockFileSystemWatcherFactory : IFileSystemWatcherFactory
105
{
11-
public FileSystemWatcherBase CreateNew()
12-
{
13-
return new MockFileSystemWatcher();
14-
}
6+
public FileSystemWatcherBase CreateNew() =>
7+
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
158

16-
public FileSystemWatcherBase FromPath(string path)
17-
{
18-
return new MockFileSystemWatcher {Path = path};
19-
}
9+
public FileSystemWatcherBase FromPath(string path) =>
10+
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
2011
}
2112
}

System.IO.Abstractions.TestingHelpers/Properties/Resources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@
144144
<data name="COULD_NOT_FIND_FILE_EXCEPTION" xml:space="preserve">
145145
<value>Could not find file '{0}'.</value>
146146
</data>
147-
</root>
147+
<data name="FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION" xml:space="preserve">
148+
<value>MockFileSystem does not have a built-in FileSystemWatcher implementation. You must provide your own mock or implementation of IFileSystemWatcherFactory and assign it to MockFileSystem.FileSystemWatcher.</value>
149+
</data>
150+
</root>

0 commit comments

Comments
 (0)