|
| 1 | +#![cfg(target_os = "windows")] |
| 2 | +#![cfg(feature = "network_tests")] |
| 3 | + |
| 4 | +use std::{env, path::Path}; |
| 5 | + |
| 6 | +use crossbeam_channel::unbounded; |
| 7 | +use notify::*; |
| 8 | +use tempfile::TempDir; |
| 9 | + |
| 10 | +use utils::*; |
| 11 | +mod utils; |
| 12 | + |
| 13 | +const NETWORK_PATH: &str = ""; // eg.: \\\\MY-PC\\Users\\MyName |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn watch_relative_network_directory() { |
| 17 | + let tdir = TempDir::new_in(NETWORK_PATH).expect("failed to create temporary directory"); |
| 18 | + tdir.create("dir1"); |
| 19 | + |
| 20 | + env::set_current_dir(tdir.path()).expect("failed to change working directory"); |
| 21 | + |
| 22 | + let (tx, rx) = unbounded(); |
| 23 | + let mut watcher: RecommendedWatcher = |
| 24 | + Watcher::new(tx).expect("failed to create recommended watcher"); |
| 25 | + watcher |
| 26 | + .watch(Path::new("dir1"), RecursiveMode::Recursive) |
| 27 | + .expect("failed to watch directory"); |
| 28 | + |
| 29 | + sleep_windows(100); |
| 30 | + |
| 31 | + tdir.create("dir1/file1"); |
| 32 | + |
| 33 | + assert_eq!( |
| 34 | + recv_events(&rx), |
| 35 | + vec![( |
| 36 | + tdir.path().join("dir1/file1"), |
| 37 | + EventKind::Create(event::CreateKind::Any), |
| 38 | + None |
| 39 | + ),] |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn watch_relative_network_file() { |
| 45 | + let tdir = TempDir::new_in(NETWORK_PATH).expect("failed to create temporary directory"); |
| 46 | + tdir.create("file1"); |
| 47 | + |
| 48 | + env::set_current_dir(tdir.path()).expect("failed to change working directory"); |
| 49 | + |
| 50 | + let (tx, rx) = unbounded(); |
| 51 | + let mut watcher: RecommendedWatcher = |
| 52 | + Watcher::new(tx).expect("failed to create recommended watcher"); |
| 53 | + watcher |
| 54 | + .watch(Path::new("file1"), RecursiveMode::Recursive) |
| 55 | + .expect("failed to watch file"); |
| 56 | + |
| 57 | + sleep_windows(100); |
| 58 | + |
| 59 | + tdir.write("file1"); |
| 60 | + |
| 61 | + assert_eq!( |
| 62 | + recv_events(&rx), |
| 63 | + vec![( |
| 64 | + tdir.path().join("file1"), |
| 65 | + EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)), |
| 66 | + None |
| 67 | + ),] |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +fn watch_absolute_network_directory() { |
| 73 | + if NETWORK_PATH.is_empty() { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + let tdir = TempDir::new_in(NETWORK_PATH).expect("failed to create temporary directory"); |
| 78 | + tdir.create("dir1"); |
| 79 | + |
| 80 | + let watch_path = tdir.path().join("dir1"); |
| 81 | + let (tx, rx) = unbounded(); |
| 82 | + let mut watcher: RecommendedWatcher = |
| 83 | + Watcher::new(tx).expect("failed to create recommended watcher"); |
| 84 | + watcher |
| 85 | + .watch(&watch_path, RecursiveMode::Recursive) |
| 86 | + .expect("failed to watch directory"); |
| 87 | + |
| 88 | + sleep_windows(100); |
| 89 | + |
| 90 | + tdir.create("dir1/file1"); |
| 91 | + |
| 92 | + assert_eq!( |
| 93 | + recv_events(&rx), |
| 94 | + vec![( |
| 95 | + watch_path.join("file1"), |
| 96 | + EventKind::Create(event::CreateKind::Any), |
| 97 | + None |
| 98 | + ),] |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn watch_absolute_network_file() { |
| 104 | + if NETWORK_PATH.is_empty() { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + let tdir = TempDir::new_in(NETWORK_PATH).expect("failed to create temporary directory"); |
| 109 | + tdir.create("file1"); |
| 110 | + |
| 111 | + let watch_path = tdir.path().join("file1"); |
| 112 | + let (tx, rx) = unbounded(); |
| 113 | + let mut watcher: RecommendedWatcher = |
| 114 | + Watcher::new(tx).expect("failed to create recommended watcher"); |
| 115 | + watcher |
| 116 | + .watch(&watch_path, RecursiveMode::Recursive) |
| 117 | + .expect("failed to watch directory"); |
| 118 | + |
| 119 | + sleep_windows(100); |
| 120 | + |
| 121 | + tdir.write("file1"); |
| 122 | + |
| 123 | + assert_eq!( |
| 124 | + recv_events(&rx), |
| 125 | + vec![( |
| 126 | + watch_path, |
| 127 | + EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)), |
| 128 | + None |
| 129 | + ),] |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +#[test] |
| 134 | +fn watch_canonicalized_network_directory() { |
| 135 | + if NETWORK_PATH.is_empty() { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + let tdir = TempDir::new_in(NETWORK_PATH).expect("failed to create temporary directory"); |
| 140 | + tdir.create("dir1"); |
| 141 | + |
| 142 | + let watch_path = tdir |
| 143 | + .path() |
| 144 | + .canonicalize() |
| 145 | + .expect("failed to canonicalize path") |
| 146 | + .join("dir1"); |
| 147 | + let (tx, rx) = unbounded(); |
| 148 | + let mut watcher: RecommendedWatcher = |
| 149 | + Watcher::new(tx).expect("failed to create recommended watcher"); |
| 150 | + watcher |
| 151 | + .watch(&watch_path, RecursiveMode::Recursive) |
| 152 | + .expect("failed to watch directory"); |
| 153 | + |
| 154 | + sleep_windows(100); |
| 155 | + |
| 156 | + tdir.create("dir1/file1"); |
| 157 | + |
| 158 | + assert_eq!( |
| 159 | + recv_events(&rx), |
| 160 | + vec![( |
| 161 | + watch_path.join("file1"), |
| 162 | + EventKind::Create(event::CreateKind::Any), |
| 163 | + None |
| 164 | + ),] |
| 165 | + ); |
| 166 | +} |
| 167 | + |
| 168 | +#[test] |
| 169 | +fn watch_canonicalized_network_file() { |
| 170 | + if NETWORK_PATH.is_empty() { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + let tdir = TempDir::new_in(NETWORK_PATH).expect("failed to create temporary directory"); |
| 175 | + tdir.create("file1"); |
| 176 | + |
| 177 | + let watch_path = tdir |
| 178 | + .path() |
| 179 | + .canonicalize() |
| 180 | + .expect("failed to canonicalize path") |
| 181 | + .join("file1"); |
| 182 | + let (tx, rx) = unbounded(); |
| 183 | + let mut watcher: RecommendedWatcher = |
| 184 | + Watcher::new(tx).expect("failed to create recommended watcher"); |
| 185 | + watcher |
| 186 | + .watch(&watch_path, RecursiveMode::Recursive) |
| 187 | + .expect("failed to watch directory"); |
| 188 | + |
| 189 | + sleep_windows(100); |
| 190 | + |
| 191 | + tdir.write("file1"); |
| 192 | + |
| 193 | + assert_eq!( |
| 194 | + recv_events(&rx), |
| 195 | + vec![( |
| 196 | + watch_path, |
| 197 | + EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)), |
| 198 | + None |
| 199 | + ),] |
| 200 | + ); |
| 201 | +} |
0 commit comments