Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import tests from v4 #415

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
version:
- 1.56.0 # MSRV
- 1.59.0 # MSRV
- stable
- nightly
os: [ubuntu-latest, macos-latest, windows-latest]
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "notify"
version = "5.0.0-pre.15"
rust-version = "1.56"
rust-version = "1.59"
description = "Cross-platform filesystem notification library"
documentation = "https://docs.rs/notify"
homepage = "https://github.com/notify-rs/notify"
Expand Down Expand Up @@ -55,6 +55,7 @@ nix = "0.23.1"
default = ["macos_fsevent"]
timing_tests = []
manual_tests = []
network_tests = []
macos_kqueue = ["kqueue", "mio"]
macos_fsevent = ["fsevent-sys"]

Expand Down
292 changes: 292 additions & 0 deletions tests/event_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
use std::{
env,
path::{Path, PathBuf},
};

use crossbeam_channel::{unbounded, Receiver};
use notify::*;

use utils::*;
mod utils;

const TEMP_DIR: &str = "temp_dir";

#[cfg(target_os = "windows")]
fn recv_events_simple(rx: &Receiver<Result<Event>>) -> Vec<(PathBuf, EventKind, Option<usize>)> {
recv_events(&rx)
}

#[cfg(target_os = "macos")]
fn recv_events_simple(rx: &Receiver<Result<Event>>) -> Vec<(PathBuf, EventKind, Option<usize>)> {
let mut events = Vec::new();
for (path, ev, cookie) in recv_events(&rx) {
if let EventKind::Create(_) | EventKind::Modify(event::ModifyKind::Data(_)) = ev {
events.push((
path,
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
cookie,
));
} else {
events.push((path, ev, cookie));
}
}
events
}

#[cfg(target_os = "linux")]
fn recv_events_simple(rx: &Receiver<Result<Event>>) -> Vec<(PathBuf, EventKind, Option<usize>)> {
let mut events = recv_events(rx);
events.retain(|(_, ref ev, _)| matches!(ev, EventKind::Access(_)));
events
}

#[test]
fn watch_relative() {
// both of the following tests set the same environment variable, so they must not run in parallel
{
// watch_relative_directory
let tdir = tempfile::Builder::new()
.prefix(TEMP_DIR)
.tempdir()
.expect("failed to create temporary directory");
tdir.create("dir1");

sleep_macos(10);

env::set_current_dir(tdir.path()).expect("failed to change working directory");

let (tx, rx) = unbounded();
let mut watcher: RecommendedWatcher =
Watcher::new(tx).expect("failed to create recommended watcher");
watcher
.watch(Path::new("dir1"), RecursiveMode::Recursive)
.expect("failed to watch directory");

sleep_windows(100);

tdir.create("dir1/file1");

if cfg!(target_os = "macos") {
assert_eq!(
recv_events_simple(&rx),
vec![
(
tdir.mkpath("dir1/file1"),
EventKind::Create(event::CreateKind::Any),
None
), // fsevents always returns canonicalized paths
]
);
} else {
assert_eq!(
recv_events_simple(&rx),
vec![(
tdir.path().join("dir1/file1"),
EventKind::Create(event::CreateKind::Any),
None
),]
);
}
}
{
// watch_relative_file
let tdir = tempfile::Builder::new()
.prefix(TEMP_DIR)
.tempdir()
.expect("failed to create temporary directory");
tdir.create("file1");

env::set_current_dir(tdir.path()).expect("failed to change working directory");

let (tx, rx) = unbounded();
let mut watcher: RecommendedWatcher =
Watcher::new(tx).expect("failed to create recommended watcher");
watcher
.watch(Path::new("file1"), RecursiveMode::Recursive)
.expect("failed to watch file");

sleep_windows(100);

tdir.write("file1");

if cfg!(target_os = "macos") {
assert_eq!(
recv_events_simple(&rx),
vec![
(
tdir.mkpath("file1"),
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
None
), // fsevents always returns canonicalized paths
]
);
} else {
assert_eq!(
recv_events_simple(&rx),
vec![(
tdir.path().join("file1"),
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
None
),]
);
}
}
}

#[test]
fn watch_absolute_directory() {
let tdir = tempfile::Builder::new()
.prefix(TEMP_DIR)
.tempdir()
.expect("failed to create temporary directory");
tdir.create("dir1");

sleep_macos(10);

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

sleep_windows(100);

tdir.create("dir1/file1");

if cfg!(target_os = "macos") {
assert_eq!(
recv_events_simple(&rx),
vec![
(
tdir.mkpath("dir1/file1"),
EventKind::Create(event::CreateKind::Any),
None
), // fsevents always returns canonicalized paths
]
);
} else {
assert_eq!(
recv_events_simple(&rx),
vec![(
watch_path.join("file1"),
EventKind::Create(event::CreateKind::Any),
None
),]
);
}
}

