diff --git a/.azure/azure-pipelines.yml b/.azure/azure-pipelines.yml index b490d597a1..a388c8d115 100644 --- a/.azure/azure-pipelines.yml +++ b/.azure/azure-pipelines.yml @@ -46,13 +46,13 @@ steps: echo "##vso[task.prependpath]$HOME/.cargo/bin" rustup component add rustfmt displayName: Install Rust - - bash: RUSTFLAGS="-D warnings" cargo test --all --features stable,test-bins + - bash: RUSTFLAGS="-D warnings" cargo test --all --features stable condition: eq(variables['style'], 'unflagged') displayName: Run tests - bash: RUSTFLAGS="-D warnings" cargo clippy --all --features=stable -- -D clippy::result_unwrap_used -D clippy::option_unwrap_used condition: eq(variables['style'], 'unflagged') displayName: Check clippy lints - - bash: NUSHELL_ENABLE_ALL_FLAGS=1 RUSTFLAGS="-D warnings" cargo test --all --features stable,test-bins + - bash: NUSHELL_ENABLE_ALL_FLAGS=1 RUSTFLAGS="-D warnings" cargo test --all --features stable condition: eq(variables['style'], 'canary') displayName: Run tests - bash: NUSHELL_ENABLE_ALL_FLAGS=1 RUSTFLAGS="-D warnings" cargo clippy --all --features=stable -- -D clippy::result_unwrap_used -D clippy::option_unwrap_used diff --git a/Cargo.toml b/Cargo.toml index 3ec88708c1..aa3419912b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,9 +60,6 @@ serde = { version = "1.0.110", features = ["derive"] } nu-build = { version = "0.14.1", path = "./crates/nu-build" } [features] -# Test executables -test-bins = [] - default = ["sys", "ps", "textview", "inc", "str"] stable = ["default", "starship-prompt", "binaryview", "match", "tree", "average", "parse", "post", "fetch", "clipboard-cli", "trash-support", "start"] @@ -88,31 +85,6 @@ clipboard-cli = ["nu-cli/clipboard-cli"] starship-prompt = ["nu-cli/starship-prompt"] trash-support = ["nu-cli/trash-support"] -[[bin]] -name = "fail" -path = "crates/nu-test-support/src/bins/fail.rs" -required-features = ["test-bins"] - -[[bin]] -name = "chop" -path = "crates/nu-test-support/src/bins/chop.rs" -required-features = ["test-bins"] - -[[bin]] -name = "cococo" -path = "crates/nu-test-support/src/bins/cococo.rs" -required-features = ["test-bins"] - -[[bin]] -name = "nonu" -path = "crates/nu-test-support/src/bins/nonu.rs" -required-features = ["test-bins"] - -[[bin]] -name = "iecho" -path = "crates/nu-test-support/src/bins/iecho.rs" -required-features = ["test-bins"] - # Core plugins that ship with `cargo install nu` by default # Currently, Cargo limits us to installing only one binary # unless we use [[bin]], so we use this as a workaround diff --git a/crates/nu-cli/src/lib.rs b/crates/nu-cli/src/lib.rs index 3b50b353aa..9ee6917832 100644 --- a/crates/nu-cli/src/lib.rs +++ b/crates/nu-cli/src/lib.rs @@ -26,7 +26,7 @@ mod git; mod path; mod shell; mod stream; -mod utils; +pub mod utils; pub use crate::cli::{ cli, create_default_context, load_plugins, run_pipeline_standalone, run_vec_of_pipelines, diff --git a/crates/nu-cli/src/utils.rs b/crates/nu-cli/src/utils.rs index a32cec0a8f..37f4619c8a 100644 --- a/crates/nu-cli/src/utils.rs +++ b/crates/nu-cli/src/utils.rs @@ -1,5 +1,6 @@ pub mod data; pub mod data_processing; +pub mod test_bins; use crate::path::canonicalize; use nu_errors::ShellError; @@ -21,6 +22,7 @@ pub struct ValueResource { impl ValueResource {} +#[derive(Default)] pub struct ValueStructure { pub resources: Vec, } @@ -96,6 +98,7 @@ pub struct Res { impl Res {} +#[derive(Default)] pub struct FileStructure { pub resources: Vec, } diff --git a/crates/nu-cli/src/utils/test_bins.rs b/crates/nu-cli/src/utils/test_bins.rs new file mode 100644 index 0000000000..eb8a5c098c --- /dev/null +++ b/crates/nu-cli/src/utils/test_bins.rs @@ -0,0 +1,94 @@ +use std::io::{self, BufRead, Write}; + +pub fn cococo() { + let args: Vec = args(); + + if args.len() > 1 { + // Write back out all the arguments passed + // if given at least 1 instead of chickens + // speaking co co co. + let mut arguments = args.iter(); + arguments.next(); + + for arg in arguments { + println!("{}", &arg); + } + } else { + println!("cococo"); + } +} + +pub fn nonu() { + args().iter().skip(1).for_each(|arg| print!("{}", arg)); +} + +pub fn iecho() { + // println! panics if stdout gets closed, whereas writeln gives us an error + let mut stdout = io::stdout(); + let _ = args() + .iter() + .skip(1) + .cycle() + .try_for_each(|v| writeln!(stdout, "{}", v)); +} + +pub fn fail() { + std::process::exit(1); +} + +pub fn chop() { + if did_chop_arguments() { + // we are done and don't care about standard input. + std::process::exit(0); + } + + // if no arguments given, chop from standard input and exit. + let stdin = io::stdin(); + let mut stdout = io::stdout(); + + for line in stdin.lock().lines() { + if let Ok(given) = line { + let chopped = if given.is_empty() { + &given + } else { + let to = given.len() - 1; + &given[..to] + }; + + if let Err(_e) = writeln!(stdout, "{}", chopped) { + break; + } + } + } + + std::process::exit(0); +} + +fn did_chop_arguments() -> bool { + let args: Vec = args(); + + if args.len() > 1 { + let mut arguments = args.iter(); + arguments.next(); + + for arg in arguments { + let chopped = if arg.is_empty() { + &arg + } else { + let to = arg.len() - 1; + &arg[..to] + }; + + println!("{}", chopped); + } + + return true; + } + + false +} + +fn args() -> Vec { + // skip (--testbin bin_name args) + std::env::args().skip(2).collect() +} diff --git a/crates/nu-test-support/src/bins/chop.rs b/crates/nu-test-support/src/bins/chop.rs deleted file mode 100644 index e1e63f29ef..0000000000 --- a/crates/nu-test-support/src/bins/chop.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::io::{self, BufRead, Write}; - -fn main() { - if did_chop_arguments() { - // we are done and don't care about standard input. - std::process::exit(0); - } - - // if no arguments given, chop from standard input and exit. - let stdin = io::stdin(); - let mut stdout = io::stdout(); - for line in stdin.lock().lines() { - if let Ok(given) = line { - if let Err(_e) = writeln!(stdout, "{}", chop(&given)) { - break; - } - } - } - - std::process::exit(0); -} - -fn chop(word: &str) -> &str { - if word.is_empty() { - word - } else { - let to = word.len() - 1; - &word[..to] - } -} - -fn did_chop_arguments() -> bool { - let args: Vec = std::env::args().collect(); - - if args.len() > 1 { - let mut arguments = args.iter(); - arguments.next(); - - for arg in arguments { - println!("{}", chop(arg)); - } - - return true; - } - - false -} diff --git a/crates/nu-test-support/src/bins/cococo.rs b/crates/nu-test-support/src/bins/cococo.rs deleted file mode 100644 index baf154444a..0000000000 --- a/crates/nu-test-support/src/bins/cococo.rs +++ /dev/null @@ -1,17 +0,0 @@ -fn main() { - let args: Vec = std::env::args().collect(); - - if args.len() > 1 { - // Write back out all the arguments passed - // if given at least 1 instead of chickens - // speaking co co co. - let mut arguments = args.iter(); - arguments.next(); - - for arg in arguments { - println!("{}", &arg); - } - } else { - println!("cococo"); - } -} diff --git a/crates/nu-test-support/src/bins/fail.rs b/crates/nu-test-support/src/bins/fail.rs deleted file mode 100644 index 081ca869c8..0000000000 --- a/crates/nu-test-support/src/bins/fail.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - std::process::exit(1); -} diff --git a/crates/nu-test-support/src/bins/iecho.rs b/crates/nu-test-support/src/bins/iecho.rs deleted file mode 100644 index 711d498c52..0000000000 --- a/crates/nu-test-support/src/bins/iecho.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::io::{self, Write}; - -fn main() { - let args: Vec = std::env::args().collect(); - - // println! panics if stdout gets closed, whereas writeln gives us an error - let mut stdout = io::stdout(); - let _ = args - .iter() - .skip(1) - .cycle() - .try_for_each(|v| writeln!(stdout, "{}", v)); -} diff --git a/crates/nu-test-support/src/bins/nonu.rs b/crates/nu-test-support/src/bins/nonu.rs deleted file mode 100644 index 4c1c4349f5..0000000000 --- a/crates/nu-test-support/src/bins/nonu.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - std::env::args().skip(1).for_each(|arg| print!("{}", arg)); -} diff --git a/src/main.rs b/src/main.rs index 2ccfcbee11..c80887df3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use clap::{App, Arg}; use log::LevelFilter; +use nu_cli::utils::test_bins as binaries; use nu_cli::{create_default_context, EnvironmentSyncer}; use std::error::Error; use std::fs::File; @@ -16,6 +17,14 @@ fn main() -> Result<(), Box> { .possible_values(&["error", "warn", "info", "debug", "trace"]) .takes_value(true), ) + .arg( + Arg::with_name("testbin") + .hidden(true) + .long("testbin") + .value_name("TESTBIN") + .possible_values(&["cococo", "iecho", "fail", "nonu", "chop"]) + .takes_value(true), + ) .arg( Arg::with_name("commands") .short("c") @@ -48,6 +57,19 @@ fn main() -> Result<(), Box> { ) .get_matches(); + if let Some(bin) = matches.value_of("testbin") { + match bin { + "cococo" => binaries::cococo(), + "iecho" => binaries::iecho(), + "fail" => binaries::fail(), + "nonu" => binaries::nonu(), + "chop" => binaries::chop(), + _ => unreachable!(), + } + + return Ok(()); + } + let loglevel = match matches.value_of("loglevel") { None => LevelFilter::Warn, Some("error") => LevelFilter::Error, diff --git a/tests/shell/pipeline/commands/external.rs b/tests/shell/pipeline/commands/external.rs index c197d8ee59..d3247b0546 100644 --- a/tests/shell/pipeline/commands/external.rs +++ b/tests/shell/pipeline/commands/external.rs @@ -74,7 +74,7 @@ mod it_evaluation { ls | sort-by name | get name - | cococo $it + | nu --testbin cococo $it | lines | nth 1 | echo $it @@ -101,7 +101,7 @@ mod it_evaluation { r#" open nu_candies.txt | lines - | chop $it + | nu --testbin chop $it | lines | nth 1 | echo $it @@ -145,7 +145,7 @@ mod stdin_evaluation { let actual = nu!( cwd: ".", pipeline(r#" - nonu "where's the nuline?" + nu --testbin nonu "where's the nuline?" | count "# )); @@ -158,9 +158,9 @@ mod stdin_evaluation { let stdout = nu!( cwd: ".", pipeline(r#" - iecho yes - | chop - | chop + nu --testbin iecho yes + | nu --testbin chop + | nu --testbin chop | lines | first 1 "# @@ -177,7 +177,7 @@ mod external_words { #[test] fn relaxed_external_words() { let actual = nu!(cwd: ".", r#" - cococo joturner@foo.bar.baz + nu --testbin cococo joturner@foo.bar.baz "#); assert_eq!(actual.out, "joturner@foo.bar.baz"); @@ -227,7 +227,7 @@ mod tilde_expansion { let actual = nu!( cwd: ".", r#" - cococo ~ + nu --testbin cococo ~ "# ); @@ -242,7 +242,7 @@ mod tilde_expansion { let actual = nu!( cwd: ".", r#" - cococo "1~1" + nu --testbin cococo "1~1" "# ); diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index facf2b17a3..ede7b4568d 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -24,7 +24,7 @@ fn takes_rows_of_nu_value_strings_and_pipes_it_to_stdin_of_external() { open nu_times.csv | get name | ^echo $it - | chop + | nu --testbin chop | nth 3 | echo $it "# @@ -90,7 +90,7 @@ fn invocation_handles_dot() { echo $(open nu_times.csv) | get name | ^echo $it - | chop + | nu --testbin chop | nth 3 | echo $it "# @@ -104,7 +104,7 @@ fn invocation_handles_dot() { fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() { let actual = nu!( cwd: ".", - r#"echo "nushelll" | chop"# + r#"echo "nushelll" | nu --testbin chop"# ); assert_eq!(actual.out, "nushell");