nushell/tests/repl/test_config.rs
Devyn Cairns 8e2917b9ae
Make assignment and const consistent with let/mut (#13385)
# Description

This makes assignment operations and `const` behave the same way `let`
and `mut` do, absorbing the rest of the pipeline.

Changes the lexer to be able to recognize assignment operators as a
separate token, and then makes the lite parser continue to push spans
into the same command regardless of any redirections or pipes if an
assignment operator is encountered. Because the pipeline is no longer
split up by the lite parser at this point, it's trivial to just parse
the right hand side as if it were a subexpression not contained within
parentheses.

# User-Facing Changes
Big breaking change. These are all now possible:

```nushell
const path = 'a' | path join 'b'

mut x = 2
$x = random int
$x = [1 2 3] | math sum

$env.FOO = random chars
```

In the past, these would have led to (an attempt at) bare word string
parsing. So while `$env.FOO = bar` would have previously set the
environment variable `FOO` to the string `"bar"`, it now tries to run
the command named `bar`, hence the major breaking change.

However, this is desirable because it is very consistent - if you see
the `=`, you can just assume it absorbs everything else to the right of
it.

# Tests + Formatting
Added tests for the new behaviour. Adjusted some existing tests that
depended on the right hand side of assignments being parsed as
barewords.

# After Submitting
- [ ] release notes (breaking change!)
2024-07-30 18:55:22 -05:00

170 lines
4 KiB
Rust

use crate::repl::tests::{fail_test, run_test, run_test_std, TestResult};
#[test]
fn mutate_nu_config() -> TestResult {
run_test_std(
r#"$env.config.footer_mode = 30; $env.config.footer_mode"#,
"30",
)
}
#[test]
fn mutate_nu_config_nested_ls() -> TestResult {
run_test_std(
r#"$env.config.ls.clickable_links = false; $env.config.ls.clickable_links"#,
"false",
)
}
#[test]
fn mutate_nu_config_nested_table() -> TestResult {
run_test_std(
r#"
$env.config.table.trim.methodology = 'wrapping'
$env.config.table.trim.wrapping_try_keep_words = false
$env.config.table.trim.wrapping_try_keep_words
"#,
"false",
)
}
#[test]
fn mutate_nu_config_nested_menu() -> TestResult {
run_test_std(
r#"
$env.config.menus = [
{
name: menu
only_buffer_difference: true
marker: "M "
type: {}
style: {}
}
];
$env.config.menus.0.type.columns = 3;
$env.config.menus.0.type.columns
"#,
"3",
)
}
#[test]
fn mutate_nu_config_nested_keybindings() -> TestResult {
run_test_std(
r#"
$env.config.keybindings = [
{
name: completion_previous
modifier: shift
keycode: backtab
mode: [ vi_normal, vi_insert ]
event: { send: menuprevious }
}
];
$env.config.keybindings.0.keycode = 'char_x';
$env.config.keybindings.0.keycode
"#,
"char_x",
)
}
#[test]
fn mutate_nu_config_nested_color_nested() -> TestResult {
run_test_std(
r#"$env.config.color_config.shape_flag = 'cyan'; $env.config.color_config.shape_flag"#,
"cyan",
)
}
#[test]
fn mutate_nu_config_nested_completion() -> TestResult {
run_test_std(
r#"$env.config.completions.external.enable = false; $env.config.completions.external.enable"#,
"false",
)
}
#[test]
fn mutate_nu_config_nested_history() -> TestResult {
run_test_std(
r#"$env.config.history.max_size = 100; $env.config.history.max_size"#,
"100",
)
}
#[test]
fn mutate_nu_config_nested_filesize() -> TestResult {
run_test_std(
r#"$env.config.filesize.format = 'kb'; $env.config.filesize.format"#,
"kb",
)
}
#[test]
fn mutate_nu_config_plugin() -> TestResult {
run_test_std(
r#"
$env.config.plugins = {
config: {
key1: value
key2: other
}
};
$env.config.plugins.config.key1 = 'updated'
$env.config.plugins.config.key1
"#,
"updated",
)
}
#[test]
fn reject_nu_config_plugin_non_record() -> TestResult {
fail_test(r#"$env.config.plugins = 5"#, "should be a record")
}
#[test]
fn mutate_nu_config_plugin_gc_default_enabled() -> TestResult {
run_test(
r#"
$env.config.plugin_gc.default.enabled = false
$env.config.plugin_gc.default.enabled
"#,
"false",
)
}
#[test]
fn mutate_nu_config_plugin_gc_default_stop_after() -> TestResult {
run_test(
r#"
$env.config.plugin_gc.default.stop_after = 20sec
$env.config.plugin_gc.default.stop_after
"#,
"20sec",
)
}
#[test]
fn mutate_nu_config_plugin_gc_default_stop_after_negative() -> TestResult {
fail_test(
r#"
$env.config.plugin_gc.default.stop_after = -1sec
$env.config.plugin_gc.default.stop_after
"#,
"must not be negative",
)
}
#[test]
fn mutate_nu_config_plugin_gc_plugins() -> TestResult {
run_test(
r#"
$env.config.plugin_gc.plugins.inc = {
enabled: true
stop_after: 0sec
}
$env.config.plugin_gc.plugins.inc.stop_after
"#,
"0sec",
)
}