refactor: use create_test_items for test environment setup
This commit is contained in:
96
src/tests.rs
96
src/tests.rs
@@ -73,44 +73,13 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_save_item() {
|
fn test_save_item() {
|
||||||
with_temp_env(|data_dir| {
|
with_temp_env(|data_dir| {
|
||||||
|
// Create test items with the common environment setup
|
||||||
|
create_test_items(data_dir, |input, tag| {
|
||||||
// Set the data directory for this test
|
// Set the data directory for this test
|
||||||
let env = format!("KEEP_DIR={} cargo run --", data_dir.display());
|
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
|
// Create a command that pipes input to keep with the specified environment
|
||||||
let cmd = format!("echo {} | {}", input, env);
|
let cmd = format!("echo {} | {} {}", input, env, tag);
|
||||||
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());
|
let output = run_sh(cmd.as_str());
|
||||||
assert!(
|
assert!(
|
||||||
output.status.success(),
|
output.status.success(),
|
||||||
@@ -119,20 +88,19 @@ mod tests {
|
|||||||
output.status
|
output.status
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_item() {
|
fn test_get_item() {
|
||||||
with_temp_env(|data_dir| {
|
with_temp_env(|data_dir| {
|
||||||
|
// Create test items with the common environment setup
|
||||||
|
create_test_items(data_dir, |input, tag| {
|
||||||
// Set the data directory for this test
|
// Set the data directory for this test
|
||||||
let env = format!("KEEP_DIR={} cargo run --", data_dir.display());
|
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
|
// Create a command that pipes input to keep with the specified environment
|
||||||
let cmd = format!("echo {} A | {} tag tag_a", input_a, env);
|
let cmd = format!("echo {} | {} {}", input, env, tag);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
assert!(
|
assert!(
|
||||||
output.status.success(),
|
output.status.success(),
|
||||||
@@ -140,15 +108,9 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
output.status
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
let cmd = format!("echo {} | {} tag tag_b", input_b, env);
|
let env = format!("KEEP_DIR={} cargo run --", data_dir.display());
|
||||||
let output = run_sh(cmd.as_str());
|
|
||||||
assert!(
|
|
||||||
output.status.success(),
|
|
||||||
"Command failed: {} RC={}",
|
|
||||||
cmd,
|
|
||||||
output.status
|
|
||||||
);
|
|
||||||
|
|
||||||
let cmd = format!("{} --get 1", env);
|
let cmd = format!("{} --get 1", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -158,7 +120,7 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
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 cmd = format!("{} -g 1", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -168,7 +130,7 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
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 cmd = format!("{} 1", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -178,7 +140,7 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
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 cmd = format!("{} --get", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -188,7 +150,7 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
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 cmd = format!("{} --get tag_a", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -198,7 +160,7 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
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 cmd = format!("{} --get tag_b", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -208,7 +170,7 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
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 cmd = format!("{} --get tag", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -218,7 +180,7 @@ mod tests {
|
|||||||
cmd,
|
cmd,
|
||||||
output.status
|
output.status
|
||||||
);
|
);
|
||||||
assert!(String::from_utf8_lossy(&output.stdout).contains(input_b));
|
assert!(String::from_utf8_lossy(&output.stdout).contains("test content B"));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,31 +247,23 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_delete_item() {
|
fn test_delete_item() {
|
||||||
with_temp_env(|data_dir| {
|
with_temp_env(|data_dir| {
|
||||||
|
// Create test items with the common environment setup
|
||||||
|
create_test_items(data_dir, |input, tag| {
|
||||||
// Set the data directory for this test
|
// Set the data directory for this test
|
||||||
let env = format!("KEEP_DIR={} cargo run --", data_dir.display());
|
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
|
// Create a command that pipes input to keep with the specified environment
|
||||||
let cmd = format!("echo {} | {} tag tag_a", input_a, env);
|
let cmd = format!("echo {} | {} {}", input, env, tag);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
assert!(
|
assert!(
|
||||||
output.status.success(),
|
output.status.success(),
|
||||||
"Command failed with status: {} RC={}",
|
"Command failed: {} RC={}",
|
||||||
cmd,
|
cmd,
|
||||||
output.status
|
output.status
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
let cmd = format!("echo {} | {} tag tag_b", input_b, env);
|
let env = format!("KEEP_DIR={} cargo run --", data_dir.display());
|
||||||
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 cmd = format!("{} --delete tag", env);
|
||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
@@ -324,7 +278,7 @@ mod tests {
|
|||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
assert!(
|
assert!(
|
||||||
output.status.success(),
|
output.status.success(),
|
||||||
"Command failed with status: {} RC={}",
|
"Command failed: {} RC={}",
|
||||||
cmd,
|
cmd,
|
||||||
output.status
|
output.status
|
||||||
);
|
);
|
||||||
@@ -333,7 +287,7 @@ mod tests {
|
|||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
assert!(
|
assert!(
|
||||||
output.status.success(),
|
output.status.success(),
|
||||||
"Command failed with status: {} RC={}",
|
"Command failed: {} RC={}",
|
||||||
cmd,
|
cmd,
|
||||||
output.status
|
output.status
|
||||||
);
|
);
|
||||||
@@ -342,7 +296,7 @@ mod tests {
|
|||||||
let output = run_sh(cmd.as_str());
|
let output = run_sh(cmd.as_str());
|
||||||
assert!(
|
assert!(
|
||||||
output.status.success(),
|
output.status.success(),
|
||||||
"Command failed with status: {} RC={}",
|
"Command failed: {} RC={}",
|
||||||
cmd,
|
cmd,
|
||||||
output.status
|
output.status
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user