test: update tests to use keep binary directly instead of cargo run
Co-authored-by: aider (openai/andrew.openrouter.qwen.qwen3-coder) <aider@aider.chat>
This commit is contained in:
530
src/tests.rs
530
src/tests.rs
@@ -1,6 +1,7 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
@@ -10,14 +11,25 @@ mod tests {
|
||||
|
||||
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 run the keep binary with arguments
|
||||
fn run_keep(args: &[&str], stdin_data: Option<&str>, keep_dir: &Path) -> std::process::Output {
|
||||
let mut cmd = Command::new(env!("CARGO_BIN_EXE_keep"));
|
||||
cmd.args(args)
|
||||
.env("KEEP_DIR", keep_dir);
|
||||
|
||||
if let Some(data) = stdin_data {
|
||||
cmd.stdin(std::process::Stdio::piped());
|
||||
}
|
||||
|
||||
let mut child = cmd.spawn().expect("Failed to execute keep command");
|
||||
|
||||
if let Some(data) = stdin_data {
|
||||
if let Some(mut stdin) = child.stdin.take() {
|
||||
stdin.write_all(data.as_bytes()).expect("Failed to write to stdin");
|
||||
}
|
||||
}
|
||||
|
||||
child.wait_with_output().expect("Failed to wait for command")
|
||||
}
|
||||
|
||||
// Helper function to create a temporary test environment
|
||||
@@ -26,366 +38,248 @@ mod tests {
|
||||
F: FnOnce(&Path),
|
||||
{
|
||||
let dir = tempdir().expect("Failed to create temporary directory");
|
||||
let _db_path = dir.path().join("keep-1.db");
|
||||
let data_path = dir.path();
|
||||
|
||||
// Create the data directory structure
|
||||
fs::create_dir_all(dir.path()).expect("Failed to create directory");
|
||||
fs::create_dir_all(data_path).expect("Failed to create directory");
|
||||
|
||||
// Run the test
|
||||
f(dir.path());
|
||||
f(data_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 keep_cmd = format!("KEEP_DIR={} cargo run -- --verbose", dir.display());
|
||||
|
||||
fn create_test_items(data_path: &Path) {
|
||||
// Create first item with tag_a and tag
|
||||
let cmd = format!("echo {} | {} tag tag_a tag", INPUT_A, keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
let output = run_keep(&["tag_a", "tag"], Some(INPUT_A), data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
"Failed to create first test item: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
// Create second item with tag_b and tag
|
||||
let cmd = format!("echo {} | {} tag tag_b tag", INPUT_B, keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
let output = run_keep(&["tag_b", "tag"], Some(INPUT_B), data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
"Failed to create second test item: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
// 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 keep_cmd = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display());
|
||||
|
||||
with_temp_env(|data_path| {
|
||||
// Test content and tags
|
||||
let input = "test content";
|
||||
let tag = "tag";
|
||||
let tag = "test_tag";
|
||||
|
||||
// Create a command that pipes input to keep with the specified environment
|
||||
let cmd = format!("echo {} | {} {}", input, keep_cmd, tag);
|
||||
let output = run_sh(cmd.as_str());
|
||||
// Save an item
|
||||
let output = run_keep(&[tag], Some(input), data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
"Failed to save item: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
// Verify item was saved by listing
|
||||
let output = run_keep(&["--list"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to list items: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(
|
||||
output_str.contains(tag),
|
||||
"List output does not contain expected tag. Output: {}",
|
||||
output_str
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_item() {
|
||||
with_temp_env(|data_dir| {
|
||||
// Create test items with the common environment setup
|
||||
create_test_items(data_dir);
|
||||
with_temp_env(|data_path| {
|
||||
// Create test items
|
||||
create_test_items(data_path);
|
||||
|
||||
let keep_cmd = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display());
|
||||
|
||||
let cmd = format!("{} --get 1", keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
// Get item by ID
|
||||
let output = run_keep(&["--get", "1"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
"Failed to get item by ID: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let output_str = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(
|
||||
output_str.contains(INPUT_A),
|
||||
"Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"",
|
||||
cmd,
|
||||
output_str,
|
||||
INPUT_A
|
||||
"Get output does not contain expected content. Output: {}",
|
||||
output_str
|
||||
);
|
||||
|
||||
let cmd = format!("{} -g 1", keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
// Get item by tag
|
||||
let output = run_keep(&["--get", "tag_a"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
"Failed to get item by tag: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let output_str = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||
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", keep_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
|
||||
);
|
||||
|
||||
let cmd = format!("{} --get", keep_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_B),
|
||||
"Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"",
|
||||
cmd,
|
||||
output_str,
|
||||
INPUT_B
|
||||
);
|
||||
|
||||
let cmd = format!("{} --get tag_a", keep_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
|
||||
);
|
||||
|
||||
let cmd = format!("{} --get tag_b", keep_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_B),
|
||||
"Command output does not contain expected string. Command: {} Output: {} Expected: \"{}\"",
|
||||
cmd,
|
||||
output_str,
|
||||
INPUT_B
|
||||
);
|
||||
|
||||
let cmd = format!("{} --get tag", keep_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_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 keep_cmd = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display());
|
||||
|
||||
let cmd = format!("{} --list", keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
);
|
||||
|
||||
let cmd = format!("{} -l", keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
);
|
||||
|
||||
let cmd = format!("{} --list tag_a", keep_cmd);
|
||||
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 keep_cmd = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display());
|
||||
|
||||
let cmd = format!("{} --delete tag", keep_cmd);
|
||||
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", keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
);
|
||||
|
||||
let cmd = format!("{} -d 2", keep_cmd);
|
||||
let output = run_sh(cmd.as_str());
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Command failed: {} {}",
|
||||
cmd,
|
||||
output.status
|
||||
);
|
||||
|
||||
let cmd = format!("{} --delete 9999", keep_cmd);
|
||||
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 keep_cmd = format!("KEEP_DIR={} cargo run -- --verbose", data_dir.display());
|
||||
|
||||
let cmd = format!("{} --diff 1 2", keep_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
|
||||
);
|
||||
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", keep_cmd);
|
||||
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", keep_cmd);
|
||||
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", keep_cmd);
|
||||
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,
|
||||
"Get by tag output does not contain expected content. Output: {}",
|
||||
output_str
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list_items() {
|
||||
with_temp_env(|data_path| {
|
||||
// Create test items
|
||||
create_test_items(data_path);
|
||||
|
||||
// List all items
|
||||
let output = run_keep(&["--list"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to list items: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
// List items with specific tag
|
||||
let output = run_keep(&["--list", "tag_a"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to list items by tag: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete_item() {
|
||||
with_temp_env(|data_path| {
|
||||
// Create test items
|
||||
create_test_items(data_path);
|
||||
|
||||
// Try to delete with tag (should fail)
|
||||
let output = run_keep(&["--delete", "tag"], None, data_path);
|
||||
assert!(
|
||||
!output.status.success(),
|
||||
"Delete with tag should have failed but succeeded"
|
||||
);
|
||||
|
||||
// Delete item by ID
|
||||
let output = run_keep(&["--delete", "1"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to delete item by ID: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
// Try to delete non-existent item (should succeed silently)
|
||||
let output = run_keep(&["--delete", "9999"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Delete non-existent item should succeed: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff_items() {
|
||||
with_temp_env(|data_path| {
|
||||
// Create test items
|
||||
create_test_items(data_path);
|
||||
|
||||
// Diff two items by ID
|
||||
let output = run_keep(&["--diff", "1", "2"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to diff items: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
// Try to diff with tag (should fail)
|
||||
let output = run_keep(&["--diff", "tag_a", "tag_b"], None, data_path);
|
||||
assert!(
|
||||
!output.status.success(),
|
||||
"Diff with tags should have failed but succeeded"
|
||||
);
|
||||
|
||||
// Try to diff non-existent item (should fail)
|
||||
let output = run_keep(&["--diff", "9999", "1"], None, data_path);
|
||||
assert!(
|
||||
!output.status.success(),
|
||||
"Diff with non-existent item should have failed but succeeded"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_info_item() {
|
||||
with_temp_env(|data_path| {
|
||||
// Create test items
|
||||
create_test_items(data_path);
|
||||
|
||||
// Get info for item by ID
|
||||
let output = run_keep(&["--info", "1"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to get item info: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
// Get info for last item
|
||||
let output = run_keep(&["--info"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to get last item info: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_update_item() {
|
||||
with_temp_env(|data_path| {
|
||||
// Create test items
|
||||
create_test_items(data_path);
|
||||
|
||||
// Update item tags
|
||||
let output = run_keep(&["--update", "1", "new_tag"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to update item: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
|
||||
// Verify update by listing
|
||||
let output = run_keep(&["--list"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to list items after update: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_status() {
|
||||
with_temp_env(|data_path| {
|
||||
// Get status
|
||||
let output = run_keep(&["--status"], None, data_path);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"Failed to get status: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user