diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-18 13:19:17 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-18 13:19:17 +0200 |
| commit | fe9c6f722f8ce767ed56b73e024aaf257bd741b8 (patch) | |
| tree | 44212acbf6bfa8e8839c7271365f291d6d32f128 /src | |
| parent | 0065feab2673862d168a6271e680c6115a7e9a73 (diff) | |
Add configuration status tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/command/status/configuration_status.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/command/status/configuration_status.rs b/src/command/status/configuration_status.rs index 7babff8..6f7db14 100644 --- a/src/command/status/configuration_status.rs +++ b/src/command/status/configuration_status.rs @@ -49,3 +49,77 @@ fn get_directory_stats(label: &str, directory: &PathBuf) -> Result<String> { Ok(status_message) } + +#[cfg(test)] +mod tests { + use std::fs::{Permissions, create_dir_all, metadata, set_permissions}; + use std::path::PathBuf; + + #[cfg(unix)] + use std::os::unix::fs::PermissionsExt; + + #[cfg(windows)] + use std::os::windows::fs::PermissionsExt; + + use crate::configuration::Configuration; + + use test_utilities::*; + + use super::*; + + #[test] + fn get_directory_stats_detects_nonexistent_directories() { + let test_dir = PathBuf::from("/krillinnoooootravez"); + + if let Ok(output) = get_directory_stats("krillin", &test_dir) { + assert!(output.contains("Does not exist.")); + } else { + panic!("Could not get stats for directory that doesn't exist."); + } + } + + #[test] + fn get_directory_detects_writability() { + let test_dir = setup_test_dir(); + + if let Ok(output) = get_directory_stats("yajirobe", &test_dir) { + assert!(output.contains("Exists")); + assert!(output.contains("is readable")); + } else { + panic!("Could not get stats for directory that doesn't exist."); + } + + cleanup_test_dir(&test_dir); + } + + #[test] + fn get_directory_detects_lack_of_writability() { + let test_dir = setup_test_dir(); + let metadata = metadata(&test_dir).expect("Could not fetch metadata for test dir"); + let mut permissions = metadata.permissions(); + + #[cfg(unix)] + { + permissions.set_mode(0o311); + } + + set_permissions(&test_dir, permissions.clone()) + .expect("Could not set read permissions on dir"); + + if let Ok(output) = get_directory_stats("roshi", &test_dir) { + assert!(output.contains("Exists")); + assert!(output.contains("is not readable")); + } else { + panic!("Could not get stats for directory that doesn't exist."); + } + + #[cfg(unix)] + { + permissions.set_mode(0o755); + } + + set_permissions(&test_dir, permissions).expect("Could not reset permissions on dir"); + + cleanup_test_dir(&test_dir); + } +} |