コード例 #1
0
        private void When_multiple_watchers_are_attached_to_the_same_file_system_it_must_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string fileNameToUpdate = "file.txt";

            string pathToFileToUpdate = Path.Combine(directoryToWatch, fileNameToUpdate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToFileToUpdate)
                                        .Build();

            using (FakeFileSystemWatcher watcher1 = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher1.NotifyFilter = TestNotifyFilters.All;

                string text1;
                string text2;

                using (var listener1 = new FileSystemWatcherEventListener(watcher1))
                {
                    fileSystem.File.SetLastWriteTimeUtc(pathToFileToUpdate, 1.January(2002));

                    using (FakeFileSystemWatcher watcher2 = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
                    {
                        watcher2.NotifyFilter = TestNotifyFilters.All;

                        using (var listener2 = new FileSystemWatcherEventListener(watcher2))
                        {
                            fileSystem.File.SetLastWriteTimeUtc(pathToFileToUpdate, 2.January(2002));

                            watcher2.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                            text2 = string.Join(Environment.NewLine, listener2.GetEventsCollectedAsText());
                        }
                    }

                    fileSystem.File.SetLastWriteTimeUtc(pathToFileToUpdate, 3.January(2002));

                    watcher1.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    text1 = string.Join(Environment.NewLine, listener1.GetEventsCollectedAsText());
                }

                // Assert
                text1.Should().Be(@"
                        * file.txt
                        * file.txt
                        * file.txt
                        ".TrimLines());

                text2.Should().Be(@"
                        * file.txt
                        ".TrimLines());
            }
        }
コード例 #2
0
        private void When_creating_file_using_absolute_path_without_drive_letter_it_must_raise_event()
        {
            // Arrange
            const string directory = @"c:\some";

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(directory)
                                        .Build();

            fileSystem.Directory.SetCurrentDirectory(directory);

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                watcher.NotifyFilter = TestNotifyFilters.All;
                watcher.Path         = @"c:\";

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    using (fileSystem.File.Create(@"\file.txt"))
                    {
                    }

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().HaveCount(1);

                    FileSystemEventArgs args = listener.CreateEventArgsCollected.Single();
                    args.ChangeType.Should().Be(WatcherChangeTypes.Created);
                    args.FullPath.Should().Be(@"c:\file.txt");
                    args.Name.Should().Be("file.txt");
                }
            }
        }
コード例 #3
0
        private void When_getting_directory_last_write_time_in_UTC_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            string       directoryPath    = Path.Combine(directoryToWatch, "Subfolder");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(directoryPath)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.GetLastWriteTimeUtc(directoryPath);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().BeEmpty();
                }
            }
        }
