Default plugins are independent and called from Nu. (#1322)

This commit is contained in:
Andrés N. Robalino
2020-01-31 17:45:33 -05:00
committed by GitHub
parent 4e201d20ca
commit 3610baa227
27 changed files with 805 additions and 1511 deletions

View File

@ -0,0 +1,73 @@
#[cfg(test)]
mod tests;
use crate::inc::{Action, SemVerAction};
use crate::Inc;
use nu_errors::ShellError;
use nu_plugin::Plugin;
use nu_protocol::{
CallInfo, Primitive, ReturnSuccess, ReturnValue, ShellTypeName, Signature, SyntaxShape,
UntaggedValue, Value,
};
use nu_source::{HasSpan, SpannedItem};
use nu_value_ext::ValueExt;
impl Plugin for Inc {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("inc")
.desc("Increment a value or version. Optionally use the column of a table.")
.switch("major", "increment the major version (eg 1.2.1 -> 2.0.0)")
.switch("minor", "increment the minor version (eg 1.2.1 -> 1.3.0)")
.switch("patch", "increment the patch version (eg 1.2.1 -> 1.2.2)")
.rest(SyntaxShape::ColumnPath, "the column(s) to update")
.filter())
}
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
if call_info.args.has("major") {
self.for_semver(SemVerAction::Major);
}
if call_info.args.has("minor") {
self.for_semver(SemVerAction::Minor);
}
if call_info.args.has("patch") {
self.for_semver(SemVerAction::Patch);
}
if let Some(args) = call_info.args.positional {
for arg in args {
match arg {
table @ Value {
value: UntaggedValue::Primitive(Primitive::ColumnPath(_)),
..
} => {
self.field = Some(table.as_column_path()?);
}
value => {
return Err(ShellError::type_error(
"table",
value.type_name().spanned(value.span()),
))
}
}
}
}
if self.action.is_none() {
self.action = Some(Action::Default);
}
match &self.error {
Some(reason) => Err(ShellError::untagged_runtime_error(format!(
"{}: {}",
reason,
Inc::usage()
))),
None => Ok(vec![]),
}
}
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![ReturnSuccess::value(self.inc(input)?)])
}
}

View File

@ -0,0 +1,135 @@
mod integration {
use crate::inc::{Action, SemVerAction};
use crate::Inc;
use nu_errors::ShellError;
use nu_plugin::test_helpers::value::{column_path, string};
use nu_plugin::test_helpers::{plugin, CallStub};
#[test]
fn picks_up_one_action_flag_only() {
plugin(&mut Inc::new())
.args(
CallStub::new()
.with_long_flag("major")
.with_long_flag("minor")
.create(),
)
.setup(|plugin, returned_values| {
let actual = format!("{}", returned_values.unwrap_err());
assert!(actual.contains("can only apply one"));
assert_eq!(plugin.error, Some("can only apply one".to_string()));
});
}
#[test]
fn picks_up_major_flag() {
plugin(&mut Inc::new())
.args(CallStub::new().with_long_flag("major").create())
.setup(|plugin, _| {
let sem_version_part = SemVerAction::Major;
plugin.expect_action(Action::SemVerAction(sem_version_part))
});
}
#[test]
fn picks_up_minor_flag() {
plugin(&mut Inc::new())
.args(CallStub::new().with_long_flag("minor").create())
.setup(|plugin, _| {
let sem_version_part = SemVerAction::Minor;
plugin.expect_action(Action::SemVerAction(sem_version_part))
});
}
#[test]
fn picks_up_patch_flag() {
plugin(&mut Inc::new())
.args(CallStub::new().with_long_flag("patch").create())
.setup(|plugin, _| {
let sem_version_part = SemVerAction::Patch;
plugin.expect_action(Action::SemVerAction(sem_version_part))
});
}
#[test]
fn picks_up_argument_for_field() -> Result<(), ShellError> {
plugin(&mut Inc::new())
.args(CallStub::new().with_parameter("package.version")?.create())
.setup(|plugin, _| {
//FIXME: this will need to be updated
if let Ok(column_path) = column_path(&[string("package"), string("version")]) {
plugin.expect_field(column_path)
}
});
Ok(())
}
mod sem_ver {
use crate::Inc;
use nu_errors::ShellError;
use nu_plugin::test_helpers::value::{get_data, string, structured_sample_record};
use nu_plugin::test_helpers::{expect_return_value_at, plugin, CallStub};
fn cargo_sample_record(with_version: &str) -> nu_protocol::Value {
structured_sample_record("version", with_version)
}
#[test]
fn major_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
let run = plugin(&mut Inc::new())
.args(
CallStub::new()
.with_long_flag("major")
.with_parameter("version")?
.create(),
)
.input(cargo_sample_record("0.1.3"))
.setup(|_, _| {})
.test();
let actual = expect_return_value_at(run, 0);
assert_eq!(get_data(actual, "version"), string("1.0.0"));
Ok(())
}
#[test]
fn minor_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
let run = plugin(&mut Inc::new())
.args(
CallStub::new()
.with_long_flag("minor")
.with_parameter("version")?
.create(),
)
.input(cargo_sample_record("0.1.3"))
.setup(|_, _| {})
.test();
let actual = expect_return_value_at(run, 0);
assert_eq!(get_data(actual, "version"), string("0.2.0"));
Ok(())
}
#[test]
fn patch_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
let run = plugin(&mut Inc::new())
.args(
CallStub::new()
.with_long_flag("patch")
.with_parameter("version")?
.create(),
)
.input(cargo_sample_record("0.1.3"))
.setup(|_, _| {})
.test();
let actual = expect_return_value_at(run, 0);
assert_eq!(get_data(actual, "version"), string("0.1.4"));
Ok(())
}
}
}