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
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,27 @@ private bool MatchesFilter(ChangeDescription changeDescription)
if (!changeDescription.Path.StartsWith(fullPath,
_fileSystem.Execute.StringComparisonMode))
{
return changeDescription.ChangeType == WatcherChangeTypes.Renamed &&
changeDescription.OldPath?.StartsWith(fullPath,
_fileSystem.Execute.StringComparisonMode) == true;
if (!(changeDescription.ChangeType == WatcherChangeTypes.Renamed &&
changeDescription.OldPath?.StartsWith(fullPath,
_fileSystem.Execute.StringComparisonMode) == true))
{
return false;
}
}
}
else if (!string.Equals(
_fileSystem.Execute.Path.GetDirectoryName(changeDescription.Path),
fullPath,
_fileSystem.Execute.StringComparisonMode))
{
return false;
if (!(changeDescription.ChangeType == WatcherChangeTypes.Renamed &&
string.Equals(
_fileSystem.Execute.Path.GetDirectoryName(changeDescription.OldPath),
fullPath,
_fileSystem.Execute.StringComparisonMode)))
{
return false;
}
}

if ((NotifyFilter & changeDescription.NotifyFilters) == 0)
Expand Down Expand Up @@ -538,28 +548,12 @@ 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 (!item.Path.StartsWith(Path, _fileSystem.Execute.StringComparisonMode) &&
item.OldPath != null)
{
Deleted?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Deleted, item.OldPath, item.OldName));
}
else if (_fileSystem.Execute.IsWindows)
if (_fileSystem.Execute.IsWindows)
{
if (TryMakeRenamedEventArgs(item,
out RenamedEventArgs? eventArgs))
Expand All @@ -568,10 +562,39 @@ private void TriggerRenameNotification(ChangeDescription item)
}
else if (item.OldPath != null)
{
Deleted?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Deleted, item.OldPath, item.OldName));
Created?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Created, item.Path, item.Name));
string fullPath = _fileSystem.Execute.Path.GetFullPath(Path);
if (IncludeSubdirectories)
{
if (item.OldPath.StartsWith(fullPath, _fileSystem.Execute.StringComparisonMode))
{
Deleted?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Deleted, item.OldPath, item.OldName));
}
if (item.Path.StartsWith(fullPath, _fileSystem.Execute.StringComparisonMode))
{
Created?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Created, item.Path, item.Name));
}
}
else
{
if (string.Equals(
_fileSystem.Execute.Path.GetDirectoryName(item.OldPath),
fullPath,
_fileSystem.Execute.StringComparisonMode))
{
Deleted?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Deleted, item.OldPath, item.OldName));
}
if (string.Equals(
_fileSystem.Execute.Path.GetDirectoryName(item.Path),
fullPath,
_fileSystem.Execute.StringComparisonMode))
{
Created?.Invoke(this, ToFileSystemEventArgs(
WatcherChangeTypes.Created, item.Path, item.Name));
}
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public async Task RenamedEventArgs_ShouldUseDirectorySeparatorFromSimulatedFileS
fileSystem.File.WriteAllText(expectedOldFullPath, "foo");

using IFileSystemWatcher fileSystemWatcher =
fileSystem.FileSystemWatcher.New(fileSystem.Path.GetFullPath(parentDirectory));
fileSystem.FileSystemWatcher.New(parentDirectory);
using ManualResetEventSlim ms = new();
fileSystemWatcher.Renamed += (_, eventArgs) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ public async Task NotifyFilter_MoveFile_ShouldTriggerChangedEventOnNotifyFilters

[Theory]
[InlineAutoData(NotifyFilters.DirectoryName)]
public async Task NotifyFilter_MoveDirectory_ShouldTriggerChangedEventOnNotifyFilters(
public async Task NotifyFilter_MoveDirectory_ShouldTriggerRenamedEventOnNotifyFilters(
NotifyFilters notifyFilter, string sourceName, string destinationName)
{
SkipIfLongRunningTestsShouldBeSkipped();
Expand Down Expand Up @@ -654,11 +654,13 @@ public async Task NotifyFilter_MoveDirectory_ShouldTriggerChangedEventOnNotifyFi
}

[Theory]
[InlineAutoData(NotifyFilters.DirectoryName)]
public async Task NotifyFilter_MoveDirectoryOutOfTheWatchedDirectory_ShouldTriggerChangedEventOnNotifyFilters(
NotifyFilters notifyFilter, string sourceName, string destinationName)
[InlineAutoData(NotifyFilters.DirectoryName, true)]
[InlineAutoData(NotifyFilters.DirectoryName, false)]
public async Task NotifyFilter_MoveDirectoryOutOfTheWatchedDirectory_ShouldTriggerDeletedEventOnNotifyFilters_OnWindows(
NotifyFilters notifyFilter, bool includeSubdirectories, string sourceName, string destinationName)
{
SkipIfLongRunningTestsShouldBeSkipped();
Skip.IfNot(Test.RunsOnWindows);

FileSystem.Initialize().WithSubdirectory("watched");
var sourcePath = FileSystem.Path.Combine("watched", sourceName);
Expand All @@ -682,18 +684,61 @@ public async Task NotifyFilter_MoveDirectoryOutOfTheWatchedDirectory_ShouldTrigg
};

fileSystemWatcher.NotifyFilter = notifyFilter;
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.IncludeSubdirectories = includeSubdirectories;
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.FullPath).IsEqualTo(FileSystem.Path.GetFullPath(sourcePath));
await That(result.Name).IsEqualTo(sourceName);
}

[Theory]
[InlineAutoData(NotifyFilters.DirectoryName, true)]
[InlineAutoData(NotifyFilters.DirectoryName, false)]
public async Task NotifyFilter_MoveDirectoryInToTheWatchedDirectory_ShouldTriggerCreatedEventOnNotifyFilters_OnWindows(
NotifyFilters notifyFilter, bool includeSubdirectories, string sourceName, string destinationName)
{
SkipIfLongRunningTestsShouldBeSkipped();
Skip.IfNot(Test.RunsOnWindows);

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

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

FileSystem.Directory.Move(sourceName, destinationPath);

await That(ms.Wait(ExpectSuccess, TestContext.Current.CancellationToken)).IsTrue();
await That(result).IsNotNull();
await That(result!.ChangeType).IsEqualTo(WatcherChangeTypes.Created);
await That(result.FullPath).IsEqualTo(FileSystem.Path.GetFullPath(destinationPath));
await That(result.Name).IsEqualTo(destinationPath);
}

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