コード例 #4
0
        private void When_changing_directory_attributes_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch       = @"c:\some";
            const string containerDirectoryName = "Container";
            const string directoryNameToUpdate  = "Subfolder";

            string pathToDirectoryToUpdate = Path.Combine(directoryToWatch, containerDirectoryName, directoryNameToUpdate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(pathToDirectoryToUpdate)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.SetAttributes(pathToDirectoryToUpdate, FileAttributes.ReadOnly);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #5
0
        private void When_changing_file_creation_time_in_local_zone_to_existing_value_it_must_raise_events(NotifyFilters filters,
                                                                                                           [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string fileName         = "file.txt";
            string       filePath         = Path.Combine(directoryToWatch, fileName);

            var clock = new SystemClock(() => DefaultTimeUtc);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder(clock)
                                        .IncludingTextFile(filePath, "CONTENT")
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.SetCreationTime(filePath, DefaultTimeUtc.ToLocalTime());

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #6
0
        private void When_renaming_file_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch    = @"c:\some";
            const string sourceFileName      = "source.txt";
            const string destinationFileName = "target.txt";

            string pathToSourceFile      = Path.Combine(directoryToWatch, sourceFileName);
            string pathToDestinationFile = Path.Combine(directoryToWatch, destinationFileName);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToSourceFile, "CONTENT")
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.Move(pathToSourceFile, pathToDestinationFile);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #7
0
        private void When_unlocking_bytes_in_stream_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            string       filePath         = Path.Combine(directoryToWatch, "file.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingBinaryFile(filePath, BufferFactory.Create(4096))
                                        .Build();

            using (IFileStream stream = fileSystem.File.Open(filePath, FileMode.Open))
            {
                stream.Lock(0, 256);

                using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
                {
                    watcher.NotifyFilter          = TestNotifyFilters.All;
                    watcher.IncludeSubdirectories = true;

                    using (var listener = new FileSystemWatcherEventListener(watcher))
                    {
                        // Act
                        stream.Unlock(0, 256);

                        watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                        // Assert
                        listener.EventsCollected.Should().BeEmpty();
                    }
                }
            }
        }
        private void When_enumerating_entries_recursively_for_directory_tree_it_must_raise_events(NotifyFilters filters,
                                                                                                  [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch           = @"c:\some";
            string       pathToDirectoryToEnumerate = Path.Combine(directoryToWatch, "EnumerateMe");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(Path.Combine(pathToDirectoryToEnumerate, @"TopLevel\FolderA\SubFolderA"))
                                        .IncludingEmptyFile(Path.Combine(pathToDirectoryToEnumerate, @"TopLevel\FolderA\FileInA.txt"))
                                        .IncludingEmptyFile(Path.Combine(pathToDirectoryToEnumerate, @"TopLevel\FolderB\FileInB.txt"))
                                        .IncludingEmptyFile(Path.Combine(pathToDirectoryToEnumerate, @"TopLevel\FolderC\SubFolderC\FileInSubFolderC.txt"))
                                        .IncludingEmptyFile(Path.Combine(pathToDirectoryToEnumerate, @"TopLevel\FileInRoot2.txt"))
                                        .IncludingEmptyFile(Path.Combine(pathToDirectoryToEnumerate, @"TopLevel\FileInRoot1.txt"))
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.GetFileSystemEntries(pathToDirectoryToEnumerate,
                                                              searchOption: SearchOption.AllDirectories);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #9
0
        private void When_flushing_and_timeout_expires_it_must_fail()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(directoryToWatch)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter = TestNotifyFilters.All;
                watcher.Created     += (sender, args) => { Thread.Sleep(500); };

                watcher.EnableRaisingEvents = true;

                fileSystem.File.WriteAllText(@"c:\some\file.txt", "Content");

                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => watcher.FinishAndWaitForFlushed(1);

                // Assert
                action.Should().ThrowExactly <TimeoutException>().WithMessage(
                    "Timed out waiting for notification event handlers to finish.");
            }
        }
コード例 #10
0
        private void When_getting_temp_file_name_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch = @"c:\User";
            const string tempDirectory    = @"c:\User\Temp";

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .WithEmptyFilesInTempDirectory(tempDirectory, 0x1111)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Path.GetTempFileName();

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #11
0
        private void When_stopping_watcher_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToFileToUpdate = Path.Combine(directoryToWatch, "file.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToFileToUpdate)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter = TestNotifyFilters.All;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    BlockUntilChangeProcessed(watcher, () =>
                    {
                        fileSystem.File.SetAttributes(pathToFileToUpdate, FileAttributes.ReadOnly);
                    });

                    // Act
                    watcher.EnableRaisingEvents = false;

                    fileSystem.File.SetAttributes(pathToFileToUpdate, FileAttributes.Hidden);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().HaveCount(1);
                    listener.ChangeEventArgsCollected.Should().HaveSameCount(listener.EventsCollected);
                }
            }
        }
コード例 #12
0
        private void When_disposing_started_watcher_multiple_times_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToFileToUpdate = Path.Combine(directoryToWatch, "file.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToFileToUpdate)
                                        .Build();

            FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch);

            watcher.NotifyFilter        = TestNotifyFilters.All;
            watcher.EnableRaisingEvents = true;

            BlockUntilChangeProcessed(watcher, () =>
            {
                fileSystem.File.SetAttributes(pathToFileToUpdate, FileAttributes.ReadOnly);
            });

            // Act
            Action action = () =>
            {
                watcher.Dispose();
                watcher.Dispose();
            };

            // Assert
            action.Should().NotThrow();
        }
コード例 #13
0
        private void When_changing_attributes_in_watched_directory_with_IncludeSubdirectories_disabled_it_must_raise_events()
        {
            // Arrange
            const string directoryToWatch      = @"c:\some";
            const string directoryNameToUpdate = "TargetFolder";

            string pathToDirectoryToUpdate = Path.Combine(directoryToWatch, directoryNameToUpdate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(pathToDirectoryToUpdate)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = false;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.SetAttributes(pathToDirectoryToUpdate, FileAttributes.ReadOnly);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().HaveCount(1);
                    listener.ChangeEventArgsCollected.Should().HaveSameCount(listener.EventsCollected);

                    FileSystemEventArgs args = listener.ChangeEventArgsCollected.Single();
                    args.ChangeType.Should().Be(WatcherChangeTypes.Changed);
                    args.FullPath.Should().Be(pathToDirectoryToUpdate);
                    args.Name.Should().Be(directoryNameToUpdate);
                }
            }
        }
