Always populate config record during startup (#14435)

# Description

As a bit of a follow-on to #13802 and #14249, this (pretty much a
"one-line" change) really does *always* populate the `$env.config`
record with the `nu-protocol::config` defaults during startup. This
means that an `$env.config` record is value (with defaults) even during:

* `nu -n` to suppress loading of config files
* `nu -c <commandstring>`
* `nu <script>`

# User-Facing Changes

There should be no case in which there isn't a valid `$env.config`.

* Before:

  ```nushell
  nu -c "$env.config"
  # -> Error
  ```

* After:

  ```nushell
  nu -c "$env.config"
  # -> Default $env.config record
  ```

Startup time impact is negligible (17.072µs from `perf!` on my system) -
Seems well worth it.

# Tests + Formatting

Added tests for several `-n -c` cases.

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

Config chapter update still in progress.
This commit is contained in:
Douglas
2024-11-27 00:52:47 -05:00
committed by GitHub
parent 547c436281
commit 1c18e37a7c
2 changed files with 87 additions and 2 deletions

View File

@ -298,3 +298,81 @@ fn test_xdg_config_symlink() {
);
});
}
#[test]
fn no_config_does_not_load_env_files() {
let nu = nu_test_support::fs::executable_path().display().to_string();
let cmd = format!(
r#"
{nu} -n -c "view files | where filename =~ 'env\\.nu$' | length"
"#
);
let actual = nu!(cmd);
assert_eq!(actual.out, "0");
}
#[test]
fn no_config_does_not_load_config_files() {
let nu = nu_test_support::fs::executable_path().display().to_string();
let cmd = format!(
r#"
{nu} -n -c "view files | where filename =~ 'config\\.nu$' | length"
"#
);
let actual = nu!(cmd);
assert_eq!(actual.out, "0");
}
#[test]
fn commandstring_does_not_load_config_files() {
let nu = nu_test_support::fs::executable_path().display().to_string();
let cmd = format!(
r#"
{nu} -c "view files | where filename =~ 'config\\.nu$' | length"
"#
);
let actual = nu!(cmd);
assert_eq!(actual.out, "0");
}
#[test]
fn commandstring_does_not_load_user_env() {
let nu = nu_test_support::fs::executable_path().display().to_string();
let cmd = format!(
r#"
{nu} -c "view files | where filename =~ '[^_]env\\.nu$' | length"
"#
);
let actual = nu!(cmd);
assert_eq!(actual.out, "0");
}
#[test]
fn commandstring_loads_default_env() {
let nu = nu_test_support::fs::executable_path().display().to_string();
let cmd = format!(
r#"
{nu} -c "view files | where filename =~ 'default_env\\.nu$' | length"
"#
);
let actual = nu!(cmd);
assert_eq!(actual.out, "1");
}
#[test]
fn commandstring_populates_config_record() {
let nu = nu_test_support::fs::executable_path().display().to_string();
let cmd = format!(
r#"
{nu} --no-std-lib -n -c "$env.config.show_banner"
"#
);
let actual = nu!(cmd);
assert_eq!(actual.out, "true");
}