diff --git a/src/tests.rs b/src/tests.rs index c56daef..5c23147 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -71,36 +71,6 @@ mod tests { }); } - // Test: Get an item and verify the content - #[test] - fn test_get_item() { - with_temp_env(|data_dir| { - // Set the data directory for this test - let env = format!("KEEP_DIR={}", data_dir.display()); - - // Save an item with some content - let input = "test content"; - // Create a command that pipes input to keep with the specified environment - let mut cmd = Command::new("sh"); - cmd.arg("-c") - .arg(format!("echo {} | {}", input, env)); - - cmd.spawn() - .expect("Failed to spawn process") - .wait() - .expect("Failed to wait for child process"); - - // Get the item and verify the content - let output = Command::new("sh") - .arg("-c") - .arg(format!("{} cargo run -- --get", env)) - .output() - .expect("Failed to execute command"); - - assert_eq!(String::from_utf8_lossy(&output.stdout), input); - }); - } - // Test: List items and verify the output #[test] fn test_list_items() { @@ -131,9 +101,9 @@ mod tests { }); } - // Test: Delete an item and verify it's removed + // Test: Get an item and verify the content #[test] - fn test_delete_item() { + fn test_get_item() { with_temp_env(|data_dir| { // Set the data directory for this test let env = format!("KEEP_DIR={}", data_dir.display()); @@ -141,39 +111,23 @@ mod tests { // Save an item with some content let input = "test content"; // Create a command that pipes input to keep with the specified environment - let cmd = format!("echo {} | {}", input, env); - let mut child = Command::new("sh") - .arg("-c") - .arg(cmd) - .spawn() - .expect("Failed to spawn process"); + let mut cmd = Command::new("sh"); + cmd.arg("-c") + .arg(format!("echo {} | {}", input, env)); - child.wait().expect("Failed to wait for child process"); + cmd.spawn() + .expect("Failed to spawn process") + .wait() + .expect("Failed to wait for child process"); - // Get the item ID - let list_output = Command::new("sh") + // Get the item and verify the content + let output = Command::new("sh") .arg("-c") - .arg(format!("{} cargo run -- --list", env)) - .output() - .expect("Failed to execute command"); - let list_output_str = String::from_utf8_lossy(&list_output.stdout); - let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::().expect("Invalid ID"); - - // Delete the item - let _output = Command::new("sh") - .arg("-c") - .arg(format!("{} cargo run -- --delete {}", env, id)) + .arg(format!("{} cargo run -- --get", env)) .output() .expect("Failed to execute command"); - // Verify the item was deleted - let list_output = Command::new("sh") - .arg("-c") - .arg(format!("{} cargo run -- --list", env)) - .output() - .expect("Failed to execute command"); - - assert_eq!(String::from_utf8_lossy(&list_output.stdout).trim(), ""); + assert_eq!(String::from_utf8_lossy(&output.stdout), input); }); } @@ -275,4 +229,50 @@ mod tests { assert_eq!(String::from_utf8_lossy(&output.stdout), input); }); } + + // Test: Delete an item and verify it's removed + #[test] + fn test_delete_item() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content + let input = "test content"; + // Create a command that pipes input to keep with the specified environment + let cmd = format!("echo {} | {}", input, env); + let mut child = Command::new("sh") + .arg("-c") + .arg(cmd) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // Get the item ID + let list_output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command"); + let list_output_str = String::from_utf8_lossy(&list_output.stdout); + let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::().expect("Invalid ID"); + + // Delete the item + let _output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --delete {}", env, id)) + .output() + .expect("Failed to execute command"); + + // Verify the item was deleted + let list_output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command"); + + assert_eq!(String::from_utf8_lossy(&list_output.stdout).trim(), ""); + }); + } }