nushell/tests/repl/test_config.rs

171 lines
4.0 KiB
Rust
Raw Normal View History

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",
)
}
Allow plugins to receive configuration from the nushell configuration (#10955) # Description When nushell calls a plugin it now sends a configuration `Value` from the nushell config under `$env.config.plugins.PLUGIN_SHORT_NAME`. This allows plugin authors to read configuration provided by plugin users. The `PLUGIN_SHORT_NAME` must match the registered filename after `nu_plugin_`. If you register `target/debug/nu_plugin_config` the `PLUGIN_NAME` will be `config` and the nushell config will loook like: $env.config = { # ... plugins: { config: [ some values ] } } Configuration may also use a closure which allows passing values from `$env` to a plugin: $env.config = { # ... plugins: { config: {|| $env.some_value } } } This is a breaking change for the plugin API as the `Plugin::run()` function now accepts a new configuration argument which is an `&Option<Value>`. If no configuration was supplied the value is `None`. Plugins compiled after this change should work with older nushell, and will behave as if the configuration was not set. Initially discussed in #10867 # User-Facing Changes * Plugins can read configuration data stored in `$env.config.plugins` * The plugin `CallInfo` now includes a `config` entry, existing plugins will require updates # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting - [ ] Update [Creating a plugin (in Rust)](https://www.nushell.sh/contributor-book/plugins.html#creating-a-plugin-in-rust) [source](https://github.com/nushell/nushell.github.io/blob/main/contributor-book/plugins.md) - [ ] Add "Configuration" section to [Plugins documentation](https://www.nushell.sh/contributor-book/plugins.html)
2024-01-15 09:59:47 +01:00
#[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")
}
Keep plugins persistently running in the background (#12064) # Description This PR uses the new plugin protocol to intelligently keep plugin processes running in the background for further plugin calls. Running plugins can be seen by running the new `plugin list` command, and stopped by running the new `plugin stop` command. This is an enhancement for the performance of plugins, as starting new plugin processes has overhead, especially for plugins in languages that take a significant amount of time on startup. It also enables plugins that have persistent state between commands, making the migration of features like dataframes and `stor` to plugins possible. Plugins are automatically stopped by the new plugin garbage collector, configurable with `$env.config.plugin_gc`: ```nushell $env.config.plugin_gc = { # Configuration for plugin garbage collection default: { enabled: true # true to enable stopping of inactive plugins stop_after: 10sec # how long to wait after a plugin is inactive to stop it } plugins: { # alternate configuration for specific plugins, by name, for example: # # gstat: { # enabled: false # } } } ``` If garbage collection is enabled, plugins will be stopped after `stop_after` passes after they were last active. Plugins are counted as inactive if they have no running plugin calls. Reading the stream from the response of a plugin call is still considered to be activity, but if a plugin holds on to a stream but the call ends without an active streaming response, it is not counted as active even if it is reading it. Plugins can explicitly disable the GC as appropriate with `engine.set_gc_disabled(true)`. The `version` command now lists plugin names rather than plugin commands. The list of plugin commands is accessible via `plugin list`. Recommend doing this together with #12029, because it will likely force plugin developers to do the right thing with mutability and lead to less unexpected behavior when running plugins nested / in parallel. # User-Facing Changes - new command: `plugin list` - new command: `plugin stop` - changed command: `version` (now lists plugin names, rather than commands) - new config: `$env.config.plugin_gc` - Plugins will keep running and be reused, at least for the configured GC period - Plugins that used mutable state in weird ways like `inc` did might misbehave until fixed - Plugins can disable GC if they need to - Had to change plugin signature to accept `&EngineInterface` so that the GC disable feature works. #12029 does this anyway, and I'm expecting (resolvable) conflicts with that # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` Because there is some specific OS behavior required for plugins to not respond to Ctrl-C directly, I've developed against and tested on both Linux and Windows to ensure that works properly. # After Submitting I think this probably needs to be in the book somewhere
2024-03-10 00:10:22 +01:00
#[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",
)
}