aboutsummaryrefslogtreecommitdiff
path: root/test_utilities
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-01-03 23:17:06 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2025-01-03 23:17:06 +0100
commit1c5797fadeea6be505c01f13508203ba234cbdfa (patch)
treedd18df7a04b6c3496a2066573575b1760e550590 /test_utilities
parent260e8ec69b8e08b9fd105bf688e7a3a9fafecd61 (diff)
Update main and file_finder tests
Diffstat (limited to 'test_utilities')
-rw-r--r--test_utilities/Cargo.toml10
-rw-r--r--test_utilities/src/lib.rs34
2 files changed, 44 insertions, 0 deletions
diff --git a/test_utilities/Cargo.toml b/test_utilities/Cargo.toml
new file mode 100644
index 0000000..c8a753f
--- /dev/null
+++ b/test_utilities/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "test_utilities"
+version = "1.4.0"
+edition = "2021"
+license = "AGPL-3.0-or-later"
+description = "Shared test utilities for page"
+homepage = "https://r.bdr.sh/page.html"
+authors = ["Rubén Beltrán del Río <page@r.bdr.sh>"]
+
+[dependencies]
diff --git a/test_utilities/src/lib.rs b/test_utilities/src/lib.rs
new file mode 100644
index 0000000..cb69853
--- /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!("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());
+}