public void ShouldFireIfCreatedRemoved() { var watchdog = new DirectoryWatchdog(_directory, "*.txt", _watchdogThread); var events = new BlockingCollection <WatchdogEventArgs>(); watchdog.Changed += (o, e) => events.Add(e); watchdog.Start(); var subdirectory = Path.Combine(_directory, "sub"); Directory.CreateDirectory(subdirectory); File.WriteAllBytes(Path.Combine(_directory, "1.pdf"), new byte[] { 1, 2, 3 }); File.WriteAllBytes(Path.Combine(subdirectory, "1.txt"), new byte[] { 1, 2, 3 }); var ev = events.Take(); Assert.AreEqual(Path.Combine(subdirectory, "1.txt"), ev.FilePath); Assert.AreEqual(ChangeKind.Created, ev.ChangeKind); File.Delete(Path.Combine(subdirectory, "1.txt")); ev = events.Take(); Assert.AreEqual(Path.Combine(subdirectory, "1.txt"), ev.FilePath); Assert.AreEqual(ChangeKind.Deleted, ev.ChangeKind); NoMoreEvents(events); watchdog.Dispose(); }
public void ShouldFireIfMovedFromOutside() { var watchdog = new DirectoryWatchdog(_directory, "*.txt", _watchdogThread); var events = new BlockingCollection <WatchdogEventArgs>(); watchdog.Changed += (o, e) => events.Add(e); watchdog.Start(); var subdirectory = Path.Combine(_directory, "sub"); Directory.CreateDirectory(subdirectory); var tempFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); File.WriteAllBytes(tempFilePath, new byte[] { 1, 2, 3 }); File.Move(tempFilePath, Path.Combine(subdirectory, "1.txt")); var ev = events.Take(); Assert.AreEqual(Path.Combine(subdirectory, "1.txt"), ev.FilePath); Assert.AreEqual(ChangeKind.Created, ev.ChangeKind); NoMoreEvents(events); watchdog.Dispose(); File.Delete(tempFilePath); }
public void ShouldFireOnExistingFiles() { File.WriteAllBytes(Path.Combine(_directory, "1.txt"), new byte[] { 1, 2, 3 }); File.WriteAllBytes(Path.Combine(_directory, "2.pdf"), new byte[] { 1, 2, 3 }); var watchdog = new DirectoryWatchdog(_directory, "*.txt", _watchdogThread); var events = new BlockingCollection <WatchdogEventArgs>(); watchdog.Changed += (o, e) => events.Add(e); watchdog.Start(); var ev = events.Take(); Assert.AreEqual(Path.Combine(_directory, "1.txt"), ev.FilePath); Assert.AreEqual(ChangeKind.Existed, ev.ChangeKind); NoMoreEvents(events); watchdog.Dispose(); }