public void Can_Delete_A_Folder()
        {
            var  rootFolder  = new FolderDirectoryItem("root", null);
            var  childFolder = rootFolder.GetOrAddFolder("child");
            bool notified    = false;

            childFolder.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.True(deletedItem.IsFolder);
                Assert.Equal("child", deletedItem.Name);
                notified = true;
            };

            childFolder.Delete();
            Assert.True(notified);
        }
Example #2
0
        public void Can_Watch_Directory_For_Deleted_Items()
        {
            // Arrange
            IDirectory directory = new InMemoryDirectory();

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

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

            watcher.AddFilter(watchPattern);

            //
            bool notified = false;

            watcher.ItemDeleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e.DirectoryItemEventArgs;
                var            matchedFilters      = e.MatchedFilters;
                IDirectoryItem deletedItem         = args.DeletedItem;
                Assert.Equal("foo.txt", deletedItem.Name);
                notified = true;
            };


            var fooFile = directory.GetFile("/some/dir/folder/foo.txt");

            fooFile.Delete();
            Assert.True(notified);

            notified = false;
            var barFile = directory.GetFile("/some/dir/folder/bar.txt");

            barFile.Delete();
            Assert.False(notified);
        }
        public void Can_Delete_A_Folder_Containing_Items()
        {
            // given /root/child/grandchild/foo.txt
            // deleting the "child" folder should also delete all descendents which are
            // "grandchild" folder, and foo.txt.

            var rootFolder       = new FolderDirectoryItem("root", null);
            var childFolder      = rootFolder.GetOrAddFolder("child");
            var grandchildFolder = childFolder.GetOrAddFolder("grandchild");

            var fileInfo = new StringFileInfo("contents", "foo.txt");
            var fileItem = grandchildFolder.AddFile(fileInfo);

            // Should get notified when each folder and file is deleted.
            bool childDeletionNotified = false;

            childFolder.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.True(deletedItem.IsFolder);
                Assert.Equal("child", deletedItem.Name);
                childDeletionNotified = true;
            };

            bool grandchildDeletionNotified = false;

            grandchildFolder.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.True(deletedItem.IsFolder);
                Assert.Equal("grandchild", deletedItem.Name);
                grandchildDeletionNotified = true;
            };

            bool fileDeletionNotified = false;

            fileItem.Deleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e;
                IDirectoryItem deletedItem         = e.DeletedItem;
                Assert.False(deletedItem.IsFolder);
                Assert.Equal("foo.txt", deletedItem.Name);
                fileDeletionNotified = true;
            };

            childFolder.Delete(true);
            Assert.True(childDeletionNotified);
            Assert.True(grandchildDeletionNotified);
            Assert.True(fileDeletionNotified);

            // the deleted items should have had their fileinfo's set to not exist.
            Assert.False(childFolder.FileInfo.Exists);
            Assert.False(grandchildFolder.FileInfo.Exists);
            Assert.False(fileItem.FileInfo.Exists);

            // verify child items are no longer in directory.
            var item = rootFolder.NavigateToNext("child");

            Assert.Null(item);

            item = childFolder.NavigateToNext("grandchild");
            Assert.Null(item);

            item = grandchildFolder.NavigateToNext("foo.txt");
            Assert.Null(item);
        }