Skip to content

Commit cc29ac2

Browse files
authored
Add support for passing a filter to IFileSystemWatcherFactory.CreateNew (#585)
Fixes #577 BREAKING CHANGE: IFileSystemWatcherFactory.FromPath is removed. Use IFileSystemWatcherFactory.CreateNew instead.
1 parent 7337796 commit cc29ac2

6 files changed

Lines changed: 64 additions & 22 deletions

File tree

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,23 +341,43 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir
341341
}
342342

343343
[Test]
344-
public void MockFileSystem_FileSystemWatcher_ShouldBeAssignable()
344+
public void MockFileSystem_FileSystemWatcher_PathShouldBeAssignable()
345345
{
346346
var path = XFS.Path(@"C:\root");
347347
var fileSystem = new MockFileSystem { FileSystemWatcher = new TestFileSystemWatcherFactory() };
348-
var watcher = fileSystem.FileSystemWatcher.FromPath(path);
348+
var watcher = fileSystem.FileSystemWatcher.CreateNew(path);
349349
Assert.AreEqual(path, watcher.Path);
350350
}
351351

352+
[Test]
353+
public void MockFileSystem_FileSystemWatcher_PathAndFilterShouldBeAssignable()
354+
{
355+
var path = XFS.Path(@"C:\root");
356+
var filter = "*.txt";
357+
var fileSystem = new MockFileSystem { FileSystemWatcher = new TestFileSystemWatcherFactory() };
358+
var watcher = fileSystem.FileSystemWatcher.CreateNew(path, filter);
359+
Assert.AreEqual(path, watcher.Path);
360+
Assert.AreEqual(filter, watcher.Filter);
361+
}
362+
352363
private class TestFileSystemWatcherFactory : IFileSystemWatcherFactory
353364
{
354365
public IFileSystemWatcher CreateNew() => new TestFileSystemWatcher(null);
366+
public IFileSystemWatcher CreateNew(string path) => new TestFileSystemWatcher(path);
367+
public IFileSystemWatcher CreateNew(string path, string filter) => new TestFileSystemWatcher(path, filter);
355368
public IFileSystemWatcher FromPath(string path) => new TestFileSystemWatcher(path);
356369
}
357370

358371
private class TestFileSystemWatcher : FileSystemWatcherBase
359372
{
360373
public TestFileSystemWatcher(string path) => Path = path;
374+
375+
public TestFileSystemWatcher(string path, string filter)
376+
{
377+
Path = path;
378+
Filter = filter;
379+
}
380+
361381
public override string Path { get; set; }
362382
public override bool IncludeSubdirectories { get; set; }
363383
public override bool EnableRaisingEvents { get; set; }

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ public void MockFileSystemWatcherFactory_CreateNew_ShouldThrowNotImplementedExce
1313
Assert.Throws<NotImplementedException>(() => factory.CreateNew());
1414
}
1515

16+
[Test]
17+
public void MockFileSystemWatcherFactory_CreateNewWithPath_ShouldThrowNotImplementedException()
18+
{
19+
var path = XFS.Path(@"y:\test");
20+
var factory = new MockFileSystemWatcherFactory();
21+
Assert.Throws<NotImplementedException>(() => factory.CreateNew(path));
22+
}
23+
24+
[Test]
25+
public void MockFileSystemWatcherFactory_CreateNewWithPathAndFilter_ShouldThrowNotImplementedException()
26+
{
27+
var path = XFS.Path(@"y:\test");
28+
var filter = "*.txt";
29+
var factory = new MockFileSystemWatcherFactory();
30+
Assert.Throws<NotImplementedException>(() => factory.CreateNew(path, filter));
31+
}
32+
1633
[Test]
1734
public void MockFileSystemWatcherFactory_FromPath_ShouldThrowNotImplementedException()
1835
{

System.IO.Abstractions.TestingHelpers/MockFileSystemWatcherFactory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ public class MockFileSystemWatcherFactory : IFileSystemWatcherFactory
66
public IFileSystemWatcher CreateNew() =>
77
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
88

9+
public IFileSystemWatcher CreateNew(string path) =>
10+
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
11+
12+
public IFileSystemWatcher CreateNew(string path, string filter) =>
13+
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
14+
915
public IFileSystemWatcher FromPath(string path) =>
1016
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
1117
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
6-
namespace System.IO.Abstractions
1+
namespace System.IO.Abstractions
72
{
83
[Serializable]
94
public class FileSystemWatcherFactory : IFileSystemWatcherFactory
@@ -13,9 +8,10 @@ public IFileSystemWatcher CreateNew()
138
return new FileSystemWatcherWrapper();
149
}
1510

16-
public IFileSystemWatcher FromPath(string path)
17-
{
18-
return new FileSystemWatcherWrapper(path);
19-
}
11+
public IFileSystemWatcher CreateNew(string path) =>
12+
new FileSystemWatcherWrapper(path);
13+
14+
public IFileSystemWatcher CreateNew(string path, string filter)
15+
=> new FileSystemWatcherWrapper(path, filter);
2016
}
2117
}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
6-
namespace System.IO.Abstractions
1+
namespace System.IO.Abstractions
72
{
83
public interface IFileSystemWatcherFactory
94
{
@@ -14,10 +9,18 @@ public interface IFileSystemWatcherFactory
149
IFileSystemWatcher CreateNew();
1510

1611
/// <summary>
17-
/// Initializes a new instance of the <see cref="FileSystemWatcherBase"/> class, which acts as a wrapper for a FileSystemWatcher
12+
/// Initializes a new instance of the <see cref="FileSystemWatcherBase"/> class, given the specified directory to monitor, which acts as a wrapper for a FileSystemWatcher
13+
/// </summary>
14+
/// <param name="path">The directory to monitor, in standard or Universal Naming Convention (UNC) notation.</param>
15+
/// <returns></returns>
16+
IFileSystemWatcher CreateNew(string path);
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="FileSystemWatcherBase"/> class, given the specified directory and type of files to monitor, which acts as a wrapper for a FileSystemWatcher
1820
/// </summary>
19-
/// <param name="path">Path to generate the FileSystemWatcherBase for</param>
21+
/// <param name="path">The directory to monitor, in standard or Universal Naming Convention (UNC) notation.</param>
22+
/// <param name="filter">The type of files to watch. For example, "*.txt" watches for changes to all text files.</param>
2023
/// <returns></returns>
21-
IFileSystemWatcher FromPath(string path);
24+
IFileSystemWatcher CreateNew(string path, string filter);
2225
}
2326
}

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": "10.0",
3+
"version": "11.0",
44
"assemblyVersion": {
55
"precision": "major"
66
},

0 commit comments

Comments
 (0)