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 | |
parent | cbbc1aab01db4caa24872fd402567d1fc8ee49c3 (diff) |
Reorganize and add test for get_themes
-rw-r--r-- | src/main.rs | 29 | ||||
-rw-r--r-- | src/options/get.rs | 54 | ||||
-rw-r--r-- | src/tests/integration_test_utils.rs | 2 |
3 files changed, 57 insertions, 28 deletions
diff --git a/src/main.rs b/src/main.rs index a90d4871..fb4628ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,6 @@ mod style; mod syntect_color; mod tests; -use lazy_static::lazy_static; -use regex::Regex; use std::io::{self, ErrorKind, Read, Write}; use std::path::PathBuf; use std::process; @@ -41,6 +39,7 @@ use crate::bat_utils::assets::{list_languages, HighlightingAssets}; use crate::bat_utils::output::{OutputType, PagingMode}; use crate::config::delta_unreachable; use crate::delta::delta; +use crate::options::get::get_themes; use crate::options::theme::is_light_syntax_theme; pub mod errors { @@ -313,30 +312,6 @@ where } } -lazy_static! { - static ref GIT_CONFIG_THEME_REGEX: Regex = Regex::new(r"^delta\.(.+)\.is-theme$").unwrap(); -} - -fn get_themes() -> Vec<String> { - let _git_config = git_config::GitConfig::try_create(); - 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(); - dbg!(entry_name, entry_value); - 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 -} - fn show_themes() -> std::io::Result<()> { use bytelines::ByteLines; use sample_diff::DIFF; @@ -361,7 +336,7 @@ fn show_themes() -> std::io::Result<()> { let title_style = ansi_term::Style::new().bold(); let writer = output_type.handle().unwrap(); - for theme in &get_themes() { + for theme in &get_themes(git_config::GitConfig::try_create()) { writeln!(writer, "\n\nTheme: {}\n", title_style.paint(theme))?; let opt = cli::Opt::from_iter_and_git_config(&["", "", "--features", &theme], &mut git_config); 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(); + } } diff --git a/src/tests/integration_test_utils.rs b/src/tests/integration_test_utils.rs index 8eb36748..1fdb215a 100644 --- a/src/tests/integration_test_utils.rs +++ b/src/tests/integration_test_utils.rs @@ -69,7 +69,7 @@ pub mod integration_test_utils { config::Config::from(make_options_from_args(args)) } - fn make_git_config(contents: &[u8], path: &str, honor_env_var: bool) -> GitConfig { + pub fn make_git_config(contents: &[u8], path: &str, honor_env_var: bool) -> GitConfig { let path = Path::new(path); let mut file = File::create(path).unwrap(); file.write_all(contents).unwrap(); |