19 releases (12 breaking)

new 0.104.0 Apr 30, 2025
0.103.0 Mar 19, 2025
0.102.0 Feb 5, 2025
0.101.0 Dec 22, 2024
0.96.1 Jul 29, 2024

#128 in Operating systems

Download history 119/week @ 2025-01-10 126/week @ 2025-01-17 66/week @ 2025-01-24 198/week @ 2025-01-31 187/week @ 2025-02-07 103/week @ 2025-02-14 89/week @ 2025-02-21 97/week @ 2025-02-28 54/week @ 2025-03-07 243/week @ 2025-03-14 132/week @ 2025-03-21 70/week @ 2025-03-28 123/week @ 2025-04-04 143/week @ 2025-04-11 84/week @ 2025-04-18 231/week @ 2025-04-25

586 downloads per month
Used in 22 crates

MIT and maybe CC-PDDC

2.5MB
62K SLoC

Test support for Nushell plugins.

Example

use std::sync::Arc;

use nu_plugin::*;
use nu_plugin_test_support::PluginTest;
use nu_protocol::{
    Example, IntoInterruptiblePipelineData, LabeledError, PipelineData, ShellError, Signals,
    Signature, Span, Type, Value,
};

struct LowercasePlugin;
struct Lowercase;

impl PluginCommand for Lowercase {
    type Plugin = LowercasePlugin;

    fn name(&self) -> &str {
        "lowercase"
    }

    fn description(&self) -> &str {
        "Convert each string in a stream to lowercase"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name()).input_output_type(
            Type::List(Type::String.into()),
            Type::List(Type::String.into()),
        )
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            example: r#"[Hello wORLD] | lowercase"#,
            description: "Lowercase a list of strings",
            result: Some(Value::test_list(vec![
                Value::test_string("hello"),
                Value::test_string("world"),
            ])),
        }]
    }

    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))
            },
            &Signals::empty(),
        )?)
    }
}

impl Plugin for LowercasePlugin {
    fn version(&self) -> String {
        env!("CARGO_PKG_VERSION").into()
    }

    fn commands(&self) -> Vec<Box<dyn PluginCommand<Plugin=Self>>> {
        vec![Box::new(Lowercase)]
    }
}

// #[test]
fn test_examples() -> Result<(), ShellError> {
    PluginTest::new("lowercase", LowercasePlugin.into())?
        .test_command_examples(&Lowercase)
}

// #[test]
fn test_lowercase() -> Result<(), ShellError> {
    let input = vec![Value::test_string("FooBar")].into_pipeline_data(Span::test_data(), Signals::empty());
    let output = PluginTest::new("lowercase", LowercasePlugin.into())?
        .eval_with("lowercase", input)?
        .into_value(Span::test_data())?;

    assert_eq!(
        Value::test_list(vec![
            Value::test_string("foobar")
        ]),
        output
    );
    Ok(())
}
#

nu-plugin-test-support

This crate provides helpers for running tests on plugin commands, and is intended to be included in the dev-dependencies of plugin crates for testing.

Dependencies

~23–53MB
~1M SLoC