Skip to content

Commit

Permalink
Made FileSystemWatcher on MockFileSystem mockable (#409)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
rkoeninger authored and fgreinacher committed Dec 1, 2018
1 parent ec1b673 commit ccc8000
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 88 deletions.
35 changes: 35 additions & 0 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -304,5 +305,39 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir

Assert.IsTrue(actualCurrentDirectory.Exists);
}

[Test]
public void MockFileSystem_FileSystemWatcher_ShouldBeAssignable()
{
var path = XFS.Path(@"C:\root");
var fileSystem = new MockFileSystem {FileSystemWatcher = new TestFileSystemWatcherFactory()};
var watcher = fileSystem.FileSystemWatcher.FromPath(path);
Assert.AreEqual(path, watcher.Path);
}

private class TestFileSystemWatcherFactory : IFileSystemWatcherFactory
{
public FileSystemWatcherBase CreateNew() => new TestFileSystemWatcher(null);
public FileSystemWatcherBase FromPath(string path) => new TestFileSystemWatcher(path);
}

private class TestFileSystemWatcher : FileSystemWatcherBase
{
public TestFileSystemWatcher(string path) => Path = path;
public override string Path { get; set; }
public override bool IncludeSubdirectories { get; set; }
public override bool EnableRaisingEvents { get; set; }
public override string Filter { get; set; }
public override int InternalBufferSize { get; set; }
public override NotifyFilters NotifyFilter { get; set; }
#if NET40
public override ISite Site { get; set; }
public override ISynchronizeInvoke SynchronizingObject { get; set; }
public override void BeginInit() {}
public override void EndInit() {}
#endif
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) => default(WaitForChangedResult);
public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) => default(WaitForChangedResult);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
[TestFixture]
public class MockFileSystemWatcherFactoryTests
{
[Test]
public void MockFileSystemWatcherFactory_CreateNew_ShouldReturnNonNullMockWatcher()
public void MockFileSystemWatcherFactory_CreateNew_ShouldThrowNotImplementedException()
{
// Arrange
var factory = new MockFileSystemWatcherFactory();

// Act
var result = factory.CreateNew();

// Assert
Assert.IsNotNull(result);
Assert.Throws<NotImplementedException>(() => factory.CreateNew());
}

[Test]
public void MockFileSystemWatcherFactory_FromPath_ShouldReturnNonNullMockWatcher()
public void MockFileSystemWatcherFactory_FromPath_ShouldThrowNotImplementedException()
{
// Arrange
var path = XFS.Path(@"y:\test");
var factory = new MockFileSystemWatcherFactory();

// Act
var result = factory.FromPath(@"y:\test");

// Assert
Assert.IsNotNull(result);
}

[Test]
public void MockFileSystemWatcherFactory_FromPath_ShouldReturnWatcherForSpecifiedPath()
{
// Arrange
const string path = @"z:\test";
var factory = new MockFileSystemWatcherFactory();

// Act
var result = factory.FromPath(path);

// Assert
Assert.AreEqual(path, result.Path);
Assert.Throws<NotImplementedException>(() => factory.FromPath(path));
}
}
}
2 changes: 1 addition & 1 deletion System.IO.Abstractions.TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir

public IDriveInfoFactory DriveInfo { get; }

public IFileSystemWatcherFactory FileSystemWatcher { get; }
public IFileSystemWatcherFactory FileSystemWatcher { get; set; }

public IFileSystem FileSystem => this;

Expand Down
38 changes: 0 additions & 38 deletions System.IO.Abstractions.TestingHelpers/MockFileSystemWatcher.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.IO.Abstractions.TestingHelpers
namespace System.IO.Abstractions.TestingHelpers
{
[Serializable]
public class MockFileSystemWatcherFactory : IFileSystemWatcherFactory
{
public FileSystemWatcherBase CreateNew()
{
return new MockFileSystemWatcher();
}
public FileSystemWatcherBase CreateNew() =>
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));

public FileSystemWatcherBase FromPath(string path)
{
return new MockFileSystemWatcher {Path = path};
}
public FileSystemWatcherBase FromPath(string path) =>
throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@
<data name="COULD_NOT_FIND_FILE_EXCEPTION" xml:space="preserve">
<value>Could not find file '{0}'.</value>
</data>
</root>
<data name="FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION" xml:space="preserve">
<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>
</data>
</root>

0 comments on commit ccc8000

Please sign in to comment.