mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-25 03:16:01 +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:
parent
e5cec9ea50
commit
b8a167db57
@ -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
|
||||
pub fn get_home(&self) -> Option<PathBuf> {
|
||||
home_dir(&self.env)
|
||||
@ -790,6 +799,7 @@ fn parse_width(width: &str) -> Result<usize, ParseIntError> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test::default_context;
|
||||
use std::io;
|
||||
|
||||
fn testdir(paths: &[&str]) -> Result<tempfile::TempDir, std::io::Error> {
|
||||
@ -981,6 +991,16 @@ mod tests {
|
||||
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)]
|
||||
#[test]
|
||||
fn strip_extended_path_prefix() {
|
||||
|
106
src/print.rs
106
src/print.rs
@ -493,24 +493,18 @@ fn preset_list() -> String {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::config::StarshipConfig;
|
||||
use crate::test::default_context;
|
||||
use crate::utils;
|
||||
|
||||
#[test]
|
||||
fn main_prompt() {
|
||||
let mut context = default_context();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
add_newline=false
|
||||
format="$character"
|
||||
[character]
|
||||
format=">\n>"
|
||||
}),
|
||||
};
|
||||
context.root_config.format = "$character".to_string();
|
||||
});
|
||||
context.target = Target::Main;
|
||||
context.root_config.add_newline = false;
|
||||
|
||||
let expected = String::from(">\n>");
|
||||
let actual = get_prompt(context);
|
||||
@ -519,15 +513,11 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn right_prompt() {
|
||||
let mut context = default_context();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
right_format="$character"
|
||||
[character]
|
||||
format=">\n>"
|
||||
}),
|
||||
};
|
||||
context.root_config.right_format = "$character".to_string();
|
||||
});
|
||||
context.target = Target::Right;
|
||||
|
||||
let expected = String::from(">>"); // should strip new lines
|
||||
@ -537,22 +527,14 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn custom_prompt() {
|
||||
let mut context = default_context();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
add_newline = false
|
||||
[profiles]
|
||||
test="0_0$character"
|
||||
[character]
|
||||
format=">>"
|
||||
}),
|
||||
};
|
||||
context
|
||||
.root_config
|
||||
.profiles
|
||||
.insert("test".to_string(), "0_0$character".to_string());
|
||||
});
|
||||
context.target = Target::Profile("test".to_string());
|
||||
context.root_config.add_newline = false;
|
||||
|
||||
let expected = String::from("0_0>>");
|
||||
let actual = get_prompt(context);
|
||||
@ -561,22 +543,14 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn custom_prompt_fallback() {
|
||||
let mut context = default_context();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
add_newline=false
|
||||
[profiles]
|
||||
test="0_0$character"
|
||||
[character]
|
||||
format=">>"
|
||||
}),
|
||||
};
|
||||
context
|
||||
.root_config
|
||||
.profiles
|
||||
.insert("test".to_string(), "0_0$character".to_string());
|
||||
});
|
||||
context.target = Target::Profile("wrong_prompt".to_string());
|
||||
context.root_config.add_newline = false;
|
||||
|
||||
let expected = String::from(">");
|
||||
let actual = get_prompt(context);
|
||||
@ -585,13 +559,9 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn continuation_prompt() {
|
||||
let mut context = default_context();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
continuation_prompt="><>"
|
||||
}),
|
||||
};
|
||||
context.root_config.continuation_prompt = "><>".to_string();
|
||||
});
|
||||
context.target = Target::Continuation;
|
||||
|
||||
let expected = String::from("><>");
|
||||
@ -634,10 +604,7 @@ mod test {
|
||||
#[test]
|
||||
fn custom_expands() -> std::io::Result<()> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
let mut context = default_context();
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
format="$custom"
|
||||
[custom.a]
|
||||
when=true
|
||||
@ -645,9 +612,8 @@ mod test {
|
||||
[custom.b]
|
||||
when=true
|
||||
format="b"
|
||||
}),
|
||||
};
|
||||
context.root_config.format = "$custom".to_string();
|
||||
});
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
|
||||
let expected = String::from("\nab");
|
||||
let actual = get_prompt(context);
|
||||
@ -657,9 +623,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn env_expands() {
|
||||
let mut context = default_context();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
format="$env_var"
|
||||
[env_var]
|
||||
format="$env_value"
|
||||
@ -668,9 +632,7 @@ mod test {
|
||||
format="$env_value"
|
||||
[env_var.c]
|
||||
format="$env_value"
|
||||
}),
|
||||
};
|
||||
context.root_config.format = "$env_var".to_string();
|
||||
});
|
||||
context.env.insert("a", "a".to_string());
|
||||
context.env.insert("b", "b".to_string());
|
||||
context.env.insert("c", "c".to_string());
|
||||
@ -683,10 +645,7 @@ mod test {
|
||||
#[test]
|
||||
fn custom_mixed() -> std::io::Result<()> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
let mut context = default_context();
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
format="${custom.c}$custom${custom.b}"
|
||||
[custom.a]
|
||||
when=true
|
||||
@ -697,9 +656,8 @@ mod test {
|
||||
[custom.c]
|
||||
when=true
|
||||
format="c"
|
||||
}),
|
||||
};
|
||||
context.root_config.format = "${custom.c}$custom${custom.b}".to_string();
|
||||
});
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
|
||||
let expected = String::from("\ncab");
|
||||
let actual = get_prompt(context);
|
||||
@ -709,9 +667,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn env_mixed() {
|
||||
let mut context = default_context();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
format="${env_var.c}$env_var${env_var.b}"
|
||||
[env_var]
|
||||
format="$env_value"
|
||||
@ -722,9 +678,7 @@ mod test {
|
||||
format="$env_value"
|
||||
[env_var.c]
|
||||
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("b", "b".to_string());
|
||||
context.env.insert("c", "c".to_string());
|
||||
@ -738,10 +692,7 @@ mod test {
|
||||
#[test]
|
||||
fn custom_subset() -> std::io::Result<()> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
let mut context = default_context();
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
format="${custom.b}"
|
||||
[custom.a]
|
||||
when=true
|
||||
@ -749,9 +700,8 @@ mod test {
|
||||
[custom.b]
|
||||
when=true
|
||||
format="b"
|
||||
}),
|
||||
};
|
||||
context.root_config.format = "${custom.b}".to_string();
|
||||
});
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
|
||||
let expected = String::from("\nb");
|
||||
let actual = get_prompt(context);
|
||||
@ -762,17 +712,13 @@ mod test {
|
||||
#[test]
|
||||
fn custom_missing() -> std::io::Result<()> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
let mut context = default_context();
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
context.config = StarshipConfig {
|
||||
config: Some(toml::toml! {
|
||||
let mut context = default_context().set_config(toml::toml! {
|
||||
format="${custom.b}"
|
||||
[custom.a]
|
||||
when=true
|
||||
format="a"
|
||||
}),
|
||||
};
|
||||
context.root_config.format = "${custom.b}".to_string();
|
||||
});
|
||||
context.current_dir = dir.path().to_path_buf();
|
||||
|
||||
let expected = String::from("\n");
|
||||
let actual = get_prompt(context);
|
||||
|
@ -1,8 +1,7 @@
|
||||
use crate::context::{Context, Shell, Target};
|
||||
use crate::logger::StarshipLogger;
|
||||
use crate::{
|
||||
config::{ModuleConfig, StarshipConfig},
|
||||
configs::StarshipRootConfig,
|
||||
config::StarshipConfig,
|
||||
utils::{create_command, CommandOutput},
|
||||
};
|
||||
use log::{Level, LevelFilter};
|
||||
@ -89,10 +88,7 @@ impl<'a> ModuleRenderer<'a> {
|
||||
|
||||
/// Sets the config of the underlying context
|
||||
pub fn config(mut self, config: toml::Table) -> Self {
|
||||
self.context.root_config = StarshipRootConfig::load(&config);
|
||||
self.context.config = StarshipConfig {
|
||||
config: Some(config),
|
||||
};
|
||||
self.context = self.context.set_config(config);
|
||||
self
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user