コード例 #1
0
        public void Can_Watch_Directory_For_Updated_Items()
        {
            // Arrange
            IDirectory directory = new InMemoryDirectory();

            directory.AddFile("/some/dir/folder/", new StringFileInfo("hi", "newfile.txt"));

            var watcher      = new DirectoryWatcher(directory);
            var watchPattern = "/some/dir/folder/new*.txt";

            watcher.AddFilter(watchPattern);

            //
            bool notified = false;

            watcher.ItemUpdated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e.DirectoryItemEventArgs;
                var matchedFilters = e.MatchedFilters;
                Assert.Equal("newfile.txt", args.OldItem.Name);
                Assert.Equal("newfile.csv", args.NewItem.Name);
                notified = true;
            };

            // now update the file
            var existingItem = directory.GetFile("/some/dir/folder/newfile.txt");

            existingItem.Update(new StringFileInfo("changed", "newfile.csv"));
            Assert.True(notified);

            notified = false;
            existingItem.Update(new StringFileInfo("changed again", "newfile.csv"));
            // second update shouldnt trigger as our watch pattern is only watching newfile*.txt files.
            Assert.False(notified);
        }
コード例 #2
0
        public void Can_Watch_Directory_For_Moved_Items()
        {
            // Arrange
            // Prepare our in memory directory.
            IDirectory directory = new InMemoryDirectory();

            directory.AddFile("/some/dir/folder/", new StringFileInfo("hi", "newfile.txt"));
            directory.AddFile("/some/dir/another/", new StringFileInfo("hi", "newfile.txt"));
            directory.AddFile("/some/dir/hello/", new StringFileInfo("hi", "newfile.txt"));

            // Watch the directory using a pattern as a filter.
            var watcher      = new DirectoryWatcher(directory);
            var watchPattern = "/some/dir/**/new*.txt";

            watcher.AddFilter(watchPattern);

            // Register for the watchers ItemUpdated event, this is how it should notify us of updated
            // files.
            int notifyCount = 0;

            watcher.ItemUpdated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e.DirectoryItemEventArgs;
                var matchedFilters = e.MatchedFilters; // contains the patterns that were matched (the reason we are being notified)
                // we aren't renaming the file so the file should have the same name.
                // We are renaming a parent folder, so only the items full path should be different.
                Assert.Equal("newfile.txt", args.OldItem.Name);
                Assert.Equal("newfile.txt", args.NewItem.Name);
                Assert.StartsWith("/some/dir/", args.OldItem.Path);          // the old folder path.
                Assert.StartsWith("/newfoldername/dir/", args.NewItem.Path); // the new folder path.

                //Assert.Equal("/changed/dir/folder/newfile.txt", args.NewItem.Path);
                notifyCount = notifyCount + 1;
            };

            // Act
            // Rename the root folder /some too be /newfoldername
            // this should cause the watcher to notifiy us of effected items we are interested in.
            var folder = directory.GetFolder("/some");

            folder.Rename("newfoldername");

            // Watcher should have notified us on of 3 items we were watching (matched by the pattern)
            Assert.Equal(3, notifyCount);

            // now rename the file again - this time the watcher should not notify us, as the items live in a directory
            // that no longer match out pattern - i.e the previous rename operation has moved them outside of our filter pattern.
            notifyCount = 0;
            var existingItem = directory.GetFile("/newfoldername/dir/folder/newfile.txt");

            existingItem.Update(new StringFileInfo("changed", "newfile.csv"));
            Assert.Equal(0, notifyCount);
        }
        public void Can_Rename_A_Folder_With_Child_Items()
        {
            // arrange
            // build a directory /root/child/grandchild
            var rootFolder       = new FolderDirectoryItem("root", null);
            var childFolder      = rootFolder.GetOrAddFolder("child");
            var grandChildFolder = childFolder.GetOrAddFolder("grandchild");

            // add a file /root/child/foo.txt
            var fileInfo      = new StringFileInfo("contents", "foo.txt");
            var someOtherFile = childFolder.AddFile(fileInfo);

            // add a file /root/child/grandchild/bar.txt
            var barFile = grandChildFolder.AddFile(new StringFileInfo("should trigger", "bar.txt"));

            // Subscribe to event handlers as we want to test if we are notified appropriately when
            // a folder is renamed.
            bool notified     = false;
            bool fileNotified = false;

            // We are going to rename this folder, so we should see if we get notified that it is
            // updated.
            grandChildFolder.Updated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e;
                IDirectoryItem oldItem             = e.OldItem;
                IDirectoryItem newItem             = e.NewItem;
                Assert.Equal("grandchild", oldItem.Name);
                Assert.Equal("grandchild-renamed", newItem.Name);
                notified = true;
            };

            // We are going to rename the folder which this file lives in, which means this files
            // path will have changed. We should register to see if we get notified that it was updated.
            barFile.Updated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e;
                IDirectoryItem oldItem             = e.OldItem;
                IDirectoryItem newItem             = e.NewItem;
                Assert.Equal("bar.txt", oldItem.Name);
                Assert.Equal("bar.txt", newItem.Name);
                Assert.Equal("root/child/grandchild/bar.txt", oldItem.Path);
                Assert.Equal("root/child/grandchild-renamed/bar.txt", newItem.Path);
                fileNotified = true;
            };

            grandChildFolder.Rename("grandchild-renamed");
            Assert.True(notified);
            Assert.True(fileNotified);
        }
        public void Can_Rename_A_Folder()
        {
            var rootFolder  = new FolderDirectoryItem("root", null);
            var childFolder = rootFolder.GetOrAddFolder("child");

            var  fileInfo      = new StringFileInfo("contents", "foo.txt");
            var  someOtherFile = childFolder.AddFile(fileInfo);
            bool notified      = false;

            childFolder.Updated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e;
                IDirectoryItem oldItem             = e.OldItem;
                IDirectoryItem newItem             = e.NewItem;
                Assert.Equal("child", oldItem.Name);
                Assert.Equal("child-renamed", newItem.Name);
                notified = true;
            };

            childFolder.Rename("child-renamed");
            Assert.True(notified);
            Assert.Equal("root/child-renamed/foo.txt", someOtherFile.Path);
        }