Skip to main content

ntfs_reader/
test_utils.rs

1use std::env;
2use std::path::{Path, PathBuf};
3
4// Return the letter of the volume to use for tests.
5// Priority:
6// 1. CI: use the current working directory's drive letter.
7// 2. Local: use the R: ramdisk if available.
8// 3. Fallback: use the system drive (typically C:).
9pub fn test_volume_letter() -> String {
10    if env::var_os("CI").is_some() {
11        if let Ok(cwd) = env::current_dir() {
12            let s = cwd.display().to_string();
13            // Expect a Windows path like "C:\..." - take the first
14            // character before ':' as the drive letter.
15            if s.len() >= 2 && s.chars().nth(1) == Some(':') {
16                return s.chars().next().unwrap().to_string();
17            }
18        }
19    }
20
21    // Prefer the ramdisk letter used in local dev machines.
22    // Fall back to the system drive if R: is unavailable.
23    let ramdisk = Path::new("R:\\");
24    if ramdisk.exists() {
25        return "R".to_string();
26    }
27
28    env::var("SystemDrive")
29        .ok()
30        .and_then(|s| s.chars().next())
31        .unwrap_or('C')
32        .to_string()
33}
34
35pub struct TempDirGuard(pub PathBuf);
36
37impl TempDirGuard {
38    pub fn new<P: AsRef<Path>>(path: P) -> std::io::Result<Self> {
39        let p = path.as_ref().to_path_buf();
40        let _ = std::fs::remove_dir_all(&p);
41        std::fs::create_dir_all(&p)?;
42        Ok(TempDirGuard(p))
43    }
44
45    pub fn path(&self) -> &Path {
46        &self.0
47    }
48}
49
50impl Drop for TempDirGuard {
51    fn drop(&mut self) {
52        let _ = std::fs::remove_dir_all(&self.0);
53    }
54}