mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 06:30:08 +02:00
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
This commit is contained in:
@ -47,3 +47,23 @@ fn tee_save_stderr_to_file() {
|
||||
assert_eq!("teststring\n", file_contents(dirs.test().join("copy.txt")));
|
||||
})
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user