mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:25:38 +02:00
engine-q merge
This commit is contained in:
@ -1,7 +1,12 @@
|
||||
<<<<<<< HEAD
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{did_you_mean, ColumnPath, Primitive, ShellTypeName, UntaggedValue, Value};
|
||||
use nu_source::{span_for_spanned_list, HasSpan, SpannedItem, Tagged};
|
||||
use nu_value_ext::{get_data_by_column_path, ValueExt};
|
||||
=======
|
||||
use nu_plugin::LabeledError;
|
||||
use nu_protocol::{Span, Value};
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Action {
|
||||
@ -18,7 +23,10 @@ pub enum SemVerAction {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Inc {
|
||||
<<<<<<< HEAD
|
||||
pub field: Option<Tagged<ColumnPath>>,
|
||||
=======
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
pub error: Option<String>,
|
||||
pub action: Option<Action>,
|
||||
}
|
||||
@ -28,12 +36,25 @@ impl Inc {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
fn apply(&self, input: &str) -> UntaggedValue {
|
||||
=======
|
||||
fn apply(&self, input: &str, head: Span) -> Value {
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
match &self.action {
|
||||
Some(Action::SemVerAction(act_on)) => {
|
||||
let mut ver = match semver::Version::parse(input) {
|
||||
Ok(parsed_ver) => parsed_ver,
|
||||
<<<<<<< HEAD
|
||||
Err(_) => return UntaggedValue::string(input.to_string()),
|
||||
=======
|
||||
Err(_) => {
|
||||
return Value::String {
|
||||
val: input.to_string(),
|
||||
span: head,
|
||||
}
|
||||
}
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
};
|
||||
|
||||
match act_on {
|
||||
@ -42,11 +63,28 @@ impl Inc {
|
||||
SemVerAction::Patch => ver.increment_patch(),
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
UntaggedValue::string(ver.to_string())
|
||||
}
|
||||
Some(Action::Default) | None => match input.parse::<u64>() {
|
||||
Ok(v) => UntaggedValue::string((v + 1).to_string()),
|
||||
Err(_) => UntaggedValue::string(input),
|
||||
=======
|
||||
Value::String {
|
||||
val: ver.to_string(),
|
||||
span: head,
|
||||
}
|
||||
}
|
||||
Some(Action::Default) | None => match input.parse::<u64>() {
|
||||
Ok(v) => Value::String {
|
||||
val: (v + 1).to_string(),
|
||||
span: head,
|
||||
},
|
||||
Err(_) => Value::String {
|
||||
val: input.to_string(),
|
||||
span: head,
|
||||
},
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -71,6 +109,7 @@ impl Inc {
|
||||
"Usage: inc field [--major|--minor|--patch]"
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
pub fn inc(&self, value: Value) -> Result<Value, ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
@ -139,6 +178,28 @@ impl Inc {
|
||||
"incrementable value",
|
||||
value.type_name().spanned(value.span()),
|
||||
)),
|
||||
=======
|
||||
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
|
||||
match value {
|
||||
Value::Int { val, span } => Ok(Value::Int {
|
||||
val: val + 1,
|
||||
span: *span,
|
||||
}),
|
||||
Value::String { val, .. } => Ok(self.apply(val, head)),
|
||||
x => {
|
||||
let msg = x.as_string().map_err(|e| LabeledError {
|
||||
label: "Unable to extract string".into(),
|
||||
msg: format!("value cannot be converted to string {:?} - {}", x, e),
|
||||
span: Some(head),
|
||||
})?;
|
||||
|
||||
Err(LabeledError {
|
||||
label: "Incorrect value".into(),
|
||||
msg,
|
||||
span: Some(head),
|
||||
})
|
||||
}
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -146,6 +207,7 @@ impl Inc {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
mod semver {
|
||||
<<<<<<< HEAD
|
||||
use crate::inc::SemVerAction;
|
||||
use crate::Inc;
|
||||
use nu_test_support::value::string;
|
||||
@ -155,20 +217,56 @@ mod tests {
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Major);
|
||||
assert_eq!(inc.apply("0.1.3"), string("1.0.0").value);
|
||||
=======
|
||||
use nu_protocol::{Span, Value};
|
||||
|
||||
use crate::inc::SemVerAction;
|
||||
use crate::Inc;
|
||||
|
||||
#[test]
|
||||
fn major() {
|
||||
let expected = Value::String {
|
||||
val: "1.0.0".to_string(),
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Major);
|
||||
assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minor() {
|
||||
<<<<<<< HEAD
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Minor);
|
||||
assert_eq!(inc.apply("0.1.3"), string("0.2.0").value);
|
||||
=======
|
||||
let expected = Value::String {
|
||||
val: "0.2.0".to_string(),
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Minor);
|
||||
assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn patch() {
|
||||
<<<<<<< HEAD
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Patch);
|
||||
assert_eq!(inc.apply("0.1.3"), string("0.1.4").value);
|
||||
=======
|
||||
let expected = Value::String {
|
||||
val: "0.1.4".to_string(),
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let mut inc = Inc::new();
|
||||
inc.for_semver(SemVerAction::Patch);
|
||||
assert_eq!(inc.apply("0.1.3", Span::test_data()), expected)
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ mod inc;
|
||||
mod nu;
|
||||
|
||||
pub use inc::Inc;
|
||||
<<<<<<< HEAD
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@ -33,3 +34,5 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
=======
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
|
@ -1,6 +1,14 @@
|
||||
<<<<<<< HEAD
|
||||
use nu_plugin::serve_plugin;
|
||||
use nu_plugin_inc::Inc;
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Inc::new())
|
||||
=======
|
||||
use nu_plugin::{serve_plugin, CapnpSerializer};
|
||||
use nu_plugin_inc::Inc;
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Inc::new(), CapnpSerializer {})
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
<<<<<<< HEAD
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@ -15,6 +16,16 @@ use nu_value_ext::ValueExt;
|
||||
impl Plugin for Inc {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("inc")
|
||||
=======
|
||||
use crate::inc::SemVerAction;
|
||||
use crate::Inc;
|
||||
use nu_plugin::{EvaluatedCall, LabeledError, Plugin};
|
||||
use nu_protocol::{Signature, Value};
|
||||
|
||||
impl Plugin for Inc {
|
||||
fn signature(&self) -> Vec<Signature> {
|
||||
vec![Signature::build("inc")
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
.desc("Increment a value or version. Optionally use the column of a table.")
|
||||
.switch(
|
||||
"major",
|
||||
@ -30,6 +41,7 @@ impl Plugin for Inc {
|
||||
"patch",
|
||||
"increment the patch version (eg 1.2.1 -> 1.2.2)",
|
||||
Some('p'),
|
||||
<<<<<<< HEAD
|
||||
)
|
||||
.rest("rest", SyntaxShape::ColumnPath, "the column(s) to update")
|
||||
.filter())
|
||||
@ -81,5 +93,31 @@ impl Plugin for Inc {
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![ReturnSuccess::value(self.inc(input)?)])
|
||||
=======
|
||||
)]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&mut self,
|
||||
name: &str,
|
||||
call: &EvaluatedCall,
|
||||
input: &Value,
|
||||
) -> Result<Value, LabeledError> {
|
||||
if name != "inc" {
|
||||
return Ok(Value::Nothing { span: call.head });
|
||||
}
|
||||
|
||||
if call.has_flag("major") {
|
||||
self.for_semver(SemVerAction::Major);
|
||||
}
|
||||
if call.has_flag("minor") {
|
||||
self.for_semver(SemVerAction::Minor);
|
||||
}
|
||||
if call.has_flag("patch") {
|
||||
self.for_semver(SemVerAction::Patch);
|
||||
}
|
||||
|
||||
self.inc(call.head, input)
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
}
|
||||
}
|
||||
|
@ -1,144 +0,0 @@
|
||||
mod integration {
|
||||
use crate::inc::{Action, SemVerAction};
|
||||
use crate::Inc;
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::test_helpers::{plugin, CallStub};
|
||||
use nu_protocol::{Primitive, UntaggedValue};
|
||||
use nu_test_support::value::column_path;
|
||||
use nu_value_ext::ValueExt;
|
||||
|
||||
#[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 = returned_values.unwrap_err().to_string();
|
||||
|
||||
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("package.version").as_column_path() {
|
||||
let column_path =
|
||||
UntaggedValue::Primitive(Primitive::ColumnPath(column_path.item))
|
||||
.into_value(column_path.tag);
|
||||
plugin.expect_field(column_path)
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
mod sem_ver {
|
||||
use crate::Inc;
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::test_helpers::{expect_return_value_at, plugin, CallStub};
|
||||
use nu_protocol::TaggedDictBuilder;
|
||||
use nu_source::Tag;
|
||||
use nu_test_support::value::string;
|
||||
use nu_value_ext::get_data;
|
||||
|
||||
fn cargo_sample_record(with_version: &str) -> nu_protocol::Value {
|
||||
TaggedDictBuilder::build(Tag::unknown(), |row| {
|
||||
row.insert_value("version".to_string(), string(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").borrow(), &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").borrow(), &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").borrow(), &string("0.1.4"));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user