mirror of
https://github.com/nushell/nushell
synced 2025-01-15 14:44:14 +00:00
c79c43d2f8
# Description Adds a `nu-plugin-test-support` crate with an interface that supports testing plugins. Unlike in reality, these plugins run in the same process on separate threads. This will allow testing aspects of the plugin internal state and handling serialized plugin custom values easily. We still serialize their custom values and all of the engine to plugin logic is still in play, so from a logical perspective this should still expose any bugs that would have been caused by that. The only difference is that it doesn't run in a different process, and doesn't try to serialize everything to the final wire format for stdin/stdout. TODO still: - [x] Clean up warnings about private types exposed in trait definition - [x] Automatically deserialize plugin custom values in the result so they can be inspected - [x] Automatic plugin examples test function - [x] Write a bit more documentation - [x] More tests - [x] Add MIT License file to new crate # User-Facing Changes Plugin developers get a nice way to test their plugins. # Tests + Formatting Run the tests with `cargo test -p nu-plugin-test-support -- --show-output` to see some examples of what the failing test output for examples can look like. I used the `difference` crate (MIT licensed) to make it look nice. - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting - [ ] Add a section to the book about testing - [ ] Test some of the example plugins this way - [ ] Add example tests to nu_plugin_template so plugin developers have something to start with
129 lines
3.6 KiB
Rust
129 lines
3.6 KiB
Rust
use super::{GetPlugin, PluginExecutionCommandContext, PluginSource};
|
|
use crate::protocol::{CallInfo, EvaluatedCall};
|
|
use std::sync::Arc;
|
|
|
|
use nu_engine::get_eval_expression;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{ast::Call, PluginSignature, Signature};
|
|
use nu_protocol::{Example, PipelineData, PluginIdentity, ShellError};
|
|
|
|
#[doc(hidden)] // Note: not for plugin authors / only used in nu-parser
|
|
#[derive(Clone)]
|
|
pub struct PluginDeclaration {
|
|
name: String,
|
|
signature: PluginSignature,
|
|
source: PluginSource,
|
|
}
|
|
|
|
impl PluginDeclaration {
|
|
pub fn new(plugin: Arc<dyn GetPlugin>, signature: PluginSignature) -> Self {
|
|
Self {
|
|
name: signature.sig.name.clone(),
|
|
signature,
|
|
source: PluginSource::new(plugin),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Command for PluginDeclaration {
|
|
fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
self.signature.sig.clone()
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
self.signature.sig.usage.as_str()
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
self.signature.sig.extra_usage.as_str()
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
self.signature
|
|
.sig
|
|
.search_terms
|
|
.iter()
|
|
.map(|term| term.as_str())
|
|
.collect()
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
let mut res = vec![];
|
|
for e in self.signature.examples.iter() {
|
|
res.push(Example {
|
|
example: &e.example,
|
|
description: &e.description,
|
|
result: e.result.clone(),
|
|
})
|
|
}
|
|
res
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let eval_expression = get_eval_expression(engine_state);
|
|
|
|
// Create the EvaluatedCall to send to the plugin first - it's best for this to fail early,
|
|
// before we actually try to run the plugin command
|
|
let evaluated_call =
|
|
EvaluatedCall::try_from_call(call, engine_state, stack, eval_expression)?;
|
|
|
|
// Get the engine config
|
|
let engine_config = nu_engine::get_config(engine_state, stack);
|
|
|
|
// Get, or start, the plugin.
|
|
let plugin = self
|
|
.source
|
|
.persistent(None)
|
|
.and_then(|p| {
|
|
// Set the garbage collector config from the local config before running
|
|
p.set_gc_config(engine_config.plugin_gc.get(p.identity().name()));
|
|
p.get_plugin(Some((engine_state, stack)))
|
|
})
|
|
.map_err(|err| {
|
|
let decl = engine_state.get_decl(call.decl_id);
|
|
ShellError::GenericError {
|
|
error: format!("Unable to spawn plugin for `{}`", decl.name()),
|
|
msg: err.to_string(),
|
|
span: Some(call.head),
|
|
help: None,
|
|
inner: vec![],
|
|
}
|
|
})?;
|
|
|
|
// Create the context to execute in - this supports engine calls and custom values
|
|
let mut context = PluginExecutionCommandContext::new(
|
|
self.source.identity.clone(),
|
|
engine_state,
|
|
stack,
|
|
call,
|
|
);
|
|
|
|
plugin.run(
|
|
CallInfo {
|
|
name: self.name.clone(),
|
|
call: evaluated_call,
|
|
input,
|
|
},
|
|
&mut context,
|
|
)
|
|
}
|
|
|
|
fn is_plugin(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn plugin_identity(&self) -> Option<&PluginIdentity> {
|
|
Some(&self.source.identity)
|
|
}
|
|
}
|