Add some config manager tests

This commit is contained in:
Federico Terzi 2019-09-10 23:32:43 +02:00
parent 20fbb622a1
commit 550f298d54
2 changed files with 138 additions and 8 deletions

View File

@ -187,6 +187,8 @@ impl ConfigSet {
return Err(ConfigLoadError::NameDuplicate(path.to_owned()));
}
// TODO: check if it contains at least a filter, and warn the user about the problem
name_set.insert(config.name.clone());
specific.push(config);
}
@ -479,26 +481,36 @@ mod tests {
assert_eq!(config_set.unwrap_err(), ConfigLoadError::MissingName(specific_path_copy))
}
#[test]
fn test_config_set_specific_file_duplicate_name() {
pub fn create_temp_espanso_directory() -> TempDir {
let tmp_dir = TempDir::new().expect("unable to create temp directory");
let default_path = tmp_dir.path().join(DEFAULT_CONFIG_FILE_NAME);
fs::write(default_path, DEFAULT_CONFIG_FILE_CONTENT);
let specific_path = tmp_dir.path().join("specific.yaml");
tmp_dir
}
pub fn create_temp_file_in_dir(tmp_dir: &TempDir, name: &str, content: &str) -> PathBuf {
let specific_path = tmp_dir.path().join(name);
let specific_path_copy = specific_path.clone();
fs::write(specific_path, r###"
fs::write(specific_path, content);
specific_path_copy
}
#[test]
fn test_config_set_specific_file_duplicate_name() {
let tmp_dir = create_temp_espanso_directory();
let specific_path = create_temp_file_in_dir(&tmp_dir, "specific.yaml", r###"
name: specific1
"###);
let specific_path2 = tmp_dir.path().join("specific2.yaml");
let specific_path_copy2 = specific_path2.clone();
fs::write(specific_path2, r###"
let specific_path2 = create_temp_file_in_dir(&tmp_dir, "specific2.yaml", r###"
name: specific1
"###);
let config_set = ConfigSet::load(tmp_dir.path());
assert!(config_set.is_err());
assert_eq!(config_set.unwrap_err(), ConfigLoadError::NameDuplicate(specific_path_copy2))
assert_eq!(config_set.unwrap_err(), ConfigLoadError::NameDuplicate(specific_path2))
}
}

View File

@ -176,4 +176,122 @@ impl <'a, S: SystemManager> super::ConfigManager<'a> for RuntimeConfigManager<'a
fn matches(&'a self) -> &'a Vec<Match> {
&self.active_config().matches
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::{NamedTempFile, TempDir};
use crate::config::{DEFAULT_CONFIG_FILE_NAME, DEFAULT_CONFIG_FILE_CONTENT};
use std::fs;
use std::path::PathBuf;
use crate::config::tests::{create_temp_espanso_directory, create_temp_file_in_dir};
struct DummySystemManager {}
impl SystemManager for DummySystemManager {
fn get_current_window_title(&self) -> Option<String> {
Some("title".to_owned())
}
fn get_current_window_class(&self) -> Option<String> {
Some("class".to_owned())
}
fn get_current_window_executable(&self) -> Option<String> {
Some("exec".to_owned())
}
}
impl DummySystemManager {
pub fn new() -> DummySystemManager {
DummySystemManager{}
}
}
#[test]
fn test_runtime_constructor_regex_load_correctly() {
let tmp_dir = create_temp_espanso_directory();
let specific_path = create_temp_file_in_dir(&tmp_dir, "specific.yaml", r###"
name: myname1
filter_exec: "Title"
"###);
let specific_path2 = create_temp_file_in_dir(&tmp_dir, "specific2.yaml", r###"
name: myname2
filter_title: "Yeah"
filter_class: "Car"
"###);
let specific_path3 = create_temp_file_in_dir(&tmp_dir, "specific3.yaml", r###"
name: myname3
filter_title: "Nice"
"###);
let config_set = ConfigSet::load(tmp_dir.path());
assert!(config_set.is_ok());
let dummy_system_manager = DummySystemManager::new();
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
assert_eq!(config_manager.exec_regexps.len(), 3);
assert_eq!(config_manager.title_regexps.len(), 3);
assert_eq!(config_manager.class_regexps.len(), 3);
assert!(config_manager.class_regexps[0].is_none());
assert!(config_manager.class_regexps[1].is_some());
assert!(config_manager.class_regexps[2].is_none());
assert!(config_manager.title_regexps[0].is_none());
assert!(config_manager.title_regexps[1].is_some());
assert!(config_manager.title_regexps[2].is_some());
assert!(config_manager.exec_regexps[0].is_some());
assert!(config_manager.exec_regexps[1].is_none());
assert!(config_manager.exec_regexps[2].is_none());
}
#[test]
fn test_runtime_constructor_malformed_regexes_are_ignored() {
let tmp_dir = create_temp_espanso_directory();
let specific_path = create_temp_file_in_dir(&tmp_dir, "specific.yaml", r###"
name: myname1
filter_exec: "[`-_]"
"###);
let specific_path2 = create_temp_file_in_dir(&tmp_dir, "specific2.yaml", r###"
name: myname2
filter_title: "[`-_]"
filter_class: "Car"
"###);
let specific_path3 = create_temp_file_in_dir(&tmp_dir, "specific3.yaml", r###"
name: myname3
filter_title: "Nice"
"###);
let config_set = ConfigSet::load(tmp_dir.path());
assert!(config_set.is_ok());
let dummy_system_manager = DummySystemManager::new();
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
assert_eq!(config_manager.exec_regexps.len(), 3);
assert_eq!(config_manager.title_regexps.len(), 3);
assert_eq!(config_manager.class_regexps.len(), 3);
assert!(config_manager.class_regexps[0].is_none());
assert!(config_manager.class_regexps[1].is_some());
assert!(config_manager.class_regexps[2].is_none());
assert!(config_manager.title_regexps[0].is_none());
assert!(config_manager.title_regexps[1].is_none());
assert!(config_manager.title_regexps[2].is_some());
assert!(config_manager.exec_regexps[0].is_none());
assert!(config_manager.exec_regexps[1].is_none());
assert!(config_manager.exec_regexps[2].is_none());
}
}