diff --git a/src/tests.rs b/src/tests.rs index 727b439..c9fefa0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -73,83 +73,45 @@ mod tests { #[test] fn test_save_item() { with_temp_env(|data_dir| { - // Set the data directory for this test - let env = format!("KEEP_DIR={} cargo run --", data_dir.display()); + // Create test items with the common environment setup + create_test_items(data_dir, |input, tag| { + // Set the data directory for this test + let env = format!("KEEP_DIR={} cargo run --", 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 output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed: {} RC={}", - cmd, - output.status - ); - - // Create a command that pipes input to keep with the specified environment - let cmd = format!("echo {} | {} --save", input, env); - let output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed: {} RC={}", - cmd, - output.status - ); - - // Create a command that pipes input to keep with the specified environment - let cmd = format!("echo {} | {} -s", input, env); - let output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed: {} RC={}", - cmd, - output.status - ); - - // Create a command that pipes input to keep with the specified environment - let cmd = format!("echo {} | {} tag1 tag2", input, env); - let output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed: {} RC={}", - cmd, - output.status - ); + // 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: {} RC={}", + cmd, + output.status + ); + }); }); } #[test] fn test_get_item() { with_temp_env(|data_dir| { - // Set the data directory for this test + // Create test items with the common environment setup + create_test_items(data_dir, |input, tag| { + // Set the data directory for this test + let env = format!("KEEP_DIR={} cargo run --", data_dir.display()); + + // 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: {} RC={}", + cmd, + output.status + ); + }); + let env = format!("KEEP_DIR={} cargo run --", data_dir.display()); - // Save an item with some content - let input_a = "test content A"; - let input_b = "test content B"; - - // Create a command that pipes input to keep with the specified environment - let cmd = format!("echo {} A | {} tag tag_a", input_a, env); - let output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed: {} RC={}", - cmd, - output.status - ); - - let cmd = format!("echo {} | {} tag tag_b", input_b, env); - let output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed: {} RC={}", - cmd, - output.status - ); - let cmd = format!("{} --get 1", env); let output = run_sh(cmd.as_str()); assert!( @@ -158,7 +120,7 @@ mod tests { cmd, output.status ); - assert!(String::from_utf8_lossy(&output.stdout).contains(input_a)); + assert!(String::from_utf8_lossy(&output.stdout).contains("test content A")); let cmd = format!("{} -g 1", env); let output = run_sh(cmd.as_str()); @@ -168,7 +130,7 @@ mod tests { cmd, output.status ); - assert!(String::from_utf8_lossy(&output.stdout).contains(input_a)); + assert!(String::from_utf8_lossy(&output.stdout).contains("test content A")); let cmd = format!("{} 1", env); let output = run_sh(cmd.as_str()); @@ -178,7 +140,7 @@ mod tests { cmd, output.status ); - assert!(String::from_utf8_lossy(&output.stdout).contains(input_a)); + assert!(String::from_utf8_lossy(&output.stdout).contains("test content A")); let cmd = format!("{} --get", env); let output = run_sh(cmd.as_str()); @@ -188,7 +150,7 @@ mod tests { cmd, output.status ); - assert!(String::from_utf8_lossy(&output.stdout).contains(input_b)); + assert!(String::from_utf8_lossy(&output.stdout).contains("test content B")); let cmd = format!("{} --get tag_a", env); let output = run_sh(cmd.as_str()); @@ -198,7 +160,7 @@ mod tests { cmd, output.status ); - assert!(String::from_utf8_lossy(&output.stdout).contains(input_a)); + assert!(String::from_utf8_lossy(&output.stdout).contains("test content A")); let cmd = format!("{} --get tag_b", env); let output = run_sh(cmd.as_str()); @@ -208,7 +170,7 @@ mod tests { cmd, output.status ); - assert!(String::from_utf8_lossy(&output.stdout).contains(input_b)); + assert!(String::from_utf8_lossy(&output.stdout).contains("test content B")); let cmd = format!("{} --get tag", env); let output = run_sh(cmd.as_str()); @@ -218,7 +180,7 @@ mod tests { cmd, output.status ); - assert!(String::from_utf8_lossy(&output.stdout).contains(input_b)); + assert!(String::from_utf8_lossy(&output.stdout).contains("test content B")); }); } @@ -285,32 +247,24 @@ mod tests { #[test] fn test_delete_item() { with_temp_env(|data_dir| { - // Set the data directory for this test + // Create test items with the common environment setup + create_test_items(data_dir, |input, tag| { + // Set the data directory for this test + let env = format!("KEEP_DIR={} cargo run --", data_dir.display()); + + // 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: {} RC={}", + cmd, + output.status + ); + }); + let env = format!("KEEP_DIR={} cargo run --", data_dir.display()); - // Save an item with some content - let input_a = "test content A"; - let input_b = "test content B"; - - // Create a command that pipes input to keep with the specified environment - let cmd = format!("echo {} | {} tag tag_a", input_a, env); - let output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed with status: {} RC={}", - cmd, - output.status - ); - - let cmd = format!("echo {} | {} tag tag_b", input_b, env); - let output = run_sh(cmd.as_str()); - assert!( - output.status.success(), - "Command failed with status: {} RC={}", - cmd, - output.status - ); - let cmd = format!("{} --delete tag", env); let output = run_sh(cmd.as_str()); assert!( @@ -324,7 +278,7 @@ mod tests { let output = run_sh(cmd.as_str()); assert!( output.status.success(), - "Command failed with status: {} RC={}", + "Command failed: {} RC={}", cmd, output.status ); @@ -333,7 +287,7 @@ mod tests { let output = run_sh(cmd.as_str()); assert!( output.status.success(), - "Command failed with status: {} RC={}", + "Command failed: {} RC={}", cmd, output.status ); @@ -342,7 +296,7 @@ mod tests { let output = run_sh(cmd.as_str()); assert!( output.status.success(), - "Command failed with status: {} RC={}", + "Command failed: {} RC={}", cmd, output.status );