#[cfg(test)] mod tests { use std::fs; use std::path::Path; use std::process::Command; // Global test input values const INPUT_A: &str = "test content A"; const INPUT_B: &str = "test content B"; use tempfile::tempdir; // Helper function to run a command in sh -c fn run_sh(cmd: &str) -> std::process::Output { let output = Command::new("sh") .arg("-c") .arg(cmd) .output() .expect("Failed to execute command"); output } // Helper function to create a temporary test environment fn with_temp_env(f: F) where F: FnOnce(&Path), { let dir = tempdir().expect("Failed to create temporary directory"); let _db_path = dir.path().join("keep-1.db"); // Create the data directory structure fs::create_dir_all(dir.path()).expect("Failed to create directory"); // Run the test f(dir.path()); // Clean up dir.close().expect("Failed to remove temporary directory"); } // Helper function to create test items with specific content and tags fn create_test_items(dir: &Path) { // Set the data directory for this test let env = format!("KEEP_DIR={} cargo run -- --verbose", dir.display()); // Create first item with tag_a and tag let cmd = format!("echo {} | {} tag tag_a tag", INPUT_A, env); let output = run_sh(cmd.as_str()); assert!(output.status.success(), "Command failed: {}", INPUT_A); // Create second item with tag_b and tag let cmd = format!("echo {} | {} tag tag_b tag", INPUT_B, env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); } // Helper function to check if a file exists in the data directory fn file_exists(data_dir: &Path, id: i64) -> bool { let path = data_dir.join(id.to_string()); path.exists() } // Helper function to get the content of a file fn file_content(data_dir: &Path, id: i64) -> String { let path = data_dir.join(id.to_string()); std::fs::read_to_string(path).expect("Failed to read file") } // Test: Save an item and verify it's stored #[test] fn test_save_item() { with_temp_env(|data_dir| { // Create test items with the common environment setup create_test_items(data_dir); // Set the data directory for this test let env = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display()); // Test content and tags let input = "test content"; let tag = "tag"; // Create a command that pipes input to keep with the specified environment let cmd = format!("echo {} | {} {}", input, env, tag); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); }); } #[test] fn test_get_item() { with_temp_env(|data_dir| { // Create test items with the common environment setup create_test_items(data_dir); let env = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display()); let cmd = format!("{} --get 1", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); let cmd = format!("{} -g 1", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); let cmd = format!("{} 1", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); let cmd = format!("{} --get", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_B), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_B); let cmd = format!("{} --get tag_a", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); let cmd = format!("{} --get tag_b", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_B), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_B); let cmd = format!("{} --get tag", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_B), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_B); }); } // Test: List items and verify the output #[test] fn test_list_items() { with_temp_env(|data_dir| { // Create test items with the common environment setup create_test_items(data_dir); let env = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display()); let cmd = format!("{} --list", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} -l", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} --list tag_a", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); }); } // Test: Delete an item and verify it's removed #[test] fn test_delete_item() { with_temp_env(|data_dir| { // Create test items with the common environment setup create_test_items(data_dir); let env = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display()); let cmd = format!("{} --delete tag", env); let output = run_sh(cmd.as_str()); assert!( !output.status.success(), "Command succeeded when it should have failed: {} {}", cmd, output.status ); let cmd = format!("{} --delete 1", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} -d 2", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} --delete 9999", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); //assert!(!output.status.success(), "Command succeded when it should have failed: {} {}", cmd, output.status); }); } // Test: Diff two items and verify the output #[test] fn test_diff_items() { with_temp_env(|data_dir| { // Create test items with the common environment setup create_test_items(data_dir); let env = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display()); let cmd = format!("{} --diff 1 2", env); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_B), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_B); let cmd = format!("{} --diff tag_a tag_b", env); let output = run_sh(cmd.as_str()); assert!( !output.status.success(), "Command succeeded when it should have failed: {} {}", cmd, output.status ); let cmd = format!("{} --diff tag_a 2", env); let output = run_sh(cmd.as_str()); assert!( !output.status.success(), "Command succeeded when it should have failed: {} {}", cmd, output.status ); let cmd = format!("{} --diff 9999 2", env); let output = run_sh(cmd.as_str()); assert!( !output.status.success(), "Command succeeded when it should have failed: {} {}", cmd, output.status ); // Output should be empty for non-existent item let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!( output_str.is_empty(), "Command output is not empty. Command: {} Output: {}", cmd, output_str ); }); } // Test: Compression functionality with different algorithms #[test] fn test_compression() { with_temp_env(|data_dir| { // Set the data directory for this test let env = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display()); // Test with no compression let cmd = format!("echo {} | {} -c unknown unknown", INPUT_A, env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( !output.status.success(), "Command succeeded when it should have failed: {} {}", cmd, output.status ); // Test with no compression let cmd = format!("echo {} | {} -c none none", INPUT_A, env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} --get none", env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); // Test with lz4 compression let cmd = format!("echo {} | {} -c lz4 lz4", INPUT_A, env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} --get lz4", env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); // Test with gzip compression let cmd = format!("echo {} | {} -c gzip gzip", INPUT_A, env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} --get gzip", env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); // Test with bzip2 compression let cmd = format!("echo {} | {} -c bzip2 bzip2", INPUT_A, env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let cmd = format!("{} --get bzip2", env); println!("RUNNING: {}", cmd); let output = run_sh(cmd.as_str()); assert!( output.status.success(), "Command failed: {} {}", cmd, output.status ); let output_str = String::from_utf8_lossy(&output.stdout).to_string(); assert!(output_str.contains(INPUT_A), "Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"", cmd, output_str, INPUT_A); }); } }