summaryrefslogtreecommitdiffstats
path: root/tests/async_builder.rs
blob: bead9d38e7930e8be3abf45e0ba00378b47c7f85 (plain)
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
137
138
139
140
141
142
use async_trait::async_trait;
use config::{AsyncSource, Config, ConfigError, FileFormat, Format, Map, Value};
use std::{env, fs, path, str::FromStr};
use tokio::fs::read_to_string;

#[derive(Debug)]
struct AsyncFile {
    path: String,
    format: FileFormat,
}

/// This is a test only implementation to be used in tests
impl AsyncFile {
    pub fn new(path: String, format: FileFormat) -> Self {
        Self { path, format }
    }
}

#[async_trait]
impl AsyncSource for AsyncFile {
    async fn collect(&self) -> Result<Map<String, Value>, ConfigError> {
        let mut path = env::current_dir().unwrap();
        let local = path::PathBuf::from_str(&self.path).unwrap();

        path.extend(local.iter());
        let path = fs::canonicalize(path).map_err(|e| ConfigError::Foreign(Box::new(e)))?;

        let text = read_to_string(path)
            .await
            .map_err(|e| ConfigError::Foreign(Box::new(e)))?;

        self.format
            .parse(Some(&self.path), &text)
            .map_err(|e| ConfigError::Foreign(e))
    }
}

#[tokio::test]
async fn test_single_async_file_source() {
    let config = Config::builder()
        .add_async_source(AsyncFile::new(
            "tests/Settings.json".to_owned(),
            FileFormat::Json,
        ))
        .build()
        .await
        .unwrap();

    assert!(config.get::<bool>("debug").unwrap());
}

#[tokio::test]
async fn test_two_async_file_sources() {
    let config = Config::builder()
        .add_async_source(AsyncFile::new(
            "tests/Settings.json".to_owned(),
            FileFormat::Json,
        ))
        .add_async_source(AsyncFile::new(
            "tests/Settings.toml".to_owned(),
            FileFormat::Toml,
        ))
        .build()
        .await
        .unwrap();

    assert_eq!("Torre di Pisa", config.get::<String>("place.name").unwrap());
    assert!(config.get::<bool>("debug_json").unwrap());
    assert_eq!(1, config.get::<i32>("place.number").unwrap());
}

#[tokio::test]
async fn test_sync_to_async_file_sources() {
    let config = Config::builder()
        .add_source(config::File::new("tests/Settings", FileFormat::Json))
        .add_async_source(AsyncFile::new(
            "tests/Settings.toml".to_owned(),
            FileFormat::Toml,
        ))
        .build()
        .await
        .unwrap();

    assert_eq!("Torre di Pisa", config.get::<String>("place.name").unwrap());
    assert_eq!(1, config.get::<i32>("place.number").unwrap());
}

#[tokio::test]
async fn test_async_to_sync_file_sources() {
    let config = Config::builder()
        .add_async_source(AsyncFile::new(
            "tests/Settings.toml".to_owned(),
            FileFormat::Toml,
        ))
        .add_source(config::File::new("tests/Settings", FileFormat::Json))
        .build()
        .await
        .unwrap();

    assert_eq!("Torre di Pisa", config.get::<String>("place.name").unwrap());
    assert_eq!(1, config.get::<i32>("place.number").unwrap());
}

#[tokio::test]
async fn test_async_file_sources_with_defaults() {
    let config = Config::builder()
        .set_default("place.name", "Tower of London")
        .unwrap()
        .set_default("place.sky", "blue")
        .unwrap()
        .add_async_source(AsyncFile::new(
            "tests/Settings.toml".to_owned(),
            FileFormat::Toml,
        ))
        .build()
        .await
        .unwrap();

    assert_eq!("Torre di Pisa", config.get::<String>("place.name").unwrap());
    assert_eq!("blue", config.get::<String>("place.sky").unwrap());
    assert_eq!(1, config.get::<i32>("place.number").unwrap());
}

#[tokio::test]
async fn test_async_file_sources_with_overrides() {
    let config = Config::builder()
        .set_override("place.name", "Tower of London")
        .unwrap()
        .add_async_source(AsyncFile::new(
            "tests/Settings.toml".to_owned(),
            FileFormat::Toml,
        ))
        .build()
        .await
        .unwrap();

    assert_eq!(
        "Tower of London",
        config.get::<String>("place.name").unwrap()
    );
    assert_eq!(1, config.get::<i32>("place.number").unwrap());
}