コード例 #14
0
        private void When_changing_attributes_of_watched_directory_with_IncludeSubdirectories_enabled_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch        = @"c:\some";
            const string pathToDirectoryToUpdate = directoryToWatch;

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(pathToDirectoryToUpdate)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.SetAttributes(pathToDirectoryToUpdate, FileAttributes.ReadOnly);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().BeEmpty();
                }
            }
        }
コード例 #15
0
        private void When_creating_extended_local_file_it_must_raise_event()
        {
            // Arrange
            const string directory = @"\\?\C:\folder";
            const string path      = @"\\?\C:\folder\file.txt";

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(directory)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                watcher.NotifyFilter = TestNotifyFilters.All;
                watcher.Path         = @"C:\folder";

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    using (fileSystem.File.Create(path))
                    {
                    }

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().HaveCount(1);

                    FileSystemEventArgs args = listener.CreateEventArgsCollected.Single();
                    args.ChangeType.Should().Be(WatcherChangeTypes.Created);
                    args.FullPath.Should().Be(@"C:\folder\file.txt");
                    args.Name.Should().Be("file.txt");
                }
            }
        }
コード例 #16
0
        private void When_decrypting_unencrypted_file_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch       = @"c:\some";
            const string containerDirectoryName = "Container";
            const string fileNameToDecrypt      = "file.txt";

            string pathToFileToDecrypt = Path.Combine(directoryToWatch, containerDirectoryName, fileNameToDecrypt);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToFileToDecrypt, "SecretContent")
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.Decrypt(pathToFileToDecrypt);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().BeEmpty();
                }
            }
        }
コード例 #17
0
        private void When_moving_empty_directory_to_parent_directory_it_must_raise_events(NotifyFilters filters,
                                                                                          [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch         = @"c:\some";
            const string containerDirectoryName   = "Container";
            const string sourceDirectoryName      = @"MoveSource\SubFolderA";
            const string destinationDirectoryName = "MovedSubFolderA";

            string pathToContainerDirectory   = Path.Combine(directoryToWatch, containerDirectoryName);
            string pathToSourceDirectory      = Path.Combine(pathToContainerDirectory, sourceDirectoryName);
            string pathToDestinationDirectory = Path.Combine(pathToContainerDirectory, destinationDirectoryName);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(Path.Combine(pathToSourceDirectory, @"FolderA\SubFolderA"))
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.Move(pathToSourceDirectory, pathToDestinationDirectory);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #18
0
        private void When_creating_file_with_delete_on_close_it_must_raise_events(NotifyFilters filters,
                                                                                  [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string fileNameToCreate = "file.txt";

            string pathToFileToCreate = Path.Combine(directoryToWatch, fileNameToCreate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(directoryToWatch)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    using (fileSystem.File.Create(pathToFileToCreate, options: FileOptions.DeleteOnClose))
                    {
                    }

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
        private void When_enumerating_entries_for_directory_tree_it_must_raise_events(NotifyFilters filters,
                                                                                      [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch           = @"c:\some";
            const string directoryNameToEnumerate   = "EnumerateMe";
            string       pathToDirectoryToEnumerate = Path.Combine(directoryToWatch, directoryNameToEnumerate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(Path.Combine(pathToDirectoryToEnumerate, "file.txt"))
                                        .IncludingEmptyFile(Path.Combine(pathToDirectoryToEnumerate, "Subfolder", "SubFile.txt"))
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.GetFileSystemEntries(pathToDirectoryToEnumerate);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #20
0
        private void When_appending_to_file_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch   = @"c:\some";
            const string fileNameToAppend   = "file.txt";
            string       pathToFileToAppend = Path.Combine(directoryToWatch, fileNameToAppend);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToFileToAppend)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.AppendAllText(pathToFileToAppend, "Extra");

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #21
0
        private void When_renaming_file_to_itself_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string fileName         = "file.txt";

            string pathToSourceFile      = Path.Combine(directoryToWatch, fileName);
            string pathToDestinationFile = Path.Combine(directoryToWatch.ToUpperInvariant(), fileName);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToSourceFile, "CONTENT")
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.Move(pathToSourceFile, pathToDestinationFile);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().BeEmpty();
                }
            }
        }
コード例 #22
0
        private void When_creating_partially_missing_directory_tree_it_must_raise_events(NotifyFilters filters,
                                                                                         [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch        = @"c:\some";
            const string directoryNameToCreate   = @"Deeper\Nested\Subfolder";
            string       pathToDirectoryToCreate = Path.Combine(directoryToWatch, directoryNameToCreate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(Path.Combine(directoryToWatch, "DEEPER"))
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.CreateDirectory(pathToDirectoryToCreate);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #23
0
        private void When_getting_file_creation_time_in_local_zone_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            string       filePath         = Path.Combine(directoryToWatch, "file.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(filePath, "CONTENT")
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.GetCreationTime(filePath);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().BeEmpty();
                }
            }
        }
コード例 #24
0
        private void When_creating_existing_directory_tree_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch        = @"c:\some";
            const string directoryNameToCreate   = @"Deeper\Nested\Subfolder";
            string       pathToDirectoryToCreate = Path.Combine(directoryToWatch, directoryNameToCreate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(pathToDirectoryToCreate)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.CreateDirectory(pathToDirectoryToCreate);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().BeEmpty();
                }
            }
        }
コード例 #25
0
        private void When_filtering_directory_tree_with_subdirectory_pattern_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToFileToUpdate1 = Path.Combine(directoryToWatch, "RootFile1.txt");
            string pathToFileToUpdate2 = Path.Combine(directoryToWatch, "SubFolder", "SubFile1.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToFileToUpdate1)
                                        .IncludingEmptyFile(pathToFileToUpdate2)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;
                watcher.Filter = @"SubFolder\*.txt";

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.SetAttributes(pathToFileToUpdate1, FileAttributes.Hidden);
                    fileSystem.File.SetAttributes(pathToFileToUpdate2, FileAttributes.Hidden);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    watcher.Filter.Should().Be(@"SubFolder\*.txt");

                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().BeEmpty();
                }
            }
        }
