Exemple #1
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();
                }
            }
        }
Exemple #2
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");
                }
            }
        }
Exemple #3
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);
                }
            }
        }
Exemple #4
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");
                }
            }
        }
Exemple #5
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);
                }
            }
        }
Exemple #6
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);
                }
            }
        }
        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_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);
                }
            }
        }
Exemple #9
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);
                }
            }
        }
        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);
                }
            }
        }
Exemple #11
0
        private void When_replacing_file_in_different_directory_with_existing_backup_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 pathToBackupFile      = Path.Combine(directoryToWatch, containerName, "backupDir", "backup.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingTextFile(pathToSourceFile, "SourceText")
                                        .IncludingTextFile(pathToDestinationFile, "DestinationText")
                                        .IncludingTextFile(pathToBackupFile, "Backup-Text")
                                        .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_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);
                }
            }
        }
        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();
                }
            }
        }
        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);
                }
            }
        }
        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();
                }
            }
        }
Exemple #16
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);
                }
            }
        }
        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();
                }
            }
        }
Exemple #18
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);
                }
            }
        }
Exemple #19
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();
                }
            }
        }
Exemple #20
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();
                }
            }
        }
Exemple #21
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);
                }
            }
        }
Exemple #22
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");
                }
            }
        }
        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();
                }
            }
        }
Exemple #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();
                }
            }
        }
Exemple #25
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);
                }
            }
        }
        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);
                }
            }
        }
Exemple #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);
                }
            }
        }
        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();
                    }
                }
            }
        }
Exemple #29
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());
            }
        }
Exemple #30
0
        private void When_stopping_and_resuming_watcher_it_must_raise_events()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

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

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

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

                using (var listener = new FileSystemWatcherEventListener(watcher))
                {
                    fileSystem.File.SetAttributes(pathToFileToUpdate1, FileAttributes.Hidden);
                    Thread.Sleep(SleepTimeToEnsureOperationHasArrivedAtWatcherConsumerLoop);

                    // Act
                    watcher.EnableRaisingEvents = false;

                    fileSystem.File.SetAttributes(pathToFileToUpdate2, FileAttributes.Hidden);
                    Thread.Sleep(SleepTimeToEnsureOperationHasArrivedAtWatcherConsumerLoop);

                    watcher.EnableRaisingEvents = true;

                    fileSystem.File.SetAttributes(pathToFileToUpdate3, FileAttributes.Hidden);
                    Thread.Sleep(SleepTimeToEnsureOperationHasArrivedAtWatcherConsumerLoop);

                    watcher.EnableRaisingEvents = false;

                    fileSystem.File.SetAttributes(pathToFileToUpdate4, FileAttributes.Hidden);
                    Thread.Sleep(SleepTimeToEnsureOperationHasArrivedAtWatcherConsumerLoop);

                    watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds);

                    // Assert
                    string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText());
                    text.Should().Be(@"
                        * file1.txt
                        * file3.txt
                        ".TrimLines());
                }
            }
        }