]> git.r.bdr.sh - rbdr/page/blob - test_utilities/src/lib.rs
cb69853b2aff416d522999eee8c653d35ce26ea7
[rbdr/page] / test_utilities / src / lib.rs
1 use std::env::temp_dir;
2 use std::fs::{create_dir_all, read_to_string, remove_dir_all, File};
3 use std::io::Write;
4 use std::path::PathBuf;
5 use std::time::{SystemTime, UNIX_EPOCH};
6 use std::process::id;
7
8 pub fn setup_test_dir() -> PathBuf {
9 let timestamp = SystemTime::now()
10 .duration_since(UNIX_EPOCH)
11 .unwrap()
12 .as_nanos();
13 let process_id = id();
14 let test_dir = temp_dir().join(format!("page_test_{}_{}", timestamp, process_id));
15 create_dir_all(&test_dir)
16 .expect("Could not create test directory");
17 test_dir
18 }
19
20 pub fn cleanup_test_dir(test_dir: &PathBuf) {
21 if test_dir.exists() {
22 remove_dir_all(test_dir).unwrap();
23 }
24 }
25
26 pub fn create_test_file(path: &PathBuf, content: &str) {
27 let mut file = File::create(path).unwrap();
28 file.write_all(content.as_bytes()).unwrap();
29 }
30
31 pub fn assert_file_contents(path: &PathBuf, expected: &str) {
32 let content = read_to_string(path).unwrap();
33 assert_eq!(content.trim(), expected.trim());
34 }