#![cfg(test)]
use super::*;
use assert_fs::{prelude::FileTouch, NamedTempFile, TempDir};
#[test]
#[should_panic(expected = "NotFound")]
fn test_load_config_file_absent() {
let config_path = NamedTempFile::new("config.toml").unwrap();
let _config = Config::load_config(&config_path).unwrap();
}
#[test]
fn test_load_config_file_exists() {
let config_path = NamedTempFile::new("config.toml").unwrap();
FileTouch::touch(&config_path).unwrap();
assert!(config_path.exists());
let config = Config::load_config(&config_path).unwrap();
assert_eq!(
config,
Config {
path: config_path.to_path_buf(),
..Config::default()
}
);
}
#[test]
#[should_panic(expected = "NotFound")]
fn test_save_config_file_absent() {
let config_path = NamedTempFile::new("config.toml").unwrap();
let config = Config {
path: config_path.to_path_buf(),
..Default::default()
};
config.save_config().unwrap();
}
#[test]
fn test_save_config_file_exists() {
let config_path = NamedTempFile::new("config.toml").unwrap();
FileTouch::touch(&config_path).unwrap();
assert!(config_path.exists());
let config = Config {
path: config_path.to_path_buf(),
files: FilesConfig {
include: vec!["test1".into(), "test2".into()],
},
..Default::default()
};
config.save_config().unwrap();
assert!(config_path.exists());
let config_str = fs::read_to_string(&config_path).unwrap();
let config_toml: Config = Config {
path: config_path.to_path_buf(),
..toml::from_str(&config_str).unwrap()
};
assert_eq!(config_toml, config);
}
#[test]
fn test_create_config_file_absent() {
let temp_path = TempDir::new().unwrap();
let config_path = temp_path.path().join("some/sub/dirs/config.toml");
assert!(!config_path.exists());
Config::create_config(&config_path).unwrap();
assert!(config_path.exists());
let config_str = fs::read_to_string(&config_path).unwrap();
let config_toml: Config = Config {
path: config_path.clone(),
..toml::from_str(&config_str).unwrap()
};
assert_eq!(
config_toml,
Config {
path: config_path,
..Config::default()
}
);
}
#[test]
#[should_panic(expected = "AlreadyExists")]
fn test_create_config_file_exists() {
let config_path = NamedTempFile::new("config.toml").unwrap();
FileTouch::touch(&config_path).unwrap();
assert!(config_path.exists());
Config::create_config(&config_path).unwrap();
}