Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions internal/live-preview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ getifs = { version = "0.6.1", optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"], optional = true }

# The cool-down tests move the clock rather than sleeping through a
# 60-second pairing deadline.
[dev-dependencies]
tempfile = "3"
tokio = { version = "1.48.0", features = ["test-util"] }

[lib]
Expand Down
36 changes: 12 additions & 24 deletions internal/live-preview/file_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ impl FileWatcherImpl for notify::RecommendedWatcher {
notify::ErrorKind::PathNotFound
| notify::ErrorKind::WatchNotFound
| notify::ErrorKind::Generic(_) => true,
notify::ErrorKind::Io(e) => e.kind() == std::io::ErrorKind::NotFound,
// `InvalidInput` is `inotify_rm_watch` on a watch the kernel already dropped with
// the deleted directory, before notify processed that event.
notify::ErrorKind::Io(e) => {
matches!(e.kind(), std::io::ErrorKind::NotFound | std::io::ErrorKind::InvalidInput)
}
_ => false,
}
}
Expand Down Expand Up @@ -574,28 +578,18 @@ mod tests {
use super::*;

use std::fs;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{self, Receiver};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::Duration;

const WATCHER_SETTLE_DELAY: Duration = Duration::from_millis(50);
const EVENT_TIMEOUT: Duration = Duration::from_secs(1);
const QUIET_TIMEOUT: Duration = Duration::from_millis(50);

fn new_test_root() -> PathBuf {
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);

let unique_id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
let root = std::env::temp_dir()
.join(format!("slint-file-watcher-{timestamp}-{unique_id}-{}", std::process::id()));
fs::create_dir_all(&root).unwrap();
root
}

struct TestContext {
root: PathBuf,
/// Declared before `root` so that it is dropped first: the watcher must not follow the
/// vanished paths up to the shared temp directory.
watcher: FileWatcher,
root: tempfile::TempDir,
events: Receiver<WatchEvent>,
errors: Receiver<notify::Error>,
}
Expand All @@ -606,7 +600,7 @@ mod tests {
}

fn new_with_passthrough(pass_through_unwatched_events: bool) -> Self {
let root = new_test_root();
let root = tempfile::Builder::new().prefix("slint-file-watcher-").tempdir().unwrap();
let (event_tx, events) = mpsc::channel();
let (error_tx, errors) = mpsc::channel();

Expand All @@ -627,11 +621,11 @@ mod tests {
)
.unwrap();

Self { root, watcher, events, errors }
Self { watcher, root, events, errors }
}

fn path(&self, relative: impl AsRef<Path>) -> PathBuf {
self.root.join(relative)
self.root.path().join(relative)
}

fn create_dir_all(&self, relative: impl AsRef<Path>) -> PathBuf {
Expand Down Expand Up @@ -736,12 +730,6 @@ mod tests {
}
}

impl Drop for TestContext {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.root);
}
}

#[test]
fn reports_changed_for_existing_watched_file() {
let mut ctx = TestContext::new();
Expand Down
Loading