feat(src/commands): split argument building into build_args and add tests
The refactor extracts argument construction into a reusable build_args function and adds unit tests, improving modularity and test coverage. New Features - Introduce build_args helper to construct inhibit command arguments. - Add comprehensive unit tests for both empty and non‑empty argument scenarios. Enhancements - Refactor existing argument handling to use build_args, eliminating duplicated logic. - Improve readability and maintainability of the inhibit command implementation. Bug Fixes - None
This commit is contained in:
1 file changed
+62
-11
+62
-11
@@ -2,27 +2,17 @@ use log::{error, info};
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
pub fn execute(command_args: &[String], why: &str, who: &str, daemon: bool) {
|
pub fn execute(command_args: &[String], why: &str, who: &str, daemon: bool) {
|
||||||
let mut args = vec![
|
let args = build_args(command_args, why, who);
|
||||||
"--why".to_string(),
|
|
||||||
why.to_string(),
|
|
||||||
"--who".to_string(),
|
|
||||||
who.to_string(),
|
|
||||||
"--what".to_string(),
|
|
||||||
"sleep:idle".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
if command_args.is_empty() {
|
if command_args.is_empty() {
|
||||||
if !daemon {
|
if !daemon {
|
||||||
info!("No command provided. Inhibiting sleep indefinitely. Press Ctrl+C to stop.");
|
info!("No command provided. Inhibiting sleep indefinitely. Press Ctrl+C to stop.");
|
||||||
}
|
}
|
||||||
args.push("sleep".to_string());
|
|
||||||
args.push("infinity".to_string());
|
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"Running command while inhibiting sleep: {}",
|
"Running command while inhibiting sleep: {}",
|
||||||
command_args.join(" ")
|
command_args.join(" ")
|
||||||
);
|
);
|
||||||
args.extend(command_args.iter().cloned());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if daemon {
|
if daemon {
|
||||||
@@ -52,3 +42,64 @@ pub fn execute(command_args: &[String], why: &str, who: &str, daemon: bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn build_args(command_args: &[String], why: &str, who: &str) -> Vec<String> {
|
||||||
|
let mut args = vec![
|
||||||
|
"--why".to_string(),
|
||||||
|
why.to_string(),
|
||||||
|
"--who".to_string(),
|
||||||
|
who.to_string(),
|
||||||
|
"--what".to_string(),
|
||||||
|
"sleep:idle".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
if command_args.is_empty() {
|
||||||
|
args.push("sleep".to_string());
|
||||||
|
args.push("infinity".to_string());
|
||||||
|
} else {
|
||||||
|
args.extend(command_args.iter().cloned());
|
||||||
|
}
|
||||||
|
args
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_args_no_command() {
|
||||||
|
let args = build_args(&[], "testing", "lapctl");
|
||||||
|
assert_eq!(
|
||||||
|
args,
|
||||||
|
vec![
|
||||||
|
"--why",
|
||||||
|
"testing",
|
||||||
|
"--who",
|
||||||
|
"lapctl",
|
||||||
|
"--what",
|
||||||
|
"sleep:idle",
|
||||||
|
"sleep",
|
||||||
|
"infinity"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_args_with_command() {
|
||||||
|
let command_args = vec!["ls".to_string(), "-l".to_string()];
|
||||||
|
let args = build_args(&command_args, "testing", "lapctl");
|
||||||
|
assert_eq!(
|
||||||
|
args,
|
||||||
|
vec![
|
||||||
|
"--why",
|
||||||
|
"testing",
|
||||||
|
"--who",
|
||||||
|
"lapctl",
|
||||||
|
"--what",
|
||||||
|
"sleep:idle",
|
||||||
|
"ls",
|
||||||
|
"-l"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user