Ejemplo n.º 1
0
        private void When_setting_path_to_invalid_drive_it_must_fail()
        {
            // Arrange
            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => watcher.Path = "_:";

                // Assert
                action.Should().ThrowExactly <ArgumentException>().WithMessage("The directory name _: is invalid.");
            }
        }
Ejemplo n.º 2
0
        private void When_filtering_directory_with_null_pattern_it_must_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToFileToUpdate1      = Path.Combine(directoryToWatch, "MatchingFile.txt");
            string pathToFileToUpdate2      = Path.Combine(directoryToWatch, "MatchingFile");
            string pathToDirectoryToUpdate1 = Path.Combine(directoryToWatch, "MatchingFolder.txt");
            string pathToDirectoryToUpdate2 = Path.Combine(directoryToWatch, "MatchingFolder");

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = TestNotifyFilters.All;
                watcher.IncludeSubdirectories = true;
                // ReSharper disable once AssignNullToNotNullAttribute
                watcher.Filter = null;

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

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    watcher.Filter.Should().Be("*.*");

                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(@"
                        * MatchingFile.txt
                        * MatchingFile
                        * MatchingFolder.txt
                        * MatchingFolder
                        ".TrimLines());
                }
            }
        }
Ejemplo n.º 3
0
        private void When_moving_readonly_directory_tree_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(pathToContainerDirectory, FileAttributes.ReadOnly)
                                        .IncludingDirectory(pathToSourceDirectory, FileAttributes.ReadOnly)
                                        .IncludingDirectory(Path.Combine(pathToSourceDirectory, @"FolderA"), FileAttributes.ReadOnly)
                                        .IncludingDirectory(Path.Combine(pathToSourceDirectory, @"FolderA\SubFolderA"), FileAttributes.ReadOnly)
                                        .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FolderA\FileInA.txt"), FileAttributes.ReadOnly)
                                        .IncludingDirectory(Path.Combine(pathToSourceDirectory, @"FolderB"), FileAttributes.ReadOnly)
                                        .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FolderB\FileInB.txt"), FileAttributes.ReadOnly)
                                        .IncludingDirectory(Path.Combine(pathToSourceDirectory, @"FolderC"), FileAttributes.ReadOnly)
                                        .IncludingDirectory(Path.Combine(pathToSourceDirectory, @"FolderC\SubFolderC"), FileAttributes.ReadOnly)
                                        .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FolderC\SubFolderC\FileInSubFolderC.txt"),
                                                            FileAttributes.ReadOnly)
                                        .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FileInRoot2.txt"), FileAttributes.ReadOnly)
                                        .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FileInRoot1.txt"), FileAttributes.ReadOnly)
                                        .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);
                }
            }
        }
Ejemplo n.º 4
0
        private void When_setting_InternalBufferSize_to_high_value_it_must_store_value()
        {
            // Arrange
            const int oneMegabyte = 1024 * 1024;

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                // Act
                watcher.InternalBufferSize = oneMegabyte;

                // Assert
                watcher.InternalBufferSize.Should().Be(oneMegabyte);
            }
        }
Ejemplo n.º 5
0
        private void When_filtering_directory_with_name_pattern_in_rename_it_must_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string sourcePathToFile1 = Path.Combine(directoryToWatch, "SourceHit1.txt");
            string targetPathToFile1 = Path.Combine(directoryToWatch, "TargetHit1.txt");
            string sourcePathToFile2 = Path.Combine(directoryToWatch, "SourceHit2.txt");
            string targetPathToFile2 = Path.Combine(directoryToWatch, "TargetMiss2.txt");
            string sourcePathToFile3 = Path.Combine(directoryToWatch, "SourceMiss3.txt");
            string targetPathToFile3 = Path.Combine(directoryToWatch, "TargetHit3.txt");
            string sourcePathToFile4 = Path.Combine(directoryToWatch, "SourceMiss4.txt");
            string targetPathToFile4 = Path.Combine(directoryToWatch, "TargetMiss4.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(sourcePathToFile1)
                                        .IncludingEmptyFile(sourcePathToFile2)
                                        .IncludingEmptyFile(sourcePathToFile3)
                                        .IncludingEmptyFile(sourcePathToFile4)
                                        .Build();

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

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.File.Move(sourcePathToFile1, targetPathToFile1);
                    fileSystem.File.Move(sourcePathToFile2, targetPathToFile2);
                    fileSystem.File.Move(sourcePathToFile3, targetPathToFile3);
                    fileSystem.File.Move(sourcePathToFile4, targetPathToFile4);
                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(@"
                        > SourceHit1.txt => TargetHit1.txt
                        > SourceHit2.txt => TargetMiss2.txt
                        > SourceMiss3.txt => TargetHit3.txt
                        ".TrimLines());
                }
            }
        }
