grimoire_css_lib/core/
filesystem.rs

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use super::GrimoireCSSError;
use crate::buffer::add_message;
use std::{
    fs,
    path::{Path, PathBuf},
};

pub struct Filesystem;

impl Filesystem {
    /// Retrieves or creates the path for the configuration file.
    /// # Errors
    ///
    ///
    /// Returns a `GrimoireCSSError` if the path cannot be accessed or created.
    pub fn get_config_path(current_dir: &Path) -> Result<PathBuf, GrimoireCSSError> {
        let grimoire_dir = Self::get_or_create_grimoire_path(current_dir)?;
        let config_path = grimoire_dir.join("config");

        if !config_path.exists() {
            fs::create_dir(&config_path)?;
        }

        Ok(config_path.join("grimoire.config.json"))
    }

    /// Gets or creates the path for the GrimoireCSS folder.
    ///
    /// # Errors
    ///
    /// Returns a `GrimoireCSSError` if the directory cannot be created.
    pub fn get_or_create_grimoire_path(cwd: &Path) -> Result<PathBuf, GrimoireCSSError> {
        let grimoire_path = cwd.join("grimoire");

        if !grimoire_path.exists() {
            fs::create_dir(&grimoire_path)?;
            let config_path = grimoire_path.join("config");
            if !config_path.exists() {
                fs::create_dir(&config_path)?;
            }
            add_message(format!(
                "Configuration and directories created successfully at `{}`",
                "./grimoire"
            ));
        }

        Ok(grimoire_path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_get_or_create_grimoire_path_creates_directory() {
        let temp_dir = tempdir().unwrap();
        let cwd = temp_dir.path();

        let grimoire_path = cwd.join("grimoire");
        assert!(!grimoire_path.exists());

        let result = Filesystem::get_or_create_grimoire_path(cwd)
            .expect("Failed to get or create grimoire path");
        assert_eq!(result, grimoire_path);

        assert!(grimoire_path.exists());
        assert!(grimoire_path.is_dir());

        let config_path = grimoire_path.join("config");
        assert!(config_path.exists());
        assert!(config_path.is_dir());
    }

    #[test]
    fn test_get_or_create_grimoire_path_does_not_create_if_exists() {
        let temp_dir = tempdir().unwrap();
        let cwd = temp_dir.path();

        let grimoire_path = cwd.join("grimoire");
        let config_path = grimoire_path.join("config");
        fs::create_dir(&grimoire_path).unwrap();
        fs::create_dir(&config_path).unwrap();

        let result = Filesystem::get_or_create_grimoire_path(cwd)
            .expect("Failed to get or create grimoire path");
        assert_eq!(result, grimoire_path);

        assert!(grimoire_path.exists());
        assert!(grimoire_path.is_dir());
        assert!(config_path.exists());
        assert!(config_path.is_dir());
    }

    #[test]
    fn test_get_config_path_creates_config_file_path() {
        let temp_dir = tempdir().unwrap();
        let cwd = temp_dir.path();

        let result = Filesystem::get_config_path(cwd).expect("Failed to get or create config path");

        let expected_path = cwd.join("grimoire/config/grimoire.config.json");
        assert_eq!(result, expected_path);

        let grimoire_path = cwd.join("grimoire");
        let config_dir = grimoire_path.join("config");
        assert!(grimoire_path.exists());
        assert!(grimoire_path.is_dir());
        assert!(config_dir.exists());
        assert!(config_dir.is_dir());
    }

    #[test]
    fn test_get_config_path_does_not_create_if_exists() {
        let temp_dir = tempdir().unwrap();
        let cwd = temp_dir.path();

        let grimoire_path = cwd.join("grimoire");
        let config_dir = grimoire_path.join("config");
        let config_file_path = config_dir.join("grimoire.config.json");
        fs::create_dir(&grimoire_path).unwrap();
        fs::create_dir(&config_dir).unwrap();
        fs::write(&config_file_path, b"{}").unwrap();

        let result = Filesystem::get_config_path(cwd).expect("Failed to get or create config path");
        assert_eq!(result, config_file_path);

        assert!(grimoire_path.exists());
        assert!(grimoire_path.is_dir());
        assert!(config_dir.exists());
        assert!(config_dir.is_dir());
        assert!(config_file_path.exists());
    }
}