Skip to main content

update_kit/utils/
fs.rs

1use std::io;
2use std::path::Path;
3
4/// Ensures that a directory exists, creating it and all parent directories
5/// if necessary.
6pub async fn ensure_dir(path: &Path) -> io::Result<()> {
7    tokio::fs::create_dir_all(path).await
8}
9
10#[cfg(test)]
11mod tests {
12    use super::*;
13    use tempfile::TempDir;
14
15    #[tokio::test]
16    async fn ensure_dir_creates_nested_directories() {
17        let dir = TempDir::new().unwrap();
18        let nested = dir.path().join("a").join("b").join("c");
19
20        assert!(!nested.exists());
21        ensure_dir(&nested).await.unwrap();
22        assert!(nested.exists());
23        assert!(nested.is_dir());
24    }
25
26    #[tokio::test]
27    async fn ensure_dir_is_idempotent() {
28        let dir = TempDir::new().unwrap();
29        let target = dir.path().join("existing");
30
31        ensure_dir(&target).await.unwrap();
32        // Calling again should not fail
33        ensure_dir(&target).await.unwrap();
34        assert!(target.is_dir());
35    }
36}