diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index dc1d746188..1187955897 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -135,7 +135,7 @@ pub(crate) use command::{ pub(crate) use alias::Alias; pub(crate) use ansi::Ansi; -pub(crate) use append::Append; +pub(crate) use append::Command as Append; pub(crate) use autoenv::Autoenv; pub(crate) use autoenv_trust::AutoenvTrust; pub(crate) use autoenv_untrust::AutoenvUnTrust; @@ -164,7 +164,7 @@ pub(crate) use echo::Echo; pub(crate) use if_::If; pub(crate) use is_empty::IsEmpty; pub(crate) use nu::NuPlugin; -pub(crate) use update::Update; +pub(crate) use update::Command as Update; pub(crate) mod kill; pub(crate) use kill::Kill; pub(crate) mod clear; @@ -193,13 +193,13 @@ pub(crate) use from_xml::FromXML; pub(crate) use from_yaml::FromYAML; pub(crate) use from_yaml::FromYML; pub(crate) use get::Get; -pub(crate) use group_by::GroupBy; +pub(crate) use group_by::Command as GroupBy; pub(crate) use group_by_date::GroupByDate; pub(crate) use headers::Headers; pub(crate) use help::Help; pub(crate) use histogram::Histogram; pub(crate) use history::History; -pub(crate) use insert::Insert; +pub(crate) use insert::Command as Insert; pub(crate) use into_int::IntoInt; pub(crate) use keep::{Keep, KeepUntil, KeepWhile}; pub(crate) use last::Last; @@ -270,3 +270,40 @@ pub(crate) use where_::Where; pub(crate) use which_::Which; pub(crate) use with_env::WithEnv; pub(crate) use wrap::Wrap; + +#[cfg(test)] +mod tests { + use super::*; + use crate::commands::whole_stream_command; + use crate::examples::{test_anchors, test_examples}; + use nu_errors::ShellError; + + fn commands() -> Vec { + vec![ + // Table operations + whole_stream_command(Append), + whole_stream_command(GroupBy), + // Row specific operations + whole_stream_command(Insert), + whole_stream_command(Update), + ] + } + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + for cmd in commands() { + test_examples(cmd)?; + } + + Ok(()) + } + + #[test] + fn tracks_metadata() -> Result<(), ShellError> { + for cmd in commands() { + test_anchors(cmd)?; + } + + Ok(()) + } +} diff --git a/crates/nu-cli/src/commands/alias.rs b/crates/nu-cli/src/commands/alias.rs index 6735bed497..cb421f0438 100644 --- a/crates/nu-cli/src/commands/alias.rs +++ b/crates/nu-cli/src/commands/alias.rs @@ -340,11 +340,12 @@ fn find_block_shapes(block: &Block, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Alias {}) + Ok(test_examples(Alias {})?) } } diff --git a/crates/nu-cli/src/commands/ansi.rs b/crates/nu-cli/src/commands/ansi.rs index 1f2d78c26c..e24d14c31f 100644 --- a/crates/nu-cli/src/commands/ansi.rs +++ b/crates/nu-cli/src/commands/ansi.rs @@ -135,11 +135,12 @@ pub fn str_to_ansi_color(s: String) -> Option { #[cfg(test)] mod tests { use super::Ansi; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Ansi {}) + Ok(test_examples(Ansi {})?) } } diff --git a/crates/nu-cli/src/commands/append.rs b/crates/nu-cli/src/commands/append.rs index a73ef487a6..a27839ea78 100644 --- a/crates/nu-cli/src/commands/append.rs +++ b/crates/nu-cli/src/commands/append.rs @@ -2,17 +2,17 @@ use crate::command_registry::CommandRegistry; use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; #[derive(Deserialize)] -struct AppendArgs { +struct Arguments { row: Value, } -pub struct Append; +pub struct Command; #[async_trait] -impl WholeStreamCommand for Append { +impl WholeStreamCommand for Command { fn name(&self) -> &str { "append" } @@ -34,11 +34,18 @@ impl WholeStreamCommand for Append { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - let (AppendArgs { row }, input) = args.process(registry).await?; + let (Arguments { mut row }, input) = args.process(registry).await?; - let eos = futures::stream::iter(vec![row]); + let input: Vec = input.collect().await; - Ok(input.chain(eos).to_output_stream()) + if let Some(first) = input.get(0) { + row.tag = first.tag(); + } + + Ok( + futures::stream::iter(input.into_iter().chain(vec![row]).map(ReturnSuccess::value)) + .to_output_stream(), + ) } fn examples(&self) -> Vec { @@ -54,15 +61,3 @@ impl WholeStreamCommand for Append { }] } } - -#[cfg(test)] -mod tests { - use super::Append; - - #[test] - fn examples_work_as_expected() { - use crate::examples::test as test_examples; - - test_examples(Append {}) - } -} diff --git a/crates/nu-cli/src/commands/autoview/command.rs b/crates/nu-cli/src/commands/autoview/command.rs index 5f6149b787..2d7d56a49a 100644 --- a/crates/nu-cli/src/commands/autoview/command.rs +++ b/crates/nu-cli/src/commands/autoview/command.rs @@ -326,11 +326,12 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawComm #[cfg(test)] mod tests { use super::Command; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/cal.rs b/crates/nu-cli/src/commands/cal.rs index 5b84d10048..e55778ad2b 100644 --- a/crates/nu-cli/src/commands/cal.rs +++ b/crates/nu-cli/src/commands/cal.rs @@ -339,11 +339,12 @@ fn add_month_to_table( #[cfg(test)] mod tests { use super::Cal; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Cal {}) + Ok(test_examples(Cal {})?) } } diff --git a/crates/nu-cli/src/commands/cd.rs b/crates/nu-cli/src/commands/cd.rs index 294c8bc0e7..01dde698df 100644 --- a/crates/nu-cli/src/commands/cd.rs +++ b/crates/nu-cli/src/commands/cd.rs @@ -72,11 +72,12 @@ impl WholeStreamCommand for Cd { #[cfg(test)] mod tests { use super::Cd; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Cd {}) + Ok(test_examples(Cd {})?) } } diff --git a/crates/nu-cli/src/commands/char_.rs b/crates/nu-cli/src/commands/char_.rs index ccda668f4e..acc2010a34 100644 --- a/crates/nu-cli/src/commands/char_.rs +++ b/crates/nu-cli/src/commands/char_.rs @@ -130,11 +130,12 @@ fn str_to_character(s: &str) -> Option { #[cfg(test)] mod tests { use super::Char; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Char {}) + Ok(test_examples(Char {})?) } } diff --git a/crates/nu-cli/src/commands/chart.rs b/crates/nu-cli/src/commands/chart.rs index d93fd2f709..dc7af90284 100644 --- a/crates/nu-cli/src/commands/chart.rs +++ b/crates/nu-cli/src/commands/chart.rs @@ -42,11 +42,12 @@ impl WholeStreamCommand for Chart { #[cfg(test)] mod tests { use super::Chart; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Chart {}) + Ok(test_examples(Chart {})?) } } diff --git a/crates/nu-cli/src/commands/clear.rs b/crates/nu-cli/src/commands/clear.rs index 574e9da362..3635f67f08 100644 --- a/crates/nu-cli/src/commands/clear.rs +++ b/crates/nu-cli/src/commands/clear.rs @@ -43,15 +43,3 @@ impl WholeStreamCommand for Clear { }] } } - -#[cfg(test)] -mod tests { - use super::Clear; - - #[test] - fn examples_work_as_expected() { - use crate::examples::test as test_examples; - - test_examples(Clear {}) - } -} diff --git a/crates/nu-cli/src/commands/clip.rs b/crates/nu-cli/src/commands/clip.rs index 4f62c597af..72a62ada8e 100644 --- a/crates/nu-cli/src/commands/clip.rs +++ b/crates/nu-cli/src/commands/clip.rs @@ -104,11 +104,12 @@ pub async fn clip( #[cfg(test)] mod tests { use super::Clip; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Clip {}) + Ok(test_examples(Clip {})?) } } diff --git a/crates/nu-cli/src/commands/compact.rs b/crates/nu-cli/src/commands/compact.rs index d869dc28d9..c61b67e9cf 100644 --- a/crates/nu-cli/src/commands/compact.rs +++ b/crates/nu-cli/src/commands/compact.rs @@ -84,11 +84,12 @@ pub async fn compact( #[cfg(test)] mod tests { use super::Compact; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Compact {}) + Ok(test_examples(Compact {})?) } } diff --git a/crates/nu-cli/src/commands/count.rs b/crates/nu-cli/src/commands/count.rs index 456ac4ccef..f3e8a3cda1 100644 --- a/crates/nu-cli/src/commands/count.rs +++ b/crates/nu-cli/src/commands/count.rs @@ -80,11 +80,12 @@ impl WholeStreamCommand for Count { #[cfg(test)] mod tests { use super::Count; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Count {}) + Ok(test_examples(Count {})?) } } diff --git a/crates/nu-cli/src/commands/cp.rs b/crates/nu-cli/src/commands/cp.rs index 1f51ba05fe..e8e831b00a 100644 --- a/crates/nu-cli/src/commands/cp.rs +++ b/crates/nu-cli/src/commands/cp.rs @@ -66,11 +66,12 @@ impl WholeStreamCommand for Cpy { #[cfg(test)] mod tests { use super::Cpy; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Cpy {}) + Ok(test_examples(Cpy {})?) } } diff --git a/crates/nu-cli/src/commands/date/command.rs b/crates/nu-cli/src/commands/date/command.rs index e1f09b0b69..74d5d4d374 100644 --- a/crates/nu-cli/src/commands/date/command.rs +++ b/crates/nu-cli/src/commands/date/command.rs @@ -36,11 +36,12 @@ impl WholeStreamCommand for Command { #[cfg(test)] mod tests { use super::Command; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/date/format.rs b/crates/nu-cli/src/commands/date/format.rs index 5dc6038b4e..f7fcf01df5 100644 --- a/crates/nu-cli/src/commands/date/format.rs +++ b/crates/nu-cli/src/commands/date/format.rs @@ -61,3 +61,16 @@ pub async fn format( Ok(OutputStream::one(value)) } + +#[cfg(test)] +mod tests { + use super::Date; + use super::ShellError; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + Ok(test_examples(Date {})?) + } +} diff --git a/crates/nu-cli/src/commands/date/now.rs b/crates/nu-cli/src/commands/date/now.rs index ebe608a0db..235cf24f75 100644 --- a/crates/nu-cli/src/commands/date/now.rs +++ b/crates/nu-cli/src/commands/date/now.rs @@ -48,3 +48,16 @@ pub async fn now( Ok(OutputStream::one(value)) } + +#[cfg(test)] +mod tests { + use super::Date; + use super::ShellError; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + Ok(test_examples(Date {})?) + } +} diff --git a/crates/nu-cli/src/commands/date/utc.rs b/crates/nu-cli/src/commands/date/utc.rs index 4dbaca688c..8a08778c29 100644 --- a/crates/nu-cli/src/commands/date/utc.rs +++ b/crates/nu-cli/src/commands/date/utc.rs @@ -48,3 +48,16 @@ pub async fn utc( Ok(OutputStream::one(value)) } + +#[cfg(test)] +mod tests { + use super::Date; + use super::ShellError; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + Ok(test_examples(Date {})?) + } +} diff --git a/crates/nu-cli/src/commands/debug.rs b/crates/nu-cli/src/commands/debug.rs index f46f79bd81..3b60b22330 100644 --- a/crates/nu-cli/src/commands/debug.rs +++ b/crates/nu-cli/src/commands/debug.rs @@ -55,11 +55,12 @@ async fn debug_value( #[cfg(test)] mod tests { use super::Debug; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Debug {}) + Ok(test_examples(Debug {})?) } } diff --git a/crates/nu-cli/src/commands/default.rs b/crates/nu-cli/src/commands/default.rs index ea52106729..34b63d6a42 100644 --- a/crates/nu-cli/src/commands/default.rs +++ b/crates/nu-cli/src/commands/default.rs @@ -83,11 +83,12 @@ async fn default( #[cfg(test)] mod tests { use super::Default; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Default {}) + Ok(test_examples(Default {})?) } } diff --git a/crates/nu-cli/src/commands/describe.rs b/crates/nu-cli/src/commands/describe.rs index 84d69a1beb..7be6fe6c5d 100644 --- a/crates/nu-cli/src/commands/describe.rs +++ b/crates/nu-cli/src/commands/describe.rs @@ -50,11 +50,12 @@ pub async fn describe( #[cfg(test)] mod tests { use super::Describe; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Describe {}) + Ok(test_examples(Describe {})?) } } diff --git a/crates/nu-cli/src/commands/do_.rs b/crates/nu-cli/src/commands/do_.rs index 388fc4b0d4..966df433a6 100644 --- a/crates/nu-cli/src/commands/do_.rs +++ b/crates/nu-cli/src/commands/do_.rs @@ -117,11 +117,12 @@ async fn do_( #[cfg(test)] mod tests { use super::Do; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Do {}) + Ok(test_examples(Do {})?) } } diff --git a/crates/nu-cli/src/commands/drop.rs b/crates/nu-cli/src/commands/drop.rs index b7d6de7796..da2e2feac9 100644 --- a/crates/nu-cli/src/commands/drop.rs +++ b/crates/nu-cli/src/commands/drop.rs @@ -85,11 +85,12 @@ async fn drop(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Drop {}) + Ok(test_examples(Drop {})?) } } diff --git a/crates/nu-cli/src/commands/du.rs b/crates/nu-cli/src/commands/du.rs index 5827032848..6b88b5a8f4 100644 --- a/crates/nu-cli/src/commands/du.rs +++ b/crates/nu-cli/src/commands/du.rs @@ -427,11 +427,12 @@ impl From for Value { #[cfg(test)] mod tests { use super::Du; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Du {}) + Ok(test_examples(Du {})?) } } diff --git a/crates/nu-cli/src/commands/each/command.rs b/crates/nu-cli/src/commands/each/command.rs index 834ac5b9b5..085062f45a 100644 --- a/crates/nu-cli/src/commands/each/command.rs +++ b/crates/nu-cli/src/commands/each/command.rs @@ -164,11 +164,12 @@ async fn each( #[cfg(test)] mod tests { use super::Each; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Each {}) + Ok(test_examples(Each {})?) } } diff --git a/crates/nu-cli/src/commands/each/group.rs b/crates/nu-cli/src/commands/each/group.rs index 2af3d63864..6de4c01a85 100644 --- a/crates/nu-cli/src/commands/each/group.rs +++ b/crates/nu-cli/src/commands/each/group.rs @@ -123,11 +123,12 @@ pub(crate) fn run_block_on_vec( #[cfg(test)] mod tests { use super::EachGroup; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(EachGroup {}) + Ok(test_examples(EachGroup {})?) } } diff --git a/crates/nu-cli/src/commands/each/window.rs b/crates/nu-cli/src/commands/each/window.rs index 22630b0457..7e3f22faff 100644 --- a/crates/nu-cli/src/commands/each/window.rs +++ b/crates/nu-cli/src/commands/each/window.rs @@ -103,11 +103,12 @@ impl WholeStreamCommand for EachWindow { #[cfg(test)] mod tests { use super::EachWindow; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(EachWindow {}) + Ok(test_examples(EachWindow {})?) } } diff --git a/crates/nu-cli/src/commands/echo.rs b/crates/nu-cli/src/commands/echo.rs index c5910b64ec..98ffaa5d80 100644 --- a/crates/nu-cli/src/commands/echo.rs +++ b/crates/nu-cli/src/commands/echo.rs @@ -162,11 +162,12 @@ impl Iterator for RangeIterator { #[cfg(test)] mod tests { use super::Echo; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Echo {}) + Ok(test_examples(Echo {})?) } } diff --git a/crates/nu-cli/src/commands/enter.rs b/crates/nu-cli/src/commands/enter.rs index f84d4d0186..bc739bab26 100644 --- a/crates/nu-cli/src/commands/enter.rs +++ b/crates/nu-cli/src/commands/enter.rs @@ -191,11 +191,12 @@ async fn enter( #[cfg(test)] mod tests { use super::Enter; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Enter {}) + Ok(test_examples(Enter {})?) } } diff --git a/crates/nu-cli/src/commands/every.rs b/crates/nu-cli/src/commands/every.rs index 1cc47aca29..4810a716dd 100644 --- a/crates/nu-cli/src/commands/every.rs +++ b/crates/nu-cli/src/commands/every.rs @@ -93,11 +93,12 @@ async fn every(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Every {}) + Ok(test_examples(Every {})?) } } diff --git a/crates/nu-cli/src/commands/exit.rs b/crates/nu-cli/src/commands/exit.rs index eff1cf5f4e..2f5fee97c6 100644 --- a/crates/nu-cli/src/commands/exit.rs +++ b/crates/nu-cli/src/commands/exit.rs @@ -63,11 +63,12 @@ pub async fn exit( #[cfg(test)] mod tests { use super::Exit; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Exit {}) + Ok(test_examples(Exit {})?) } } diff --git a/crates/nu-cli/src/commands/first.rs b/crates/nu-cli/src/commands/first.rs index 344ea9acd4..f2e1868ed0 100644 --- a/crates/nu-cli/src/commands/first.rs +++ b/crates/nu-cli/src/commands/first.rs @@ -72,11 +72,12 @@ async fn first(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(First {}) + Ok(test_examples(First {})?) } } diff --git a/crates/nu-cli/src/commands/format.rs b/crates/nu-cli/src/commands/format.rs index f676c1fa2e..43ff65ac9a 100644 --- a/crates/nu-cli/src/commands/format.rs +++ b/crates/nu-cli/src/commands/format.rs @@ -151,11 +151,12 @@ fn format(input: &str) -> Vec { #[cfg(test)] mod tests { use super::Format; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Format {}) + Ok(test_examples(Format {})?) } } diff --git a/crates/nu-cli/src/commands/from.rs b/crates/nu-cli/src/commands/from.rs index e5ee313707..6c1eeb29d1 100644 --- a/crates/nu-cli/src/commands/from.rs +++ b/crates/nu-cli/src/commands/from.rs @@ -35,11 +35,12 @@ impl WholeStreamCommand for From { #[cfg(test)] mod tests { use super::From; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(From {}) + Ok(test_examples(From {})?) } } diff --git a/crates/nu-cli/src/commands/from_csv.rs b/crates/nu-cli/src/commands/from_csv.rs index a4051bc637..30b5eb259f 100644 --- a/crates/nu-cli/src/commands/from_csv.rs +++ b/crates/nu-cli/src/commands/from_csv.rs @@ -109,11 +109,12 @@ async fn from_csv( #[cfg(test)] mod tests { use super::FromCSV; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromCSV {}) + Ok(test_examples(FromCSV {})?) } } diff --git a/crates/nu-cli/src/commands/from_eml.rs b/crates/nu-cli/src/commands/from_eml.rs index a896f291f7..0fedae3a63 100644 --- a/crates/nu-cli/src/commands/from_eml.rs +++ b/crates/nu-cli/src/commands/from_eml.rs @@ -130,11 +130,12 @@ async fn from_eml( #[cfg(test)] mod tests { use super::FromEML; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromEML {}) + Ok(test_examples(FromEML {})?) } } diff --git a/crates/nu-cli/src/commands/from_ics.rs b/crates/nu-cli/src/commands/from_ics.rs index e61ca93e6e..5eb801104b 100644 --- a/crates/nu-cli/src/commands/from_ics.rs +++ b/crates/nu-cli/src/commands/from_ics.rs @@ -249,11 +249,12 @@ fn params_to_value(params: Vec<(String, Vec)>, tag: Tag) -> Value { #[cfg(test)] mod tests { use super::FromIcs; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromIcs {}) + Ok(test_examples(FromIcs {})?) } } diff --git a/crates/nu-cli/src/commands/from_ini.rs b/crates/nu-cli/src/commands/from_ini.rs index 2a9a9799e3..928736dd83 100644 --- a/crates/nu-cli/src/commands/from_ini.rs +++ b/crates/nu-cli/src/commands/from_ini.rs @@ -95,11 +95,12 @@ async fn from_ini( #[cfg(test)] mod tests { use super::FromINI; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromINI {}) + Ok(test_examples(FromINI {})?) } } diff --git a/crates/nu-cli/src/commands/from_json.rs b/crates/nu-cli/src/commands/from_json.rs index fbd10502c1..30da4ba06d 100644 --- a/crates/nu-cli/src/commands/from_json.rs +++ b/crates/nu-cli/src/commands/from_json.rs @@ -144,11 +144,12 @@ async fn from_json( #[cfg(test)] mod tests { use super::FromJSON; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromJSON {}) + Ok(test_examples(FromJSON {})?) } } diff --git a/crates/nu-cli/src/commands/from_ods.rs b/crates/nu-cli/src/commands/from_ods.rs index 76839651bc..1ccf2fe1a8 100644 --- a/crates/nu-cli/src/commands/from_ods.rs +++ b/crates/nu-cli/src/commands/from_ods.rs @@ -102,11 +102,12 @@ async fn from_ods( #[cfg(test)] mod tests { use super::FromODS; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromODS {}) + Ok(test_examples(FromODS {})?) } } diff --git a/crates/nu-cli/src/commands/from_ssv.rs b/crates/nu-cli/src/commands/from_ssv.rs index c0b3a6151a..8849f41ad4 100644 --- a/crates/nu-cli/src/commands/from_ssv.rs +++ b/crates/nu-cli/src/commands/from_ssv.rs @@ -302,7 +302,9 @@ async fn from_ssv( #[cfg(test)] mod tests { + use super::ShellError; use super::*; + fn owned(x: &str, y: &str) -> (String, String) { (String::from(x), String::from(y)) } @@ -504,10 +506,10 @@ mod tests { } #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use super::FromSSV; use crate::examples::test as test_examples; - test_examples(FromSSV {}) + Ok(test_examples(FromSSV {})?) } } diff --git a/crates/nu-cli/src/commands/from_toml.rs b/crates/nu-cli/src/commands/from_toml.rs index e764e9e1df..c1eb438539 100644 --- a/crates/nu-cli/src/commands/from_toml.rs +++ b/crates/nu-cli/src/commands/from_toml.rs @@ -101,11 +101,12 @@ pub async fn from_toml( #[cfg(test)] mod tests { use super::FromTOML; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromTOML {}) + Ok(test_examples(FromTOML {})?) } } diff --git a/crates/nu-cli/src/commands/from_tsv.rs b/crates/nu-cli/src/commands/from_tsv.rs index 98046d7cd3..70b37a4d28 100644 --- a/crates/nu-cli/src/commands/from_tsv.rs +++ b/crates/nu-cli/src/commands/from_tsv.rs @@ -52,11 +52,12 @@ async fn from_tsv( #[cfg(test)] mod tests { use super::FromTSV; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromTSV {}) + Ok(test_examples(FromTSV {})?) } } diff --git a/crates/nu-cli/src/commands/from_url.rs b/crates/nu-cli/src/commands/from_url.rs index fa48eb550f..be06788780 100644 --- a/crates/nu-cli/src/commands/from_url.rs +++ b/crates/nu-cli/src/commands/from_url.rs @@ -64,11 +64,12 @@ async fn from_url( #[cfg(test)] mod tests { use super::FromURL; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromURL {}) + Ok(test_examples(FromURL {})?) } } diff --git a/crates/nu-cli/src/commands/from_vcf.rs b/crates/nu-cli/src/commands/from_vcf.rs index fef9e9c0a4..eee9b8fc3c 100644 --- a/crates/nu-cli/src/commands/from_vcf.rs +++ b/crates/nu-cli/src/commands/from_vcf.rs @@ -104,11 +104,12 @@ fn params_to_value(params: Vec<(String, Vec)>, tag: Tag) -> Value { #[cfg(test)] mod tests { use super::FromVcf; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromVcf {}) + Ok(test_examples(FromVcf {})?) } } diff --git a/crates/nu-cli/src/commands/from_xlsx.rs b/crates/nu-cli/src/commands/from_xlsx.rs index 0236823eef..be1f5a9051 100644 --- a/crates/nu-cli/src/commands/from_xlsx.rs +++ b/crates/nu-cli/src/commands/from_xlsx.rs @@ -102,11 +102,12 @@ async fn from_xlsx( #[cfg(test)] mod tests { use super::FromXLSX; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromXLSX {}) + Ok(test_examples(FromXLSX {})?) } } diff --git a/crates/nu-cli/src/commands/from_xml.rs b/crates/nu-cli/src/commands/from_xml.rs index 5f8ec73c9c..9937cacad9 100644 --- a/crates/nu-cli/src/commands/from_xml.rs +++ b/crates/nu-cli/src/commands/from_xml.rs @@ -135,6 +135,7 @@ async fn from_xml( #[cfg(test)] mod tests { + use super::ShellError; use crate::commands::from_xml; use indexmap::IndexMap; use nu_protocol::{UntaggedValue, Value}; @@ -304,10 +305,10 @@ mod tests { } #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use super::FromXML; use crate::examples::test as test_examples; - test_examples(FromXML {}) + Ok(test_examples(FromXML {})?) } } diff --git a/crates/nu-cli/src/commands/from_yaml.rs b/crates/nu-cli/src/commands/from_yaml.rs index 8ad72630bb..6ddfae8c0c 100644 --- a/crates/nu-cli/src/commands/from_yaml.rs +++ b/crates/nu-cli/src/commands/from_yaml.rs @@ -173,15 +173,16 @@ async fn from_yaml( #[cfg(test)] mod tests { + use super::ShellError; use super::*; use nu_plugin::row; use nu_plugin::test_helpers::value::string; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(FromYAML {}) + Ok(test_examples(FromYAML {})?) } #[test] diff --git a/crates/nu-cli/src/commands/get.rs b/crates/nu-cli/src/commands/get.rs index 1bba16f0a5..df6dc5912e 100644 --- a/crates/nu-cli/src/commands/get.rs +++ b/crates/nu-cli/src/commands/get.rs @@ -267,11 +267,12 @@ pub fn get_column_from_row_error( #[cfg(test)] mod tests { use super::Get; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Get {}) + Ok(test_examples(Get {})?) } } diff --git a/crates/nu-cli/src/commands/group_by.rs b/crates/nu-cli/src/commands/group_by.rs index 9dfd072c7c..9f8f10a3c4 100644 --- a/crates/nu-cli/src/commands/group_by.rs +++ b/crates/nu-cli/src/commands/group_by.rs @@ -7,15 +7,15 @@ use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use nu_value_ext::as_string; -pub struct GroupBy; +pub struct Command; #[derive(Deserialize)] -pub struct GroupByArgs { +pub struct Arguments { grouper: Option, } #[async_trait] -impl WholeStreamCommand for GroupBy { +impl WholeStreamCommand for Command { fn name(&self) -> &str { "group-by" } @@ -40,17 +40,45 @@ impl WholeStreamCommand for GroupBy { group_by(args, registry).await } + #[allow(clippy::unwrap_used)] fn examples(&self) -> Vec { + use nu_data::value::date_naive_from_str as date; + vec![ Example { description: "group items by column named \"type\"", example: r#"ls | group-by type"#, - result: None, - }, - Example { - description: "blocks can be used for generating a grouping key (same as above)", - example: r#"ls | group-by { get type }"#, - result: None, + result: Some(vec![UntaggedValue::row(indexmap! { + "File".to_string() => UntaggedValue::Table(vec![ + UntaggedValue::row(indexmap! { + "modified".to_string() => date("2019-07-23".tagged_unknown()).unwrap().into(), + "name".to_string() => UntaggedValue::string("Andrés.txt").into(), + "type".to_string() => UntaggedValue::string("File").into(), + "chickens".to_string() => UntaggedValue::int(10).into(), + }).into(), + UntaggedValue::row(indexmap! { + "modified".to_string() => date("2019-09-24".tagged_unknown()).unwrap().into(), + "name".to_string() => UntaggedValue::string("Andrés.txt").into(), + "type".to_string() => UntaggedValue::string("File").into(), + "chickens".to_string() => UntaggedValue::int(20).into(), + }).into(), + ]).into(), + "Dir".to_string() => UntaggedValue::Table(vec![ + UntaggedValue::row(indexmap! { + "modified".to_string() => date("2019-07-23".tagged_unknown()).unwrap().into(), + "name".to_string() => UntaggedValue::string("Jonathan").into(), + "type".to_string() => UntaggedValue::string("Dir").into(), + "chickens".to_string() => UntaggedValue::int(5).into(), + }).into(), + UntaggedValue::row(indexmap! { + "modified".to_string() => date("2019-09-24".tagged_unknown()).unwrap().into(), + "name".to_string() => UntaggedValue::string("Yehuda").into(), + "type".to_string() => UntaggedValue::string("Dir").into(), + "chickens".to_string() => UntaggedValue::int(4).into(), + }).into(), + ]).into(), + }) + .into()]), }, Example { description: "you can also group by raw values by leaving out the argument", @@ -75,10 +103,26 @@ impl WholeStreamCommand for GroupBy { .into()]), }, Example { - description: "write pipelines for a more involved grouping key", - example: - "echo [1 3 1 3 2 1 1] | group-by { echo `({{$it}} - 1) % 3` | calc | str from }", - result: None, + description: + "use the block form to generate a grouping key when each row gets processed", + example: "echo [1 3 1 3 2 1 1] | group-by { = ($it - 1) mod 3 }", + result: Some(vec![UntaggedValue::row(indexmap! { + "0".to_string() => UntaggedValue::Table(vec![ + UntaggedValue::int(1).into(), + UntaggedValue::int(1).into(), + UntaggedValue::int(1).into(), + UntaggedValue::int(1).into(), + + ]).into(), + "2".to_string() => UntaggedValue::Table(vec![ + UntaggedValue::int(3).into(), + UntaggedValue::int(3).into(), + ]).into(), + "1".to_string() => UntaggedValue::Table(vec![ + UntaggedValue::int(2).into(), + ]).into(), + }) + .into()]), }, ] } @@ -98,7 +142,7 @@ pub async fn group_by( let head = Arc::new(args.call_info.args.head.clone()); let scope = args.call_info.scope.clone(); let context = Arc::new(EvaluationContext::from_raw(&args, ®istry)); - let (GroupByArgs { grouper }, input) = args.process(®istry).await?; + let (Arguments { grouper }, input) = args.process(®istry).await?; let values: Vec = input.collect().await; let mut keys: Vec> = vec![]; @@ -168,6 +212,14 @@ pub async fn group_by( )); } + let first = values[0].clone(); + + let name = if first.tag.anchor().is_some() { + first.tag + } else { + name + }; + let values = UntaggedValue::table(&values).into_value(&name); let group_value = match group_strategy { @@ -180,9 +232,9 @@ pub async fn group_by( None => as_string(row), }); - nu_data::utils::group(&values, &Some(block), &name) + nu_data::utils::group(&values, &Some(block), name) } - Grouper::ByColumn(column_name) => group(&column_name, &values, name), + Grouper::ByColumn(column_name) => group(&column_name, &values, &name), }; Ok(OutputStream::one(ReturnSuccess::value(group_value?))) @@ -287,12 +339,4 @@ mod tests { Ok(()) } - - #[test] - fn examples_work_as_expected() { - use super::GroupBy; - use crate::examples::test as test_examples; - - test_examples(GroupBy {}) - } } diff --git a/crates/nu-cli/src/commands/group_by_date.rs b/crates/nu-cli/src/commands/group_by_date.rs index 6f3190cd6f..3838a2c56f 100644 --- a/crates/nu-cli/src/commands/group_by_date.rs +++ b/crates/nu-cli/src/commands/group_by_date.rs @@ -141,11 +141,12 @@ pub async fn group_by_date( #[cfg(test)] mod tests { use super::GroupByDate; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(GroupByDate {}) + Ok(test_examples(GroupByDate {})?) } } diff --git a/crates/nu-cli/src/commands/headers.rs b/crates/nu-cli/src/commands/headers.rs index fc3e14d0c9..be93f929ad 100644 --- a/crates/nu-cli/src/commands/headers.rs +++ b/crates/nu-cli/src/commands/headers.rs @@ -116,11 +116,12 @@ pub async fn headers( #[cfg(test)] mod tests { use super::Headers; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Headers {}) + Ok(test_examples(Headers {})?) } } diff --git a/crates/nu-cli/src/commands/help.rs b/crates/nu-cli/src/commands/help.rs index 879e4ee467..231fd2d270 100644 --- a/crates/nu-cli/src/commands/help.rs +++ b/crates/nu-cli/src/commands/help.rs @@ -234,11 +234,12 @@ pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> Str #[cfg(test)] mod tests { use super::Help; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Help {}) + Ok(test_examples(Help {})?) } } diff --git a/crates/nu-cli/src/commands/histogram.rs b/crates/nu-cli/src/commands/histogram.rs index 68f04b1f16..c249dd3875 100644 --- a/crates/nu-cli/src/commands/histogram.rs +++ b/crates/nu-cli/src/commands/histogram.rs @@ -227,11 +227,12 @@ fn splitter( #[cfg(test)] mod tests { use super::Histogram; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Histogram {}) + Ok(test_examples(Histogram {})?) } } diff --git a/crates/nu-cli/src/commands/history.rs b/crates/nu-cli/src/commands/history.rs index fdbf56f436..5b5b963e1f 100644 --- a/crates/nu-cli/src/commands/history.rs +++ b/crates/nu-cli/src/commands/history.rs @@ -79,11 +79,12 @@ fn history(args: CommandArgs, _registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(History {}) + Ok(test_examples(History {})?) } } diff --git a/crates/nu-cli/src/commands/if_.rs b/crates/nu-cli/src/commands/if_.rs index 81afa0ede8..85078d95a0 100644 --- a/crates/nu-cli/src/commands/if_.rs +++ b/crates/nu-cli/src/commands/if_.rs @@ -174,11 +174,12 @@ async fn if_command( #[cfg(test)] mod tests { use super::If; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(If {}) + Ok(test_examples(If {})?) } } diff --git a/crates/nu-cli/src/commands/insert.rs b/crates/nu-cli/src/commands/insert.rs index 69526ed0c8..15cce8a7f9 100644 --- a/crates/nu-cli/src/commands/insert.rs +++ b/crates/nu-cli/src/commands/insert.rs @@ -9,16 +9,18 @@ use nu_protocol::{ use nu_value_ext::ValueExt; use futures::stream::once; -pub struct Insert; +use indexmap::indexmap; + +pub struct Command; #[derive(Deserialize)] -pub struct InsertArgs { +pub struct Arguments { column: ColumnPath, value: Value, } #[async_trait] -impl WholeStreamCommand for Insert { +impl WholeStreamCommand for Command { fn name(&self) -> &str { "insert" } @@ -44,6 +46,28 @@ impl WholeStreamCommand for Insert { ) -> Result { insert(args, registry).await } + + fn examples(&self) -> Vec { + vec![Example { + description: "Insert a column with a value", + example: "echo [[author, commits]; ['Andrés', 1]] | insert branches 5", + result: Some(vec![UntaggedValue::row(indexmap! { + "author".to_string() => Value::from("Andrés"), + "commits".to_string() => UntaggedValue::int(1).into(), + "branches".to_string() => UntaggedValue::int(5).into(), + }) + .into()]), + },Example { + description: "Use in block form for more involved insertion logic", + example: "echo [[author, lucky_number]; ['Yehuda', 4]] | insert success { = $it.lucky_number * 10 }", + result: Some(vec![UntaggedValue::row(indexmap! { + "author".to_string() => Value::from("Yehuda"), + "lucky_number".to_string() => UntaggedValue::int(4).into(), + "success".to_string() => UntaggedValue::int(40).into(), + }) + .into()]), + }] + } } async fn process_row( @@ -135,7 +159,7 @@ async fn insert( let registry = registry.clone(); let scope = raw_args.call_info.scope.clone(); let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); - let (InsertArgs { column, value }, input) = raw_args.process(®istry).await?; + let (Arguments { column, value }, input) = raw_args.process(®istry).await?; let value = Arc::new(value); let column = Arc::new(column); @@ -156,15 +180,3 @@ async fn insert( .flatten() .to_output_stream()) } - -#[cfg(test)] -mod tests { - use super::Insert; - - #[test] - fn examples_work_as_expected() { - use crate::examples::test as test_examples; - - test_examples(Insert {}) - } -} diff --git a/crates/nu-cli/src/commands/into_int.rs b/crates/nu-cli/src/commands/into_int.rs index 24db82f440..1a02da8b5a 100644 --- a/crates/nu-cli/src/commands/into_int.rs +++ b/crates/nu-cli/src/commands/into_int.rs @@ -78,11 +78,12 @@ async fn into_int( #[cfg(test)] mod tests { use super::IntoInt; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(IntoInt {}) + Ok(test_examples(IntoInt {})?) } } diff --git a/crates/nu-cli/src/commands/is_empty.rs b/crates/nu-cli/src/commands/is_empty.rs index 0beef1c80d..c44324c5cc 100644 --- a/crates/nu-cli/src/commands/is_empty.rs +++ b/crates/nu-cli/src/commands/is_empty.rs @@ -207,11 +207,12 @@ async fn is_empty( #[cfg(test)] mod tests { use super::IsEmpty; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(IsEmpty {}) + Ok(test_examples(IsEmpty {})?) } } diff --git a/crates/nu-cli/src/commands/keep/command.rs b/crates/nu-cli/src/commands/keep/command.rs index fc8a6ff2d9..6219dd120a 100644 --- a/crates/nu-cli/src/commands/keep/command.rs +++ b/crates/nu-cli/src/commands/keep/command.rs @@ -74,11 +74,12 @@ async fn keep(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/keep/until.rs b/crates/nu-cli/src/commands/keep/until.rs index db2efb21d1..b23023587e 100644 --- a/crates/nu-cli/src/commands/keep/until.rs +++ b/crates/nu-cli/src/commands/keep/until.rs @@ -101,12 +101,13 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/keep/while_.rs b/crates/nu-cli/src/commands/keep/while_.rs index c41e8b0c53..e738dbce9c 100644 --- a/crates/nu-cli/src/commands/keep/while_.rs +++ b/crates/nu-cli/src/commands/keep/while_.rs @@ -100,12 +100,13 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/kill.rs b/crates/nu-cli/src/commands/kill.rs index de12ebb646..8a02b9625d 100644 --- a/crates/nu-cli/src/commands/kill.rs +++ b/crates/nu-cli/src/commands/kill.rs @@ -121,11 +121,12 @@ async fn kill(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Kill {}) + Ok(test_examples(Kill {})?) } } diff --git a/crates/nu-cli/src/commands/last.rs b/crates/nu-cli/src/commands/last.rs index cf44e870bf..80d9941da0 100644 --- a/crates/nu-cli/src/commands/last.rs +++ b/crates/nu-cli/src/commands/last.rs @@ -83,11 +83,12 @@ async fn last(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Last {}) + Ok(test_examples(Last {})?) } } diff --git a/crates/nu-cli/src/commands/lines.rs b/crates/nu-cli/src/commands/lines.rs index bcc2addcd5..5b6f163493 100644 --- a/crates/nu-cli/src/commands/lines.rs +++ b/crates/nu-cli/src/commands/lines.rs @@ -125,11 +125,12 @@ async fn lines(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Lines {}) + Ok(test_examples(Lines {})?) } } diff --git a/crates/nu-cli/src/commands/ls.rs b/crates/nu-cli/src/commands/ls.rs index 0fc1023645..bb734f39cb 100644 --- a/crates/nu-cli/src/commands/ls.rs +++ b/crates/nu-cli/src/commands/ls.rs @@ -89,11 +89,12 @@ impl WholeStreamCommand for Ls { #[cfg(test)] mod tests { use super::Ls; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Ls {}) + Ok(test_examples(Ls {})?) } } diff --git a/crates/nu-cli/src/commands/math/avg.rs b/crates/nu-cli/src/commands/math/avg.rs index c88b67e59c..6f59d49f52 100644 --- a/crates/nu-cli/src/commands/math/avg.rs +++ b/crates/nu-cli/src/commands/math/avg.rs @@ -188,12 +188,13 @@ pub fn average(values: &[Value], name: &Tag) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/command.rs b/crates/nu-cli/src/commands/math/command.rs index 6451b011f5..1a0dc4310d 100644 --- a/crates/nu-cli/src/commands/math/command.rs +++ b/crates/nu-cli/src/commands/math/command.rs @@ -44,10 +44,10 @@ mod tests { use std::str::FromStr; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } #[test] diff --git a/crates/nu-cli/src/commands/math/eval.rs b/crates/nu-cli/src/commands/math/eval.rs index 19ca87961d..3b6876428a 100644 --- a/crates/nu-cli/src/commands/math/eval.rs +++ b/crates/nu-cli/src/commands/math/eval.rs @@ -100,12 +100,13 @@ pub fn parse>(math_expression: &str, tag: T) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/max.rs b/crates/nu-cli/src/commands/math/max.rs index f4db69b11f..98e9a38e93 100644 --- a/crates/nu-cli/src/commands/math/max.rs +++ b/crates/nu-cli/src/commands/math/max.rs @@ -58,12 +58,13 @@ pub fn maximum(values: &[Value], _name: &Tag) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/median.rs b/crates/nu-cli/src/commands/math/median.rs index 5eca61701d..79791e3bff 100644 --- a/crates/nu-cli/src/commands/math/median.rs +++ b/crates/nu-cli/src/commands/math/median.rs @@ -186,12 +186,13 @@ fn compute_average(values: &[Value], name: impl Into) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/min.rs b/crates/nu-cli/src/commands/math/min.rs index 884e95712d..e15bc79ff0 100644 --- a/crates/nu-cli/src/commands/math/min.rs +++ b/crates/nu-cli/src/commands/math/min.rs @@ -58,12 +58,13 @@ pub fn minimum(values: &[Value], _name: &Tag) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/mode.rs b/crates/nu-cli/src/commands/math/mode.rs index 010bf19a01..2b38695593 100644 --- a/crates/nu-cli/src/commands/math/mode.rs +++ b/crates/nu-cli/src/commands/math/mode.rs @@ -83,12 +83,13 @@ pub fn mode(values: &[Value], name: &Tag) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/product.rs b/crates/nu-cli/src/commands/math/product.rs index f745545d36..1618638fde 100644 --- a/crates/nu-cli/src/commands/math/product.rs +++ b/crates/nu-cli/src/commands/math/product.rs @@ -115,12 +115,13 @@ pub fn product(values: &[Value], name: &Tag) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/stddev.rs b/crates/nu-cli/src/commands/math/stddev.rs index d8866d2230..1975696b5d 100644 --- a/crates/nu-cli/src/commands/math/stddev.rs +++ b/crates/nu-cli/src/commands/math/stddev.rs @@ -141,12 +141,13 @@ pub fn compute_stddev(values: &[Value], n: usize, name: &Tag) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/sum.rs b/crates/nu-cli/src/commands/math/sum.rs index 6005be35f7..c2da0dbb65 100644 --- a/crates/nu-cli/src/commands/math/sum.rs +++ b/crates/nu-cli/src/commands/math/sum.rs @@ -125,12 +125,13 @@ pub fn summation(values: &[Value], name: &Tag) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/math/variance.rs b/crates/nu-cli/src/commands/math/variance.rs index c8bfa60021..eabe23b54c 100644 --- a/crates/nu-cli/src/commands/math/variance.rs +++ b/crates/nu-cli/src/commands/math/variance.rs @@ -240,12 +240,13 @@ pub fn compute_variance(values: &[Value], n: usize, name: &Tag) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/merge.rs b/crates/nu-cli/src/commands/merge.rs index 64b11e9712..7151e8f83b 100644 --- a/crates/nu-cli/src/commands/merge.rs +++ b/crates/nu-cli/src/commands/merge.rs @@ -101,11 +101,12 @@ async fn merge( #[cfg(test)] mod tests { use super::Merge; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Merge {}) + Ok(test_examples(Merge {})?) } } diff --git a/crates/nu-cli/src/commands/mkdir.rs b/crates/nu-cli/src/commands/mkdir.rs index b003a36e51..556fa8adb0 100644 --- a/crates/nu-cli/src/commands/mkdir.rs +++ b/crates/nu-cli/src/commands/mkdir.rs @@ -60,11 +60,12 @@ async fn mkdir(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Mkdir {}) + Ok(test_examples(Mkdir {})?) } } diff --git a/crates/nu-cli/src/commands/move_/column.rs b/crates/nu-cli/src/commands/move_/column.rs index 1d91d9e4f0..6457a39b00 100644 --- a/crates/nu-cli/src/commands/move_/column.rs +++ b/crates/nu-cli/src/commands/move_/column.rs @@ -324,12 +324,13 @@ fn move_before( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/move_/command.rs b/crates/nu-cli/src/commands/move_/command.rs index f563a98c74..d71ccde0e0 100644 --- a/crates/nu-cli/src/commands/move_/command.rs +++ b/crates/nu-cli/src/commands/move_/command.rs @@ -36,11 +36,12 @@ impl WholeStreamCommand for Command { #[cfg(test)] mod tests { use super::Command; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/move_/mv.rs b/crates/nu-cli/src/commands/move_/mv.rs index b442a6cb50..2555219bf9 100644 --- a/crates/nu-cli/src/commands/move_/mv.rs +++ b/crates/nu-cli/src/commands/move_/mv.rs @@ -79,11 +79,12 @@ async fn mv(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Mv {}) + Ok(test_examples(Mv {})?) } } diff --git a/crates/nu-cli/src/commands/next.rs b/crates/nu-cli/src/commands/next.rs index b008b890dc..f40d00945f 100644 --- a/crates/nu-cli/src/commands/next.rs +++ b/crates/nu-cli/src/commands/next.rs @@ -35,11 +35,12 @@ fn next(_args: CommandArgs, _registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Next {}) + Ok(test_examples(Next {})?) } } diff --git a/crates/nu-cli/src/commands/nth.rs b/crates/nu-cli/src/commands/nth.rs index 8941af9c88..cbba36490a 100644 --- a/crates/nu-cli/src/commands/nth.rs +++ b/crates/nu-cli/src/commands/nth.rs @@ -96,11 +96,12 @@ async fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Nth {}) + Ok(test_examples(Nth {})?) } } diff --git a/crates/nu-cli/src/commands/nu/plugin.rs b/crates/nu-cli/src/commands/nu/plugin.rs index ecf38b5cfb..0592d2efcc 100644 --- a/crates/nu-cli/src/commands/nu/plugin.rs +++ b/crates/nu-cli/src/commands/nu/plugin.rs @@ -113,12 +113,13 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/open.rs b/crates/nu-cli/src/commands/open.rs index 102ea00cfe..e14edea18a 100644 --- a/crates/nu-cli/src/commands/open.rs +++ b/crates/nu-cli/src/commands/open.rs @@ -243,11 +243,12 @@ pub async fn fetch( #[cfg(test)] mod tests { use super::Open; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Open {}) + Ok(test_examples(Open {})?) } } diff --git a/crates/nu-cli/src/commands/parse/command.rs b/crates/nu-cli/src/commands/parse/command.rs index 75364f2c9b..9359522069 100644 --- a/crates/nu-cli/src/commands/parse/command.rs +++ b/crates/nu-cli/src/commands/parse/command.rs @@ -176,11 +176,12 @@ fn column_names(regex: &Regex) -> Vec { #[cfg(test)] mod tests { use super::Command; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/path/basename.rs b/crates/nu-cli/src/commands/path/basename.rs index 930c17a6f2..eaa4b1b7b8 100644 --- a/crates/nu-cli/src/commands/path/basename.rs +++ b/crates/nu-cli/src/commands/path/basename.rs @@ -51,11 +51,12 @@ fn action(path: &Path) -> UntaggedValue { #[cfg(test)] mod tests { use super::PathBasename; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(PathBasename {}) + Ok(test_examples(PathBasename {})?) } } diff --git a/crates/nu-cli/src/commands/path/command.rs b/crates/nu-cli/src/commands/path/command.rs index 17d3452e09..0e351a3037 100644 --- a/crates/nu-cli/src/commands/path/command.rs +++ b/crates/nu-cli/src/commands/path/command.rs @@ -36,11 +36,12 @@ impl WholeStreamCommand for Path { #[cfg(test)] mod tests { use super::Path; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Path {}) + Ok(test_examples(Path {})?) } } diff --git a/crates/nu-cli/src/commands/path/dirname.rs b/crates/nu-cli/src/commands/path/dirname.rs index 84e0328f35..0788fe2422 100644 --- a/crates/nu-cli/src/commands/path/dirname.rs +++ b/crates/nu-cli/src/commands/path/dirname.rs @@ -50,11 +50,12 @@ fn action(path: &Path) -> UntaggedValue { #[cfg(test)] mod tests { use super::PathDirname; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(PathDirname {}) + Ok(test_examples(PathDirname {})?) } } diff --git a/crates/nu-cli/src/commands/path/exists.rs b/crates/nu-cli/src/commands/path/exists.rs index 0b0aed80ce..d6926c77fe 100644 --- a/crates/nu-cli/src/commands/path/exists.rs +++ b/crates/nu-cli/src/commands/path/exists.rs @@ -43,3 +43,16 @@ impl WholeStreamCommand for PathExists { fn action(path: &Path) -> UntaggedValue { UntaggedValue::boolean(path.exists()) } + +#[cfg(test)] +mod tests { + use super::PathExists; + use super::ShellError; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + Ok(test_examples(PathExists {})?) + } +} diff --git a/crates/nu-cli/src/commands/path/expand.rs b/crates/nu-cli/src/commands/path/expand.rs index 75ae5d211d..fbcb7b76fa 100644 --- a/crates/nu-cli/src/commands/path/expand.rs +++ b/crates/nu-cli/src/commands/path/expand.rs @@ -2,7 +2,7 @@ use super::{operate, DefaultArguments}; use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use std::path::Path; pub struct PathExpand; @@ -35,7 +35,8 @@ impl WholeStreamCommand for PathExpand { vec![Example { description: "Expand relative directories", example: "echo '/home/joe/foo/../bar' | path expand", - result: Some(vec![Value::from("/home/joe/bar")]), + result: None, + //Some(vec![Value::from("/home/joe/bar")]), }] } } @@ -49,3 +50,16 @@ fn action(path: &Path) -> UntaggedValue { Err(_) => ps.to_string(), }) } + +#[cfg(test)] +mod tests { + use super::PathExpand; + use super::ShellError; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + Ok(test_examples(PathExpand {})?) + } +} diff --git a/crates/nu-cli/src/commands/path/extension.rs b/crates/nu-cli/src/commands/path/extension.rs index 2e99c88a01..fd36abf1e3 100644 --- a/crates/nu-cli/src/commands/path/extension.rs +++ b/crates/nu-cli/src/commands/path/extension.rs @@ -58,11 +58,12 @@ fn action(path: &Path) -> UntaggedValue { #[cfg(test)] mod tests { use super::PathExtension; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(PathExtension {}) + Ok(test_examples(PathExtension {})?) } } diff --git a/crates/nu-cli/src/commands/path/filestem.rs b/crates/nu-cli/src/commands/path/filestem.rs index 6facde0628..4a0caf1746 100644 --- a/crates/nu-cli/src/commands/path/filestem.rs +++ b/crates/nu-cli/src/commands/path/filestem.rs @@ -51,11 +51,12 @@ fn action(path: &Path) -> UntaggedValue { #[cfg(test)] mod tests { use super::PathFilestem; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(PathFilestem {}) + Ok(test_examples(PathFilestem {})?) } } diff --git a/crates/nu-cli/src/commands/path/type.rs b/crates/nu-cli/src/commands/path/type.rs index d044af22ba..aaae3d4fad 100644 --- a/crates/nu-cli/src/commands/path/type.rs +++ b/crates/nu-cli/src/commands/path/type.rs @@ -52,11 +52,12 @@ fn action(path: &Path) -> UntaggedValue { #[cfg(test)] mod tests { use super::PathType; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(PathType {}) + Ok(test_examples(PathType {})?) } } diff --git a/crates/nu-cli/src/commands/pivot.rs b/crates/nu-cli/src/commands/pivot.rs index 30d56c5a7c..07fbb03468 100644 --- a/crates/nu-cli/src/commands/pivot.rs +++ b/crates/nu-cli/src/commands/pivot.rs @@ -155,11 +155,12 @@ pub async fn pivot( #[cfg(test)] mod tests { use super::Pivot; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Pivot {}) + Ok(test_examples(Pivot {})?) } } diff --git a/crates/nu-cli/src/commands/prepend.rs b/crates/nu-cli/src/commands/prepend.rs index c7ef5118c4..78795e9a2e 100644 --- a/crates/nu-cli/src/commands/prepend.rs +++ b/crates/nu-cli/src/commands/prepend.rs @@ -67,11 +67,12 @@ async fn prepend( #[cfg(test)] mod tests { use super::Prepend; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Prepend {}) + Ok(test_examples(Prepend {})?) } } diff --git a/crates/nu-cli/src/commands/prev.rs b/crates/nu-cli/src/commands/prev.rs index 95da241d1d..cb103e8c4b 100644 --- a/crates/nu-cli/src/commands/prev.rs +++ b/crates/nu-cli/src/commands/prev.rs @@ -36,11 +36,12 @@ fn previous(_args: CommandArgs, _registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Previous {}) + Ok(test_examples(Previous {})?) } } diff --git a/crates/nu-cli/src/commands/pwd.rs b/crates/nu-cli/src/commands/pwd.rs index b1d27f27ec..a9af376ecf 100644 --- a/crates/nu-cli/src/commands/pwd.rs +++ b/crates/nu-cli/src/commands/pwd.rs @@ -50,11 +50,12 @@ pub async fn pwd( #[cfg(test)] mod tests { use super::Pwd; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Pwd {}) + Ok(test_examples(Pwd {})?) } } diff --git a/crates/nu-cli/src/commands/random/bool.rs b/crates/nu-cli/src/commands/random/bool.rs index 147bf3b911..bae68d0316 100644 --- a/crates/nu-cli/src/commands/random/bool.rs +++ b/crates/nu-cli/src/commands/random/bool.rs @@ -86,12 +86,13 @@ pub async fn bool_command( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/random/dice.rs b/crates/nu-cli/src/commands/random/dice.rs index d43414cf9b..dddb8f3e08 100644 --- a/crates/nu-cli/src/commands/random/dice.rs +++ b/crates/nu-cli/src/commands/random/dice.rs @@ -92,12 +92,13 @@ pub async fn dice( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/random/integer.rs b/crates/nu-cli/src/commands/random/integer.rs index 8988e16d3a..76c9560085 100644 --- a/crates/nu-cli/src/commands/random/integer.rs +++ b/crates/nu-cli/src/commands/random/integer.rs @@ -101,12 +101,13 @@ pub async fn integer( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/random/uuid.rs b/crates/nu-cli/src/commands/random/uuid.rs index 84372b9df8..629a0746d8 100644 --- a/crates/nu-cli/src/commands/random/uuid.rs +++ b/crates/nu-cli/src/commands/random/uuid.rs @@ -48,12 +48,13 @@ pub async fn uuid( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/range.rs b/crates/nu-cli/src/commands/range.rs index bb65d4a7a9..88253d13aa 100644 --- a/crates/nu-cli/src/commands/range.rs +++ b/crates/nu-cli/src/commands/range.rs @@ -72,11 +72,12 @@ async fn range(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Range {}) + Ok(test_examples(Range {})?) } } diff --git a/crates/nu-cli/src/commands/reduce.rs b/crates/nu-cli/src/commands/reduce.rs index c85b7d3186..d4c775817c 100644 --- a/crates/nu-cli/src/commands/reduce.rs +++ b/crates/nu-cli/src/commands/reduce.rs @@ -185,11 +185,12 @@ async fn reduce( #[cfg(test)] mod tests { use super::Reduce; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Reduce {}) + Ok(test_examples(Reduce {})?) } } diff --git a/crates/nu-cli/src/commands/reject.rs b/crates/nu-cli/src/commands/reject.rs index d0d1031dee..1fe98dc672 100644 --- a/crates/nu-cli/src/commands/reject.rs +++ b/crates/nu-cli/src/commands/reject.rs @@ -65,11 +65,12 @@ async fn reject(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Reject {}) + Ok(test_examples(Reject {})?) } } diff --git a/crates/nu-cli/src/commands/rename.rs b/crates/nu-cli/src/commands/rename.rs index 51d95715a7..be6bd84886 100644 --- a/crates/nu-cli/src/commands/rename.rs +++ b/crates/nu-cli/src/commands/rename.rs @@ -117,11 +117,12 @@ pub async fn rename( #[cfg(test)] mod tests { use super::Rename; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Rename {}) + Ok(test_examples(Rename {})?) } } diff --git a/crates/nu-cli/src/commands/reverse.rs b/crates/nu-cli/src/commands/reverse.rs index 0f01650bae..f45cde7118 100644 --- a/crates/nu-cli/src/commands/reverse.rs +++ b/crates/nu-cli/src/commands/reverse.rs @@ -58,11 +58,12 @@ async fn reverse( #[cfg(test)] mod tests { use super::Reverse; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Reverse {}) + Ok(test_examples(Reverse {})?) } } diff --git a/crates/nu-cli/src/commands/rm.rs b/crates/nu-cli/src/commands/rm.rs index 7986c43cff..122538462c 100644 --- a/crates/nu-cli/src/commands/rm.rs +++ b/crates/nu-cli/src/commands/rm.rs @@ -100,11 +100,12 @@ async fn rm(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Remove {}) + Ok(test_examples(Remove {})?) } } diff --git a/crates/nu-cli/src/commands/save.rs b/crates/nu-cli/src/commands/save.rs index 864d3f4627..e27c1ecfe8 100644 --- a/crates/nu-cli/src/commands/save.rs +++ b/crates/nu-cli/src/commands/save.rs @@ -280,11 +280,12 @@ fn string_from(input: &[Value]) -> String { #[cfg(test)] mod tests { use super::Save; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Save {}) + Ok(test_examples(Save {})?) } } diff --git a/crates/nu-cli/src/commands/select.rs b/crates/nu-cli/src/commands/select.rs index f801aa4b43..87922962b8 100644 --- a/crates/nu-cli/src/commands/select.rs +++ b/crates/nu-cli/src/commands/select.rs @@ -175,11 +175,12 @@ async fn select(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Select {}) + Ok(test_examples(Select {})?) } } diff --git a/crates/nu-cli/src/commands/shells.rs b/crates/nu-cli/src/commands/shells.rs index 9468465b32..f950165e1e 100644 --- a/crates/nu-cli/src/commands/shells.rs +++ b/crates/nu-cli/src/commands/shells.rs @@ -52,12 +52,13 @@ fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Shells {}) + Ok(test_examples(Shells {})?) } } diff --git a/crates/nu-cli/src/commands/shuffle.rs b/crates/nu-cli/src/commands/shuffle.rs index 76cfb3313e..5b22a05b81 100644 --- a/crates/nu-cli/src/commands/shuffle.rs +++ b/crates/nu-cli/src/commands/shuffle.rs @@ -42,12 +42,13 @@ async fn shuffle( #[cfg(test)] mod tests { + use super::ShellError; use super::Shuffle; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Shuffle {}) + Ok(test_examples(Shuffle {})?) } } diff --git a/crates/nu-cli/src/commands/size.rs b/crates/nu-cli/src/commands/size.rs index 1fa033a634..4ffff1271a 100644 --- a/crates/nu-cli/src/commands/size.rs +++ b/crates/nu-cli/src/commands/size.rs @@ -119,12 +119,13 @@ fn count(contents: &str, tag: impl Into) -> Value { #[cfg(test)] mod tests { + use super::ShellError; use super::Size; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Size {}) + Ok(test_examples(Size {})?) } } diff --git a/crates/nu-cli/src/commands/skip/command.rs b/crates/nu-cli/src/commands/skip/command.rs index 68adf21bda..dffb4a5fc1 100644 --- a/crates/nu-cli/src/commands/skip/command.rs +++ b/crates/nu-cli/src/commands/skip/command.rs @@ -61,11 +61,12 @@ async fn skip(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/skip/until.rs b/crates/nu-cli/src/commands/skip/until.rs index 4e2bd0810d..0498ee1426 100644 --- a/crates/nu-cli/src/commands/skip/until.rs +++ b/crates/nu-cli/src/commands/skip/until.rs @@ -100,12 +100,13 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/skip/while_.rs b/crates/nu-cli/src/commands/skip/while_.rs index b10c660943..cdfe0dda09 100644 --- a/crates/nu-cli/src/commands/skip/while_.rs +++ b/crates/nu-cli/src/commands/skip/while_.rs @@ -101,12 +101,13 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/sleep.rs b/crates/nu-cli/src/commands/sleep.rs index 48ae88599c..01ad1f503e 100644 --- a/crates/nu-cli/src/commands/sleep.rs +++ b/crates/nu-cli/src/commands/sleep.rs @@ -178,18 +178,21 @@ impl Future for SleepFuture { #[cfg(test)] mod tests { use super::Sleep; + use nu_errors::ShellError; use std::time::Instant; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; let start = Instant::now(); - test_examples(Sleep {}); + let results = test_examples(Sleep {}); let elapsed = start.elapsed(); println!("{:?}", elapsed); // only examples with actual output are run assert!(elapsed >= std::time::Duration::from_secs(1)); assert!(elapsed < std::time::Duration::from_secs(2)); + + Ok(results?) } } diff --git a/crates/nu-cli/src/commands/sort_by.rs b/crates/nu-cli/src/commands/sort_by.rs index af096cce03..3c2ae431ce 100644 --- a/crates/nu-cli/src/commands/sort_by.rs +++ b/crates/nu-cli/src/commands/sort_by.rs @@ -194,12 +194,13 @@ pub fn sort( #[cfg(test)] mod tests { + use super::ShellError; use super::SortBy; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SortBy {}) + Ok(test_examples(SortBy {})?) } } diff --git a/crates/nu-cli/src/commands/split/chars.rs b/crates/nu-cli/src/commands/split/chars.rs index 1feae2934f..5a7ba0c2e5 100644 --- a/crates/nu-cli/src/commands/split/chars.rs +++ b/crates/nu-cli/src/commands/split/chars.rs @@ -73,12 +73,13 @@ async fn split_chars( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/split/column.rs b/crates/nu-cli/src/commands/split/column.rs index 6c419f3d30..10e7f6f531 100644 --- a/crates/nu-cli/src/commands/split/column.rs +++ b/crates/nu-cli/src/commands/split/column.rs @@ -116,12 +116,13 @@ async fn split_column( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/split/command.rs b/crates/nu-cli/src/commands/split/command.rs index 0164ea23f7..9598b3f6ed 100644 --- a/crates/nu-cli/src/commands/split/command.rs +++ b/crates/nu-cli/src/commands/split/command.rs @@ -36,11 +36,12 @@ impl WholeStreamCommand for Command { #[cfg(test)] mod tests { use super::Command; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/split/row.rs b/crates/nu-cli/src/commands/split/row.rs index df73bcdd3d..7629ce8d2d 100644 --- a/crates/nu-cli/src/commands/split/row.rs +++ b/crates/nu-cli/src/commands/split/row.rs @@ -85,12 +85,13 @@ async fn split_row( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/split_by.rs b/crates/nu-cli/src/commands/split_by.rs index 52b419354a..c73daa076e 100644 --- a/crates/nu-cli/src/commands/split_by.rs +++ b/crates/nu-cli/src/commands/split_by.rs @@ -100,6 +100,7 @@ pub fn split( #[cfg(test)] mod tests { use super::split; + use super::ShellError; use nu_data::utils::helpers::{committers_grouped_by_date, date, int, row, string, table}; use nu_protocol::UntaggedValue; use nu_source::*; @@ -168,10 +169,10 @@ mod tests { } #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use super::SplitBy; use crate::examples::test as test_examples; - test_examples(SplitBy {}) + Ok(test_examples(SplitBy {})?) } } diff --git a/crates/nu-cli/src/commands/str_/capitalize.rs b/crates/nu-cli/src/commands/str_/capitalize.rs index f487a5dec6..aea8bcb5e0 100644 --- a/crates/nu-cli/src/commands/str_/capitalize.rs +++ b/crates/nu-cli/src/commands/str_/capitalize.rs @@ -110,15 +110,16 @@ fn action(input: &Value, tag: impl Into) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::{action, SubCommand}; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/case/camel_case.rs b/crates/nu-cli/src/commands/str_/case/camel_case.rs index 30440d81e5..c10d9c1c84 100644 --- a/crates/nu-cli/src/commands/str_/case/camel_case.rs +++ b/crates/nu-cli/src/commands/str_/case/camel_case.rs @@ -43,16 +43,17 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::{to_camel_case, SubCommand}; use crate::commands::str_::case::action; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/case/kebab_case.rs b/crates/nu-cli/src/commands/str_/case/kebab_case.rs index c43b5266a3..4688704dfc 100644 --- a/crates/nu-cli/src/commands/str_/case/kebab_case.rs +++ b/crates/nu-cli/src/commands/str_/case/kebab_case.rs @@ -43,16 +43,17 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::{to_kebab_case, SubCommand}; use crate::commands::str_::case::action; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/case/pascal_case.rs b/crates/nu-cli/src/commands/str_/case/pascal_case.rs index 113b77ba1e..fe25f7a7ff 100644 --- a/crates/nu-cli/src/commands/str_/case/pascal_case.rs +++ b/crates/nu-cli/src/commands/str_/case/pascal_case.rs @@ -43,16 +43,17 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::{to_pascal_case, SubCommand}; use crate::commands::str_::case::action; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs b/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs index 726c92b7c2..afb76c525b 100644 --- a/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs +++ b/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs @@ -43,16 +43,17 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::{to_screaming_snake_case, SubCommand}; use crate::commands::str_::case::action; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/case/snake_case.rs b/crates/nu-cli/src/commands/str_/case/snake_case.rs index cb6bfcf422..849691fcbd 100644 --- a/crates/nu-cli/src/commands/str_/case/snake_case.rs +++ b/crates/nu-cli/src/commands/str_/case/snake_case.rs @@ -43,16 +43,17 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::{to_snake_case, SubCommand}; use crate::commands::str_::case::action; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/collect.rs b/crates/nu-cli/src/commands/str_/collect.rs index bc68c22ed8..d3e3ce9a59 100644 --- a/crates/nu-cli/src/commands/str_/collect.rs +++ b/crates/nu-cli/src/commands/str_/collect.rs @@ -66,12 +66,13 @@ pub async fn collect( #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/str_/command.rs b/crates/nu-cli/src/commands/str_/command.rs index 210ece0d6e..dfdd02ff0f 100644 --- a/crates/nu-cli/src/commands/str_/command.rs +++ b/crates/nu-cli/src/commands/str_/command.rs @@ -39,11 +39,12 @@ impl WholeStreamCommand for Command { #[cfg(test)] mod tests { use super::Command; + use super::ShellError; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Command {}) + Ok(test_examples(Command {})?) } } diff --git a/crates/nu-cli/src/commands/str_/contains.rs b/crates/nu-cli/src/commands/str_/contains.rs index 1975120362..74b011ba4b 100644 --- a/crates/nu-cli/src/commands/str_/contains.rs +++ b/crates/nu-cli/src/commands/str_/contains.rs @@ -128,16 +128,17 @@ fn action( #[cfg(test)] mod tests { + use super::ShellError; use super::{action, SubCommand}; use nu_plugin::test_helpers::value::string; use nu_protocol::UntaggedValue; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/downcase.rs b/crates/nu-cli/src/commands/str_/downcase.rs index 3955acce7f..8986218c80 100644 --- a/crates/nu-cli/src/commands/str_/downcase.rs +++ b/crates/nu-cli/src/commands/str_/downcase.rs @@ -98,15 +98,16 @@ fn action(input: &Value, tag: impl Into) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::{action, SubCommand}; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/ends_with.rs b/crates/nu-cli/src/commands/str_/ends_with.rs index 02d0a1becf..65a405fee8 100644 --- a/crates/nu-cli/src/commands/str_/ends_with.rs +++ b/crates/nu-cli/src/commands/str_/ends_with.rs @@ -102,16 +102,17 @@ fn action(input: &Value, pattern: &str, tag: impl Into) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/find_replace.rs b/crates/nu-cli/src/commands/str_/find_replace.rs index 80258ea955..e229e4e990 100644 --- a/crates/nu-cli/src/commands/str_/find_replace.rs +++ b/crates/nu-cli/src/commands/str_/find_replace.rs @@ -147,15 +147,16 @@ fn action( #[cfg(test)] mod tests { + use super::ShellError; use super::{action, FindReplace, SubCommand}; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/from.rs b/crates/nu-cli/src/commands/str_/from.rs index c1ee17d9bf..13f008f851 100644 --- a/crates/nu-cli/src/commands/str_/from.rs +++ b/crates/nu-cli/src/commands/str_/from.rs @@ -244,12 +244,13 @@ fn round_decimal(decimal: &BigDecimal, mut digits: u64) -> BigDecimal { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/str_/index_of.rs b/crates/nu-cli/src/commands/str_/index_of.rs index dcb8f38e56..1f1b7e8c84 100644 --- a/crates/nu-cli/src/commands/str_/index_of.rs +++ b/crates/nu-cli/src/commands/str_/index_of.rs @@ -246,16 +246,17 @@ fn process_range(input: &Value, range: &Value) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/length.rs b/crates/nu-cli/src/commands/str_/length.rs index 1cd22434a9..361568d592 100644 --- a/crates/nu-cli/src/commands/str_/length.rs +++ b/crates/nu-cli/src/commands/str_/length.rs @@ -54,12 +54,13 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/str_/lpad.rs b/crates/nu-cli/src/commands/str_/lpad.rs index feca5e6ce4..9db60c1192 100644 --- a/crates/nu-cli/src/commands/str_/lpad.rs +++ b/crates/nu-cli/src/commands/str_/lpad.rs @@ -144,15 +144,16 @@ fn action( #[cfg(test)] mod tests { use super::{action, SubCommand}; + use nu_errors::ShellError; use nu_plugin::test_helpers::value::string; use nu_protocol::UntaggedValue; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/reverse.rs b/crates/nu-cli/src/commands/str_/reverse.rs index fa9a3dc8a7..d312a724b5 100644 --- a/crates/nu-cli/src/commands/str_/reverse.rs +++ b/crates/nu-cli/src/commands/str_/reverse.rs @@ -47,12 +47,13 @@ impl WholeStreamCommand for SubCommand { #[cfg(test)] mod tests { + use super::ShellError; use super::SubCommand; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } } diff --git a/crates/nu-cli/src/commands/str_/rpad.rs b/crates/nu-cli/src/commands/str_/rpad.rs index 39c318ed4e..85c13f0993 100644 --- a/crates/nu-cli/src/commands/str_/rpad.rs +++ b/crates/nu-cli/src/commands/str_/rpad.rs @@ -144,15 +144,16 @@ fn action( #[cfg(test)] mod tests { use super::{action, SubCommand}; + use nu_errors::ShellError; use nu_plugin::test_helpers::value::string; use nu_protocol::UntaggedValue; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/set.rs b/crates/nu-cli/src/commands/str_/set.rs index e45c78277e..576209d764 100644 --- a/crates/nu-cli/src/commands/str_/set.rs +++ b/crates/nu-cli/src/commands/str_/set.rs @@ -99,15 +99,16 @@ fn action(_input: &Value, options: &Replace, tag: impl Into) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/starts_with.rs b/crates/nu-cli/src/commands/str_/starts_with.rs index 4ab33444b8..5eacdfb00e 100644 --- a/crates/nu-cli/src/commands/str_/starts_with.rs +++ b/crates/nu-cli/src/commands/str_/starts_with.rs @@ -102,16 +102,17 @@ fn action(input: &Value, pattern: &str, tag: impl Into) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/substring.rs b/crates/nu-cli/src/commands/str_/substring.rs index 795ca077d8..c4113e9b7f 100644 --- a/crates/nu-cli/src/commands/str_/substring.rs +++ b/crates/nu-cli/src/commands/str_/substring.rs @@ -283,15 +283,16 @@ fn process_arguments(range: Value, name: impl Into) -> Result<(isize, isize #[cfg(test)] mod tests { + use super::ShellError; use super::{action, SubCommand, Substring}; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } struct Expectation<'a> { diff --git a/crates/nu-cli/src/commands/str_/to_datetime.rs b/crates/nu-cli/src/commands/str_/to_datetime.rs index 4adce2add6..ef871082de 100644 --- a/crates/nu-cli/src/commands/str_/to_datetime.rs +++ b/crates/nu-cli/src/commands/str_/to_datetime.rs @@ -174,16 +174,17 @@ fn action( #[cfg(test)] mod tests { + use super::ShellError; use super::{action, DatetimeFormat, SubCommand}; use nu_plugin::test_helpers::value::string; use nu_protocol::{Primitive, UntaggedValue}; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/to_decimal.rs b/crates/nu-cli/src/commands/str_/to_decimal.rs index ce8e06c12e..9ad4b2fc5d 100644 --- a/crates/nu-cli/src/commands/str_/to_decimal.rs +++ b/crates/nu-cli/src/commands/str_/to_decimal.rs @@ -112,15 +112,16 @@ fn action(input: &Value, tag: impl Into) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::{action, SubCommand}; use nu_plugin::test_helpers::value::{decimal_from_float, string}; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/to_integer.rs b/crates/nu-cli/src/commands/str_/to_integer.rs index 6f8a887c37..86af40c3e4 100644 --- a/crates/nu-cli/src/commands/str_/to_integer.rs +++ b/crates/nu-cli/src/commands/str_/to_integer.rs @@ -112,15 +112,16 @@ fn action(input: &Value, tag: impl Into) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::{action, SubCommand}; use nu_plugin::test_helpers::value::{int, string}; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs b/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs index 2c099f136d..a47ac1d33d 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs @@ -62,6 +62,7 @@ fn trim(s: &str, char_: Option) -> String { #[cfg(test)] mod tests { + use super::ShellError; use super::{trim, SubCommand}; use crate::commands::str_::trim::{action, ActionMode}; use nu_plugin::{ @@ -71,10 +72,10 @@ mod tests { use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/trim/trim_left.rs b/crates/nu-cli/src/commands/str_/trim/trim_left.rs index 29400f58e8..34147cff34 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_left.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_left.rs @@ -63,6 +63,7 @@ fn trim_left(s: &str, char_: Option) -> String { #[cfg(test)] mod tests { + use super::ShellError; use super::{trim_left, SubCommand}; use crate::commands::str_::trim::{action, ActionMode}; use nu_plugin::{ @@ -72,10 +73,10 @@ mod tests { use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/trim/trim_right.rs b/crates/nu-cli/src/commands/str_/trim/trim_right.rs index 203562f02f..3cb22d606e 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_right.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_right.rs @@ -63,6 +63,7 @@ fn trim_right(s: &str, char_: Option) -> String { #[cfg(test)] mod tests { + use super::ShellError; use super::{trim_right, SubCommand}; use crate::commands::str_::trim::{action, ActionMode}; use nu_plugin::{ @@ -72,10 +73,10 @@ mod tests { use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/str_/upcase.rs b/crates/nu-cli/src/commands/str_/upcase.rs index 5b8e0160d8..37d812c74f 100644 --- a/crates/nu-cli/src/commands/str_/upcase.rs +++ b/crates/nu-cli/src/commands/str_/upcase.rs @@ -98,15 +98,16 @@ fn action(input: &Value, tag: impl Into) -> Result { #[cfg(test)] mod tests { + use super::ShellError; use super::{action, SubCommand}; use nu_plugin::test_helpers::value::string; use nu_source::Tag; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(SubCommand {}) + Ok(test_examples(SubCommand {})?) } #[test] diff --git a/crates/nu-cli/src/commands/table/command.rs b/crates/nu-cli/src/commands/table/command.rs index 0d21275861..f27dd012cb 100644 --- a/crates/nu-cli/src/commands/table/command.rs +++ b/crates/nu-cli/src/commands/table/command.rs @@ -252,3 +252,16 @@ async fn table( Ok(OutputStream::empty()) } + +#[cfg(test)] +mod tests { + use super::Command; + use super::ShellError; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + Ok(test_examples(Command {})?) + } +} diff --git a/crates/nu-cli/src/commands/tags.rs b/crates/nu-cli/src/commands/tags.rs index 06ea54f33e..f9cdeccede 100644 --- a/crates/nu-cli/src/commands/tags.rs +++ b/crates/nu-cli/src/commands/tags.rs @@ -59,12 +59,13 @@ fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Tags {}) + Ok(test_examples(Tags {})?) } } diff --git a/crates/nu-cli/src/commands/to.rs b/crates/nu-cli/src/commands/to.rs index 5c35d945e0..44283dabac 100644 --- a/crates/nu-cli/src/commands/to.rs +++ b/crates/nu-cli/src/commands/to.rs @@ -35,12 +35,13 @@ impl WholeStreamCommand for To { #[cfg(test)] mod tests { + use super::ShellError; use super::To; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(To {}) + Ok(test_examples(To {})?) } } diff --git a/crates/nu-cli/src/commands/to_csv.rs b/crates/nu-cli/src/commands/to_csv.rs index b41aeb674e..676139a988 100644 --- a/crates/nu-cli/src/commands/to_csv.rs +++ b/crates/nu-cli/src/commands/to_csv.rs @@ -84,12 +84,13 @@ async fn to_csv(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToCSV {}) + Ok(test_examples(ToCSV {})?) } } diff --git a/crates/nu-cli/src/commands/to_html.rs b/crates/nu-cli/src/commands/to_html.rs index 15cb2aa728..7809d1daa5 100644 --- a/crates/nu-cli/src/commands/to_html.rs +++ b/crates/nu-cli/src/commands/to_html.rs @@ -759,12 +759,13 @@ fn run_regexes(hash: &HashMap, contents: &str) -> S #[cfg(test)] mod tests { + use super::ShellError; use super::*; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToHTML {}) + Ok(test_examples(ToHTML {})?) } } diff --git a/crates/nu-cli/src/commands/to_json.rs b/crates/nu-cli/src/commands/to_json.rs index 702feda3f7..561cca21c5 100644 --- a/crates/nu-cli/src/commands/to_json.rs +++ b/crates/nu-cli/src/commands/to_json.rs @@ -264,12 +264,13 @@ async fn to_json( #[cfg(test)] mod tests { + use super::ShellError; use super::ToJSON; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToJSON {}) + Ok(test_examples(ToJSON {})?) } } diff --git a/crates/nu-cli/src/commands/to_md.rs b/crates/nu-cli/src/commands/to_md.rs index 1cf8d200a7..5f973cbddd 100644 --- a/crates/nu-cli/src/commands/to_md.rs +++ b/crates/nu-cli/src/commands/to_md.rs @@ -82,12 +82,13 @@ async fn to_html( #[cfg(test)] mod tests { + use super::ShellError; use super::ToMarkdown; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToMarkdown {}) + Ok(test_examples(ToMarkdown {})?) } } diff --git a/crates/nu-cli/src/commands/to_toml.rs b/crates/nu-cli/src/commands/to_toml.rs index bbd012d47b..9c1aacb415 100644 --- a/crates/nu-cli/src/commands/to_toml.rs +++ b/crates/nu-cli/src/commands/to_toml.rs @@ -186,14 +186,15 @@ async fn to_toml( #[cfg(test)] mod tests { + use super::ShellError; use super::*; use nu_protocol::Dictionary; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToTOML {}) + Ok(test_examples(ToTOML {})?) } #[test] diff --git a/crates/nu-cli/src/commands/to_tsv.rs b/crates/nu-cli/src/commands/to_tsv.rs index 974884986e..df1e33548c 100644 --- a/crates/nu-cli/src/commands/to_tsv.rs +++ b/crates/nu-cli/src/commands/to_tsv.rs @@ -48,12 +48,13 @@ async fn to_tsv(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToTSV {}) + Ok(test_examples(ToTSV {})?) } } diff --git a/crates/nu-cli/src/commands/to_url.rs b/crates/nu-cli/src/commands/to_url.rs index d939ca271f..6b268f35a6 100644 --- a/crates/nu-cli/src/commands/to_url.rs +++ b/crates/nu-cli/src/commands/to_url.rs @@ -80,12 +80,13 @@ async fn to_url(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToURL {}) + Ok(test_examples(ToURL {})?) } } diff --git a/crates/nu-cli/src/commands/to_xml.rs b/crates/nu-cli/src/commands/to_xml.rs index 830113297d..9226d140dd 100644 --- a/crates/nu-cli/src/commands/to_xml.rs +++ b/crates/nu-cli/src/commands/to_xml.rs @@ -194,12 +194,13 @@ async fn to_xml(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToXML {}) + Ok(test_examples(ToXML {})?) } } diff --git a/crates/nu-cli/src/commands/to_yaml.rs b/crates/nu-cli/src/commands/to_yaml.rs index 8cc5c64199..fef85c9bff 100644 --- a/crates/nu-cli/src/commands/to_yaml.rs +++ b/crates/nu-cli/src/commands/to_yaml.rs @@ -172,12 +172,13 @@ async fn to_yaml( #[cfg(test)] mod tests { + use super::ShellError; use super::ToYAML; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(ToYAML {}) + Ok(test_examples(ToYAML {})?) } } diff --git a/crates/nu-cli/src/commands/touch.rs b/crates/nu-cli/src/commands/touch.rs index 19b3e62cb1..d9c68d994b 100644 --- a/crates/nu-cli/src/commands/touch.rs +++ b/crates/nu-cli/src/commands/touch.rs @@ -77,12 +77,13 @@ async fn touch(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Touch {}) + Ok(test_examples(Touch {})?) } } diff --git a/crates/nu-cli/src/commands/uniq.rs b/crates/nu-cli/src/commands/uniq.rs index 6c4158c8d8..9a743ceb57 100644 --- a/crates/nu-cli/src/commands/uniq.rs +++ b/crates/nu-cli/src/commands/uniq.rs @@ -128,12 +128,12 @@ async fn uniq(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Uniq {}) + Ok(test_examples(Uniq {})?) } } diff --git a/crates/nu-cli/src/commands/update.rs b/crates/nu-cli/src/commands/update.rs index 6539b677ee..3523a750f4 100644 --- a/crates/nu-cli/src/commands/update.rs +++ b/crates/nu-cli/src/commands/update.rs @@ -11,16 +11,17 @@ use nu_source::HasFallibleSpan; use nu_value_ext::ValueExt; use futures::stream::once; -pub struct Update; + +pub struct Command; #[derive(Deserialize)] -pub struct UpdateArgs { +pub struct Arguments { field: ColumnPath, replacement: Value, } #[async_trait] -impl WholeStreamCommand for Update { +impl WholeStreamCommand for Command { fn name(&self) -> &str { "update" } @@ -54,10 +55,18 @@ impl WholeStreamCommand for Update { fn examples(&self) -> Vec { vec![Example { description: "Update a column value", - example: "echo [[a, b]; [1, 2]] | update a 'nu'", + example: "echo [[name, stars]; ['nu', 5]] | update name 'Nushell'", result: Some(vec![UntaggedValue::row(indexmap! { - "a".to_string() => Value::from("nu"), - "b".to_string() => UntaggedValue::int(2).into(), + "name".to_string() => Value::from("Nushell"), + "stars".to_string() => UntaggedValue::int(5).into(), + }) + .into()]), + },Example { + description: "Use in block form for more involved updating logic", + example: "echo [[project, authors]; ['nu', ['Andrés', 'Jonathan', 'Yehuda']]] | update authors { get authors | str collect ',' }", + result: Some(vec![UntaggedValue::row(indexmap! { + "project".to_string() => Value::from("nu"), + "authors".to_string() => Value::from("Andrés,Jonathan,Yehuda"), }) .into()]), }] @@ -170,7 +179,7 @@ async fn update( let name_tag = Arc::new(raw_args.call_info.name_tag.clone()); let scope = raw_args.call_info.scope.clone(); let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry)); - let (UpdateArgs { field, replacement }, input) = raw_args.process(®istry).await?; + let (Arguments { field, replacement }, input) = raw_args.process(®istry).await?; let replacement = Arc::new(replacement); let field = Arc::new(field); @@ -192,15 +201,3 @@ async fn update( .flatten() .to_output_stream()) } - -#[cfg(test)] -mod tests { - use super::Update; - - #[test] - fn examples_work_as_expected() { - use crate::examples::test as test_examples; - - test_examples(Update {}) - } -} diff --git a/crates/nu-cli/src/commands/url_/command.rs b/crates/nu-cli/src/commands/url_/command.rs index 16e764503b..48dc76f76e 100644 --- a/crates/nu-cli/src/commands/url_/command.rs +++ b/crates/nu-cli/src/commands/url_/command.rs @@ -35,12 +35,13 @@ impl WholeStreamCommand for Url { #[cfg(test)] mod tests { + use super::ShellError; use super::Url; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Url {}) + Ok(test_examples(Url {})?) } } diff --git a/crates/nu-cli/src/commands/url_/host.rs b/crates/nu-cli/src/commands/url_/host.rs index 4e3472b38b..6adeb1c497 100644 --- a/crates/nu-cli/src/commands/url_/host.rs +++ b/crates/nu-cli/src/commands/url_/host.rs @@ -47,12 +47,13 @@ fn host(url: &Url) -> &str { #[cfg(test)] mod tests { + use super::ShellError; use super::UrlHost; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(UrlHost {}) + Ok(test_examples(UrlHost {})?) } } diff --git a/crates/nu-cli/src/commands/url_/path.rs b/crates/nu-cli/src/commands/url_/path.rs index 58c8560aed..6c5658dca9 100644 --- a/crates/nu-cli/src/commands/url_/path.rs +++ b/crates/nu-cli/src/commands/url_/path.rs @@ -50,12 +50,13 @@ impl WholeStreamCommand for UrlPath { #[cfg(test)] mod tests { + use super::ShellError; use super::UrlPath; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(UrlPath {}) + Ok(test_examples(UrlPath {})?) } } diff --git a/crates/nu-cli/src/commands/url_/query.rs b/crates/nu-cli/src/commands/url_/query.rs index 32a0d71528..48e3ccf926 100644 --- a/crates/nu-cli/src/commands/url_/query.rs +++ b/crates/nu-cli/src/commands/url_/query.rs @@ -54,12 +54,13 @@ fn query(url: &Url) -> &str { #[cfg(test)] mod tests { + use super::ShellError; use super::UrlQuery; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(UrlQuery {}) + Ok(test_examples(UrlQuery {})?) } } diff --git a/crates/nu-cli/src/commands/url_/scheme.rs b/crates/nu-cli/src/commands/url_/scheme.rs index 33b446d860..a2b60a39e9 100644 --- a/crates/nu-cli/src/commands/url_/scheme.rs +++ b/crates/nu-cli/src/commands/url_/scheme.rs @@ -49,12 +49,13 @@ impl WholeStreamCommand for UrlScheme { #[cfg(test)] mod tests { + use super::ShellError; use super::UrlScheme; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(UrlScheme {}) + Ok(test_examples(UrlScheme {})?) } } diff --git a/crates/nu-cli/src/commands/version.rs b/crates/nu-cli/src/commands/version.rs index bba6b378f2..39bbedbfb2 100644 --- a/crates/nu-cli/src/commands/version.rs +++ b/crates/nu-cli/src/commands/version.rs @@ -84,12 +84,13 @@ fn features_enabled(tag: impl Into) -> TaggedListBuilder { #[cfg(test)] mod tests { + use super::ShellError; use super::Version; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Version {}) + Ok(test_examples(Version {})?) } } diff --git a/crates/nu-cli/src/commands/where_.rs b/crates/nu-cli/src/commands/where_.rs index 577b0fab81..fca11e50be 100644 --- a/crates/nu-cli/src/commands/where_.rs +++ b/crates/nu-cli/src/commands/where_.rs @@ -132,12 +132,13 @@ async fn where_command( #[cfg(test)] mod tests { + use super::ShellError; use super::Where; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Where {}) + Ok(test_examples(Where {})?) } } diff --git a/crates/nu-cli/src/commands/which_.rs b/crates/nu-cli/src/commands/which_.rs index 4c3eac2a74..efd826acc2 100644 --- a/crates/nu-cli/src/commands/which_.rs +++ b/crates/nu-cli/src/commands/which_.rs @@ -122,12 +122,13 @@ async fn which(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Which {}) + Ok(test_examples(Which {})?) } } diff --git a/crates/nu-cli/src/commands/with_env.rs b/crates/nu-cli/src/commands/with_env.rs index dac4149747..f8797e1dd8 100644 --- a/crates/nu-cli/src/commands/with_env.rs +++ b/crates/nu-cli/src/commands/with_env.rs @@ -123,12 +123,13 @@ async fn with_env( #[cfg(test)] mod tests { + use super::ShellError; use super::WithEnv; #[test] - fn examples_work_as_expected() { + fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(WithEnv {}) + Ok(test_examples(WithEnv {})?) } } diff --git a/crates/nu-cli/src/commands/wrap.rs b/crates/nu-cli/src/commands/wrap.rs index d679d4ad3e..e7dd047796 100644 --- a/crates/nu-cli/src/commands/wrap.rs +++ b/crates/nu-cli/src/commands/wrap.rs @@ -138,12 +138,13 @@ async fn wrap(args: CommandArgs, registry: &CommandRegistry) -> Result Result<(), ShellError> { use crate::examples::test as test_examples; - test_examples(Wrap {}) + Ok(test_examples(Wrap {})?) } } diff --git a/crates/nu-cli/src/examples.rs b/crates/nu-cli/src/examples.rs index 6a2a3676b5..c2478a60ae 100644 --- a/crates/nu-cli/src/examples.rs +++ b/crates/nu-cli/src/examples.rs @@ -1,66 +1,211 @@ -use futures::executor::block_on; - use nu_errors::ShellError; use nu_protocol::hir::ClassifiedBlock; -use nu_protocol::{Scope, ShellTypeName, Value}; +use nu_protocol::{ + ReturnSuccess, Scope, ShellTypeName, Signature, SyntaxShape, UntaggedValue, Value, +}; +use nu_source::{AnchorLocation, TaggedItem}; +use crate::prelude::*; + +use indexmap::indexmap; +use num_bigint::BigInt; + +use indexmap::IndexMap; + +use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; -use crate::commands::{whole_stream_command, BuildString, Each, Echo, StrCollect}; +use crate::commands::command::CommandArgs; +use crate::commands::{ + whole_stream_command, BuildString, Command, Each, Echo, Get, Keep, StrCollect, + WholeStreamCommand, +}; use crate::evaluation_context::EvaluationContext; -use crate::stream::InputStream; -use crate::WholeStreamCommand; +use crate::stream::{InputStream, OutputStream}; -pub fn test(cmd: impl WholeStreamCommand + 'static) { +use async_trait::async_trait; +use futures::executor::block_on; +use serde::Deserialize; + +pub fn test_examples(cmd: Command) -> Result<(), ShellError> { let examples = cmd.examples(); - let mut base_context = EvaluationContext::basic().expect("could not create basic context"); + + let mut base_context = EvaluationContext::basic()?; + + base_context.add_commands(vec![ + // Mocks + whole_stream_command(MockLs {}), + // Minimal restricted commands to aid in testing + whole_stream_command(Echo {}), + whole_stream_command(BuildString {}), + whole_stream_command(Get {}), + whole_stream_command(Keep {}), + whole_stream_command(Each {}), + whole_stream_command(StrCollect), + cmd, + ]); + + for sample_pipeline in examples { + let mut ctx = base_context.clone(); + + let block = parse_line(sample_pipeline.example, &mut ctx)?; + + if let Some(expected) = &sample_pipeline.result { + let result = block_on(evaluate_block(block, &mut ctx))?; + + ctx.with_errors(|reasons| { + reasons + .iter() + .cloned() + .take(1) + .next() + .and_then(|err| Some(err)) + }) + .map_or(Ok(()), |reason| Err(reason))?; + + if expected.len() != result.len() { + let rows_returned = + format!("expected: {}\nactual: {}", expected.len(), result.len()); + let failed_call = format!("command: {}\n", sample_pipeline.example); + + panic!( + "example command produced unexpected number of results.\n {} {}", + failed_call, rows_returned + ); + } + + for (e, a) in expected.iter().zip(result.iter()) { + if !values_equal(e, a) { + let row_errored = format!("expected: {:#?}\nactual: {:#?}", e, a); + let failed_call = format!("command: {}\n", sample_pipeline.example); + + panic!( + "example command produced unexpected result.\n {} {}", + failed_call, row_errored + ); + } + } + } + } + + Ok(()) +} + +pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> { + let examples = cmd.examples(); + + let mut base_context = EvaluationContext::basic()?; base_context.add_commands(vec![ whole_stream_command(Echo {}), whole_stream_command(BuildString {}), + whole_stream_command(Get {}), + whole_stream_command(Keep {}), whole_stream_command(Each {}), whole_stream_command(cmd), whole_stream_command(StrCollect), ]); - for example in examples { + for sample_pipeline in examples { let mut ctx = base_context.clone(); - let block = parse_line(example.example, &mut ctx).expect("failed to parse example"); - if let Some(expected) = example.result { - let result = block_on(evaluate_block(block, &mut ctx)).expect("failed to run example"); - let errors = ctx.get_errors(); + let block = parse_line(sample_pipeline.example, &mut ctx)?; - assert!( - errors.is_empty(), - "errors while running command.\ncommand: {}\nerrors: {:?}", - example.example, - errors - ); + if let Some(expected) = &sample_pipeline.result { + let result = block_on(evaluate_block(block, &mut ctx))?; - assert!(expected.len() == result.len(), "example command produced unexpected number of results.\ncommand: {}\nexpected number: {}\nactual: {}", - example.example, - expected.len(), - result.len(),); - - assert!( - expected + ctx.with_errors(|reasons| { + reasons .iter() - .zip(result.iter()) - .all(|(e, a)| values_equal(e, a)), - "example command produced unexpected result.\ncommand: {}\nexpected: {:?}\nactual: {:?}", - example.example, - expected, - result, - ); + .cloned() + .take(1) + .next() + .and_then(|err| Some(err)) + }) + .map_or(Ok(()), |reason| Err(reason))?; + + if expected.len() != result.len() { + let rows_returned = + format!("expected: {}\nactual: {}", expected.len(), result.len()); + let failed_call = format!("command: {}\n", sample_pipeline.example); + + panic!( + "example command produced unexpected number of results.\n {} {}", + failed_call, rows_returned + ); + } + + for (e, a) in expected.iter().zip(result.iter()) { + if !values_equal(e, a) { + let row_errored = format!("expected: {:#?}\nactual: {:#?}", e, a); + let failed_call = format!("command: {}\n", sample_pipeline.example); + + panic!( + "example command produced unexpected result.\n {} {}", + failed_call, row_errored + ); + } + } } } + + Ok(()) +} + +pub fn test_anchors(cmd: Command) -> Result<(), ShellError> { + let examples = cmd.examples(); + + let mut base_context = EvaluationContext::basic()?; + + base_context.add_commands(vec![ + // Minimal restricted commands to aid in testing + whole_stream_command(MockCommand {}), + whole_stream_command(MockEcho {}), + whole_stream_command(MockLs {}), + whole_stream_command(BuildString {}), + whole_stream_command(Get {}), + whole_stream_command(Keep {}), + whole_stream_command(Each {}), + whole_stream_command(StrCollect), + cmd, + ]); + + for sample_pipeline in examples { + let pipeline_with_anchor = format!("mock --open --path | {}", sample_pipeline.example); + + let mut ctx = base_context.clone(); + + let block = parse_line(&pipeline_with_anchor, &mut ctx)?; + let result = block_on(evaluate_block(block, &mut ctx))?; + + ctx.with_errors(|reasons| { + reasons + .iter() + .cloned() + .take(1) + .next() + .and_then(|err| Some(err)) + }) + .map_or(Ok(()), |reason| Err(reason))?; + + for actual in result.iter() { + if !is_anchor_carried(actual, mock_path()) { + let failed_call = format!("command: {}\n", pipeline_with_anchor); + + panic!( + "example command didn't carry anchor tag correctly.\n {} {:#?} {:#?}", + failed_call, + actual, + mock_path() + ); + } + } + } + + Ok(()) } /// Parse and run a nushell pipeline -fn parse_line( - line: &'static str, - ctx: &mut EvaluationContext, -) -> Result { +fn parse_line(line: &str, ctx: &mut EvaluationContext) -> Result { let line = if line.ends_with('\n') { &line[..line.len() - 1] } else { @@ -86,7 +231,7 @@ async fn evaluate_block( Ok(run_block(&block.block, ctx, input_stream, scope) .await? - .into_vec() + .drain_vec() .await) } @@ -113,3 +258,233 @@ fn values_equal(expected: &Value, actual: &Value) -> bool { (e, a) => unimplemented!("{} {}", e.type_name(), a.type_name()), } } + +fn is_anchor_carried(actual: &Value, anchor: AnchorLocation) -> bool { + actual.tag.anchor() == Some(anchor) +} + +#[derive(Deserialize)] +struct Arguments { + path: Option, + open: bool, +} + +struct MockCommand; + +#[async_trait] +impl WholeStreamCommand for MockCommand { + fn name(&self) -> &str { + "mock" + } + + fn signature(&self) -> Signature { + Signature::build("mock") + .switch("open", "fake opening sources", Some('o')) + .switch("path", "file open", Some('p')) + } + + fn usage(&self) -> &str { + "Generates tables and metadata that mimics behavior of real commands in controlled ways." + } + + async fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + let name_tag = args.call_info.name_tag.clone(); + + let ( + Arguments { + path: mocked_path, + open: open_mock, + }, + _input, + ) = args.process(®istry).await?; + + let out = UntaggedValue::string("Yehuda Katz in Ecuador"); + + if open_mock { + if let Some(true) = mocked_path { + let mocked_path = Some(mock_path()); + let value = out.tagged(name_tag.span).map_anchored(&mocked_path); + + return Ok(OutputStream::one(Ok(ReturnSuccess::Value( + value.item.into_value(value.tag), + )))); + } + } + + Ok(OutputStream::one(Ok(ReturnSuccess::Value( + out.into_value(name_tag), + )))) + } +} + +struct MockEcho; + +#[derive(Deserialize)] +pub struct MockEchoArgs { + pub rest: Vec, +} + +#[async_trait] +impl WholeStreamCommand for MockEcho { + fn name(&self) -> &str { + "echo" + } + + fn signature(&self) -> Signature { + Signature::build("echo").rest(SyntaxShape::Any, "the values to echo") + } + + fn usage(&self) -> &str { + "Mock echo." + } + + async fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + let name_tag = args.call_info.name_tag.clone(); + let (MockEchoArgs { rest }, input) = args.process(®istry).await?; + + let mut base_value = UntaggedValue::string("Yehuda Katz in Ecuador").into_value(name_tag); + let input: Vec = input.collect().await; + + if let Some(first) = input.get(0) { + base_value = first.clone() + } + + let stream = rest.into_iter().map(move |i| { + let base_value = base_value.clone(); + match i.as_string() { + Ok(s) => OutputStream::one(Ok(ReturnSuccess::Value( + UntaggedValue::string(s).into_value(base_value.tag.clone()), + ))), + _ => match i { + Value { + value: UntaggedValue::Table(table), + .. + } => futures::stream::iter( + table + .into_iter() + .map(move |mut v| { + v.tag = base_value.tag(); + v + }) + .map(ReturnSuccess::value), + ) + .to_output_stream(), + _ => OutputStream::one(Ok(ReturnSuccess::Value(Value { + value: i.value.clone(), + tag: base_value.tag.clone(), + }))), + }, + } + }); + + Ok(futures::stream::iter(stream).flatten().to_output_stream()) + } +} + +struct MockLs; + +#[async_trait] +impl WholeStreamCommand for MockLs { + fn name(&self) -> &str { + "ls" + } + + fn signature(&self) -> Signature { + Signature::build("ls") + } + + fn usage(&self) -> &str { + "Mock ls." + } + + async fn run( + &self, + args: CommandArgs, + _: &CommandRegistry, + ) -> Result { + let name_tag = args.call_info.name_tag.clone(); + + let mut base_value = + UntaggedValue::string("Andrés N. Robalino in Portland").into_value(name_tag); + let input: Vec = args.input.collect().await; + + if let Some(first) = input.get(0) { + base_value = first.clone() + } + + Ok(futures::stream::iter( + file_listing() + .iter() + .map(|row| Value { + value: row.value.clone(), + tag: base_value.tag.clone(), + }) + .collect::>() + .into_iter() + .map(ReturnSuccess::value), + ) + .to_output_stream()) + } +} + +fn int(s: impl Into) -> Value { + UntaggedValue::int(s).into_untagged_value() +} + +fn string(input: impl Into) -> Value { + UntaggedValue::string(input.into()).into_untagged_value() +} + +fn row(entries: IndexMap) -> Value { + UntaggedValue::row(entries).into_untagged_value() +} + +fn date(input: impl Into) -> Value { + let key = input.into().tagged_unknown(); + crate::value::Date::naive_from_str(key.borrow_tagged()) + .expect("date from string failed") + .into_untagged_value() +} + +fn file_listing() -> Vec { + vec![ + row(indexmap! { + "modified".to_string() => date("2019-07-23"), + "name".to_string() => string("Andrés.txt"), + "type".to_string() => string("File"), + "chickens".to_string() => int(10), + }), + row(indexmap! { + "modified".to_string() => date("2019-07-23"), + "name".to_string() => string("Jonathan"), + "type".to_string() => string("Dir"), + "chickens".to_string() => int(5), + }), + row(indexmap! { + "modified".to_string() => date("2019-09-24"), + "name".to_string() => string("Andrés.txt"), + "type".to_string() => string("File"), + "chickens".to_string() => int(20), + }), + row(indexmap! { + "modified".to_string() => date("2019-09-24"), + "name".to_string() => string("Yehuda"), + "type".to_string() => string("Dir"), + "chickens".to_string() => int(4), + }), + ] +} + +fn mock_path() -> AnchorLocation { + let path = String::from("path/to/las_best_arepas_in_the_world.txt"); + + AnchorLocation::File(path) +} diff --git a/crates/nu-data/src/value.rs b/crates/nu-data/src/value.rs index 40ac374e2c..aaf259eb7e 100644 --- a/crates/nu-data/src/value.rs +++ b/crates/nu-data/src/value.rs @@ -47,6 +47,10 @@ pub fn date_from_str(s: Tagged<&str>) -> Result { Date::from_regular_str(s) } +pub fn date_naive_from_str(s: Tagged<&str>) -> Result { + Date::naive_from_str(s) +} + pub fn merge_values( left: &UntaggedValue, right: &UntaggedValue, diff --git a/crates/nu-source/src/meta.rs b/crates/nu-source/src/meta.rs index 9e01062205..03a915ea02 100644 --- a/crates/nu-source/src/meta.rs +++ b/crates/nu-source/src/meta.rs @@ -314,6 +314,13 @@ impl Tag { } } + pub fn anchored(self, anchor: Option) -> Tag { + Tag { + anchor, + span: self.span, + } + } + /// Creates a `Tag` from the given `Span` with no `AnchorLocation` pub fn unknown_anchor(span: Span) -> Tag { Tag { anchor: None, span }