Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example tests (nu-plugin-test-support) for plugins in repo #12281

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions crates/nu-plugin-test-support/src/plugin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ impl PluginTest {
&mut self.engine_state
}

/// Make additional command declarations available for use by tests.
///
/// This can be used to pull in commands from `nu-cmd-lang` for example, as required.
pub fn add_decl(
&mut self,
decl: Box<dyn nu_protocol::engine::Command>,
) -> Result<&mut Self, ShellError> {
let mut working_set = StateWorkingSet::new(&self.engine_state);
working_set.add_decl(decl);
self.engine_state.merge_delta(working_set.render())?;
Ok(self)
}

/// Evaluate some Nushell source code with the plugin commands in scope with the given input to
/// the pipeline.
///
Expand Down
3 changes: 3 additions & 0 deletions crates/nu_plugin_custom_values/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ nu-plugin = { path = "../nu-plugin", version = "0.91.1" }
nu-protocol = { path = "../nu-protocol", version = "0.91.1", features = ["plugin"] }
serde = { workspace = true, default-features = false }
typetag = "0.2"

[dev-dependencies]
nu-plugin-test-support = { path = "../nu-plugin-test-support", version = "0.91.1" }
8 changes: 8 additions & 0 deletions crates/nu_plugin_custom_values/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ impl SimplePluginCommand for Generate {
Ok(CoolCustomValue::new("abc").into_value(call.head))
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;

PluginTest::new("custom_values", crate::CustomValuePlugin.into())?
.test_command_examples(&Generate)
}
8 changes: 8 additions & 0 deletions crates/nu_plugin_custom_values/src/generate2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ impl SimplePluginCommand for Generate2 {
}
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;

PluginTest::new("custom_values", crate::CustomValuePlugin.into())?
.test_command_examples(&Generate2)
}
14 changes: 13 additions & 1 deletion crates/nu_plugin_custom_values/src/second_custom_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::cmp::Ordering;

use nu_protocol::{CustomValue, ShellError, Span, Value};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct SecondCustomValue {
pub(crate) something: String,
}
Expand Down Expand Up @@ -59,6 +61,16 @@ impl CustomValue for SecondCustomValue {
))
}

fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
if let Value::CustomValue { val, .. } = other {
val.as_any()
.downcast_ref()
.and_then(|other: &SecondCustomValue| PartialOrd::partial_cmp(self, other))
} else {
None
}
}

fn as_any(&self) -> &dyn std::any::Any {
self
}
Expand Down
12 changes: 10 additions & 2 deletions crates/nu_plugin_custom_values/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ impl SimplePluginCommand for Update {
result: Some(CoolCustomValue::new("abcxyz").into_value(Span::test_data())),
},
PluginExample {
example: "custom-value generate | custom-value update".into(),
example: "custom-value generate2 | custom-value update".into(),
description: "Update a SecondCustomValue".into(),
result: Some(CoolCustomValue::new("xyzabc").into_value(Span::test_data())),
result: Some(SecondCustomValue::new("xyzabc").into_value(Span::test_data())),
},
])
}
Expand Down Expand Up @@ -56,3 +56,11 @@ impl SimplePluginCommand for Update {
.into())
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;

PluginTest::new("custom_values", crate::CustomValuePlugin.into())?
.test_command_examples(&Update)
}
4 changes: 4 additions & 0 deletions crates/nu_plugin_example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ bench = false
[dependencies]
nu-plugin = { path = "../nu-plugin", version = "0.91.1" }
nu-protocol = { path = "../nu-protocol", version = "0.91.1", features = ["plugin"] }

