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); } } }
private void When_renaming_directory_tree_it_must_raise_events(NotifyFilters filters, [NotNull] string expectedText) { // Arrange const string directoryToWatch = @"c:\some"; const string containerDirectoryName = "Container"; const string sourceDirectoryName = "MoveSource"; const string destinationDirectoryName = "MoveTarget"; 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")) .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FolderA\FileInA.txt")) .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FolderB\FileInB.txt")) .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FolderC\SubFolderC\FileInSubFolderC.txt")) .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FileInRoot2.txt")) .IncludingEmptyFile(Path.Combine(pathToSourceDirectory, @"FileInRoot1.txt")) .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); } } }
private void When_filtering_directory_with_absolute_path_without_drive_pattern_it_must_not_raise_events() { // Arrange const string directoryToWatch = @"c:\some"; string pathToFileToUpdate1 = Path.Combine(directoryToWatch, "NonMatchingFile"); string pathToDirectoryToUpdate1 = Path.Combine(directoryToWatch, "NonMatchingFolder"); FakeFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingEmptyFile(pathToFileToUpdate1) .IncludingDirectory(pathToDirectoryToUpdate1) .Build(); fileSystem.Directory.SetCurrentDirectory(@"c:\"); using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch)) { watcher.NotifyFilter = TestNotifyFilters.All; watcher.IncludeSubdirectories = true; watcher.Filter = @"\some\*"; using (var listener = new FileSystemWatcherEventListener(watcher)) { // Act fileSystem.File.SetAttributes(pathToFileToUpdate1, FileAttributes.Hidden); fileSystem.File.SetAttributes(pathToDirectoryToUpdate1, FileAttributes.Hidden); watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds); // Assert watcher.Filter.Should().Be(@"\some\*"); string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText()); text.Should().BeEmpty(); } } }
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); } } }
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); } } }
private void When_filtering_directory_tree_with_asterisk_pattern_it_must_raise_events() { // Arrange const string directoryToWatch = @"c:\some"; string pathToFileToUpdate1 = Path.Combine(directoryToWatch, "RootFile1.txt"); string pathToFileToUpdate2 = Path.Combine(directoryToWatch, "RootFile2"); string pathToFileToUpdate3 = Path.Combine(directoryToWatch, "SubFolder", "SubFile1.txt"); string pathToFileToUpdate4 = Path.Combine(directoryToWatch, "SubFolder", "SubFile2"); string pathToDirectoryToUpdate1 = Path.Combine(directoryToWatch, "RootFolder1.txt"); string pathToDirectoryToUpdate2 = Path.Combine(directoryToWatch, "RootFolder2"); string pathToDirectoryToUpdate3 = Path.Combine(directoryToWatch, "SubFolder", "DeepFolder1.txt"); string pathToDirectoryToUpdate4 = Path.Combine(directoryToWatch, "SubFolder", "DeepFolder2"); FakeFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingEmptyFile(pathToFileToUpdate1) .IncludingEmptyFile(pathToFileToUpdate2) .IncludingEmptyFile(pathToFileToUpdate3) .IncludingEmptyFile(pathToFileToUpdate4) .IncludingDirectory(pathToDirectoryToUpdate1) .IncludingDirectory(pathToDirectoryToUpdate2) .IncludingDirectory(pathToDirectoryToUpdate3) .IncludingDirectory(pathToDirectoryToUpdate4) .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(pathToFileToUpdate3, FileAttributes.Hidden); fileSystem.File.SetAttributes(pathToFileToUpdate4, FileAttributes.Hidden); fileSystem.File.SetAttributes(pathToDirectoryToUpdate1, FileAttributes.Hidden); fileSystem.File.SetAttributes(pathToDirectoryToUpdate2, FileAttributes.Hidden); fileSystem.File.SetAttributes(pathToDirectoryToUpdate3, FileAttributes.Hidden); fileSystem.File.SetAttributes(pathToDirectoryToUpdate4, FileAttributes.Hidden); watcher.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds); // Assert watcher.Filter.Should().Be("*"); string text = string.Join(Environment.NewLine, listener.GetEventsCollectedAsText()); text.Should().Be(@" * RootFile1.txt * RootFile2 * SubFolder\SubFile1.txt * SubFolder\SubFile2 * RootFolder1.txt * RootFolder2 * SubFolder\DeepFolder1.txt * SubFolder\DeepFolder2 ".TrimLines()); } } }
private void When_watchers_are_attached_to_different_file_systems_it_must_raise_events() { // 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 fileSystem1 = new FakeFileSystemBuilder() .IncludingEmptyFile(pathToFileToUpdate1) .Build(); FakeFileSystem fileSystem2 = new FakeFileSystemBuilder() .IncludingEmptyFile(pathToFileToUpdate2) .Build(); using (FakeFileSystemWatcher watcher1 = fileSystem1.ConstructFileSystemWatcher(directoryToWatch)) { watcher1.NotifyFilter = TestNotifyFilters.All; string text1; string text2; using (var listener1 = new FileSystemWatcherEventListener(watcher1)) { fileSystem1.File.SetLastWriteTimeUtc(pathToFileToUpdate1, 1.January(2002)); using (FakeFileSystemWatcher watcher2 = fileSystem2.ConstructFileSystemWatcher(directoryToWatch)) { watcher2.NotifyFilter = TestNotifyFilters.All; using (var listener2 = new FileSystemWatcherEventListener(watcher2)) { fileSystem2.File.SetLastWriteTimeUtc(pathToFileToUpdate2, 2.January(2002)); watcher2.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds); text2 = string.Join(Environment.NewLine, listener2.GetEventsCollectedAsText()); } } fileSystem1.File.SetLastWriteTimeUtc(pathToFileToUpdate1, 3.January(2002)); watcher1.FinishAndWaitForFlushed(MaxTestDurationInMilliseconds); text1 = string.Join(Environment.NewLine, listener1.GetEventsCollectedAsText()); } // Assert text1.Should().Be(@" * file1.txt * file1.txt ".TrimLines()); text2.Should().Be(@" * file2.txt ".TrimLines()); } }