2022-07-25 18:32:56 +02:00
|
|
|
use nu_test_support::nu_with_plugins;
|
2023-04-08 20:52:37 +02:00
|
|
|
use pretty_assertions::assert_eq;
|
2022-07-25 18:32:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_custom_value_from_plugin_and_instantly_collapse_it() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
2022-09-07 16:07:42 +02:00
|
|
|
plugin: ("nu_plugin_custom_values"),
|
2022-07-25 18:32:56 +02:00
|
|
|
"custom-value generate"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "I used to be a custom value! My data was (abc)");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_custom_value_from_plugin_and_pass_it_over() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
2022-09-07 16:07:42 +02:00
|
|
|
plugin: ("nu_plugin_custom_values"),
|
2022-07-25 18:32:56 +02:00
|
|
|
"custom-value generate | custom-value update"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"I used to be a custom value! My data was (abcxyz)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-25 23:32:50 +01:00
|
|
|
#[test]
|
|
|
|
fn can_get_custom_value_from_plugin_and_pass_it_over_as_an_argument() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugin: ("nu_plugin_custom_values"),
|
|
|
|
"custom-value update-arg (custom-value generate)"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"I used to be a custom value! My data was (abcxyz)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-25 18:32:56 +02:00
|
|
|
#[test]
|
|
|
|
fn can_generate_and_updated_multiple_types_of_custom_values() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
2022-09-07 16:07:42 +02:00
|
|
|
plugin: ("nu_plugin_custom_values"),
|
2022-07-25 18:32:56 +02:00
|
|
|
"custom-value generate2 | custom-value update"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"I used to be a DIFFERENT custom value! (xyzabc)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
Add support for engine calls from plugins (#12029)
# Description
This allows plugins to make calls back to the engine to get config,
evaluate closures, and do other things that must be done within the
engine process.
Engine calls can both produce and consume streams as necessary. Closures
passed to plugins can both accept stream input and produce stream output
sent back to the plugin.
Engine calls referring to a plugin call's context can be processed as
long either the response hasn't been received, or the response created
streams that haven't ended yet.
This is a breaking API change for plugins. There are some pretty major
changes to the interface that plugins must implement, including:
1. Plugins now run with `&self` and must be `Sync`. Executing multiple
plugin calls in parallel is supported, and there's a chance that a
closure passed to a plugin could invoke the same plugin. Supporting
state across plugin invocations is left up to the plugin author to do in
whichever way they feel best, but the plugin object itself is still
shared. Even though the engine doesn't run multiple plugin calls through
the same process yet, I still considered it important to break the API
in this way at this stage. We might want to consider an optional
threadpool feature for performance.
2. Plugins take a reference to `EngineInterface`, which can be cloned.
This interface allows plugins to make calls back to the engine,
including for getting config and running closures.
3. Plugins no longer take the `config` parameter. This can be accessed
from the interface via the `.get_plugin_config()` engine call.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Not only does this have plugin protocol changes, it will require plugins
to make some code changes before they will work again. But on the plus
side, the engine call feature is extensible, and we can add more things
to it as needed.
Plugin maintainers will have to change the trait signature at the very
least. If they were using `config`, they will have to call
`engine.get_plugin_config()` instead.
If they were using the mutable reference to the plugin, they will have
to come up with some strategy to work around it (for example, for `Inc`
I just cloned it). This shouldn't be such a big deal at the moment as
it's not like plugins have ever run as daemons with persistent state in
the past, and they don't in this PR either. But I thought it was
important to make the change before we support plugins as daemons, as an
exclusive mutable reference is not compatible with parallel plugin
calls.
I suggest this gets merged sometime *after* the current pending release,
so that we have some time to adjust to the previous plugin protocol
changes that don't require code changes before making ones that do.
# Tests + Formatting
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :green_circle: `toolkit test`
- :green_circle: `toolkit test stdlib`
# After Submitting
I will document the additional protocol features (`EngineCall`,
`EngineCallResponse`), and constraints on plugin call processing if
engine calls are used - basically, to be aware that an engine call could
result in a nested plugin call, so the plugin should be able to handle
that.
2024-03-09 18:26:30 +01:00
|
|
|
#[test]
|
|
|
|
fn can_generate_custom_value_and_pass_through_closure() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugin: ("nu_plugin_custom_values"),
|
|
|
|
"custom-value generate2 { custom-value update }"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"I used to be a DIFFERENT custom value! (xyzabc)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-25 18:32:56 +02:00
|
|
|
#[test]
|
|
|
|
fn can_get_describe_plugin_custom_values() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
2022-09-07 16:07:42 +02:00
|
|
|
plugin: ("nu_plugin_custom_values"),
|
2022-07-25 18:32:56 +02:00
|
|
|
"custom-value generate | describe"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "CoolCustomValue");
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are currently no custom values defined by the engine that aren't hidden behind an extra
|
2024-03-10 17:29:02 +01:00
|
|
|
// feature
|
2022-11-23 01:58:11 +01:00
|
|
|
#[cfg(feature = "sqlite")]
|
2022-07-25 18:32:56 +02:00
|
|
|
#[test]
|
|
|
|
fn fails_if_passing_engine_custom_values_to_plugins() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests/fixtures/formats",
|
2022-09-07 16:07:42 +02:00
|
|
|
plugin: ("nu_plugin_custom_values"),
|
2022-11-09 23:14:48 +01:00
|
|
|
"open sample.db | custom-value update"
|
2022-07-25 18:32:56 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual
|
|
|
|
.err
|
2024-02-25 23:32:50 +01:00
|
|
|
.contains("`SQLiteDatabase` cannot be sent to plugin"));
|
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("the `custom_values` plugin does not support this kind of value"));
|
2022-07-25 18:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fails_if_passing_custom_values_across_plugins() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugins: [
|
2022-09-07 16:07:42 +02:00
|
|
|
("nu_plugin_custom_values"),
|
|
|
|
("nu_plugin_inc")
|
2022-07-25 18:32:56 +02:00
|
|
|
],
|
|
|
|
"custom-value generate | inc --major"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual
|
|
|
|
.err
|
2024-02-25 23:32:50 +01:00
|
|
|
.contains("`CoolCustomValue` cannot be sent to plugin"));
|
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("the `inc` plugin does not support this kind of value"));
|
2022-07-25 18:32:56 +02:00
|
|
|
}
|