Ejemplo n.º 6
0
        private void When_parent_of_watcher_directory_is_deleted_it_must_raise_error_event_and_terminate()
        {
            // Arrange
            const string directoryToWatch = @"c:\parent\watchFolder";

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

            var       lockObject = new object();
            Exception lastError  = null;

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.Error += (sender, args) =>
                {
                    lock (lockObject)
                    {
                        lastError = args.GetException();
                    }
                };

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    // Act
                    fileSystem.Directory.Delete(@"c:\parent", true);

                    Thread.Sleep(SleepTimeToEnsureOperationHasArrivedAtWatcherConsumerLoop);

                    // Assert
                    watcher.EnableRaisingEvents.Should().BeFalse();

                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(@"
                        ! Access is denied
                        ".TrimLines());

                    lock (lockObject)
                    {
                        lastError.Should().NotBeNull();
                        lastError.Should().BeOfType <Win32Exception>().Subject.NativeErrorCode.Should().Be(5);
                        lastError.Message.Should().Be("Access is denied");
                    }
                }
            }
        }
        private void When_waiting_for_disposed_watcher_it_must_fail()
        {
            // Arrange
            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .Build();

            FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher();

            watcher.Dispose();

            // Act
            Action action = () => watcher.WaitForChanged(WatcherChangeTypes.All, MaxTestDurationInMilliseconds);

            // Assert
            action.Should().ThrowExactly <ObjectDisposedException>().WithMessage("Cannot access a disposed object.*")
            .And.ObjectName.Should().Be("FileSystemWatcher");
        }
Ejemplo n.º 8
0
        private void When_starting_disposed_watcher_it_must_fail()
        {
            // Arrange
            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .Build();

            FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher();

            watcher.Dispose();

            // Act
            Action action = () => watcher.EnableRaisingEvents = true;

            // Assert
            action.Should().ThrowExactly <ObjectDisposedException>().WithMessage("Cannot access a disposed object.*")
            .And.ObjectName.Should().Be("FileSystemWatcher");
        }
Ejemplo n.º 9
0
        protected static void BlockUntilChangeProcessed([NotNull] FakeFileSystemWatcher watcher, [NotNull] Action diskOperation)
        {
            using (var operationWaitHandle = new ManualResetEventSlim(false))
            {
                // ReSharper disable AccessToDisposedClosure
                watcher.Deleted += (sender, args) => operationWaitHandle.Set();
                watcher.Created += (sender, args) => operationWaitHandle.Set();
                watcher.Changed += (sender, args) => operationWaitHandle.Set();
                watcher.Renamed += (sender, args) => operationWaitHandle.Set();
                // ReSharper restore AccessToDisposedClosure

                diskOperation();

                bool waitSucceeded = operationWaitHandle.Wait(MaxTestDurationInMilliseconds);
                waitSucceeded.Should().BeTrue();
            }
        }
        private static void BlockUntilStarted([NotNull] FakeFileSystemWatcher watcher)
        {
            DateTime startTime      = DateTime.UtcNow;
            DateTime expirationTime = startTime.AddMilliseconds(MaxTestDurationInMilliseconds);

            while (DateTime.UtcNow < expirationTime)
            {
                if (watcher.EnableRaisingEvents)
                {
                    return;
                }

                Thread.Sleep(50);
            }

            throw new TimeoutException("Test timed out.");
        }
Ejemplo n.º 11
0
        private void When_setting_path_to_missing_network_share_it_must_fail()
        {
            // Arrange
            const string path = @"\\server\share";

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => watcher.Path = path;

                // Assert
                action.Should().ThrowExactly <ArgumentException>().WithMessage(@"The directory name \\server\share is invalid.");
            }
        }
        private void When_waiting_for_file_change_multiple_times_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string fileNameToUpdate = "file.txt";

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

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

            // ReSharper disable once ConvertToLocalFunction
            Action <DateTime> updateCreationTimeAction = creationTimeUtc =>
            {
                Thread.Sleep(SleepTimeToEnsureWaiterInitializationHasCompleted);
                fileSystem.File.SetCreationTimeUtc(pathToFileToUpdate, creationTimeUtc);
            };

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter = NotifyFilters.CreationTime;

                Task.Run(() => updateCreationTimeAction(1.January(2000)));

                // Act
                WaitForChangedResult result1 = watcher.WaitForChanged(WatcherChangeTypes.Changed, MaxTestDurationInMilliseconds);

                Task.Run(() => updateCreationTimeAction(2.January(2000)));

                WaitForChangedResult result2 = watcher.WaitForChanged(WatcherChangeTypes.Changed, MaxTestDurationInMilliseconds);

                // Assert
                result1.TimedOut.Should().BeFalse();
                result1.ChangeType.Should().Be(WatcherChangeTypes.Changed);
                result1.Name.Should().Be(fileNameToUpdate);
                result1.OldName.Should().BeNull();

                result2.TimedOut.Should().BeFalse();
                result2.ChangeType.Should().Be(WatcherChangeTypes.Changed);
                result2.Name.Should().Be(fileNameToUpdate);
                result2.OldName.Should().BeNull();
            }
        }
