aboutsummaryrefslogtreecommitdiff
path: root/test_utilities/src/lib.rs
blob: cb69853b2aff416d522999eee8c653d35ce26ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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!("page_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());
}