From 33a7beba468664a4c11284bf04921611d5d3ffe5 Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 3 Nov 2025 13:08:06 +0100 Subject: [PATCH] test more stuff in the updater --- Cargo.lock | 1 + crates/auto_update/Cargo.toml | 1 - crates/auto_update_helper/Cargo.toml | 3 +++ crates/auto_update_helper/src/updater.rs | 34 ++++++++++++++++++------ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ee239c3740bb0fba40f81b9cdae77a46d62d535..9d31c1b5e466f7396a99a009644e24fedfe50beb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1351,6 +1351,7 @@ dependencies = [ "anyhow", "log", "simplelog", + "tempfile", "windows 0.61.3", "winresource", ] diff --git a/crates/auto_update/Cargo.toml b/crates/auto_update/Cargo.toml index 656b8a068ce8d11a7d8bd9d441fb5329dca1f2ba..630be043dca120ca76b2552f0a729a03a684f934 100644 --- a/crates/auto_update/Cargo.toml +++ b/crates/auto_update/Cargo.toml @@ -28,7 +28,6 @@ smol.workspace = true tempfile.workspace = true util.workspace = true workspace.workspace = true -util.workspace = true [target.'cfg(not(target_os = "windows"))'.dependencies] which.workspace = true diff --git a/crates/auto_update_helper/Cargo.toml b/crates/auto_update_helper/Cargo.toml index 4b4a0126a4c178a76fd2a03b7596d42e19aa9d23..73c38d80dd12e9c42daa42b7e6f2c9d6975cf47b 100644 --- a/crates/auto_update_helper/Cargo.toml +++ b/crates/auto_update_helper/Cargo.toml @@ -21,6 +21,9 @@ simplelog.workspace = true [target.'cfg(target_os = "windows")'.dependencies] windows.workspace = true +[target.'cfg(target_os = "windows")'.dev-dependencies] +tempfile.workspace = true + [target.'cfg(target_os = "windows")'.build-dependencies] winresource = "0.1" diff --git a/crates/auto_update_helper/src/updater.rs b/crates/auto_update_helper/src/updater.rs index 5350c6723f71aa96eb8d9310659d0c4d0d1e3ae8..f146583d3bc69b167b61339278a475827bf28d0b 100644 --- a/crates/auto_update_helper/src/updater.rs +++ b/crates/auto_update_helper/src/updater.rs @@ -209,7 +209,10 @@ pub(crate) const JOBS: LazyCell<[Job; 22]> = LazyCell::new(|| { // app is single threaded #[cfg(test)] #[allow(clippy::declare_interior_mutable_const)] -pub(crate) const JOBS: LazyCell<[Job; 2]> = LazyCell::new(|| { +pub(crate) const JOBS: LazyCell<[Job; 9]> = LazyCell::new(|| { + fn p(value: &str) -> &Path { + Path::new(value) + } [ Job { apply: Box::new(|_| { @@ -229,6 +232,21 @@ pub(crate) const JOBS: LazyCell<[Job; 2]> = LazyCell::new(|| { Ok(()) }), }, + Job::mkdir(p("test1")), + Job::mkdir_if_exists(p("test_exists"), p("test1")), + Job::mkdir_if_exists(p("test_missing"), p("dont")), + Job { + apply: Box::new(|folder| { + std::fs::write(folder.join("test1/test"), "test")?; + Ok(()) + }), + rollback: Box::new(|folder| { + std::fs::remove_file(folder.join("test1/test"))?; + Ok(()) + }), + }, + Job::move_file(p("test1/test"), p("test1/moved")), + Job::move_if_exists(p("test1/test"), p("test1/noop")), Job { apply: Box::new(|_| { std::thread::sleep(Duration::from_millis(1000)); @@ -244,6 +262,7 @@ pub(crate) const JOBS: LazyCell<[Job; 2]> = LazyCell::new(|| { }), rollback: Box::new(|_| Ok(())), }, + Job::rmdir_nofail(p("test1/nofolder")), ] }); @@ -316,22 +335,21 @@ mod test { #[test] fn test_perform_update() { - let app_dir = std::path::Path::new("C:/"); + let app_dir = tempfile::tempdir().unwrap(); + let app_dir = app_dir.path(); assert!(perform_update(app_dir, None, false).is_ok()); + let app_dir = tempfile::tempdir().unwrap(); + let app_dir = app_dir.path(); // Simulate a timeout unsafe { std::env::set_var("ZED_AUTO_UPDATE", "err1") }; let ret = perform_update(app_dir, None, false); assert!( ret.is_err_and(|e| e.to_string().as_str() == "Autoupdate failed, nothing to rollback") ); - } - - #[test] - fn test_perform_update_rollback() { - let app_dir = std::path::Path::new("C:/"); - assert!(perform_update(app_dir, None, false).is_ok()); + let app_dir = tempfile::tempdir().unwrap(); + let app_dir = app_dir.path(); // Simulate a timeout unsafe { std::env::set_var("ZED_AUTO_UPDATE", "err2") }; let ret = perform_update(app_dir, None, false);