330 lines
11 KiB
Rust
330 lines
11 KiB
Rust
#[cfg(test)]
|
|
mod tests {
|
|
use std::fs;
|
|
use std::path::Path;
|
|
use std::process::Command;
|
|
|
|
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: 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 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| {
|
|
// 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 with status: {} 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 with status: {} 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 with status: {} 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 with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_item() {
|
|
with_temp_env(|data_dir| {
|
|
// 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_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 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!("{} --get 1", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input_a));
|
|
|
|
let cmd = format!("{} -g 1", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input_a));
|
|
|
|
let cmd = format!("{} 1", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input_a));
|
|
|
|
let cmd = format!("{} --get", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input_b));
|
|
|
|
let cmd = format!("{} --get tag_a", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input_a));
|
|
|
|
let cmd = format!("{} --get tag_b", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input_b));
|
|
|
|
let cmd = format!("{} --get tag", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input_b));
|
|
});
|
|
}
|
|
|
|
// Test: List items and verify the output
|
|
#[test]
|
|
fn test_list_items() {
|
|
with_temp_env(|data_dir| {
|
|
// 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_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 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!("{} --list", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
|
|
let cmd = format!("{} -l", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
|
|
let cmd = format!("{} --list tag_a", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
});
|
|
}
|
|
|
|
// 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={} 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!(
|
|
!output.status.success(),
|
|
"Command succeded when it should have failed: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
|
|
let cmd = format!("{} --delete 1", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
|
|
let cmd = format!("{} -d 2", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
|
|
let cmd = format!("{} --delete 9999", env);
|
|
let output = run_sh(cmd.as_str());
|
|
assert!(
|
|
output.status.success(),
|
|
"Command failed with status: {} RC={}",
|
|
cmd,
|
|
output.status
|
|
);
|
|
//assert!(!output.status.success(), "Command succeded when it should have failed: {} RC={}", cmd, output.status);
|
|
});
|
|
}
|
|
}
|