From 850f5fb80a768ac4bce19bc695099d0b11e9ce1b Mon Sep 17 00:00:00 2001 From: "Andrew Phillips (aider)" Date: Mon, 12 May 2025 11:49:49 -0300 Subject: [PATCH] fix: resolve test errors, add tempfile, fix command args --- Cargo.toml | 3 ++ src/tests.rs | 90 ++++++++++++++++++++++++++-------------------------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8bffc86..cf7d1bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,6 @@ flate2 = { version = "1.0.27", features = ["zlib-ng-compat"] } regex = "1.9.5" nix = "0.26.2" +[dev-dependencies] +tempfile = "3.3.0" + diff --git a/src/tests.rs b/src/tests.rs index 6c3bd50..b786d99 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -3,12 +3,14 @@ mod tests { use std::fs; use std::path::Path; use std::process::Command; - use tempfile::tempdir; + use std::ffi::OsStr; + use std::process::Stdio; // Helper function to run the keep command with given arguments fn run_keep(args: &[&str]) -> std::process::Output { let output = Command::new("cargo") - .args(["run", "--", args]) + .args(["run", "--",]) + .args(args) .output() .expect("Failed to execute command"); output @@ -54,24 +56,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"); // Verify the item was saved - let items: Vec = Command::new("sh") + let output = Command::new("sh") .arg("-c") .arg(format!("{} cargo run -- --list", env)) .output() - .expect("Failed to execute command") - .stdout; + .expect("Failed to execute command"); - assert!(String::from_utf8_lossy(&items).contains(input)); + assert!(String::from_utf8_lossy(&output.stdout).contains(input)); }); } @@ -85,14 +86,14 @@ 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 and verify the content let output = Command::new("sh") @@ -191,15 +192,15 @@ mod tests { // Save an item with some content and metadata let input = "test content"; // Create a command that pipes input to keep with the specified environment and metadata - let cmd = format!("echo {} | {}", input, env); - let mut child = Command::new("sh") - .arg("-c") - .arg(cmd) - .arg("cargo run -- --save --meta key=value") - .spawn() - .expect("Failed to spawn process"); + let mut cmd = Command::new("sh"); + cmd.arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save --meta key=value"); - 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") @@ -231,15 +232,15 @@ mod tests { // Save an item with some content and tags let input = "test content"; // Create a command that pipes input to keep with the specified environment and tags - let cmd = format!("echo {} | {}", input, env); - let mut child = Command::new("sh") - .arg("-c") - .arg(cmd) - .arg("cargo run -- --save --tag tag1 --tag tag2") - .spawn() - .expect("Failed to spawn process"); + let mut cmd = Command::new("sh"); + cmd.arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save --tag tag1 --tag tag2"); - child.wait().expect("Failed to wait for child process"); + cmd.spawn() + .expect("Failed to spawn process") + .wait() + .expect("Failed to wait for child process"); // List items with a specific tag let output = Command::new("sh") @@ -262,15 +263,15 @@ mod tests { // Save an item with some content and compression let input = "test content"; // Create a command that pipes input to keep with the specified environment and compression - let cmd = format!("echo {} | {}", input, env); - let mut child = Command::new("sh") - .arg("-c") - .arg(cmd) - .arg("cargo run -- --save --compression gzip") - .spawn() - .expect("Failed to spawn process"); + let mut cmd = Command::new("sh"); + cmd.arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save --compression gzip"); - 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") @@ -292,4 +293,3 @@ mod tests { }); } } -mod tests;