nushell/crates/nu-command/tests/commands/do_.rs

157 lines
4.5 KiB
Rust
Raw Normal View History

use nu_test_support::nu;
#[cfg(not(windows))]
use nu_test_support::pipeline;
#[test]
fn capture_errors_works() {
let actual = nu!("do -c {$env.use}");
eprintln!("actual.err: {:?}", actual.err);
assert!(actual.err.contains("column_not_found"));
}
#[test]
fn capture_errors_works_for_external() {
let actual = nu!("do -c {nu --testbin fail}");
assert!(actual.err.contains("External command failed"));
assert_eq!(actual.out, "");
}
#[test]
fn capture_errors_works_for_external_with_pipeline() {
let actual = nu!("do -c {nu --testbin fail} | echo `text`");
assert!(actual.err.contains("External command failed"));
assert_eq!(actual.out, "");
}
#[test]
fn capture_errors_works_for_external_with_semicolon() {
let actual = nu!(r#"do -c {nu --testbin fail}; echo `text`"#);
assert!(actual.err.contains("External command failed"));
assert_eq!(actual.out, "");
}
#[test]
fn do_with_semicolon_break_on_failed_external() {
let actual = nu!(r#"do { nu --not_exist_flag }; `text`"#);
assert_eq!(actual.out, "");
}
#[test]
fn ignore_shell_errors_works_for_external_with_semicolon() {
let actual = nu!(r#"do -s { open asdfasdf.txt }; "text""#);
assert_eq!(actual.err, "");
assert_eq!(actual.out, "text");
}
#[test]
fn ignore_program_errors_works_for_external_with_semicolon() {
let actual = nu!(r#"do -p { nu -c 'exit 1' }; "text""#);
assert_eq!(actual.err, "");
assert_eq!(actual.out, "text");
}
#[test]
fn ignore_error_should_work_for_external_command() {
let actual = nu!(r#"do -i { nu --testbin fail asdf }; echo post"#);
assert_eq!(actual.err, "");
assert_eq!(actual.out, "post");
}
#[test]
#[cfg(not(windows))]
fn capture_error_with_too_much_stderr_not_hang_nushell() {
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::pipeline;
use nu_test_support::playground::Playground;
Playground::setup("external with many stderr message", |dirs, sandbox| {
let bytes: usize = 81920;
let mut large_file_body = String::with_capacity(bytes);
for _ in 0..bytes {
2022-10-18 20:13:36 +02:00
large_file_body.push('a');
}
sandbox.with_files(vec![FileWithContent("a_large_file.txt", &large_file_body)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
do -c {sh -c "cat a_large_file.txt 1>&2"} | complete | get stderr
"#,
));
assert_eq!(actual.out, large_file_body);
})
}
2022-10-15 21:29:29 +02:00
#[test]
#[cfg(not(windows))]
fn capture_error_with_too_much_stdout_not_hang_nushell() {
2022-10-15 21:29:29 +02:00
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::pipeline;
use nu_test_support::playground::Playground;
Playground::setup("external with many stdout message", |dirs, sandbox| {
let bytes: usize = 81920;
let mut large_file_body = String::with_capacity(bytes);
for _ in 0..bytes {
2022-10-18 20:13:36 +02:00
large_file_body.push('a');
2022-10-15 21:29:29 +02:00
}
sandbox.with_files(vec![FileWithContent("a_large_file.txt", &large_file_body)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
do -c {sh -c "cat a_large_file.txt"} | complete | get stdout
"#,
2022-10-15 21:29:29 +02:00
));
assert_eq!(actual.out, large_file_body);
})
}
#[test]
#[cfg(not(windows))]
fn capture_error_with_both_stdout_stderr_messages_not_hang_nushell() {
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::playground::Playground;
Playground::setup(
"external with many stdout and stderr messages",
|dirs, sandbox| {
let script_body = r#"
x=$(printf '=%.0s' {1..40960})
echo $x
echo $x 1>&2
"#;
let mut expect_body = String::new();
for _ in 0..40960 {
2022-10-18 20:13:36 +02:00
expect_body.push('=');
}
2022-10-18 20:13:36 +02:00
sandbox.with_files(vec![FileWithContent("test.sh", script_body)]);
// check for stdout
let actual = nu!(
cwd: dirs.test(), pipeline(
Fix: remove unnecessary `r#"..."#` (#8670) (#9764) <!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR is related to **Tests: clean up unnecessary use of cwd, pipeline(), etc. [#8670](https://github.com/nushell/nushell/issues/8670)** - Removed the `r#"..."#` raw string literal syntax, which is unnecessary when there are no special characters that need quoting from the tests that use the `nu!` macro. - `cwd:` and `pipeline()` has not changed # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-07-21 17:32:37 +02:00
"do -c {bash test.sh} | complete | get stdout | str trim",
));
assert_eq!(actual.out, expect_body);
// check for stderr
let actual = nu!(
cwd: dirs.test(), pipeline(
Fix: remove unnecessary `r#"..."#` (#8670) (#9764) <!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR is related to **Tests: clean up unnecessary use of cwd, pipeline(), etc. [#8670](https://github.com/nushell/nushell/issues/8670)** - Removed the `r#"..."#` raw string literal syntax, which is unnecessary when there are no special characters that need quoting from the tests that use the `nu!` macro. - `cwd:` and `pipeline()` has not changed # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-07-21 17:32:37 +02:00
"do -c {bash test.sh} | complete | get stderr | str trim",
));
assert_eq!(actual.out, expect_body);
},
)
}
#[test]
fn ignore_error_works_with_list_stream() {
let actual = nu!(r#"do -i { ["a", null, "b"] | ansi strip }"#);
assert!(actual.err.is_empty());
}