Ejemplo n.º 13
0
        private void When_setting_path_to_existing_local_file_it_must_fail()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => watcher.Path = path;

                // Assert
                action.Should().ThrowExactly <ArgumentException>().WithMessage(@"The directory name c:\some\file.txt is invalid.");
            }
        }
Ejemplo n.º 14
0
        private void When_setting_path_to_empty_string_it_must_fail()
        {
            // Arrange
            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher())
            {
                watcher.Path = string.Empty;

                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => watcher.EnableRaisingEvents = true;

                // Assert
                watcher.Path.Should().Be(string.Empty);
                action.Should().ThrowExactly <FileNotFoundException>().WithMessage("Error reading the  directory.");
            }
        }
Ejemplo n.º 15
0
        private void When_setting_path_on_disposed_watcher_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

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

            FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher();

            watcher.Dispose();

            // Act
            watcher.Path = directoryToWatch;

            // Assert
            watcher.Path.Should().Be(directoryToWatch);
        }
        private void When_waiting_for_file_rename_while_files_are_renamed_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string sourceFileName1  = "source1.txt";
            const string targetFileName1  = "target1.txt";
            const string sourceFileName2  = "source2.txt";
            const string targetFileName2  = "target2.txt";

            string pathToSourceFile1 = Path.Combine(directoryToWatch, sourceFileName1);
            string pathToTargetFile1 = Path.Combine(directoryToWatch, targetFileName1);
            string pathToSourceFile2 = Path.Combine(directoryToWatch, sourceFileName2);
            string pathToTargetFile2 = Path.Combine(directoryToWatch, targetFileName2);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToSourceFile1)
                                        .IncludingEmptyFile(pathToSourceFile2)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter = NotifyFilters.FileName;

                Task.Run(() =>
                {
                    // ReSharper disable once AccessToDisposedClosure
                    BlockUntilStarted(watcher);

                    fileSystem.File.Move(pathToSourceFile1, pathToTargetFile1);
                    fileSystem.File.Move(pathToSourceFile2, pathToTargetFile2);
                });

                // Act
                WaitForChangedResult result = watcher.WaitForChanged(WatcherChangeTypes.Renamed, MaxTestDurationInMilliseconds);

                // Assert
                result.TimedOut.Should().BeFalse();
                result.ChangeType.Should().Be(WatcherChangeTypes.Renamed);
                result.Name.Should().Be(targetFileName1);
                result.OldName.Should().Be(sourceFileName1);
            }
        }
        private void When_waiting_for_changes_with_negative_timeout_it_must_fail()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => watcher.WaitForChanged(WatcherChangeTypes.All, -5);

                // Assert
                action.Should().ThrowExactly <ArgumentOutOfRangeException>().WithMessage(
                    "Specified argument was out of the range of valid values.*");
            }
        }
        private void When_waiting_for_change_it_must_apply_filters()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToMismatchingFile      = Path.Combine(directoryToWatch, "OtherName.txt");
            string pathToMismatchingDirectory = Path.Combine(directoryToWatch, "SomeFolderIgnored");
            string pathToMatchingFile         = Path.Combine(directoryToWatch, "Subfolder", "SomeFile.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToMismatchingFile)
                                        .IncludingDirectory(Path.Combine(directoryToWatch, "Subfolder"))
                                        .IncludingEmptyFile(pathToMatchingFile)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter          = NotifyFilters.FileName | NotifyFilters.Size;
                watcher.IncludeSubdirectories = true;
                watcher.Filter = "Some*";

                Task.Run(() =>
                {
                    // ReSharper disable once AccessToDisposedClosure
                    BlockUntilStarted(watcher);

                    fileSystem.Directory.CreateDirectory(pathToMismatchingDirectory);
                    fileSystem.File.AppendAllText(pathToMismatchingFile, "IgnoreMe");
                    fileSystem.File.AppendAllText(pathToMatchingFile, "NotifyForMe");
                });

                // Act
                WaitForChangedResult result = watcher.WaitForChanged(WatcherChangeTypes.All, MaxTestDurationInMilliseconds);

                // Assert
                result.TimedOut.Should().BeFalse();
                result.ChangeType.Should().Be(WatcherChangeTypes.Changed);
                result.Name.Should().Be(@"Subfolder\SomeFile.txt");
                result.OldName.Should().BeNull();
            }
        }
