test: add compression engine tests and refactor test structure
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
d14857fa47
commit
81005ec8f4
347
src/tests.rs
347
src/tests.rs
@@ -1,287 +1,136 @@
|
|||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
// Global test input values
|
|
||||||
const INPUT_A: &str = "test content A";
|
|
||||||
const INPUT_B: &str = "test content B";
|
|
||||||
|
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
// Helper function to run the keep binary with arguments
|
// Import the modules we need for testing
|
||||||
fn run_keep(args: &[&str], stdin_data: Option<&str>, keep_dir: &Path) -> std::process::Output {
|
use keep::compression_engine::{self, CompressionType};
|
||||||
let mut cmd = Command::new(env!("CARGO_BIN_EXE_keep"));
|
use keep::compression_engine::gzip::CompressionEngineGZip;
|
||||||
cmd.args(args)
|
use keep::compression_engine::lz4::CompressionEngineLZ4;
|
||||||
.env("KEEP_DIR", keep_dir);
|
use keep::compression_engine::none::CompressionEngineNone;
|
||||||
|
|
||||||
if stdin_data.is_some() {
|
#[test]
|
||||||
cmd.stdin(std::process::Stdio::piped());
|
fn test_compression_engine_gzip() {
|
||||||
} else {
|
let test_data = b"test compression data";
|
||||||
cmd.stdin(std::process::Stdio::null());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut child = cmd.spawn().expect("Failed to execute keep command");
|
// Create a temporary file
|
||||||
|
|
||||||
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
|
|
||||||
fn with_temp_env<F>(f: F)
|
|
||||||
where
|
|
||||||
F: FnOnce(&Path),
|
|
||||||
{
|
|
||||||
let dir = tempdir().expect("Failed to create temporary directory");
|
let dir = tempdir().expect("Failed to create temporary directory");
|
||||||
let data_path = dir.path();
|
let file_path = dir.path().join("test.gz");
|
||||||
|
|
||||||
// Create the data directory structure
|
// Test compression engine
|
||||||
fs::create_dir_all(data_path).expect("Failed to create directory");
|
let engine = CompressionEngineGZip::new();
|
||||||
|
assert!(engine.is_supported());
|
||||||
|
|
||||||
// Run the test
|
// Create compressed file
|
||||||
f(data_path);
|
{
|
||||||
|
let mut writer = engine.create(file_path.clone()).expect("Failed to create writer");
|
||||||
|
writer.write_all(test_data).expect("Failed to write data");
|
||||||
|
// Writer is dropped here, which should finish the compression
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up
|
// Read compressed file
|
||||||
dir.close().expect("Failed to remove temporary directory");
|
let mut reader = engine.open(file_path).expect("Failed to open reader");
|
||||||
}
|
let mut decompressed = Vec::new();
|
||||||
|
std::io::copy(&mut reader, &mut decompressed).expect("Failed to read data");
|
||||||
|
|
||||||
// Helper function to create test items with specific content and tags
|
assert_eq!(test_data, decompressed.as_slice());
|
||||||
fn create_test_items(data_path: &Path) {
|
|
||||||
// Create first item with tag_a and tag
|
|
||||||
let output = run_keep(&["tag_a", "tag"], Some(INPUT_A), data_path);
|
|
||||||
assert!(
|
|
||||||
output.status.success(),
|
|
||||||
"Failed to create first test item: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Create second item with tag_b and tag
|
|
||||||
let output = run_keep(&["tag_b", "tag"], Some(INPUT_B), data_path);
|
|
||||||
assert!(
|
|
||||||
output.status.success(),
|
|
||||||
"Failed to create second test item: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_save_item() {
|
fn test_compression_engine_lz4() {
|
||||||
with_temp_env(|data_path| {
|
let test_data = b"test compression data";
|
||||||
// Test content and tags
|
|
||||||
let input = "test content";
|
|
||||||
let tag = "test_tag";
|
|
||||||
|
|
||||||
// Save an item
|
// Create a temporary file
|
||||||
let output = run_keep(&[tag], Some(input), data_path);
|
let dir = tempdir().expect("Failed to create temporary directory");
|
||||||
assert!(
|
let file_path = dir.path().join("test.lz4");
|
||||||
output.status.success(),
|
|
||||||
"Failed to save item: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Verify item was saved by listing
|
// Test compression engine
|
||||||
let output = run_keep(&["--list"], None, data_path);
|
let engine = CompressionEngineLZ4::new();
|
||||||
assert!(
|
|
||||||
output.status.success(),
|
|
||||||
"Failed to list items: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
|
|
||||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
// Create compressed file
|
||||||
assert!(
|
{
|
||||||
output_str.contains(tag),
|
let mut writer = engine.create(file_path.clone()).expect("Failed to create writer");
|
||||||
"List output does not contain expected tag. Output: {}",
|
writer.write_all(test_data).expect("Failed to write data");
|
||||||
output_str
|
}
|
||||||
);
|
|
||||||
});
|
// Read compressed file
|
||||||
|
let mut reader = engine.open(file_path).expect("Failed to open reader");
|
||||||
|
let mut decompressed = Vec::new();
|
||||||
|
std::io::copy(&mut reader, &mut decompressed).expect("Failed to read data");
|
||||||
|
|
||||||
|
assert_eq!(test_data, decompressed.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_item() {
|
fn test_compression_engine_none() {
|
||||||
with_temp_env(|data_path| {
|
let test_data = b"test compression data";
|
||||||
// Create test items
|
|
||||||
create_test_items(data_path);
|
|
||||||
|
|
||||||
// Get item by ID
|
// Create a temporary file
|
||||||
let output = run_keep(&["--get", "1"], None, data_path);
|
let dir = tempdir().expect("Failed to create temporary directory");
|
||||||
assert!(
|
let file_path = dir.path().join("test.dat");
|
||||||
output.status.success(),
|
|
||||||
"Failed to get item by ID: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
|
||||||
assert!(
|
|
||||||
output_str.contains(INPUT_A),
|
|
||||||
"Get output does not contain expected content. Output: {}",
|
|
||||||
output_str
|
|
||||||
);
|
|
||||||
|
|
||||||
// Get item by tag
|
// Test compression engine
|
||||||
let output = run_keep(&["--get", "tag_a"], None, data_path);
|
let engine = CompressionEngineNone::new();
|
||||||
assert!(
|
|
||||||
output.status.success(),
|
// Create file
|
||||||
"Failed to get item by tag: {}",
|
{
|
||||||
String::from_utf8_lossy(&output.stderr)
|
let mut writer = engine.create(file_path.clone()).expect("Failed to create writer");
|
||||||
);
|
writer.write_all(test_data).expect("Failed to write data");
|
||||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
}
|
||||||
assert!(
|
|
||||||
output_str.contains(INPUT_A),
|
// Read file
|
||||||
"Get by tag output does not contain expected content. Output: {}",
|
let mut reader = engine.open(file_path).expect("Failed to open reader");
|
||||||
output_str
|
let mut data = Vec::new();
|
||||||
);
|
std::io::copy(&mut reader, &mut data).expect("Failed to read data");
|
||||||
});
|
|
||||||
|
assert_eq!(test_data, data.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_list_items() {
|
fn test_compression_engine_factory() {
|
||||||
with_temp_env(|data_path| {
|
// Test getting different compression engines
|
||||||
// Create test items
|
let lz4_engine = compression_engine::get_compression_engine(
|
||||||
create_test_items(data_path);
|
CompressionType::LZ4
|
||||||
|
).expect("Failed to get LZ4 engine");
|
||||||
|
assert!(lz4_engine.is_supported());
|
||||||
|
|
||||||
// List all items
|
let gzip_engine = compression_engine::get_compression_engine(
|
||||||
let output = run_keep(&["--list"], None, data_path);
|
CompressionType::GZip
|
||||||
assert!(
|
).expect("Failed to get GZip engine");
|
||||||
output.status.success(),
|
assert!(gzip_engine.is_supported());
|
||||||
"Failed to list items: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
|
|
||||||
// List items with specific tag
|
let none_engine = compression_engine::get_compression_engine(
|
||||||
let output = run_keep(&["--list", "tag_a"], None, data_path);
|
CompressionType::None
|
||||||
assert!(
|
).expect("Failed to get None engine");
|
||||||
output.status.success(),
|
assert!(none_engine.is_supported());
|
||||||
"Failed to list items by tag: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_delete_item() {
|
fn test_default_compression_type() {
|
||||||
with_temp_env(|data_path| {
|
let default = compression_engine::default_compression_type();
|
||||||
// Create test items
|
// The default should be a supported compression type
|
||||||
create_test_items(data_path);
|
let engine = compression_engine::get_compression_engine(default)
|
||||||
|
.expect("Failed to get default compression engine");
|
||||||
// Try to delete with tag (should fail)
|
assert!(engine.is_supported());
|
||||||
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]
|
#[test]
|
||||||
fn test_diff_items() {
|
fn test_compression_type_display() {
|
||||||
with_temp_env(|data_path| {
|
assert_eq!(format!("{}", CompressionType::LZ4), "LZ4");
|
||||||
// Create test items
|
assert_eq!(format!("{}", CompressionType::GZip), "GZip");
|
||||||
create_test_items(data_path);
|
assert_eq!(format!("{}", CompressionType::None), "None");
|
||||||
|
|
||||||
// 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]
|
#[test]
|
||||||
fn test_info_item() {
|
fn test_compression_type_from_str() {
|
||||||
with_temp_env(|data_path| {
|
use std::str::FromStr;
|
||||||
// Create test items
|
assert_eq!(CompressionType::from_str("lz4").unwrap(), CompressionType::LZ4);
|
||||||
create_test_items(data_path);
|
assert_eq!(CompressionType::from_str("gzip").unwrap(), CompressionType::GZip);
|
||||||
|
assert_eq!(CompressionType::from_str("none").unwrap(), CompressionType::None);
|
||||||
// Get info for item by ID
|
// Test case insensitivity
|
||||||
let output = run_keep(&["--info", "1"], None, data_path);
|
assert_eq!(CompressionType::from_str("LZ4").unwrap(), CompressionType::LZ4);
|
||||||
assert!(
|
assert_eq!(CompressionType::from_str("GZIP").unwrap(), CompressionType::GZip);
|
||||||
output.status.success(),
|
|
||||||
"Failed to get item info: {}",
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Get info for last item (need to provide an empty IDS_OR_TAGS parameter)
|
|
||||||
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