Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$env.config now always holds a record with only valid values #7309

Merged
merged 8 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix tests
  • Loading branch information
webbedspace committed Dec 9, 2022
commit 600889f543420f9800e779831b8592d5aa1ab92a
65 changes: 65 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 26 additions & 21 deletions crates/nu-protocol/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ pub struct ExploreConfig {
impl Value {
pub fn into_config(&mut self, config: &Config) -> (Config, Option<ShellError>) {
// Clone the passed-in config rather than mutating it.

let mut config = config.clone();
let mut legacy_options_used = false;

Expand Down Expand Up @@ -255,6 +256,14 @@ impl Value {
};
}

// Config record (self) mutation rules:
// * When parsing a config Record, if a config key error occurs, remove the key.
// * When parsing a config Record, if a config value error occurs, replace the value
// with a reconstructed Nu value for the current (unaltered) configuration for that setting.
// For instance:
// $env.config.ls.use_ls_colors = 2 results in an error, so
// the current use_ls_colors config setting is converted to a Value::Boolean and inserted in the
// record in place of the 2.
if let Value::Record { cols, vals, span } = self {
// Because this whole algorithm removes while iterating, this must iterate in reverse.
for index in (0..cols.len()).rev() {
Expand Down Expand Up @@ -366,8 +375,7 @@ impl Value {
}
}
"history" => {
// Used as a shortcut for various places below
macro_rules! default_history_file_format {
macro_rules! reconstruct_history_file_format {
($span:expr) => {
Value::string(
match config.history_file_format {
Expand Down Expand Up @@ -407,13 +415,13 @@ impl Value {
);
// Reconstruct
vals[index] =
default_history_file_format!(span);
reconstruct_history_file_format!(span);
}
};
} else {
invalid!(Some(*span), "should be a string");
// Reconstruct
vals[index] = default_history_file_format!(span);
vals[index] = reconstruct_history_file_format!(span);
}
}
x => {
Expand All @@ -439,15 +447,14 @@ impl Value {
vec![
Value::boolean(config.sync_history_on_enter, *span),
Value::int(config.max_history_size, *span),
default_history_file_format!(span),
reconstruct_history_file_format!(span),
],
*span,
);
}
}
"completions" => {
// Some shortcuts for structures below
macro_rules! default_external_completer {
macro_rules! reconstruct_external_completer {
($span: expr) => {
if let Some(block) = config.external_completer {
Value::Block {
Expand All @@ -459,14 +466,13 @@ impl Value {
}
};
}
// Some shortcuts for structures below
macro_rules! default_external {
macro_rules! reconstruct_external {
($span: expr) => {
Value::record(
vec!["max_results".into(), "completer".into(), "enable".into()],
vec![
Value::int(config.max_external_completion_results, *$span),
default_external_completer!($span),
reconstruct_external_completer!($span),
Value::boolean(config.enable_external_completion, *$span),
],
*$span,
Expand Down Expand Up @@ -549,7 +555,7 @@ impl Value {
"should be a block or null"
);
// Reconstruct
vals[index] = default_external_completer!(
vals[index] = reconstruct_external_completer!(
span
);
}
Expand Down Expand Up @@ -579,7 +585,7 @@ impl Value {
} else {
invalid!(Some(*span), "should be a record");
// Reconstruct
vals[index] = default_external!(span);
vals[index] = reconstruct_external!(span);
}
}
x => {
Expand Down Expand Up @@ -609,15 +615,14 @@ impl Value {
Value::boolean(config.partial_completions, *span),
Value::string(config.completion_algorithm.clone(), *span),
Value::boolean(config.case_sensitive_completions, *span),
default_external!(span),
reconstruct_external!(span),
],
*span,
);
}
}
"table" => {
// Used as shortcuts for various places below
macro_rules! default_index_mode {
macro_rules! reconstruct_index_mode {
($span:expr) => {
Value::string(
match config.table_index_mode {
Expand All @@ -629,7 +634,7 @@ impl Value {
)
};
}
macro_rules! default_trim_strategy {
macro_rules! reconstruct_trim_strategy {
($span:expr) => {
match &config.trim_strategy {
TrimStrategy::Wrap { try_to_keep_words } => Value::record(
Expand Down Expand Up @@ -691,12 +696,12 @@ impl Value {
invalid!( Some(*span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'never', 'always' or 'auto'"
);
vals[index] = default_index_mode!(span);
vals[index] = reconstruct_index_mode!(span);
}
}
} else {
invalid!(Some(*span), "should be a string");
vals[index] = default_index_mode!(span);
vals[index] = reconstruct_index_mode!(span);
}
}
"trim" => {
Expand All @@ -705,7 +710,7 @@ impl Value {
Err(e) => {
// try_parse_trim_strategy() already adds its own errors
errors.push(e);
vals[index] = default_trim_strategy!(span);
vals[index] = reconstruct_trim_strategy!(span);
}
}
}
Expand All @@ -727,8 +732,8 @@ impl Value {
vec!["mode".into(), "index_mode".into(), "trim".into()],
vec![
Value::string(config.table_mode.clone(), *span),
default_index_mode!(span),
default_trim_strategy!(span),
reconstruct_index_mode!(span),
reconstruct_trim_strategy!(span),
],
*span,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ fn config_is_mutable() {
assert_eq!(actual.out, "false");
}

#[test]
fn config_preserved_after_do() {
let actual = nu!(cwd: ".", nu_repl_code(&[r"let-env config = { ls: { clickable_links: true } }",
"do -i { $env.config.ls.clickable_links = false }",
"$env.config.ls.clickable_links"]));

assert_eq!(actual.out, "true");
}

#[test]
fn config_affected_when_mutated() {
let actual = nu!(cwd: ".", nu_repl_code(&[r#"let-env config = { filesize: { metric: false, format:"auto" } }"#,
Expand All @@ -30,8 +39,10 @@ fn config_affected_when_deep_mutated() {

#[test]
fn config_add_unsupported_key() {
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[r#"source default_config.nu"#,
r#"$env.config.foo = 2"#]));
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[
r#"source default_config.nu"#,
r#"$env.config.foo = 2"#,
r#";"#]));

assert!(actual
.err
Expand All @@ -41,41 +52,49 @@ fn config_add_unsupported_key() {
#[test]
fn config_add_unsupported_type() {
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[r#"source default_config.nu"#,
r#"$env.config.ls = '' "#]));
r#"$env.config.ls = '' "#,
r#";"#]));

assert!(actual.err.contains("should be a record"));
}

#[test]
fn config_add_unsupported_value() {
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[r#"source default_config.nu"#,
r#"$env.config.history.file_format = ''"#]));
r#"$env.config.history.file_format = ''"#,
r#";"#]));

assert!(actual.err.contains(
"unrecognized $env.config.history.file_format ''; expected either 'sqlite' or 'plaintext'"
));
}

#[test]
#[ignore = "Figure out how to make test_bins::nu_repl() continue execution after shell errors"]
fn config_unsupported_key_reverted() {
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[r#"source default_config.nu"#,
r#"do -i { $env.config.foo = 1 | print ('foo' in $env.config) }"#]));
r#"$env.config.foo = 1"#,
r#"'foo' in $env.config"#]));

assert_eq!(actual.out, "false");
}

#[test]
#[ignore = "Figure out how to make test_bins::nu_repl() continue execution after shell errors"]
fn config_unsupported_type_reverted() {
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[r#" source default_config.nu"#,
r#"do -i { $env.config.ls = '' | print ($env.config.ls | describe) }"#]));
r#"$env.config.ls = ''"#,
r#"$env.config.ls | describe"#]));

assert!(actual.out.starts_with("record"));
assert_eq!(actual.out, "record");
}

#[test]
#[ignore = "Figure out how to make test_bins::nu_repl() continue execution after errors"]
fn config_unsupported_value_reverted() {
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[r#" source default_config.nu"#, r#" $env.config.history.file_format = 'plaintext'"#,
r#"do -i { $env.config.history.file_format = ''; }"#,
let actual = nu!(cwd: "crates/nu-utils/src/sample_config", nu_repl_code(&[r#" source default_config.nu"#,
r#"$env.config.history.file_format = 'plaintext'"#,
r#"$env.config.history.file_format = ''"#,
r#"$env.config.history.file_format | to json"#]));

assert_eq!(actual.out, "\"plaintext\"");
Expand Down