From 88817ba89a094318d251e11d17e8e744e63a40ae Mon Sep 17 00:00:00 2001 From: Kyohei Uto Date: Mon, 4 Nov 2024 12:41:06 +0900 Subject: [PATCH 1/2] Fix: test_has_write_permission by set_readonly --- src/state.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/state.rs b/src/state.rs index 632a818..5ebbe6d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2052,19 +2052,17 @@ mod tests { fn test_has_write_permission() { // chmod to 444 and check if it's read-only let p = std::path::PathBuf::from("./testfiles/permission_test"); - let _status = std::process::Command::new("chmod") - .args(["444", "./testfiles/permission_test"]) - .status() - .unwrap(); + let mut perms = std::fs::metadata(&p).unwrap().permissions(); + perms.set_readonly(true); + std::fs::set_permissions(&p, perms.clone()).unwrap(); assert!(!has_write_permission(p.as_path()).unwrap()); - let _status = std::process::Command::new("chmod") - .args(["755", "./testfiles/permission_test"]) - .status() - .unwrap(); - // Test the home directory, which should pass let home_dir = dirs::home_dir().unwrap(); assert!(has_write_permission(&home_dir).unwrap()); + + // Set the file writable + perms.set_readonly(false); + std::fs::set_permissions(&p, perms).unwrap(); } #[test] From 6c444d0f541cfb01a1dae058690924c149631d99 Mon Sep 17 00:00:00 2001 From: Kyohei Uto Date: Mon, 4 Nov 2024 13:35:03 +0900 Subject: [PATCH 2/2] Add default permission test --- src/state.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/state.rs b/src/state.rs index 5ebbe6d..d607256 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2050,13 +2050,16 @@ mod tests { #[test] fn test_has_write_permission() { - // chmod to 444 and check if it's read-only let p = std::path::PathBuf::from("./testfiles/permission_test"); + assert!(has_write_permission(&p).unwrap()); + + // chmod to 444 and check if it's read-only let mut perms = std::fs::metadata(&p).unwrap().permissions(); perms.set_readonly(true); std::fs::set_permissions(&p, perms.clone()).unwrap(); - assert!(!has_write_permission(p.as_path()).unwrap()); + assert!(!has_write_permission(&p).unwrap()); + // HOME_DIR should pass as writable let home_dir = dirs::home_dir().unwrap(); assert!(has_write_permission(&home_dir).unwrap());