Ejemplo n.º 19
0
        private void When_filtering_directory_tree_with_asterisk_dot_pattern_it_must_not_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToFileToUpdate1      = Path.Combine(directoryToWatch, "RootFile");
            string pathToFileToUpdate2      = Path.Combine(directoryToWatch, "SubFolder", "SubFile");
            string pathToDirectoryToUpdate1 = Path.Combine(directoryToWatch, "RootFolder");
            string pathToDirectoryToUpdate2 = Path.Combine(directoryToWatch, "SubFolder", "Nested");

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

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

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

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    watcher.Filter.Should().Be("*.");

                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().BeEmpty();
                }
            }
        }
Ejemplo n.º 20
0
        private void When_starting_watcher_for_removed_directory_it_must_fail()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                fileSystem.Directory.Delete(directoryToWatch);

                // Act
                // ReSharper disable once AccessToDisposedClosure
                Action action = () => watcher.EnableRaisingEvents = true;

                // Assert
                action.Should().ThrowExactly <FileNotFoundException>().WithMessage(@"Error reading the c:\some directory.");
            }
        }
        private void When_waiting_for_changes_while_timeout_expires_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                // Act
                WaitForChangedResult result = watcher.WaitForChanged(WatcherChangeTypes.All, 1000);

                // Assert
                result.TimedOut.Should().BeTrue();
                result.ChangeType.Should().Be(0);
                result.Name.Should().BeNull();
                result.OldName.Should().BeNull();
            }
        }
Ejemplo n.º 22
0
        private void When_changing_filter_on_running_watcher_it_must_not_restart()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToFileToUpdate1 = Path.Combine(directoryToWatch, "file1.txt");
            string pathToFileToUpdate2 = Path.Combine(directoryToWatch, "file2.txt");

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

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

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

                    // Act
                    watcher.Filter = "*.txt";

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

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(@"
                        * file1.txt
                        * file2.txt
                        ".TrimLines());
                }
            }
        }
