Skip to content

Commit

Permalink
test(composition): file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronArinder committed Sep 19, 2024
1 parent d6c04ec commit 414bfb4
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/composition/watchers/watcher/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,41 @@ impl FileWatcher {
.boxed()
}
}

mod tests {
use std::fs::OpenOptions;

Check warning on line 42 in src/composition/watchers/watcher/file.rs

View workflow job for this annotation

GitHub Actions / Build Rover for macOS x86-64

unused import: `std::fs::OpenOptions`
use std::io::Write;

Check warning on line 43 in src/composition/watchers/watcher/file.rs

View workflow job for this annotation

GitHub Actions / Build Rover for macOS x86-64

unused import: `std::io::Write`
use std::thread::sleep;

Check warning on line 44 in src/composition/watchers/watcher/file.rs

View workflow job for this annotation

GitHub Actions / Build Rover for macOS x86-64

unused import: `std::thread::sleep`
use std::time::Duration;

Check warning on line 45 in src/composition/watchers/watcher/file.rs

View workflow job for this annotation

GitHub Actions / Build Rover for macOS x86-64

unused import: `std::time::Duration`

use super::*;

Check warning on line 47 in src/composition/watchers/watcher/file.rs

View workflow job for this annotation

GitHub Actions / Build Rover for macOS x86-64

unused import: `super::*`

#[tokio::test]
async fn it_watches() {
let some_file = tempfile::Builder::new().tempfile().unwrap();
let path = some_file.path().to_path_buf();
let watcher = FileWatcher::new(Utf8PathBuf::from_path_buf(path.clone()).unwrap());
let mut watching = watcher.watch();

// Internal to rover std fs is a blocking loop with a 1s debouncer; so, use 2s just in case
sleep(Duration::from_secs(2));

// Make a change to the file
let mut writeable_file = OpenOptions::new()
.write(true)
.truncate(true)
.open(path.clone())
.expect("Cannot open file");

writeable_file
.write("some change".as_bytes())
.expect("couldn't write to file");

let next = watching.next().await.unwrap();

assert_eq!(next, "some change".to_string());

// Close the file to emit an event for rover-std fs to close the file watcher
let _ = some_file.close();
}
}

0 comments on commit 414bfb4

Please sign in to comment.