]> git.r.bdr.sh - rbdr/page/blame_incremental - test_utilities/src/lib.rs
Bump version
[rbdr/page] / test_utilities / src / lib.rs
... / ...
CommitLineData
1use std::env::temp_dir;
2use std::fs::{create_dir_all, read_to_string, remove_dir_all, File};
3use std::io::Write;
4use std::path::PathBuf;
5use std::time::{SystemTime, UNIX_EPOCH};
6use std::process::id;
7
8pub 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
20pub fn cleanup_test_dir(test_dir: &PathBuf) {
21 if test_dir.exists() {
22 remove_dir_all(test_dir).unwrap();
23 }
24}
25
26pub 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
31pub fn assert_file_contents(path: &PathBuf, expected: &str) {
32 let content = read_to_string(path).unwrap();
33 assert_eq!(content.trim(), expected.trim());
34}