-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
59 lines (52 loc) · 1.71 KB
/
Copy pathmod.rs
File metadata and controls
59 lines (52 loc) · 1.71 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Test Fixtures - Common setup for all tests
//!
//! Reduces duplication and ensures consistent test environment.
use std::fs;
use std::path::PathBuf;
// Re-export tempfile for convenience
pub use tempfile::{self, TempDir};
/// Create a test sandbox with automatic cleanup
///
/// Returns a temporary directory that will be deleted when dropped.
/// Use this for all filesystem operation tests.
///
/// # Example
/// ```
/// let temp = test_sandbox("my_test");
/// let target = temp.path().join("test_dir");
/// fs::create_dir(&target).unwrap();
/// // temp automatically cleaned up on drop
/// ```
pub fn test_sandbox(test_name: &str) -> TempDir {
TempDir::with_prefix(format!("vsh_test_{}_", test_name)).expect("Failed to create test sandbox")
}
/// Create a test sandbox with pre-populated structure
///
/// Creates a sandbox with common directory structure for testing.
///
/// Structure:
/// ```
/// /
/// ├── existing_dir/
/// ├── existing_file.txt
/// └── nested/
/// └── deep/
/// ```
#[allow(dead_code)]
pub fn populated_sandbox(test_name: &str) -> TempDir {
let temp = test_sandbox(test_name);
// Create structure
fs::create_dir(temp.path().join("existing_dir")).unwrap();
fs::write(temp.path().join("existing_file.txt"), b"test content").unwrap();
fs::create_dir_all(temp.path().join("nested/deep")).unwrap();
temp
}
/// Helper to create a test sandbox as PathBuf for legacy compatibility
#[allow(dead_code)]
pub fn test_sandbox_path(test_name: &str) -> (TempDir, PathBuf) {
let temp = test_sandbox(test_name);
let path = temp.path().to_path_buf();
(temp, path)
}