2024-05-14 07:58:28 +01:00
|
|
|
type TestResult = Result<(), Box<dyn std::error::Error>>;
|
|
|
|
|
|
|
|
mod init {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
|
|
fn should_not_update_file_if_it_exists() -> TestResult {
|
|
|
|
let fs = kxio::fs::temp()?;
|
|
|
|
let file = fs.base().join(".git-next.toml");
|
|
|
|
fs.file_write(&file, "contents")?;
|
|
|
|
|
2024-08-05 07:39:38 +01:00
|
|
|
crate::init::run(&fs)?;
|
2024-05-14 07:58:28 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs.file_read_to_string(&file)?,
|
|
|
|
"contents",
|
|
|
|
"The file has been changed"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_create_default_file_if_not_exists() -> TestResult {
|
|
|
|
let fs = kxio::fs::temp()?;
|
|
|
|
|
2024-08-05 07:39:38 +01:00
|
|
|
crate::init::run(&fs)?;
|
2024-05-14 07:58:28 +01:00
|
|
|
|
|
|
|
let file = fs.base().join(".git-next.toml");
|
|
|
|
|
|
|
|
assert!(fs.path_exists(&file)?, "The file has not been created");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs.file_read_to_string(&file)?,
|
2024-07-14 20:14:00 +01:00
|
|
|
include_str!("../default.toml"),
|
2024-05-14 07:58:28 +01:00
|
|
|
"The file does not match the default template"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2024-08-10 20:08:07 +01:00
|
|
|
mod file_watcher {
|
|
|
|
use std::{sync::atomic::Ordering, time::Duration};
|
|
|
|
|
|
|
|
use actix::{Actor, Context, Handler};
|
|
|
|
use rstest::*;
|
|
|
|
|
|
|
|
use crate::file_watcher::{self, FileUpdated};
|
|
|
|
|
|
|
|
use super::TestResult;
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[actix::test]
|
|
|
|
#[timeout(Duration::from_millis(80))]
|
|
|
|
async fn should_not_block_calling_thread() -> TestResult {
|
|
|
|
let fs = kxio::fs::temp()?;
|
|
|
|
let path = fs.base().join("file");
|
|
|
|
fs.file_write(&path, "foo")?;
|
|
|
|
|
|
|
|
let listener = Listener;
|
|
|
|
let l_addr = listener.start();
|
|
|
|
let recipient = l_addr.recipient();
|
|
|
|
|
|
|
|
let fw_shutdown = file_watcher::watch_file(path, recipient)?;
|
|
|
|
std::thread::sleep(Duration::from_millis(10));
|
|
|
|
fw_shutdown.store(true, Ordering::Relaxed);
|
|
|
|
|
|
|
|
Ok(()) // was not blocked
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Listener;
|
|
|
|
impl Actor for Listener {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
impl Handler<FileUpdated> for Listener {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, _msg: FileUpdated, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
// todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|