1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-06-10 20:32:21 +00:00

refactor(Context): set_config method for Context (#5079)

* add `set_config` method to `Context`

* Made inline comment a doc comment

* use `default_context()` for `set_config()` test

* use `set_config()` in tests for `print.rs`

* set root config w `set_config()` (`print.rs` test)
This commit is contained in:
marcybell 2023-04-24 22:03:47 +08:00 committed by GitHub
parent e5cec9ea50
commit b8a167db57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 86 deletions

View File

@ -182,6 +182,15 @@ impl<'a> Context<'a> {
} }
} }
/// Sets the context config, overwriting the existing config
pub fn set_config(mut self, config: toml::Table) -> Context<'a> {
self.root_config = StarshipRootConfig::load(&config);
self.config = StarshipConfig {
config: Some(config),
};
self
}
// Tries to retrieve home directory from a table in testing mode or else retrieves it from the os // Tries to retrieve home directory from a table in testing mode or else retrieves it from the os
pub fn get_home(&self) -> Option<PathBuf> { pub fn get_home(&self) -> Option<PathBuf> {
home_dir(&self.env) home_dir(&self.env)
@ -790,6 +799,7 @@ fn parse_width(width: &str) -> Result<usize, ParseIntError> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::test::default_context;
use std::io; use std::io;
fn testdir(paths: &[&str]) -> Result<tempfile::TempDir, std::io::Error> { fn testdir(paths: &[&str]) -> Result<tempfile::TempDir, std::io::Error> {
@ -981,6 +991,16 @@ mod tests {
assert_eq!(expected_logical_dir, context.logical_dir); assert_eq!(expected_logical_dir, context.logical_dir);
} }
#[test]
fn set_config_method_overwrites_constructor() {
let context = default_context();
let mod_context = default_context().set_config(toml::toml! {
add_newline = true
});
assert_ne!(context.config.config, mod_context.config.config);
}
#[cfg(windows)] #[cfg(windows)]
#[test] #[test]
fn strip_extended_path_prefix() { fn strip_extended_path_prefix() {

View File

@ -493,24 +493,18 @@ fn preset_list() -> String {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::config::StarshipConfig;
use crate::test::default_context; use crate::test::default_context;
use crate::utils; use crate::utils;
#[test] #[test]
fn main_prompt() { fn main_prompt() {
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.config = StarshipConfig {
config: Some(toml::toml! {
add_newline=false add_newline=false
format="$character" format="$character"
[character] [character]
format=">\n>" format=">\n>"
}), });
};
context.root_config.format = "$character".to_string();
context.target = Target::Main; context.target = Target::Main;
context.root_config.add_newline = false;
let expected = String::from(">\n>"); let expected = String::from(">\n>");
let actual = get_prompt(context); let actual = get_prompt(context);
@ -519,15 +513,11 @@ mod test {
#[test] #[test]
fn right_prompt() { fn right_prompt() {
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.config = StarshipConfig {
config: Some(toml::toml! {
right_format="$character" right_format="$character"
[character] [character]
format=">\n>" format=">\n>"
}), });
};
context.root_config.right_format = "$character".to_string();
context.target = Target::Right; context.target = Target::Right;
let expected = String::from(">>"); // should strip new lines let expected = String::from(">>"); // should strip new lines
@ -537,22 +527,14 @@ mod test {
#[test] #[test]
fn custom_prompt() { fn custom_prompt() {
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.config = StarshipConfig {
config: Some(toml::toml! {
add_newline = false add_newline = false
[profiles] [profiles]
test="0_0$character" test="0_0$character"
[character] [character]
format=">>" format=">>"
}), });
};
context
.root_config
.profiles
.insert("test".to_string(), "0_0$character".to_string());
context.target = Target::Profile("test".to_string()); context.target = Target::Profile("test".to_string());
context.root_config.add_newline = false;
let expected = String::from("0_0>>"); let expected = String::from("0_0>>");
let actual = get_prompt(context); let actual = get_prompt(context);
@ -561,22 +543,14 @@ mod test {
#[test] #[test]
fn custom_prompt_fallback() { fn custom_prompt_fallback() {
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.config = StarshipConfig {
config: Some(toml::toml! {
add_newline=false add_newline=false
[profiles] [profiles]
test="0_0$character" test="0_0$character"
[character] [character]
format=">>" format=">>"
}), });
};
context
.root_config
.profiles
.insert("test".to_string(), "0_0$character".to_string());
context.target = Target::Profile("wrong_prompt".to_string()); context.target = Target::Profile("wrong_prompt".to_string());
context.root_config.add_newline = false;
let expected = String::from(">"); let expected = String::from(">");
let actual = get_prompt(context); let actual = get_prompt(context);
@ -585,13 +559,9 @@ mod test {
#[test] #[test]
fn continuation_prompt() { fn continuation_prompt() {
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.config = StarshipConfig {
config: Some(toml::toml! {
continuation_prompt="><>" continuation_prompt="><>"
}), });
};
context.root_config.continuation_prompt = "><>".to_string();
context.target = Target::Continuation; context.target = Target::Continuation;
let expected = String::from("><>"); let expected = String::from("><>");
@ -634,10 +604,7 @@ mod test {
#[test] #[test]
fn custom_expands() -> std::io::Result<()> { fn custom_expands() -> std::io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.current_dir = dir.path().to_path_buf();
context.config = StarshipConfig {
config: Some(toml::toml! {
format="$custom" format="$custom"
[custom.a] [custom.a]
when=true when=true
@ -645,9 +612,8 @@ mod test {
[custom.b] [custom.b]
when=true when=true
format="b" format="b"
}), });
}; context.current_dir = dir.path().to_path_buf();
context.root_config.format = "$custom".to_string();
let expected = String::from("\nab"); let expected = String::from("\nab");
let actual = get_prompt(context); let actual = get_prompt(context);
@ -657,9 +623,7 @@ mod test {
#[test] #[test]
fn env_expands() { fn env_expands() {
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.config = StarshipConfig {
config: Some(toml::toml! {
format="$env_var" format="$env_var"
[env_var] [env_var]
format="$env_value" format="$env_value"
@ -668,9 +632,7 @@ mod test {
format="$env_value" format="$env_value"
[env_var.c] [env_var.c]
format="$env_value" format="$env_value"
}), });
};
context.root_config.format = "$env_var".to_string();
context.env.insert("a", "a".to_string()); context.env.insert("a", "a".to_string());
context.env.insert("b", "b".to_string()); context.env.insert("b", "b".to_string());
context.env.insert("c", "c".to_string()); context.env.insert("c", "c".to_string());
@ -683,10 +645,7 @@ mod test {
#[test] #[test]
fn custom_mixed() -> std::io::Result<()> { fn custom_mixed() -> std::io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.current_dir = dir.path().to_path_buf();
context.config = StarshipConfig {
config: Some(toml::toml! {
format="${custom.c}$custom${custom.b}" format="${custom.c}$custom${custom.b}"
[custom.a] [custom.a]
when=true when=true
@ -697,9 +656,8 @@ mod test {
[custom.c] [custom.c]
when=true when=true
format="c" format="c"
}), });
}; context.current_dir = dir.path().to_path_buf();
context.root_config.format = "${custom.c}$custom${custom.b}".to_string();
let expected = String::from("\ncab"); let expected = String::from("\ncab");
let actual = get_prompt(context); let actual = get_prompt(context);
@ -709,9 +667,7 @@ mod test {
#[test] #[test]
fn env_mixed() { fn env_mixed() {
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.config = StarshipConfig {
config: Some(toml::toml! {
format="${env_var.c}$env_var${env_var.b}" format="${env_var.c}$env_var${env_var.b}"
[env_var] [env_var]
format="$env_value" format="$env_value"
@ -722,9 +678,7 @@ mod test {
format="$env_value" format="$env_value"
[env_var.c] [env_var.c]
format="$env_value" format="$env_value"
}), });
};
context.root_config.format = "${env_var.c}$env_var${env_var.b}".to_string();
context.env.insert("a", "a".to_string()); context.env.insert("a", "a".to_string());
context.env.insert("b", "b".to_string()); context.env.insert("b", "b".to_string());
context.env.insert("c", "c".to_string()); context.env.insert("c", "c".to_string());
@ -738,10 +692,7 @@ mod test {
#[test] #[test]
fn custom_subset() -> std::io::Result<()> { fn custom_subset() -> std::io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.current_dir = dir.path().to_path_buf();
context.config = StarshipConfig {
config: Some(toml::toml! {
format="${custom.b}" format="${custom.b}"
[custom.a] [custom.a]
when=true when=true
@ -749,9 +700,8 @@ mod test {
[custom.b] [custom.b]
when=true when=true
format="b" format="b"
}), });
}; context.current_dir = dir.path().to_path_buf();
context.root_config.format = "${custom.b}".to_string();
let expected = String::from("\nb"); let expected = String::from("\nb");
let actual = get_prompt(context); let actual = get_prompt(context);
@ -762,17 +712,13 @@ mod test {
#[test] #[test]
fn custom_missing() -> std::io::Result<()> { fn custom_missing() -> std::io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let mut context = default_context(); let mut context = default_context().set_config(toml::toml! {
context.current_dir = dir.path().to_path_buf();
context.config = StarshipConfig {
config: Some(toml::toml! {
format="${custom.b}" format="${custom.b}"
[custom.a] [custom.a]
when=true when=true
format="a" format="a"
}), });
}; context.current_dir = dir.path().to_path_buf();
context.root_config.format = "${custom.b}".to_string();
let expected = String::from("\n"); let expected = String::from("\n");
let actual = get_prompt(context); let actual = get_prompt(context);

View File

@ -1,8 +1,7 @@
use crate::context::{Context, Shell, Target}; use crate::context::{Context, Shell, Target};
use crate::logger::StarshipLogger; use crate::logger::StarshipLogger;
use crate::{ use crate::{
config::{ModuleConfig, StarshipConfig}, config::StarshipConfig,
configs::StarshipRootConfig,
utils::{create_command, CommandOutput}, utils::{create_command, CommandOutput},
}; };
use log::{Level, LevelFilter}; use log::{Level, LevelFilter};
@ -89,10 +88,7 @@ impl<'a> ModuleRenderer<'a> {
/// Sets the config of the underlying context /// Sets the config of the underlying context
pub fn config(mut self, config: toml::Table) -> Self { pub fn config(mut self, config: toml::Table) -> Self {
self.context.root_config = StarshipRootConfig::load(&config); self.context = self.context.set_config(config);
self.context.config = StarshipConfig {
config: Some(config),
};
self self
} }