winbrew_database/
bootstrap.rs

1use std::fs;
2use std::io;
3
4use crate::core::ResolvedPaths;
5
6pub(crate) fn ensure_managed_root_dirs(paths: &ResolvedPaths) -> std::io::Result<()> {
7    let db_dir = paths
8        .db
9        .parent()
10        .ok_or_else(|| {
11            io::Error::new(
12                io::ErrorKind::InvalidInput,
13                "database path is missing its parent directory",
14            )
15        })?
16        .to_path_buf();
17
18    for dir in [
19        &paths.data,
20        &paths.pkgdb,
21        &db_dir,
22        &paths.logs,
23        &paths.cache,
24    ] {
25        fs::create_dir_all(dir)?;
26    }
27
28    Ok(())
29}
30
31#[cfg(test)]
32mod tests {
33    use super::ensure_managed_root_dirs;
34    use crate::core::resolved_paths;
35    use tempfile::tempdir;
36
37    #[test]
38    fn creates_shared_managed_root_directories_only() {
39        let root = tempdir().expect("temp dir");
40        let paths = resolved_paths(
41            root.path(),
42            "${root}\\packages",
43            "${root}\\data",
44            "${root}\\data\\logs",
45            "${root}\\data\\cache",
46        );
47
48        ensure_managed_root_dirs(&paths).expect("bootstrap dirs");
49
50        assert!(paths.data.exists());
51        assert!(paths.pkgdb.exists());
52        assert!(paths.db.parent().expect("db parent").exists());
53        assert!(paths.logs.exists());
54        assert!(paths.cache.exists());
55        assert!(!paths.packages.exists());
56    }
57}