Debian Codegraph @

tests/common/mod.rs

raw symbols SCIP
1 //! Common test utilities for fork integration tests
2 //!
3 //! This module provides shared helper functions for integration tests,
4 //! reducing code duplication across test files.
5
6 #![allow(dead_code)]
7
8 use std::{
9 env, fs,
10 path::{Path, PathBuf},
11 sync::atomic::{AtomicU64, Ordering},
12 thread,
13 time::{Duration, Instant},
14 };
15
16 static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);
17
18 /// Get a unique test directory with counter to avoid conflicts
19 pub fn get_unique_test_dir(test_name: &str) -> PathBuf {
20 let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
21 env::temp_dir().join(format!("fork_test_{}_{}", test_name, counter))
22 }
23
24 /// Get a simple test directory without counter
25 pub fn get_test_dir(prefix: &str) -> PathBuf {
26 env::temp_dir().join(format!("fork_test_{}", prefix))
27 }
28
29 /// Setup a test directory (creates and cleans if exists)
30 pub fn setup_test_dir(path: PathBuf) -> PathBuf {
31 let _ = fs::remove_dir_all(&path);
32 fs::create_dir_all(&path).expect("Failed to create test directory");
33 path
34 }
35
36 /// Wait for a file to exist with timeout
37 pub fn wait_for_file(path: &Path, timeout_ms: u64) -> bool {
38 let start = Instant::now();
39 while start.elapsed().as_millis() < u128::from(timeout_ms) {
40 if path.exists() {
41 return true;
42 }
43 thread::sleep(Duration::from_millis(10));
44 }
45 false
46 }
47
48 /// Cleanup a test directory
49 pub fn cleanup_test_dir(path: &Path) {
50 let _ = fs::remove_dir_all(path);
51 }