Ejemplo n.º 23
0
        private void When_disposing_unstarted_watcher_multiple_times_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

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

            // Act
            FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch);

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

            // Assert
            action.Should().NotThrow();
        }
        private void When_async_appending_to_stream_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch       = @"c:\some";
            const string containerDirectoryName = "Container";
            const string fileNameToWrite        = "file.txt";

            string pathToFileToWrite = Path.Combine(directoryToWatch, containerDirectoryName, fileNameToWrite);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingBinaryFile(pathToFileToWrite, BufferFactory.Create(1024))
                                        .Build();

            var buffer = new byte[512];

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

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    using (IFileStream stream = fileSystem.File.Open(pathToFileToWrite, FileMode.Open, FileAccess.ReadWrite))
                    {
                        stream.Seek(0, SeekOrigin.End);

                        // Act
                        Task task = stream.WriteAsync(buffer, 0, buffer.Length);
                        task.Wait();
                    }

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
Ejemplo n.º 25
0
        private void When_setting_path_to_relative_local_directory_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();

            fileSystem.Directory.SetCurrentDirectory(@"c:\");

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

                // Act
                watcher.Path = "some";

                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(pathToFileToUpdate);
                    args.Name.Should().Be(fileNameToUpdate);
                }
            }
        }
        private void When_waiting_for_file_change_while_file_attributes_are_changed_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch  = @"c:\some";
            const string fileNameToUpdate1 = "file1.txt";
            const string fileNameToUpdate2 = "file2.txt";

            string pathToFileToUpdate1 = Path.Combine(directoryToWatch, fileNameToUpdate1);
            string pathToFileToUpdate2 = Path.Combine(directoryToWatch, fileNameToUpdate2);

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

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter = NotifyFilters.Attributes;

                Task.Run(() =>
                {
                    // ReSharper disable once AccessToDisposedClosure
                    BlockUntilStarted(watcher);

                    fileSystem.File.SetAttributes(pathToFileToUpdate1, FileAttributes.System);
                    fileSystem.File.SetAttributes(pathToFileToUpdate2, FileAttributes.System);
                });

                // Act
                WaitForChangedResult result = watcher.WaitForChanged(WatcherChangeTypes.Changed, MaxTestDurationInMilliseconds);

                // Assert
                result.TimedOut.Should().BeFalse();
                result.ChangeType.Should().Be(WatcherChangeTypes.Changed);
                result.Name.Should().Be(fileNameToUpdate1);
                result.OldName.Should().BeNull();
            }
        }
        private void When_waiting_for_directory_deletion_while_directories_are_deleted_it_must_succeed()
        {
            // Arrange
            const string directoryToWatch       = @"c:\some";
            const string directoryNameToDelete1 = "subfolder1";
            const string directoryNameToDelete2 = "subfolder2";

            string pathToDirectoryToDelete1 = Path.Combine(directoryToWatch, directoryNameToDelete1);
            string pathToDirectoryToDelete2 = Path.Combine(directoryToWatch, directoryNameToDelete2);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingDirectory(pathToDirectoryToDelete1)
                                        .IncludingDirectory(pathToDirectoryToDelete2)
                                        .Build();

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter = NotifyFilters.DirectoryName;

                Task.Run(() =>
                {
                    // ReSharper disable once AccessToDisposedClosure
                    BlockUntilStarted(watcher);

                    fileSystem.Directory.Delete(pathToDirectoryToDelete1);
                    fileSystem.Directory.Delete(pathToDirectoryToDelete2);
                });

                // Act
                WaitForChangedResult result = watcher.WaitForChanged(WatcherChangeTypes.Deleted, MaxTestDurationInMilliseconds);

                // Assert
                result.TimedOut.Should().BeFalse();
                result.ChangeType.Should().Be(WatcherChangeTypes.Deleted);
                result.Name.Should().Be(directoryNameToDelete1);
                result.OldName.Should().BeNull();
            }
        }
Ejemplo n.º 28
0
        private void When_copying_file_to_subdirectory_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch         = @"c:\some";
            const string containerDirectoryName   = "Container";
            const string sourceFileName           = "source.txt";
            const string destinationDirectoryName = "Subfolder";
            const string destinationFileName      = "target.txt";

            string pathToContainerDirectory   = Path.Combine(directoryToWatch, containerDirectoryName);
            string pathToSourceFile           = Path.Combine(pathToContainerDirectory, sourceFileName);
            string pathToDestinationDirectory = Path.Combine(pathToContainerDirectory, destinationDirectoryName);
            string pathToDestinationFile      = Path.Combine(pathToDestinationDirectory, destinationFileName);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToSourceFile, "Example")
                                        .IncludingDirectory(pathToDestinationDirectory)
                                        .Build();

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

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

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
Ejemplo n.º 29
0
        private void When_replacing_file_in_different_directory_with_backup_and_custom_attributes_it_must_raise_events(
            NotifyFilters filters, [NotNull] string expectedText)
        {
            // Arrange
            const string directoryToWatch = @"c:\some";
            const string containerName    = "Container";

            string pathToSourceFile      = Path.Combine(directoryToWatch, containerName, "sourceDir", "source.txt");
            string pathToDestinationFile = Path.Combine(directoryToWatch, containerName, "targetDir", "target.txt");
            string pathToBackupDirectory = Path.Combine(directoryToWatch, containerName, "backupDir");
            string pathToBackupFile      = Path.Combine(pathToBackupDirectory, "backup.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToSourceFile, "SourceText", attributes: FileAttributes.Hidden)
                                        .IncludingTextFile(pathToDestinationFile, "DestinationText",
                                                           attributes: FileAttributes.Hidden | FileAttributes.System)
                                        .IncludingDirectory(pathToBackupDirectory)
                                        .Build();

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

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

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(expectedText);
                }
            }
        }
        private void When_moving_readonly_hidden_system_archive_file_to_sibling_directory_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, "srcFolder", sourceFileName);
            string pathToDestinationDirectory = Path.Combine(directoryToWatch, "dstFolder");
            string pathToDestinationFile      = Path.Combine(pathToDestinationDirectory, destinationFileName);

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToSourceFile, "CONTENT",
                                                           attributes: FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System | FileAttributes.Archive)
                                        .IncludingDirectory(pathToDestinationDirectory)
                                        .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);
                }
            }
        }