summaryrefslogtreecommitdiff
path: root/learn_21/tests/load_config.rs
blob: 5fc78663903fa573529d589ad67f95efa6071324 (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
use std::env;
use std::fs::{self, File};
use std::io::{self, Write};
use std::process;

use learn_21::config::{Config, load_config, parse_config};
use learn_21::error::ConfigError;

#[test]
fn test_load_config() -> io::Result<()> {
    let mut file_path = env::temp_dir();
    file_path.push(format!("test_{}.txt", process::id()));

    {
        let mut file = File::create(&file_path)?;
        file.write_all(b"host=myhost\nport=1\nworkers=1\n")?;
    }

    assert!(load_config(&file_path).is_ok_and(|x| x ==
        Config{host:"myhost".to_string(), port:1, workers:1}));
    assert!(matches!(
        load_config(std::path::Path::new("/bad/path")),
        Err(ConfigError::Io(Error)),
    ));
    
    fs::remove_file(&file_path)?;
    Ok(())
}