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");
|
|
|
|
}
|
|
|
|
|
2024-03-12 10:37:08 +01:00
|
|
|
#[test]
|
|
|
|
fn can_get_plugin_custom_value_int_cell_path() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugin: ("nu_plugin_custom_values"),
|
|
|
|
"(custom-value generate).0"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "abc");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_get_plugin_custom_value_string_cell_path() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugin: ("nu_plugin_custom_values"),
|
|
|
|
"(custom-value generate).cool"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "abc");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_sort_plugin_custom_values() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugin: ("nu_plugin_custom_values"),
|
|
|
|
"[(custom-value generate | custom-value update) (custom-value generate)] | sort | each { print } | ignore"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"I used to be a custom value! My data was (abc)\
|
|
|
|
I used to be a custom value! My data was (abcxyz)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_append_plugin_custom_values() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugin: ("nu_plugin_custom_values"),
|
|
|
|
"(custom-value generate) ++ (custom-value generate)"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"I used to be a custom value! My data was (abcabc)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-25 18:32:56 +02:00
|
|
|
// 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
|
|
|
}
|
2024-03-12 10:37:08 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn drop_check_custom_value_prints_message_on_drop() {
|
|
|
|
let actual = nu_with_plugins!(
|
|
|
|
cwd: "tests",
|
|
|
|
plugin: ("nu_plugin_custom_values"),
|
|
|
|
// We build an array with the value copied twice to verify that it only gets dropped once
|
|
|
|
"do { |v| [$v $v] } (custom-value drop-check 'Hello') | ignore"
|
|
|
|
);
|
|
|
|
|
Reorganize plugin API around commands (#12170)
[Context on
Discord](https://discord.com/channels/601130461678272522/855947301380947968/1216517833312309419)
# Description
This is a significant breaking change to the plugin API, but one I think
is worthwhile. @ayax79 mentioned on Discord that while trying to start
on a dataframes plugin, he was a little disappointed that more wasn't
provided in terms of code organization for commands, particularly since
there are *a lot* of `dfr` commands.
This change treats plugins more like miniatures of the engine, with
dispatch of the command name being handled inherently, each command
being its own type, and each having their own signature within the trait
impl for the command type rather than having to find a way to centralize
it all into one `Vec`.
For the example plugins that have multiple commands, I definitely like
how this looks a lot better. This encourages doing code organization the
right way and feels very good.
For the plugins that have only one command, it's just a little bit more
boilerplate - but still worth it, in my opinion.
The `Box<dyn PluginCommand<Plugin = Self>>` type in `commands()` is a
little bit hairy, particularly for Rust beginners, but ultimately not so
bad, and it gives the desired flexibility for shared state for a whole
plugin + the individual commands.
# User-Facing Changes
Pretty big breaking change to plugin API, but probably one that's worth
making.
```rust
use nu_plugin::*;
use nu_protocol::{PluginSignature, PipelineData, Type, Value};
struct LowercasePlugin;
struct Lowercase;
// Plugins can now have multiple commands
impl PluginCommand for Lowercase {
type Plugin = LowercasePlugin;
// The signature lives with the command
fn signature(&self) -> PluginSignature {
PluginSignature::build("lowercase")
.usage("Convert each string in a stream to lowercase")
.input_output_type(Type::List(Type::String.into()), Type::List(Type::String.into()))
}
// We also provide SimplePluginCommand which operates on Value like before
fn run(
&self,
plugin: &LowercasePlugin,
engine: &EngineInterface,
call: &EvaluatedCall,
input: PipelineData,
) -> Result<PipelineData, LabeledError> {
let span = call.head;
Ok(input.map(move |value| {
value.as_str()
.map(|string| Value::string(string.to_lowercase(), span))
// Errors in a stream should be returned as values.
.unwrap_or_else(|err| Value::error(err, span))
}, None)?)
}
}
// Plugin now just has a list of commands, and the custom value op stuff still goes here
impl Plugin for LowercasePlugin {
fn commands(&self) -> Vec<Box<dyn PluginCommand<Plugin=Self>>> {
vec![Box::new(Lowercase)]
}
}
fn main() {
serve_plugin(&LowercasePlugin{}, MsgPackSerializer)
}
```
Time this however you like - we're already breaking stuff for 0.92, so
it might be good to do it now, but if it feels like a lot all at once,
it could wait.
# Tests + Formatting
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :green_circle: `toolkit test`
- :green_circle: `toolkit test stdlib`
# After Submitting
- [ ] Update examples in the book
- [x] Fix #12088 to match - this change would actually simplify it a
lot, because the methods are currently just duplicated between `Plugin`
and `StreamingPlugin`, but they only need to be on `Plugin` with this
change
2024-03-14 22:40:02 +01:00
|
|
|
assert_eq!(actual.err, "DropCheckValue was dropped: Hello\n");
|
2024-03-12 10:37:08 +01:00
|
|
|
assert!(actual.status.success());
|
|
|
|
}
|