spinne_core/
config.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
// Handles interactions with the config file

use std::{fs, path::PathBuf};

use serde_json::Value;
use spinne_logger::Logger;

#[derive(Debug, PartialEq)]
pub struct ConfigValues {
    pub exclude: Option<Vec<String>>,
    pub include: Option<Vec<String>>,
}

pub struct Config {
    pub path: PathBuf,
}

impl Config {
    /// Reads the config file and returns values
    pub fn read(path: PathBuf) -> Option<ConfigValues> {
        let config = fs::read_to_string(path);

        if config.is_err() {
            Logger::error("Failed to read config file");
            return None;
        }

        match serde_json::from_str::<Value>(&config.unwrap()) {
            Ok(value) => {
                let exclude_value = value.get("exclude");
                let include_value = value.get("include");

                let exclude = match exclude_value {
                    Some(value) => Some(Self::get_array_of_strings(value)),
                    None => None,
                };

                let include = match include_value {
                    Some(value) => Some(Self::get_array_of_strings(value)),
                    None => None,
                };

                Some(ConfigValues { exclude, include })
            }
            Err(err) => {
                Logger::error("Failed to parse config file");
                Logger::error(&err.to_string());
                return None;
            }
        }
    }

    /// Maps a Value to an array of strings
    fn get_array_of_strings(value: &Value) -> Vec<String> {
        value
            .as_array()
            .unwrap_or(&vec![])
            .iter()
            .map(|v| v.as_str().unwrap_or("").to_string())
            .collect::<Vec<String>>()
    }
}

#[cfg(test)]
mod tests {
    use crate::util::test_utils::create_mock_project;

    use super::*;

    #[test]
    fn test_config_read() {
        let temp_dir = create_mock_project(&vec![(
            "spinne.json",
            r#"{"exclude": ["test.tsx"], "include": ["test.tsx"]}"#,
        )]);
        let config = Config::read(temp_dir.path().join("spinne.json"));

        assert_eq!(
            config,
            Some(ConfigValues {
                exclude: Some(vec!["test.tsx".to_string()]),
                include: Some(vec!["test.tsx".to_string()])
            })
        );
    }

    #[test]
    fn test_config_read_no_config() {
        let temp_dir = create_mock_project(&vec![]);
        let config = Config::read(temp_dir.path().join("spinne.json"));

        assert_eq!(config, None);
    }

    #[test]
    fn test_config_read_invalid_config() {
        let temp_dir = create_mock_project(&vec![("spinne.json", r#"{"]ht["te)}"#)]);
        let config = Config::read(temp_dir.path().join("spinne.json"));

        assert_eq!(config, None);
    }

    #[test]
    fn test_config_without_include() {
        let temp_dir = create_mock_project(&vec![("spinne.json", r#"{"exclude": ["test.tsx"]}"#)]);
        let config = Config::read(temp_dir.path().join("spinne.json"));

        assert_eq!(
            config,
            Some(ConfigValues {
                exclude: Some(vec!["test.tsx".to_string()]),
                include: None
            })
        );
    }

    #[test]
    fn test_config_without_exclude() {
        let temp_dir = create_mock_project(&vec![("spinne.json", r#"{"include": ["test.tsx"]}"#)]);
        let config = Config::read(temp_dir.path().join("spinne.json"));

        assert_eq!(
            config,
            Some(ConfigValues {
                exclude: None,
                include: Some(vec!["test.tsx".to_string()])
            })
        );
    }
}