public static void FileSystemWatcher_Changed_Negative() { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Changed); // run all scenarios together to avoid unnecessary waits, // assert information is verbose enough to trace to failure cause watcher.EnableRaisingEvents = true; // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir"))) { // rename a file in the same directory testFile.Move(testFile.Path + "_rename"); // renaming a directory testDir.Move(testDir.Path + "_rename"); // deleting a file & directory by leaving the using block } Utility.ExpectNoEvent(eventOccured, "changed"); } }
/// <summary> /// Sets up watchers for the type given before performing a File.Move operation and checking for /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false, /// we ensure that it is not observed. /// /// This test will move the source file to a destination file in the same directory i.e. rename it /// </summary> private static void MoveAndCheck_SameDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent) { using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString())) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) { watcher.EnableRaisingEvents = true; AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType); // Move the testFile to a different name in the same directory testFile.Move(testFile.Path + "_" + eventType.ToString()); // Test that the event is observed or not observed if (moveRaisesEvent) { Utility.ExpectEvent(eventOccurred, eventType.ToString()); } else { Utility.ExpectNoEvent(eventOccurred, eventType.ToString()); } } } }
public static void FileSystemWatcher_Changed_Negative() { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Changed); // run all scenarios together to avoid unnecessary waits, // assert information is verbose enough to trace to failure cause watcher.EnableRaisingEvents = true; // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir"))) { // rename a file in the same directory testFile.Move(testFile.Path + "_rename"); // renaming a directory testDir.Move(testDir.Path + "_rename"); // deleting a file & directory by leaving the using block } Utility.ExpectNoEvent(eventOccurred, "changed"); } }
public static void FileSystemWatcher_Renamed_FileInNestedDirectory() { Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed, (AutoResetEvent are, TemporaryTestDirectory ttd) => { using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile"))) { nestedFile.Move(nestedFile.Path + "_2"); Utility.ExpectEvent(are, "renamed"); } }); }
public static void FileSystemWatcher_Renamed_FileInNestedDirectory() { Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed | WatcherChangeTypes.Created, (AutoResetEvent are, TemporaryTestDirectory ttd) => { using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile"))) { Utility.ExpectEvent(are, "file created"); nestedFile.Move(nestedFile.Path + "_2"); Utility.ExpectEvent(are, "renamed"); } }); }
/// <summary> /// Sets up watchers for the type given before performing a File.Move operation and checking for /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false, /// we ensure that it is not observed. /// /// This test will move the source file of a file within a nested directory /// </summary> private static void MoveAndCheck_NestedDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent) { Utility.TestNestedDirectoriesHelper(eventType, (AutoResetEvent eventOccured, TemporaryTestDirectory ttd) => { using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile" + eventType.ToString()))) { nestedFile.Move(nestedFile.Path + "_2"); if (moveRaisesEvent) { Utility.ExpectEvent(eventOccured, eventType.ToString()); } else { Utility.ExpectNoEvent(eventOccured, eventType.ToString()); } } }); }
/// <summary> /// Sets up watchers for the type given before performing a File.Move operation and checking for /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false, /// we ensure that it is not observed. /// /// This test will move the source file of a file within a nested directory /// </summary> private static void MoveAndCheck_NestedDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent) { Utility.TestNestedDirectoriesHelper(eventType, (AutoResetEvent eventOccurred, TemporaryTestDirectory ttd) => { using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile" + eventType.ToString()))) { nestedFile.Move(nestedFile.Path + "_2"); if (moveRaisesEvent) Utility.ExpectEvent(eventOccurred, eventType.ToString()); else Utility.ExpectNoEvent(eventOccurred, eventType.ToString()); } }); }
/// <summary> /// Sets up watchers for the type given before performing a File.Move operation and checking for /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false, /// we ensure that it is not observed. /// /// This test checks for when the file being moved has a destination directory that is outside of /// the path of the FileSystemWatcher. /// </summary> private static void MoveAndCheck_DifferentDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent) { using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString())) using (var dir_unwatched = new TemporaryTestDirectory(Path.GetRandomFileName())) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) { watcher.EnableRaisingEvents = true; AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType); // Move the testFile to a different name in the same directory testFile.Move(Path.Combine(dir_unwatched.Path, testFile.Name + "_" + eventType.ToString())); // Test which events are thrown if (moveRaisesEvent) Utility.ExpectEvent(eventOccurred, eventType.ToString()); else Utility.ExpectNoEvent(eventOccurred, eventType.ToString()); } } }