Example #1
0
    public static void FileSystemWatcher_Changed_File() 
    {
        var currentDir = Directory.GetCurrentDirectory();
        string fileName = Guid.NewGuid().ToString();

        var watcher = new PollingWatcher(currentDir, false, 100);

        using (var file = new TemporaryTestFile(fileName))
        {
            watcher.Start();
            Thread.Sleep(200);
            watcher.ChangedDetailed += (changes) =>
            {
                Assert.Equal(1, changes.Length);
                var change = changes[0];
                Assert.Equal(ChangeType.Changed, change.ChangeType);
                Assert.Equal(fileName, change.Name);
                Assert.Equal(currentDir, change.Directory);
            };
            Thread.Sleep(200);
            file.WriteByte(100);
            Thread.Sleep(200);
            watcher.Dispose();
            Thread.Sleep(200);
        }
    }
    public static void FileSystemWatcher_Renamed_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.Renamed);

            watcher.EnableRaisingEvents = true;

            // run all scenarios together to avoid unnecessary waits, 
            // assert information is verbose enough to trace to failure cause

            // create a file
            using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file")))
            using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir")))
            {
                // change a file
                testFile.WriteByte(0xFF);
                testFile.Flush();

                // deleting a file & directory by leaving the using block
            }

            Utility.ExpectNoEvent(eventOccured, "created");
        }
    }
 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");
         }
     });
 }
    public static void FileSystemWatcher_Created_File()
    {
        using (var watcher = new FileSystemWatcher("."))
        {
            string fileName = Guid.NewGuid().ToString();
            watcher.Filter = fileName;
            AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created);

            watcher.EnableRaisingEvents = true;

            using (var file = new TemporaryTestFile(fileName))
            {
                Utility.ExpectEvent(eventOccured, "created");
            }
        }
    }
    public static void FileSystemWatcher_Deleted_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.Deleted);

            // 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")))
            {
                // change a file
                testFile.WriteByte(0xFF);
                testFile.Flush();

                // renaming a directory
                //
                // We don't do this on Linux because depending on the timing of MOVED_FROM and MOVED_TO events,
                // a rename can trigger delete + create as a deliberate handling of an edge case, and this
                // test is checking that no delete events are raised.
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    testDir.Move(testDir.Path + "_rename");
                }

                Utility.ExpectNoEvent(eventOccured, "deleted");
            }
        }
    }
 /// <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());
            }
        }
    }
    public static void FileSystemWatcher_Deleted_FileDeletedInNestedDirectory()
    {
        using (var dir = Utility.CreateTestDirectory())
        using (var watcher = new FileSystemWatcher())
        {
            AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks
            AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted);

            watcher.Path = Path.GetFullPath(dir.Path);
            watcher.Filter = "*";
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;

            using (var firstDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
            {
                // Wait for the created event
                Utility.ExpectEvent(createOccured, "create", 1000 * 30);

                using (var secondDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir2")))
                {
                    // Wait for the created event
                    Utility.ExpectEvent(createOccured, "create", 1000 * 30);

                    using (var nestedDir = new TemporaryTestFile(Path.Combine(secondDir.Path, "nestedFile"))) { }

                    Utility.ExpectEvent(eventOccured, "deleted");
                }

                Utility.ExpectEvent(eventOccured, "deleted");
            }

            Utility.ExpectEvent(eventOccured, "deleted");
        }
    }
 public static void FileSystemWatcher_Changed_FileInNestedDirectory()
 {
     Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Changed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
     {
         using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
         {
             Directory.SetLastAccessTime(nestedFile.Path, DateTime.Now);
             Utility.ExpectEvent(are, "changed");
         }
     },
     NotifyFilters.DirectoryName | NotifyFilters.LastAccess | NotifyFilters.FileName);
 }
 public static void FileSystemWatcher_Created_FileCreatedInNestedDirectory()
 {
     Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Created, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
     {
         using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
         {
             Utility.ExpectEvent(are, "nested file created", 1000 * 30);
         }
     });
 }
    public static void FileSystemWatcher_Created_DeepDirectoryStructure()
    {
        // List of created directories
        List<TemporaryTestDirectory> lst = new List<TemporaryTestDirectory>();

        try
        {
            using (var dir = Utility.CreateTestDirectory())
            using (var watcher = new FileSystemWatcher())
            {
                AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created);

                // put everything in our own directory to avoid collisions
                watcher.Path = Path.GetFullPath(dir.Path);
                watcher.Filter = "*.*";
                watcher.IncludeSubdirectories = true;
                watcher.EnableRaisingEvents = true;

                // Priming directory
                TemporaryTestDirectory priming = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir"));
                lst.Add(priming);
                Utility.ExpectEvent(eventOccurred, "priming create");

                // Create a deep directory structure and expect things to work
                for (int i = 1; i < 20; i++)
                {
                    lst.Add(new TemporaryTestDirectory(Path.Combine(lst[i - 1].Path, String.Format("dir{0}", i))));
                    Utility.ExpectEvent(eventOccurred, lst[i].Path + " create");
                }

                // Put a file at the very bottom and expect it to raise an event
                using (var file = new TemporaryTestFile(Path.Combine(lst[lst.Count - 1].Path, "temp file")))
                {
                    Utility.ExpectEvent(eventOccurred, "temp file create");
                }
            }
        }
        finally
        {
            // Cleanup
            foreach (TemporaryTestDirectory d in lst)
                d.Dispose();
        }
    }