-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom env struct to store env at init (#1025)
This allow to no longer ignore some tests (marked previously as FIXME) by storing the env at the start of the program (Or creating a custom env for test purpose) This centralize almost alls calls to std::env inside one wrapper Add a test profile to increase speed for testing (5min -> 20sec on my machine) clean a few code style like this: ``` if Some(value) = ... if value ``` to ``` if Some(true) = ... ``` Co-authored-by: William Escande <[email protected]>
- Loading branch information
Showing
15 changed files
with
294 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,6 @@ features = [] | |
version = "0.12.4" | ||
default-features = false | ||
features = [] | ||
|
||
[profile.test] | ||
opt-level = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,73 @@ | ||
#[cfg(not(test))] | ||
use std::env; | ||
|
||
/// If `name` is set and, after trimming whitespace, is not empty string, then return that trimmed | ||
/// string. Else None. | ||
pub fn get_env_var(_name: &str) -> Option<String> { | ||
#[cfg(not(test))] | ||
match env::var(_name).unwrap_or_else(|_| "".to_string()).trim() { | ||
"" => None, | ||
non_empty_string => Some(non_empty_string.to_string()), | ||
const COLORTERM: &str = "COLORTERM"; | ||
const BAT_THEME: &str = "BAT_THEME"; | ||
const GIT_CONFIG_PARAMETERS: &str = "GIT_CONFIG_PARAMETERS"; | ||
const GIT_PREFIX: &str = "GIT_PREFIX"; | ||
const DELTA_FEATURES: &str = "DELTA_FEATURES"; | ||
const DELTA_NAVIGATE: &str = "DELTA_NAVIGATE"; | ||
const DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES: &str = | ||
"DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES"; | ||
const DELTA_PAGER: &str = "DELTA_PAGER"; | ||
const BAT_PAGER: &str = "BAT_PAGER"; | ||
const PAGER: &str = "PAGER"; | ||
|
||
#[derive(Default, Clone)] | ||
pub struct DeltaEnv { | ||
pub bat_theme: Option<String>, | ||
pub colorterm: Option<String>, | ||
pub current_dir: Option<std::path::PathBuf>, | ||
pub experimental_max_line_distance_for_naively_paired_lines: Option<String>, | ||
pub features: Option<String>, | ||
pub git_config_parameters: Option<String>, | ||
pub git_prefix: Option<String>, | ||
pub navigate: Option<String>, | ||
pub pagers: (Option<String>, Option<String>, Option<String>), | ||
} | ||
|
||
impl DeltaEnv { | ||
/// Create a structure with current environment variable | ||
pub fn init() -> Self { | ||
let bat_theme = env::var(BAT_THEME).ok(); | ||
let colorterm = env::var(COLORTERM).ok(); | ||
let experimental_max_line_distance_for_naively_paired_lines = | ||
env::var(DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES).ok(); | ||
let features = env::var(DELTA_FEATURES).ok(); | ||
let git_config_parameters = env::var(GIT_CONFIG_PARAMETERS).ok(); | ||
let git_prefix = env::var(GIT_PREFIX).ok(); | ||
let navigate = env::var(DELTA_NAVIGATE).ok(); | ||
|
||
let current_dir = env::current_dir().ok(); | ||
let pagers = ( | ||
env::var(DELTA_PAGER).ok(), | ||
env::var(BAT_PAGER).ok(), | ||
env::var(PAGER).ok(), | ||
); | ||
|
||
Self { | ||
bat_theme, | ||
colorterm, | ||
current_dir, | ||
experimental_max_line_distance_for_naively_paired_lines, | ||
features, | ||
git_config_parameters, | ||
git_prefix, | ||
navigate, | ||
pagers, | ||
} | ||
} | ||
#[cfg(test)] | ||
None | ||
} | ||
|
||
/// If `name` is set to any value at all (including "") then return true; else false. | ||
pub fn get_boolean_env_var(_name: &str) -> bool { | ||
#[cfg(not(test))] | ||
{ | ||
env::var(_name).ok().is_some() | ||
#[cfg(test)] | ||
pub mod tests { | ||
use super::DeltaEnv; | ||
use std::env; | ||
|
||
#[test] | ||
fn test_env_parsing() { | ||
let feature = "Awesome Feature"; | ||
env::set_var("DELTA_FEATURES", feature); | ||
let env = DeltaEnv::init(); | ||
assert_eq!(env.features, Some(feature.into())); | ||
} | ||
#[cfg(test)] | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.