Skip to content

Commit a1b6954

Browse files
authored
refactor: only use reflection in FileSystemWatcherMock when simulating other OS (#871)
This PR refactors the `FileSystemWatcherMock` class to optimize performance by conditionally using reflection only when simulating non-native operating systems, rather than always using reflection for path manipulation. ### Key Changes - Conditional reflection usage based on simulation mode to improve performance in native scenarios - Updated constructor parameters to use the `Path` property instead of local variables
1 parent 4bf0d84 commit a1b6954

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

Source/Testably.Abstractions.Testing/FileSystem/FileSystemWatcherMock.cs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -512,18 +512,21 @@ private FileSystemEventArgs ToFileSystemEventArgs(
512512
changeName,
513513
out string name);
514514

515-
FileSystemEventArgs eventArgs = new(changeType, changePath, name);
516-
// FileSystemEventArgs implicitly combines the path in https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs
517-
// HACK: Have to resort to Reflection to override this behavior!
515+
FileSystemEventArgs eventArgs = new(changeType, Path, name);
516+
if (_fileSystem.SimulationMode != SimulationMode.Native)
517+
{
518+
// FileSystemEventArgs implicitly combines the path in https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs
519+
// HACK: Have to resort to Reflection to override this behavior!
518520
#if NETFRAMEWORK
519-
typeof(FileSystemEventArgs)
520-
.GetField("fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
521-
.SetValue(eventArgs, path);
521+
typeof(FileSystemEventArgs)
522+
.GetField("fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
523+
.SetValue(eventArgs, path);
522524
#else
523-
typeof(FileSystemEventArgs)
524-
.GetField("_fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
525-
.SetValue(eventArgs, path);
525+
typeof(FileSystemEventArgs)
526+
.GetField("_fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
527+
.SetValue(eventArgs, path);
526528
#endif
529+
}
527530

528531
return eventArgs;
529532
}
@@ -618,26 +621,30 @@ private bool TryMakeRenamedEventArgs(
618621

619622
eventArgs = new RenamedEventArgs(
620623
changeDescription.ChangeType,
621-
path,
624+
Path,
622625
name,
623626
oldName);
624-
// RenamedEventArgs implicitly combines the path in https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/RenamedEventArgs.cs
625-
// HACK: Have to resort to Reflection to override this behavior!
627+
628+
if (_fileSystem.SimulationMode != SimulationMode.Native)
629+
{
630+
// RenamedEventArgs implicitly combines the path in https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/RenamedEventArgs.cs
631+
// HACK: Have to resort to Reflection to override this behavior!
626632
#if NETFRAMEWORK
627-
typeof(FileSystemEventArgs)
628-
.GetField("fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
629-
.SetValue(eventArgs, path);
630-
typeof(RenamedEventArgs)
631-
.GetField("oldFullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
632-
.SetValue(eventArgs, oldPath);
633+
typeof(FileSystemEventArgs)
634+
.GetField("fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
635+
.SetValue(eventArgs, path);
636+
typeof(RenamedEventArgs)
637+
.GetField("oldFullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
638+
.SetValue(eventArgs, oldPath);
633639
#else
634-
typeof(FileSystemEventArgs)
635-
.GetField("_fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
636-
.SetValue(eventArgs, path);
637-
typeof(RenamedEventArgs)
638-
.GetField("_oldFullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
639-
.SetValue(eventArgs, oldPath);
640+
typeof(FileSystemEventArgs)
641+
.GetField("_fullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
642+
.SetValue(eventArgs, path);
643+
typeof(RenamedEventArgs)
644+
.GetField("_oldFullPath", BindingFlags.Instance | BindingFlags.NonPublic)?
645+
.SetValue(eventArgs, oldPath);
640646
#endif
647+
}
641648

642649
return _fileSystem.Execute.Path.GetDirectoryName(changeDescription.Path)?
643650
.Equals(_fileSystem.Execute.Path.GetDirectoryName(changeDescription.OldPath),

0 commit comments

Comments
 (0)