Skip to content

Commit

Permalink
fix: correct the NotifyFilters in FileSystemWatcherMock when crea…
Browse files Browse the repository at this point in the history
…ting files (#584)

Currently `NotifyFilters.DirectoryName` is used instead of the correct `NotifyFilters.FileName`.
Also add tests to verify the behaviour.
  • Loading branch information
vbreuss committed Apr 29, 2024
1 parent 401412b commit e47ab51
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Source/Testably.Abstractions.Testing/Storage/InMemoryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,12 @@ public bool DeleteContainer(IStorageLocation location, bool recursive = false)
}
}

NotifyFilters notifyFilters =
container.Type == FileSystemTypes.Directory
? NotifyFilters.DirectoryName
: NotifyFilters.FileName;
ChangeDescription fileSystemChange =
_fileSystem.ChangeHandler.NotifyPendingChange(WatcherChangeTypes.Deleted,
_fileSystem.ChangeHandler.NotifyPendingChange(
WatcherChangeTypes.Deleted,
container.Type,
notifyFilters, location);
ToNotifyFilters(container.Type),
location);

using (container.RequestAccess(FileAccess.Write, FileShare.ReadWrite,
deleteAccess: true))
Expand Down Expand Up @@ -342,7 +340,8 @@ public IEnumerable<IStorageDrive> GetDrives()
fileSystemChange = _fileSystem.ChangeHandler.NotifyPendingChange(
WatcherChangeTypes.Created,
container.Type,
NotifyFilters.DirectoryName, location);
ToNotifyFilters(container.Type),
location);
}
return container;
Expand Down Expand Up @@ -527,7 +526,8 @@ public IEnumerable<IStorageDrive> GetDrives()
fileSystemChange = _fileSystem.ChangeHandler.NotifyPendingChange(
WatcherChangeTypes.Created,
container.Type,
NotifyFilters.DirectoryName, location);
ToNotifyFilters(container.Type),
location);
}
CheckAndAdjustParentDirectoryTimes(location);
Expand Down Expand Up @@ -618,7 +618,8 @@ private void CreateParents(MockFileSystem fileSystem, IStorageLocation location)
fileSystem.ChangeHandler.NotifyPendingChange(
WatcherChangeTypes.Created,
container.Type,
NotifyFilters.DirectoryName, parentLocation);
ToNotifyFilters(container.Type),
parentLocation);
return container;
},
(_, f) => f);
Expand Down Expand Up @@ -793,6 +794,11 @@ private void CreateParents(MockFileSystem fileSystem, IStorageLocation location)
}
}

private static NotifyFilters ToNotifyFilters(FileSystemTypes type)
=> type == FileSystemTypes.Directory
? NotifyFilters.DirectoryName
: NotifyFilters.FileName;

private static void ValidateExpression(string expression)
{
if (expression.Contains('\0', StringComparison.Ordinal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,82 @@ public void NotifyFilter_CreateDirectory_ShouldNotNotifyOnOtherFilters(string pa
result.Name.Should().Be(FileSystem.Path.GetFileName(path));
}

[SkippableTheory]
[AutoData]
public void NotifyFilter_CreateFile_ShouldNotNotifyOnOtherFilters(string path)
{
SkipIfLongRunningTestsShouldBeSkipped();

FileSystem.Initialize();
FileSystemEventArgs? result = null;
using ManualResetEventSlim ms = new();
using IFileSystemWatcher fileSystemWatcher =
FileSystem.FileSystemWatcher.New(BasePath);
fileSystemWatcher.Created += (_, eventArgs) =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
result = eventArgs;
ms.Set();
}
catch (ObjectDisposedException)
{
// Ignore any ObjectDisposedException
}
};
fileSystemWatcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.CreationTime |
NotifyFilters.DirectoryName |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.Security |
NotifyFilters.Size;
fileSystemWatcher.EnableRaisingEvents = true;

FileSystem.File.WriteAllText(path, "foo");

ms.Wait(ExpectTimeout).Should().BeFalse();
result.Should().BeNull();
}

[SkippableTheory]
[InlineAutoData(NotifyFilters.FileName)]
public void NotifyFilter_CreateFile_ShouldTriggerCreatedEventOnNotifyFilters(
NotifyFilters notifyFilter, string path)
{
SkipIfLongRunningTestsShouldBeSkipped();

FileSystem.Initialize();
FileSystemEventArgs? result = null;
using ManualResetEventSlim ms = new();
using IFileSystemWatcher fileSystemWatcher =
FileSystem.FileSystemWatcher.New(BasePath);
fileSystemWatcher.Created += (_, eventArgs) =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
result = eventArgs;
ms.Set();
}
catch (ObjectDisposedException)
{
// Ignore any ObjectDisposedException
}
};
fileSystemWatcher.NotifyFilter = notifyFilter;
fileSystemWatcher.EnableRaisingEvents = true;

FileSystem.File.WriteAllText(path, "foo");

ms.Wait(ExpectSuccess).Should().BeTrue();
result.Should().NotBeNull();
result!.FullPath.Should().Be(FileSystem.Path.GetFullPath(path));
result.ChangeType.Should().Be(WatcherChangeTypes.Created);
result.Name.Should().Be(FileSystem.Path.GetFileName(path));
}

[SkippableTheory]
[AutoData]
public void NotifyFilter_DeleteDirectory_ShouldNotNotifyOnOtherFilters(string path)
Expand Down

0 comments on commit e47ab51

Please sign in to comment.