nushell/crates/nu-command/tests/commands/bytes/at.rs
Simon Curtis f05162811c
Implementing ByteStream interuption on infinite stream (#13552)
# Description

This PR should address #13530 by explicitly handling ByteStreams. 

The issue can be replicated easily on linux by running:

```nushell
open /dev/urandom | into binary | bytes at ..10
```

Would leave the output hanging and with no way to cancel it, this was
likely because it was trying to collect the input stream and would not
complete.

I have also put in an error to say that using negative offsets for a
bytestream without a length cannot be used.

```nushell
~/git/nushell> open /dev/urandom | into binary | bytes at (-1)..
Error: nu:🐚:incorrect_value

  × Incorrect value.
   ╭─[entry #3:1:35]
 1 │ open /dev/urandom | into binary | bytes at (-1)..
   ·                                   ────┬─── ───┬──
   ·                                       │       ╰── encountered here
   ·                                       ╰── Negative range values cannot be used with streams that don't specify a length
   ╰────
   ```

# User-Facing Changes

No operation changes, only the warning you get back for negative offsets

# Tests + Formatting

Ran `toolkit check pr ` with no errors or warnings

Manual testing of the example commands above

---------

Co-authored-by: Ian Manske <ian.manske@pm.me>
Co-authored-by: Simon Curtis <simon.curtis@candc-uk.com>
2025-01-11 13:28:08 -08:00

73 lines
2.2 KiB
Rust

use nu_test_support::nu;
#[test]
pub fn returns_error_for_relative_range_on_infinite_stream() {
let actual = nu!("nu --testbin iecho 3 | bytes at ..-3");
assert!(
actual.err.contains(
"Relative range values cannot be used with streams that don't have a known length"
),
"Expected error message for negative range with infinite stream"
);
}
#[test]
pub fn returns_bytes_for_fixed_range_on_infinite_stream_including_end() {
let actual = nu!("nu --testbin iecho 3 | bytes at ..10 | decode");
assert_eq!(
actual.out, "33333",
"Expected bytes from index 0 to 10, but got different output"
);
}
#[test]
pub fn returns_bytes_for_fixed_range_on_infinite_stream_excluding_end() {
let actual = nu!("nu --testbin iecho 3 | bytes at ..<9 | decode");
assert_eq!(
actual.out, "3333",
"Expected bytes from index 0 to 8, but got different output"
);
}
#[test]
pub fn test_string_returns_correct_slice_for_simple_positive_slice() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at ..4 | decode");
assert_eq!(actual.out, "Hello");
}
#[test]
pub fn test_string_returns_correct_slice_for_negative_start() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at (-5)..10 | decode");
assert_eq!(actual.out, "World");
}
#[test]
pub fn test_string_returns_correct_slice_for_negative_end() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at ..-7 | decode");
assert_eq!(actual.out, "Hello");
}
#[test]
pub fn test_string_returns_correct_slice_for_empty_slice() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at 5..<5 | decode");
assert_eq!(actual.out, "");
}
#[test]
pub fn test_string_returns_correct_slice_for_out_of_bounds() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at ..20 | decode");
assert_eq!(actual.out, "Hello World");
}
#[test]
pub fn test_string_returns_correct_slice_for_invalid_range() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at 11..5 | decode");
assert_eq!(actual.out, "");
}
#[test]
pub fn test_string_returns_correct_slice_for_max_end() {
let actual = nu!("\"Hello World\" | encode utf8 | bytes at 6..<11 | decode");
assert_eq!(actual.out, "World");
}