diff --git a/crates/nu-std/std/log.nu b/crates/nu-std/std/log.nu index e53d95a15a..5117460be7 100644 --- a/crates/nu-std/std/log.nu +++ b/crates/nu-std/std/log.nu @@ -36,8 +36,8 @@ export def log-short-prefix [] { } } export-env { - $env.NU_LOG_FORMAT = $"%ANSI_START%%DATE%|%LEVEL%|%MSG%%ANSI_STOP%" - $env.NU_LOG_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S%.3f" + $env.NU_LOG_FORMAT = $env.NU_LOG_FORMAT? | default "%ANSI_START%%DATE%|%LEVEL%|%MSG%%ANSI_STOP%" + $env.NU_LOG_DATE_FORMAT = $env.NU_LOG_DATE_FORMAT? | default "%Y-%m-%dT%H:%M:%S%.3f" } def log-types [] { diff --git a/crates/nu-test-support/src/macros.rs b/crates/nu-test-support/src/macros.rs index 6fd3a8c307..3104c04e7d 100644 --- a/crates/nu-test-support/src/macros.rs +++ b/crates/nu-test-support/src/macros.rs @@ -143,7 +143,7 @@ macro_rules! nu_with_std { cwd: $value:expr, $($rest:tt)* ) => { - nu!(@options [ $($options)* cwd => $crate::fs::in_directory($value) ; ] $($rest)*) + nu_with_std!(@options [ $($options)* cwd => $crate::fs::in_directory($value) ; ] $($rest)*) }; // For all other options, we call `.into()` on the `$value` and hope for the best. ;) ( @@ -151,7 +151,7 @@ macro_rules! nu_with_std { $field:ident : $value:expr, $($rest:tt)* ) => { - nu!(@options [ $($options)* $field => $value.into() ; ] $($rest)*) + nu_with_std!(@options [ $($options)* $field => $value.into() ; ] $($rest)*) }; // When the `$field: $value,` pairs are all parsed, the next tokens are the `$path` and any @@ -163,11 +163,11 @@ macro_rules! nu_with_std { $(,)* ) => {{ // Here we parse the options into a `NuOpts` struct - let opts = nu!(@nu_opts $($options)*); + let opts = nu_with_std!(@nu_opts $($options)*); // and format the `$path` using the `$part`s - let path = nu!(@format_path $path, $($part),*); + let path = nu_with_std!(@format_path $path, $($part),*); // Then finally we go to the `@main` phase, where the actual work is done. - nu!(@main opts, path) + nu_with_std!(@main opts, path) }}; // Create the NuOpts struct from the `field => value ;` pairs @@ -196,7 +196,7 @@ macro_rules! nu_with_std { // This is the entrypoint for this macro. ($($token:tt)*) => {{ - nu!(@options [ ] $($token)*) + nu_with_std!(@options [ ] $($token)*) }}; } diff --git a/tests/shell/environment/env.rs b/tests/shell/environment/env.rs index 1a9c14b18a..e07259a500 100644 --- a/tests/shell/environment/env.rs +++ b/tests/shell/environment/env.rs @@ -2,7 +2,7 @@ use super::support::Trusted; use nu_test_support::fs::Stub::FileWithContent; use nu_test_support::playground::Playground; -use nu_test_support::{nu, nu_repl_code}; +use nu_test_support::{nu, nu_repl_code, nu_with_std}; use pretty_assertions::assert_eq; use serial_test::serial; @@ -215,3 +215,33 @@ fn env_var_case_insensitive() { assert!(actual.out.contains("111")); assert!(actual.out.contains("222")); } + +#[test] +fn std_log_env_vars_are_not_overridden() { + let actual = nu_with_std!( + envs: vec![ + ("NU_LOG_FORMAT".to_string(), "%MSG%".to_string()), + ("NU_LOG_DATE_FORMAT".to_string(), "%Y".to_string()), + ], + r#" + use std + print -e $env.NU_LOG_FORMAT + print -e $env.NU_LOG_DATE_FORMAT + std log error "err" + "# + ); + assert_eq!(actual.err, "%MSG%\n%Y\nerr\n"); +} + +#[test] +fn std_log_env_vars_have_defaults() { + let actual = nu_with_std!( + r#" + use std + print -e $env.NU_LOG_FORMAT + print -e $env.NU_LOG_DATE_FORMAT + "# + ); + assert!(actual.err.contains("%MSG%")); + assert!(actual.err.contains("%Y-")); +}