#[test]
fn watch_absolute_file() {
let tdir = tempfile::Builder::new()
.prefix(TEMP_DIR)
.tempdir()
.expect("failed to create temporary directory");
tdir.create("file1");

let watch_path = tdir.path().join("file1");
let (tx, rx) = unbounded();
let mut watcher: RecommendedWatcher =
Watcher::new(tx).expect("failed to create recommended watcher");
watcher
.watch(&watch_path, RecursiveMode::Recursive)
.expect("failed to watch directory");

sleep_windows(100);

tdir.write("file1");

if cfg!(target_os = "macos") {
assert_eq!(
recv_events_simple(&rx),
vec![
(
tdir.mkpath("file1"),
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
None
), // fsevents always returns canonicalized paths
]
);
} else {
assert_eq!(
recv_events_simple(&rx),
vec![(
watch_path,
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
None
),]
);
}
}

#[test]
fn watch_canonicalized_directory() {
let tdir = tempfile::Builder::new()
.prefix(TEMP_DIR)
.tempdir()
.expect("failed to create temporary directory");
tdir.create("dir1");

sleep_macos(10);

let watch_path = tdir
.path()
.canonicalize()
.expect("failed to canonicalize path")
.join("dir1");
let (tx, rx) = unbounded();
let mut watcher: RecommendedWatcher =
Watcher::new(tx).expect("failed to create recommended watcher");
watcher
.watch(&watch_path, RecursiveMode::Recursive)
.expect("failed to watch directory");

sleep_windows(100);

tdir.create("dir1/file1");

assert_eq!(
recv_events_simple(&rx),
vec![(
watch_path.join("file1"),
EventKind::Create(event::CreateKind::Any),
None
),]
);
}

#[test]
fn watch_canonicalized_file() {
let tdir = tempfile::Builder::new()
.prefix(TEMP_DIR)
.tempdir()
.expect("failed to create temporary directory");
tdir.create("file1");

let watch_path = tdir
.path()
.canonicalize()
.expect("failed to canonicalize path")
.join("file1");
let (tx, rx) = unbounded();
let mut watcher: RecommendedWatcher =
Watcher::new(tx).expect("failed to create recommended watcher");
watcher
.watch(&watch_path, RecursiveMode::Recursive)
.expect("failed to watch directory");

sleep_windows(100);

tdir.write("file1");

assert_eq!(
recv_events_simple(&rx),
vec![(
watch_path,
EventKind::Modify(event::ModifyKind::Data(event::DataChange::Any)),
None
),]
);
}
70 changes: 70 additions & 0 deletions tests/manual_tests_linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#![cfg(target_os = "linux")]
#![cfg(feature = "manual_tests")]

use std::{
fs::File,
io::prelude::*,
thread,
time::{Duration, Instant},
};

use crossbeam_channel::{unbounded, TryRecvError};
use notify::*;

use utils::*;
mod utils;

const TEMP_DIR: &str = "temp_dir";

#[test]
// Test preparation:
// 1. Run `sudo echo 10 > /proc/sys/fs/inotify/max_queued_events`
// 2. Uncomment the lines near "test inotify_queue_overflow" in inotify watcher
fn inotify_queue_overflow() {
let mut max_queued_events = String::new();
let mut f = File::open("/proc/sys/fs/inotify/max_queued_events")
.expect("failed to open max_queued_events");
f.read_to_string(&mut max_queued_events)
.expect("failed to read max_queued_events");
assert_eq!(max_queued_events.trim(), "10");

let tdir = tempfile::Builder::new()
.prefix(TEMP_DIR)
.tempdir()
.expect("failed to create temporary directory");

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

for i in 0..20 {
let filename = format!("file{}", i);
tdir.create(&filename);
tdir.remove(&filename);
}

sleep(100);

let start = Instant::now();

let mut rescan_found = false;
while !rescan_found && start.elapsed().as_secs() < 5 {
match rx.try_recv() {
Ok(Err(Error {
// TRANSLATION: this may not be correct
kind: ErrorKind::MaxFilesWatch,
..
})) => rescan_found = true,
Ok(Err(e)) => panic!("unexpected event err: {:?}", e),
Ok(Ok(_)) => (),
Err(TryRecvError::Empty) => (),
Err(e) => panic!("unexpected channel err: {:?}", e),
}
thread::sleep(Duration::from_millis(10));
}

assert!(rescan_found);
}
Loading