Skip to content

Commit

Permalink
Add self_rename_* tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaust committed Jul 19, 2016
1 parent f1ded10 commit 2375ab0
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/inotify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl mio::Handler for INotifyHandler {
o.insert(op::CHMOD);
}

if !event.is_ignored() {
if !o.is_empty() {
send_pending_rename_event(rename_event, &self.tx);
rename_event = None;

Expand Down
21 changes: 15 additions & 6 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,21 @@ unsafe extern "system" fn handle_event(error_code: u32,
if !skip {
if (*cur_entry).Action == winnt::FILE_ACTION_RENAMED_OLD_NAME {
send_pending_rename_event(rename_event, &request.tx);
COOKIE_COUNTER = COOKIE_COUNTER.wrapping_add(1);
rename_event = Some(Event {
path: Some(path),
op: Ok(op::RENAME),
cookie: Some(COOKIE_COUNTER),
});
if request.data.file.is_some() {
let _ = request.tx.send(Event {
path: Some(path),
op: Ok(op::RENAME),
cookie: None,
});
rename_event = None;
} else {
COOKIE_COUNTER = COOKIE_COUNTER.wrapping_add(1);
rename_event = Some(Event {
path: Some(path),
op: Ok(op::RENAME),
cookie: Some(COOKIE_COUNTER),
});
}
} else {
let mut o = Op::empty();
let mut c = None;
Expand Down
183 changes: 183 additions & 0 deletions tests/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,186 @@ fn self_delete_directory() {
}
}
}

#[test]
#[ignore]
fn self_rename_file() {
let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory");

tdir.create_all(vec![
"file1",
]);

if cfg!(target_os="macos") {
sleep(10);
}

let (tx, rx) = mpsc::channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx).expect("failed to create recommended watcher");
watcher.watch(&tdir.mkpath("file1"), RecursiveMode::Recursive).expect("failed to watch file");

if cfg!(target_os="windows") {
sleep(100);
}

tdir.rename("file1", "file2");

let actual = if cfg!(target_os="macos") {
inflate_events(recv_events(&rx))
} else {
recv_events(&rx)
};

if cfg!(target_os="macos") {
assert_eq!(actual, vec![
(tdir.mkpath("file1"), op::CREATE | op::RENAME, None),
]);
} else {
assert_eq!(actual, vec![
(tdir.mkpath("file1"), op::RENAME, None),
]);
}

tdir.write("file2");

let actual = if cfg!(target_os="macos") {
inflate_events(recv_events(&rx))
} else {
recv_events(&rx)
};

if cfg!(target_os="windows") {
assert_eq!(actual, vec![]);
panic!("windows back-end should update file watch path");
} else if cfg!(target_os="macos") {
// macos doesn't watch files, but paths
assert_eq!(actual, vec![]);
} else {
assert_eq!(actual, vec![
(tdir.mkpath("file1"), op::WRITE, None), // path doesn't get updated
]);
}

tdir.create("file1");

if cfg!(target_os="windows") {
assert_eq!(recv_events(&rx), vec![
(tdir.mkpath("file1"), op::CREATE, None),
]);
panic!("windows back-end should update file watch path");
} else if cfg!(target_os="macos") {
// macos doesn't watch files, but paths
assert_eq!(recv_events(&rx), vec![
(tdir.mkpath("file1"), op::CREATE | op::RENAME, None), // excessive rename event
]);
} else {
assert_eq!(recv_events(&rx), vec![]);
}

watcher.unwatch(&tdir.mkpath("file1")).expect("failed to unwatch file"); // use old path to unwatch

let result = watcher.unwatch(&tdir.mkpath("file1"));
if cfg!(target_os="windows") {
match result {
Err(e) => panic!("{:?}", e),
Ok(()) => (),
}
} else {
match result {
Err(Error::WatchNotFound) => (),
Err(e) => panic!("{:?}", e),
Ok(o) => panic!("{:?}", o),
}
}
}

#[test]
fn self_rename_directory() {
let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory");

tdir.create_all(vec![
"dir1",
]);

if cfg!(target_os="macos") {
sleep(10);
}

let (tx, rx) = mpsc::channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx).expect("failed to create recommended watcher");
watcher.watch(&tdir.mkpath("dir1"), RecursiveMode::Recursive).expect("failed to watch directory");

if cfg!(target_os="windows") {
sleep(100);
}

tdir.rename("dir1", "dir2");

let actual = if cfg!(target_os="macos") {
inflate_events(recv_events(&rx))
} else {
recv_events(&rx)
};

if cfg!(target_os="windows") {
assert_eq!(actual, vec![]);
} else if cfg!(target_os="macos") {
assert_eq!(actual, vec![
(tdir.mkpath("dir1"), op::CREATE | op::RENAME, None), // excessive create event
]);
} else {
assert_eq!(actual, vec![
(tdir.mkpath("dir1"), op::RENAME, None),
]);
}

tdir.create("dir2/file1");

let actual = if cfg!(target_os="macos") {
inflate_events(recv_events(&rx))
} else {
recv_events(&rx)
};

if cfg!(target_os="macos") {
// macos doesn't watch files, but paths
assert_eq!(actual, vec![]);
} else {
assert_eq!(actual, vec![
(tdir.mkpath("dir1/file1"), op::CREATE, None), // path doesn't get updated
]);
}

tdir.create("dir1");

let actual = if cfg!(target_os="macos") {
inflate_events(recv_events(&rx))
} else {
recv_events(&rx)
};

if cfg!(target_os="macos") {
// macos doesn't watch files, but paths
assert_eq!(actual, vec![
(tdir.mkpath("dir1"), op::CREATE | op::RENAME, None), // excessive rename event
]);
} else {
assert_eq!(actual, vec![]);
}

watcher.unwatch(&tdir.mkpath("dir1")).expect("failed to unwatch directory"); // use old path to unwatch

let result = watcher.unwatch(&tdir.mkpath("dir1"));
if cfg!(target_os="windows") {
match result {
Err(e) => panic!("{:?}", e),
Ok(()) => (),
}
} else {
match result {
Err(Error::WatchNotFound) => (),
Err(e) => panic!("{:?}", e),
Ok(o) => panic!("{:?}", o),
}
}
}

0 comments on commit 2375ab0

Please sign in to comment.