我正在尝试创建一个类来监视Linux上USB设备的到来和移除。在Linux上,USB设备表示为/dev/bus/usb
下的设备文件,这些文件是为响应这些事件而创建/删除的。
跟踪这些事件的最佳方法似乎是使用FileSystemWatcher
。为了使该类可测试,我在构建期间使用了System.IO.Abstractions
并将IFileSystem
的实例注入到该类中。我要创建的行为类似于FileSystemWatcher
,但监视对注入的IFileSystem
的更改,而不是直接监视实际的文件系统。
从FileSystemWatcherBase
看FileSystemWatcherWrapper
和System.IO.Abstractions
,我不确定如何执行此操作。目前,我有这个(我知道这是错误的):
public DevMonitor(
[NotNull] IFileSystem fileSystem,
[NotNull] IDeviceFileParser deviceFileParser,
[NotNull] ILogger logger,
[NotNull] string devDirectoryPath = DefaultDevDirectoryPath)
{
Raise.ArgumentNullException.IfIsNull(logger, nameof(logger));
Raise.ArgumentNullException.IfIsNull(devDirectoryPath, nameof(devDirectoryPath));
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
_deviceFileParser = deviceFileParser ?? throw new ArgumentNullException(nameof(deviceFileParser));
_logger = logger.ForContext<DevMonitor>();
_watcher = new FileSystemWatcherWrapper(devDirectoryPath);
}
最佳答案
鉴于System.IO.Abstractions
似乎还不支持这一点,我同意了:
我定义了扩展IWatchableFileSystem
的IFileSystem
接口:
/// <summary>
/// Represents a(n) <see cref="IFileSystem" /> that can be watched for changes.
/// </summary>
public interface IWatchableFileSystem : IFileSystem
{
/// <summary>
/// Creates a <c>FileSystemWatcher</c> that can be used to monitor changes to this file system.
/// </summary>
/// <returns>A <c>FileSystemWatcher</c>.</returns>
FileSystemWatcherBase CreateWatcher();
}
出于生产目的,我将其实现为
WatchableFileSystem
:/// <inheritdoc />
public sealed class WatchableFileSystem : IWatchableFileSystem
{
private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="WatchableFileSystem" /> class.
/// </summary>
public WatchableFileSystem() => _fileSystem = new FileSystem();
/// <inheritdoc />
public DirectoryBase Directory => _fileSystem.Directory;
/// <inheritdoc />
public IDirectoryInfoFactory DirectoryInfo => _fileSystem.DirectoryInfo;
/// <inheritdoc />
public IDriveInfoFactory DriveInfo => _fileSystem.DriveInfo;
/// <inheritdoc />
public FileBase File => _fileSystem.File;
/// <inheritdoc />
public IFileInfoFactory FileInfo => _fileSystem.FileInfo;
/// <inheritdoc />
public PathBase Path => _fileSystem.Path;
/// <inheritdoc />
public FileSystemWatcherBase CreateWatcher() => new FileSystemWatcher();
}
在我的单元测试类中,我将其实现为
MockWatchableFileSystem
,它公开了MockFileSystem
和Mock<FileSystemWatcherBase>
作为可用于在测试中进行安排和断言的属性:private class MockWatchableFileSystem : IWatchableFileSystem
{
/// <inheritdoc />
public MockWatchableFileSystem()
{
Watcher = new Mock<FileSystemWatcherBase>();
AsMock = new MockFileSystem();
AsMock.AddDirectory("/dev/bus/usb");
Watcher.SetupAllProperties();
}
public MockFileSystem AsMock { get; }
/// <inheritdoc />
public DirectoryBase Directory => AsMock.Directory;
/// <inheritdoc />
public IDirectoryInfoFactory DirectoryInfo => AsMock.DirectoryInfo;
/// <inheritdoc />
public IDriveInfoFactory DriveInfo => AsMock.DriveInfo;
/// <inheritdoc />
public FileBase File => AsMock.File;
/// <inheritdoc />
public IFileInfoFactory FileInfo => AsMock.FileInfo;
/// <inheritdoc />
public PathBase Path => AsMock.Path;
public Mock<FileSystemWatcherBase> Watcher { get; }
/// <inheritdoc />
public FileSystemWatcherBase CreateWatcher() => Watcher.Object;
}
最后,在我的客户类中,我可以做:
public DevMonitor(
[NotNull] IWatchableFileSystem fileSystem,
[NotNull] IDeviceFileParser deviceFileParser,
[NotNull] ILogger logger,
[NotNull] string devDirectoryPath = DefaultDevDirectoryPath)
{
// ...
_watcher = fileSystem.CreateWatcher();
_watcher.IncludeSubdirectories = true;
_watcher.EnableRaisingEvents = true;
_watcher.Path = devDirectoryPath;
}
在测试期间,客户端会得到一个
IWatchableFileSystem
,该MockFileSystem
包装了FileSystemWatcherBase
并返回了IWatchableFileSystem
的模拟实例。在生产期间,它会得到一个FileSystem
,该FileSystemWatcher
包裹并生成的唯一实例。关于c# - 如何使用System.IO.Abstractions中的FileSystemWatcher监视模拟文件系统?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47031711/