Skip to content

Commit 3639269

Browse files
committed
Copy path test from v4
1 parent 458f0d4 commit 3639269

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed

tests/event_path.rs

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
use std::{
2+
env,
3+
path::{Path, PathBuf},
4+
};
5+
6+
use crossbeam_channel::{unbounded, Receiver};
7+
use notify::*;
8+
9+
use utils::*;
10+
mod utils;
11+
12+
const TEMP_DIR: &str = "temp_dir";
13+
14+
#[cfg(target_os = "windows")]
15+
fn recv_events_simple(rx: &Receiver<Result<Event>>) -> Vec<(PathBuf, EventKind, Option<usize>)> {
16+
recv_events(&rx)
17+
}
18+
19+
#[cfg(target_os = "macos")]
20+
fn recv_events_simple(rx: &Receiver<Result<Event>>) -> Vec<(PathBuf, EventKind, Option<usize>)> {
21+
let mut events = Vec::new();
22+
for (path, ev, cookie) in recv_events(&rx) {
23+
if let EventKind::Create(_) | EventKind::Modify(event::ModifyKind::Data(_)) = ev {
24+
events.push((
25+
path,
26+
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
27+
cookie,
28+
));
29+
} else {
30+
events.push((path, ev, cookie));
31+
}
32+
}
33+
events
34+
}
35+
36+
#[cfg(target_os = "linux")]
37+
fn recv_events_simple(rx: &Receiver<Result<Event>>) -> Vec<(PathBuf, EventKind, Option<usize>)> {
38+
let mut events = recv_events(rx);
39+
events.retain(|(_, ref ev, _)| matches!(ev, EventKind::Access(_)));
40+
events
41+
}
42+
43+
#[test]
44+
fn watch_relative() {
45+
// both of the following tests set the same environment variable, so they must not run in parallel
46+
{
47+
// watch_relative_directory
48+
let tdir = tempfile::Builder::new()
49+
.prefix(TEMP_DIR)
50+
.tempdir()
51+
.expect("failed to create temporary directory");
52+
tdir.create("dir1");
53+
54+
sleep_macos(10);
55+
56+
env::set_current_dir(tdir.path()).expect("failed to change working directory");
57+
58+
let (tx, rx) = unbounded();
59+
let mut watcher: RecommendedWatcher =
60+
Watcher::new(tx).expect("failed to create recommended watcher");
61+
watcher
62+
.watch(Path::new("dir1"), RecursiveMode::Recursive)
63+
.expect("failed to watch directory");
64+
65+
sleep_windows(100);
66+
67+
tdir.create("dir1/file1");
68+
69+
if cfg!(target_os = "macos") {
70+
assert_eq!(
71+
recv_events_simple(&rx),
72+
vec![
73+
(
74+
tdir.mkpath("dir1/file1"),
75+
EventKind::Create(event::CreateKind::Any),
76+
None
77+
), // fsevents always returns canonicalized paths
78+
]
79+
);
80+
} else {
81+
assert_eq!(
82+
recv_events_simple(&rx),
83+
vec![(
84+
tdir.path().join("dir1/file1"),
85+
EventKind::Create(event::CreateKind::Any),
86+
None
87+
),]
88+
);
89+
}
90+
}
91+
{
92+
// watch_relative_file
93+
let tdir = tempfile::Builder::new()
94+
.prefix(TEMP_DIR)
95+
.tempdir()
96+
.expect("failed to create temporary directory");
97+
tdir.create("file1");
98+
99+
env::set_current_dir(tdir.path()).expect("failed to change working directory");
100+
101+
let (tx, rx) = unbounded();
102+
let mut watcher: RecommendedWatcher =
103+
Watcher::new(tx).expect("failed to create recommended watcher");
104+
watcher
105+
.watch(Path::new("file1"), RecursiveMode::Recursive)
106+
.expect("failed to watch file");
107+
108+
sleep_windows(100);
109+
110+
tdir.write("file1");
111+
112+
if cfg!(target_os = "macos") {
113+
assert_eq!(
114+
recv_events_simple(&rx),
115+
vec![
116+
(
117+
tdir.mkpath("file1"),
118+
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
119+
None
120+
), // fsevents always returns canonicalized paths
121+
]
122+
);
123+
} else {
124+
assert_eq!(
125+
recv_events_simple(&rx),
126+
vec![(
127+
tdir.path().join("file1"),
128+
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
129+
None
130+
),]
131+
);
132+
}
133+
}
134+
}
135+
136+
#[test]
137+
fn watch_absolute_directory() {
138+
let tdir = tempfile::Builder::new()
139+
.prefix(TEMP_DIR)
140+
.tempdir()
141+
.expect("failed to create temporary directory");
142+
tdir.create("dir1");
143+
144+
sleep_macos(10);
145+
146+
let watch_path = tdir.path().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+
if cfg!(target_os = "macos") {
159+
assert_eq!(
160+
recv_events_simple(&rx),
161+
vec![
162+
(
163+
tdir.mkpath("dir1/file1"),
164+
EventKind::Create(event::CreateKind::Any),
165+
None
166+
), // fsevents always returns canonicalized paths
167+
]
168+
);
169+
} else {
170+
assert_eq!(
171+
recv_events_simple(&rx),
172+
vec![(
173+
watch_path.join("file1"),
174+
EventKind::Create(event::CreateKind::Any),
175+
None
176+
),]
177+
);
178+
}
179+
}
180+
181+
#[test]
182+
fn watch_absolute_file() {
183+
let tdir = tempfile::Builder::new()
184+
.prefix(TEMP_DIR)
185+
.tempdir()
186+
.expect("failed to create temporary directory");
187+
tdir.create("file1");
188+
189+
let watch_path = tdir.path().join("file1");
190+
let (tx, rx) = unbounded();
191+
let mut watcher: RecommendedWatcher =
192+
Watcher::new(tx).expect("failed to create recommended watcher");
193+
watcher
194+
.watch(&watch_path, RecursiveMode::Recursive)
195+
.expect("failed to watch directory");
196+
197+
sleep_windows(100);
198+
199+
tdir.write("file1");
200+
201+
if cfg!(target_os = "macos") {
202+
assert_eq!(
203+
recv_events_simple(&rx),
204+
vec![
205+
(
206+
tdir.mkpath("file1"),
207+
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
208+
None
209+
), // fsevents always returns canonicalized paths
210+
]
211+
);
212+
} else {
213+
assert_eq!(
214+
recv_events_simple(&rx),
215+
vec![(
216+
watch_path,
217+
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
218+
None
219+
),]
220+
);
221+
}
222+
}
223+
224+
#[test]
225+
fn watch_canonicalized_directory() {
226+
let tdir = tempfile::Builder::new()
227+
.prefix(TEMP_DIR)
228+
.tempdir()
229+
.expect("failed to create temporary directory");
230+
tdir.create("dir1");
231+
232+
sleep_macos(10);
233+
234+
let watch_path = tdir
235+
.path()
236+
.canonicalize()
237+
.expect("failed to canonicalize path")
238+
.join("dir1");
239+
let (tx, rx) = unbounded();
240+
let mut watcher: RecommendedWatcher =
241+
Watcher::new(tx).expect("failed to create recommended watcher");
242+
watcher
243+
.watch(&watch_path, RecursiveMode::Recursive)
244+
.expect("failed to watch directory");
245+
246+
sleep_windows(100);
247+
248+
tdir.create("dir1/file1");
249+
250+
assert_eq!(
251+
recv_events_simple(&rx),
252+
vec![(
253+
watch_path.join("file1"),
254+
EventKind::Create(event::CreateKind::Any),
255+
None
256+
),]
257+
);
258+
}
259+
260+
#[test]
261+
fn watch_canonicalized_file() {
262+
let tdir = tempfile::Builder::new()
263+
.prefix(TEMP_DIR)
264+
.tempdir()
265+
.expect("failed to create temporary directory");
266+
tdir.create("file1");
267+
268+
let watch_path = tdir
269+
.path()
270+
.canonicalize()
271+
.expect("failed to canonicalize path")
272+
.join("file1");
273+
let (tx, rx) = unbounded();
274+
let mut watcher: RecommendedWatcher =
275+
Watcher::new(tx).expect("failed to create recommended watcher");
276+
watcher
277+
.watch(&watch_path, RecursiveMode::Recursive)
278+
.expect("failed to watch directory");
279+
280+
sleep_windows(100);
281+
282+
tdir.write("file1");
283+
284+
assert_eq!(
285+
recv_events_simple(&rx),
286+
vec![(
287+
watch_path,
288+
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
289+
None
290+
),]
291+
);
292+
}

0 commit comments

Comments
 (0)