diff options
author | Catherine Noll <noll.catherine@gmail.com> | 2021-03-27 19:46:08 -0400 |
---|---|---|
committer | Catherine Noll <noll.catherine@gmail.com> | 2021-03-27 19:46:08 -0400 |
commit | a1e42432f4413b54249700e59494e261f5718f3a (patch) | |
tree | 26afe4c4095ac28afe82f9fd1f34d1703ca1ff80 /src/options | |
parent | cbbc1aab01db4caa24872fd402567d1fc8ee49c3 (diff) |
Reorganize and add test for get_themes
Diffstat (limited to 'src/options')
-rw-r--r-- | src/options/get.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/options/get.rs b/src/options/get.rs index a36987ca..58209f5d 100644 --- a/src/options/get.rs +++ b/src/options/get.rs @@ -1,3 +1,5 @@ +use lazy_static::lazy_static; +use regex::Regex; use std::collections::HashMap; use crate::cli; @@ -38,6 +40,28 @@ where T::get_option_value(option_name, builtin_features, opt, git_config) } +lazy_static! { + static ref GIT_CONFIG_THEME_REGEX: Regex = Regex::new(r"^delta\.(.+)\.is-theme$").unwrap(); +} + +pub fn get_themes(git_config: Option<git_config::GitConfig>) -> Vec<String> { + let mut themes: Vec<String> = Vec::new(); + for e in &git_config.unwrap().config.entries(None).unwrap() { + let entry = e.unwrap(); + let entry_name = entry.name().unwrap(); + let entry_value = entry.value().unwrap(); + if entry_value == "true" { + let caps = GIT_CONFIG_THEME_REGEX.captures(entry_name); + if let Some(caps) = caps { + // need to check value i.e. whether is_theme = false + let name = caps.get(1).map_or("", |m| m.as_str()).to_string(); + themes.push(name) + } + } + } + themes +} + pub trait GetOptionValue { fn get_option_value( option_name: &str, @@ -115,6 +139,7 @@ pub mod tests { use std::env; use std::fs::remove_file; + use crate::options::get::get_themes; use crate::tests::integration_test_utils::integration_test_utils; // TODO: the followig tests are collapsed into one since they all set the same env var and thus @@ -275,4 +300,33 @@ pub mod tests { remove_file(git_config_path).unwrap(); } + + #[test] + fn test_get_themes_from_config() { + let git_config_contents = b" +[delta \"yes\"] + max-line-distance = 0.6 + is-theme = true + +[delta \"not-a-theme\"] + max-line-distance = 0.6 + is-theme = false + +[delta \"has-no-theme-entry\"] + max-line-distance = 0.6 +"; + let git_config_path = "delta__test_get_themes_git_config.gitconfig"; + + let git_config = Some(integration_test_utils::make_git_config( + git_config_contents, + git_config_path, + false, + )); + + let themes = get_themes(git_config); + + assert_eq!(themes, ["yes"]); + + remove_file(git_config_path).unwrap(); + } } |