コード例 #26
0
        private void When_getting_temp_path_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\User";
            const string tempDirectory    = @"c:\User\Temp";

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .WithTempDirectory(tempDirectory)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Path.GetTempPath();

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().BeEmpty();
                }
            }
        }
コード例 #27
0
        private void When_changing_directory_attributes_to_existing_value_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch        = @"c:\some";
            const string directoryNameToUpdate   = "file.txt";
            string       pathToDirectoryToUpdate = Path.Combine(directoryToWatch, directoryNameToUpdate);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(pathToDirectoryToUpdate, FileAttributes.ReadOnly)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.SetAttributes(pathToDirectoryToUpdate, FileAttributes.ReadOnly);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().HaveCount(0);
                }
            }
        }
コード例 #28
0
        private void When_decrypting_file_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch       = @"c:\some";
            const string containerDirectoryName = "Container";
            const string fileNameToDecrypt      = "file.txt";

            string pathToFileToDecrypt = Path.Combine(directoryToWatch, containerDirectoryName, fileNameToDecrypt);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToFileToDecrypt, "SecretContent")
                                        .Build();

            fileSystem.File.Encrypt(pathToFileToDecrypt);

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.Decrypt(pathToFileToDecrypt);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #29
0
        private void When_changing_directory_last_write_time_in_UTC_to_existing_value_it_must_raise_events(NotifyFilters filters,
                                                                                                           [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string directoryName    = "Subfolder";
            string       directoryPath    = Path.Combine(directoryToWatch, directoryName);

            var clock = new SystemClock(() => DefaultTimeUtc);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder(clock)
                                        .IncludingDirectory(directoryPath)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = filters;
                watcher.IncludeSubdirectories = true;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.SetLastWriteTimeUtc(directoryPath, DefaultTimeUtc);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
コード例 #30
0
        private void When_setting_path_to_extended_local_directory_it_must_raise_events()
        {
            // Arrange
            const string directoryToWatch   = @"\\?\c:\some";
            const string pathToFileToUpdate = @"c:\some\file.txt";

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToFileToUpdate)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                watcher.NotifyFilter = TestNotifyFilters.All;

                // Act
                watcher.Path = directoryToWatch;

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    fileSystem.File.SetAttributes(pathToFileToUpdate, FileAttributes.ReadOnly);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    listener.EventsCollected.Should().HaveCount(1);
                    listener.ChangeEventArgsCollected.Should().HaveSameCount(listener.EventsCollected);

                    FileSystemEventArgs args = listener.ChangeEventArgsCollected.Single();
                    args.ChangeType.Should().Be(WatcherChangeTypes.Changed);
                    args.FullPath.Should().Be(@"\\?\c:\some\file.txt");
                    args.Name.Should().Be("file.txt");
                }
            }
        }