diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-05 20:52:26 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-05 20:52:26 +0100 |
| commit | 0fca7c5476099212ed1fa9ed812aa456ba004300 (patch) | |
| tree | 3acacb019691f56a510ddf21f929816e45d21d9a /test_utilities/src | |
| parent | b17907faf8d9693cef94a6048d802bd4ced9102f (diff) | |
Add tests for utils and metadata
Diffstat (limited to 'test_utilities/src')
| -rw-r--r-- | test_utilities/src/lib.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/test_utilities/src/lib.rs b/test_utilities/src/lib.rs new file mode 100644 index 0000000..f5a7b39 --- /dev/null +++ b/test_utilities/src/lib.rs @@ -0,0 +1,34 @@ +use std::env::temp_dir; +use std::fs::{create_dir_all, read_to_string, remove_dir_all, File}; +use std::io::Write; +use std::path::PathBuf; +use std::time::{SystemTime, UNIX_EPOCH}; +use std::process::id; + +pub fn setup_test_dir() -> PathBuf { + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + let process_id = id(); + let test_dir = temp_dir().join(format!("blog_test_{}_{}", timestamp, process_id)); + create_dir_all(&test_dir) + .expect("Could not create test directory"); + test_dir +} + +pub fn cleanup_test_dir(test_dir: &PathBuf) { + if test_dir.exists() { + remove_dir_all(test_dir).unwrap(); + } +} + +pub fn create_test_file(path: &PathBuf, content: &str) { + let mut file = File::create(path).unwrap(); + file.write_all(content.as_bytes()).unwrap(); +} + +pub fn assert_file_contents(path: &PathBuf, expected: &str) { + let content = read_to_string(path).unwrap(); + assert_eq!(content.trim(), expected.trim()); +} |