[dev-dependencies]
nu-plugin-test-support = { path = "../nu-plugin-test-support", version = "0.91.1" }
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.91.1" }
6 changes: 6 additions & 0 deletions crates/nu_plugin_example/src/commands/collect_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ impl PluginCommand for CollectExternal {
})
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;
PluginTest::new("example", Example.into())?.test_command_examples(&CollectExternal)
}
6 changes: 6 additions & 0 deletions crates/nu_plugin_example/src/commands/for_each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ impl PluginCommand for ForEach {
Ok(PipelineData::Empty)
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;
PluginTest::new("example", Example.into())?.test_command_examples(&ForEach)
}
9 changes: 9 additions & 0 deletions crates/nu_plugin_example/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@ impl PluginCommand for Generate {
.into_pipeline_data(None))
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_cmd_lang::If;
use nu_plugin_test_support::PluginTest;
PluginTest::new("example", Example.into())?
.add_decl(Box::new(If))?
.test_command_examples(&Generate)
}
6 changes: 6 additions & 0 deletions crates/nu_plugin_example/src/commands/one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ impl SimplePluginCommand for One {
Ok(Value::nothing(call.head))
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;
PluginTest::new("example", Example.into())?.test_command_examples(&One)
}
6 changes: 6 additions & 0 deletions crates/nu_plugin_example/src/commands/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ impl PluginCommand for Seq {
Ok(PipelineData::ListStream(list_stream, None))
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;
PluginTest::new("example", Example.into())?.test_command_examples(&Seq)
}
8 changes: 7 additions & 1 deletion crates/nu_plugin_example/src/commands/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl PluginCommand for Sum {
(Type::List(Type::Float.into()), Type::Float),
])
.plugin_examples(vec![PluginExample {
example: "seq 1 5 | example sum".into(),
example: "example seq 1 5 | example sum".into(),
description: "sum values from 1 to 5".into(),
result: Some(Value::test_int(15)),
}])
Expand Down Expand Up @@ -88,3 +88,9 @@ impl IntOrFloat {
}
}
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;
PluginTest::new("example", Example.into())?.test_command_examples(&Sum)
}
3 changes: 3 additions & 0 deletions crates/nu_plugin_formats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ indexmap = { workspace = true }
eml-parser = "0.1"
ical = "0.10"
rust-ini = "0.20.0"

[dev-dependencies]
nu-plugin-test-support = { path = "../nu-plugin-test-support", version = "0.91.1" }
9 changes: 9 additions & 0 deletions crates/nu_plugin_formats/src/from/eml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub fn examples() -> Vec<PluginExample> {
example: "'From: test@email.com
Subject: Welcome
To: someone@somewhere.com

Test' | from eml"
.into(),
result: Some(Value::test_record(record! {
Expand All @@ -73,6 +74,7 @@ Test' | from eml"
example: "'From: test@email.com
Subject: Welcome
To: someone@somewhere.com

Test' | from eml -b 1"
.into(),
result: Some(Value::test_record(record! {
Expand Down Expand Up @@ -164,3 +166,10 @@ fn from_eml(input: &Value, body_preview: usize, head: Span) -> Result<Value, Lab

Ok(Value::record(collected.into_iter().collect(), head))
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;

PluginTest::new("formats", crate::FromCmds.into())?.test_command_examples(&FromEml)
}
12 changes: 10 additions & 2 deletions crates/nu_plugin_formats/src/from/ics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ impl SimplePluginCommand for FromIcs {

pub fn examples() -> Vec<PluginExample> {
vec![PluginExample {
example: "'BEGIN:VCALENDAR
END:VCALENDAR' | from ics"
example: "
'BEGIN:VCALENDAR
END:VCALENDAR' | from ics"
.into(),
description: "Converts ics formatted string to table".into(),
result: Some(Value::test_list(vec![Value::test_record(record! {
Expand Down Expand Up @@ -263,3 +264,10 @@ fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {

Value::record(row.into_iter().collect(), span)
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;

PluginTest::new("formats", crate::FromCmds.into())?.test_command_examples(&FromIcs)
}
7 changes: 7 additions & 0 deletions crates/nu_plugin_formats/src/from/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ b=2' | from ini"
})),
}]
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;

PluginTest::new("formats", crate::FromCmds.into())?.test_command_examples(&FromIni)
}
7 changes: 7 additions & 0 deletions crates/nu_plugin_formats/src/from/vcf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,10 @@ fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {

Value::record(row.into_iter().collect(), span)
}

#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;

PluginTest::new("formats", crate::FromCmds.into())?.test_command_examples(&FromVcf)
}