Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Trigger correct notifications when moving directories
  • Loading branch information
vbreuss committed Sep 16, 2025
commit d8fc93fe7a9c5b87c081dec12cc82645aaa271d9
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ private bool MatchesFilter(ChangeDescription changeDescription)
if (!changeDescription.Path.StartsWith(fullPath,
_fileSystem.Execute.StringComparisonMode))
{
return false;
return changeDescription.ChangeType == WatcherChangeTypes.Renamed &&
changeDescription.OldPath?.StartsWith(fullPath,
_fileSystem.Execute.StringComparisonMode) == true;
}
}
else if (!string.Equals(
Expand Down Expand Up @@ -536,24 +538,40 @@ private string TransformPathAndName(
}

name = transformedName;
if (!_fileSystem.Path.IsPathRooted(Path))
{
string rootedWatchedPath = _fileSystem.Path.GetFullPath(Path);
if (path?.StartsWith(rootedWatchedPath, _fileSystem.Execute.StringComparisonMode) ==
true)
{
path = _fileSystem.Path.Combine(Path, path.Substring(rootedWatchedPath.Length));
}
}

return path ?? "";
}

private void TriggerRenameNotification(ChangeDescription item)
{
if (_fileSystem.Execute.IsWindows)
{
if (TryMakeRenamedEventArgs(item,
if (!item.Path.StartsWith(Path, _fileSystem.Execute.StringComparisonMode) &&
item.OldPath != null)
{
Deleted?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Deleted, item.OldPath, item.OldName));
}
else if (TryMakeRenamedEventArgs(item,
out RenamedEventArgs? eventArgs))
{
Renamed?.Invoke(this, eventArgs);
}
else if (item.OldPath != null)
{
Deleted?.Invoke(this, ToFileSystemEventArgs(
item.ChangeType, item.OldPath, item.OldName));
WatcherChangeTypes.Deleted, item.OldPath, item.OldName));
Created?.Invoke(this, ToFileSystemEventArgs(
item.ChangeType, item.Path, item.Name));
WatcherChangeTypes.Created, item.Path, item.Name));
}
}
else
Expand Down Expand Up @@ -692,7 +710,8 @@ public WaitForChangedResultMock(
public bool TimedOut { get; }
}

internal sealed class ChangeDescriptionEventArgs(ChangeDescription changeDescription) : EventArgs
internal sealed class ChangeDescriptionEventArgs(ChangeDescription changeDescription)
: EventArgs
{
public ChangeDescription ChangeDescription { get; } = changeDescription;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ private bool IncludeItemInEnumeration(
ChangeDescription fileSystemChange =
_fileSystem.ChangeHandler.NotifyPendingChange(WatcherChangeTypes.Renamed,
container.Type,
NotifyFilters.FileName,
ToNotifyFilters(container.Type),
destination,
source);
if (_containers.TryRemove(source, out IStorageContainer? sourceContainer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,89 @@
await That(result.OldName).IsEqualTo(FileSystem.Path.GetFileName(sourceName));
}

[Theory]
[InlineAutoData(NotifyFilters.DirectoryName)]
public async Task NotifyFilter_MoveDirectory_ShouldTriggerChangedEventOnNotifyFilters(
NotifyFilters notifyFilter, string sourceName, string destinationName)
{
//SkipIfLongRunningTestsShouldBeSkipped();

Check notice on line 619 in Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/NotifyFiltersTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/Testably.Abstractions.Tests/FileSystem/FileSystemWatcher/NotifyFiltersTests.cs#L619

Remove this commented out code.

FileSystem.Initialize();
FileSystem.Directory.CreateDirectory(sourceName);
RenamedEventArgs? result = null;
using ManualResetEventSlim ms = new();
using IFileSystemWatcher fileSystemWatcher =
FileSystem.FileSystemWatcher.New(BasePath);
fileSystemWatcher.Renamed += (_, eventArgs) =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
result = eventArgs;
ms.Set();
}
catch (ObjectDisposedException)
{
// Ignore any ObjectDisposedException
}
};

fileSystemWatcher.NotifyFilter = notifyFilter;
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.EnableRaisingEvents = true;

FileSystem.Directory.Move(sourceName, destinationName);

await That(ms.Wait(ExpectSuccess, TestContext.Current.CancellationToken)).IsTrue();
await That(result).IsNotNull();
await That(result!.ChangeType).IsEqualTo(WatcherChangeTypes.Renamed);
await That(result.FullPath).IsEqualTo(FileSystem.Path.GetFullPath(destinationName));
await That(result.Name).IsEqualTo(FileSystem.Path.GetFileName(destinationName));
await That(result.OldFullPath).IsEqualTo(FileSystem.Path.GetFullPath(sourceName));
await That(result.OldName).IsEqualTo(FileSystem.Path.GetFileName(sourceName));
}

[Theory]
[InlineAutoData(NotifyFilters.DirectoryName)]
public async Task NotifyFilter_MoveDirectoryOutOfTheWatchedDirectory_ShouldTriggerChangedEventOnNotifyFilters(
NotifyFilters notifyFilter, string sourceName, string destinationName)
{
//SkipIfLongRunningTestsShouldBeSkipped();

FileSystem.Initialize().WithSubdirectory("watched");
var sourcePath = FileSystem.Path.Combine("watched", sourceName);
FileSystem.Directory.CreateDirectory(sourcePath);
FileSystemEventArgs? result = null;
using ManualResetEventSlim ms = new();
using IFileSystemWatcher fileSystemWatcher =
FileSystem.FileSystemWatcher.New("watched");
fileSystemWatcher.Deleted += (_, eventArgs) =>
{
// ReSharper disable once AccessToDisposedClosure
try
{
result = eventArgs;
ms.Set();
}
catch (ObjectDisposedException)
{
// Ignore any ObjectDisposedException
}
};

fileSystemWatcher.NotifyFilter = notifyFilter;
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.EnableRaisingEvents = true;

FileSystem.Directory.Move(sourcePath, destinationName);

await That(ms.Wait(ExpectSuccess, TestContext.Current.CancellationToken)).IsTrue();
await That(result).IsNotNull();
await That(result!.ChangeType).IsEqualTo(WatcherChangeTypes.Deleted);
await That(result.FullPath).IsEqualTo(sourcePath);
await That(result.Name).IsEqualTo(sourceName);
}

[Theory]
[AutoData]
public async Task NotifyFilter_WriteFile_ShouldNotNotifyOnOtherFilters(string fileName)
Expand Down
Loading