279 lines
11 KiB
Rust
279 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={}", 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: {}", output.status);
|
|
|
|
// Verify the item was saved
|
|
let output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --list", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input));
|
|
});
|
|
}
|
|
|
|
// 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={}", 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 mut child = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(cmd)
|
|
.spawn()
|
|
.expect("Failed to spawn process");
|
|
|
|
child.wait().expect("Failed to wait for child process");
|
|
|
|
// List items and verify the output
|
|
let output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --list", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains(input));
|
|
});
|
|
}
|
|
|
|
// Test: Get an item and verify the content
|
|
#[test]
|
|
fn test_get_item() {
|
|
with_temp_env(|data_dir| {
|
|
// Set the data directory for this test
|
|
let env = format!("KEEP_DIR={}", 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 mut cmd = Command::new("sh");
|
|
cmd.arg("-c")
|
|
.arg(format!("echo {} | {}", input, env));
|
|
|
|
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")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --get", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
assert_eq!(String::from_utf8_lossy(&output.stdout), input);
|
|
});
|
|
}
|
|
|
|
// Test: Add metadata to an item and verify it's stored
|
|
#[test]
|
|
fn test_metadata() {
|
|
with_temp_env(|data_dir| {
|
|
// Set the data directory for this test
|
|
let env = format!("KEEP_DIR={}", data_dir.display());
|
|
|
|
// 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 mut cmd = Command::new("sh");
|
|
cmd.arg("-c")
|
|
.arg(format!("echo {} | {}", input, env))
|
|
.arg("cargo run -- --save --meta key=value");
|
|
|
|
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")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --list", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
let list_output_str = String::from_utf8_lossy(&list_output.stdout);
|
|
let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::<i64>().expect("Invalid ID");
|
|
|
|
// Get the item and verify the metadata
|
|
let output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --info {}", env, id))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains("key: value"));
|
|
});
|
|
}
|
|
|
|
// Test: Use tags to organize items
|
|
#[test]
|
|
fn test_tags() {
|
|
with_temp_env(|data_dir| {
|
|
// Set the data directory for this test
|
|
let env = format!("KEEP_DIR={}", data_dir.display());
|
|
|
|
// 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 output = run_sh(cmd.as_str());
|
|
assert!(output.status.success(), "Command failed with status: {}", output.status);
|
|
|
|
// List items with a specific tag
|
|
let output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --list tag1", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
assert!(String::from_utf8_lossy(&output.stdout).contains("test content"));
|
|
});
|
|
}
|
|
|
|
// Test: Use compression
|
|
#[test]
|
|
fn test_compression() {
|
|
with_temp_env(|data_dir| {
|
|
// Set the data directory for this test
|
|
let env = format!("KEEP_DIR={}", data_dir.display());
|
|
|
|
// 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 output = run_sh(cmd.as_str());
|
|
assert!(output.status.success(), "Command failed with status: {}", output.status);
|
|
|
|
// Get the item ID
|
|
let list_output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --list", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
let list_output_str = String::from_utf8_lossy(&list_output.stdout);
|
|
let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::<i64>().expect("Invalid ID");
|
|
|
|
// Get the item and verify the content
|
|
let output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --get {}", env, id))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
assert_eq!(String::from_utf8_lossy(&output.stdout), input);
|
|
});
|
|
}
|
|
|
|
// 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={}", 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 mut child = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(cmd)
|
|
.spawn()
|
|
.expect("Failed to spawn process");
|
|
|
|
child.wait().expect("Failed to wait for child process");
|
|
|
|
// Get the item ID
|
|
let list_output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --list", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
let list_output_str = String::from_utf8_lossy(&list_output.stdout);
|
|
let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::<i64>().expect("Invalid ID");
|
|
|
|
// Delete the item
|
|
let _output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --delete {}", env, id))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
// Verify the item was deleted
|
|
let list_output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} cargo run -- --list", env))
|
|
.output()
|
|
.expect("Failed to execute command");
|
|
|
|
assert_eq!(String::from_utf8_lossy(&list_output.stdout).trim(), "");
|
|
});
|
|
}
|
|
}
|