Add `tee` command for operating on copies of streams (#11928)
<!--
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!
-->
[Related conversation on
Discord](https://discord.com/channels/601130461678272522/615329862395101194/1209951539901366292)
# 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 is inspired by the Unix tee command, but significantly more
powerful. Rather than just writing to a file, you can do any kind of
stream operation that Nushell supports within the closure.
The equivalent of Unix `tee -a file.txt` would be, for example, `command
| tee { save -a file.txt }` - but of course this is Nushell, and you can
do the same with structured data to JSON objects, or even just run any
other command on the system with it.
A `--stderr` flag is provided for operating on the stderr stream from
external programs. This may produce unexpected results if the stderr
stream is not then also printed by something else - nushell currently
doesn't. See #11929 for the fix for that.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
If someone was using the system `tee` command, they might be surprised
to find that it's different.
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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
> ```
-->
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :green_circle: `toolkit test`
- :green_circle: `toolkit test stdlib`
# 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.
-->
2024-02-29 00:08:31 +01:00
|
|
|
use nu_test_support::{fs::file_contents, nu, playground::Playground};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tee_save_values_to_file() {
|
|
|
|
Playground::setup("tee_save_values_to_file_test", |dirs, _sandbox| {
|
|
|
|
let output = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
r#"1..5 | tee { save copy.txt } | to text"#
|
|
|
|
);
|
|
|
|
assert_eq!("12345", output.out);
|
|
|
|
assert_eq!(
|
|
|
|
"1\n2\n3\n4\n5\n",
|
|
|
|
file_contents(dirs.test().join("copy.txt"))
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tee_save_stdout_to_file() {
|
|
|
|
Playground::setup("tee_save_stdout_to_file_test", |dirs, _sandbox| {
|
|
|
|
let output = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
r#"
|
|
|
|
$env.FOO = "teststring"
|
|
|
|
nu --testbin echo_env FOO | tee { save copy.txt }
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!("teststring", output.out);
|
|
|
|
assert_eq!("teststring\n", file_contents(dirs.test().join("copy.txt")));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tee_save_stderr_to_file() {
|
|
|
|
Playground::setup("tee_save_stderr_to_file_test", |dirs, _sandbox| {
|
|
|
|
let output = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"\
|
|
|
|
$env.FOO = \"teststring\"; \
|
|
|
|
do { nu --testbin echo_env_stderr FOO } | \
|
|
|
|
tee --stderr { save copy.txt } | \
|
|
|
|
complete | \
|
|
|
|
get stderr
|
|
|
|
"
|
|
|
|
);
|
|
|
|
assert_eq!("teststring", output.out);
|
|
|
|
assert_eq!("teststring\n", file_contents(dirs.test().join("copy.txt")));
|
|
|
|
})
|
|
|
|
}
|
Make `tee` work more nicely with non-collections (#13652)
# Description
This changes the behavior of `tee` to be more transparent when given a
value that isn't a list or range. Previously, anything that wasn't a
byte stream would converted to a list stream using the iterator
implementation, which led to some surprising results. Instead, now, if
the value is a string or binary, it will be treated the same way a byte
stream is, and the output of `tee` is a byte stream instead of the
original value. This is done so that we can synchronize with the other
thread on collect, and potentially capture any error produced by the
closure.
For values that can't be converted to streams, the closure is just run
with a clone of the value instead on another thread. Because we can't
wait for the other thread, there is no way to send an error back to the
original thread, so instead it's just written to stderr using
`report_error_new()`.
There are a couple of follow up edge cases I see where byte streams
aren't necessarily treated exactly the same way strings are, but this
should mostly be a good experience.
Fixes #13489.
# User-Facing Changes
Breaking change.
- `tee` now outputs and sends string/binary stream for string/binary
input.
- `tee` now outputs and sends the original value for any other input
other than lists/ranges.
# Tests + Formatting
Added for new behavior.
# After Submitting
- [ ] release notes: breaking change, command change
2024-09-01 19:03:46 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tee_single_value_streamable() {
|
|
|
|
let actual = nu!("'Hello, world!' | tee { print -e } | print");
|
|
|
|
assert!(actual.status.success());
|
|
|
|
assert_eq!("Hello, world!", actual.out);
|
|
|
|
// FIXME: note the lack of newline: this is a consequence of converting the string to a stream
|
|
|
|
// for now, but most likely the printer should be checking whether a string stream ends with a
|
|
|
|
// newline and adding it unless no_newline is true
|
|
|
|
assert_eq!("Hello, world!", actual.err);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tee_single_value_non_streamable() {
|
|
|
|
// Non-streamable values don't have any synchronization point, so we have to wait.
|
|
|
|
let actual = nu!("500 | tee { print -e } | print; sleep 1sec");
|
|
|
|
assert!(actual.status.success());
|
|
|
|
assert_eq!("500", actual.out);
|
|
|
|
assert_eq!("500\n", actual.err);
|
|
|
|
}
|