1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

yes: support non-UTF-8 args

Also, tighten the creation of the output buffer.  Rather than copy "y\n"
8192 times, or any other input some number of times, it can be doubled
in place using Vec::extend_from_within.
This commit is contained in:
Jed Denlea 2023-05-14 15:29:54 -07:00
parent caaf25a003
commit 3870ee252a
4 changed files with 161 additions and 28 deletions

View file

@ -1,3 +1,4 @@
use std::ffi::OsStr;
use std::process::{ExitStatus, Stdio};
#[cfg(unix)]
@ -15,8 +16,10 @@ fn check_termination(result: ExitStatus) {
assert!(result.success(), "yes did not exit successfully");
}
const NO_ARGS: &[&str] = &[];
/// Run `yes`, capture some of the output, close the pipe, and verify it.
fn run(args: &[&str], expected: &[u8]) {
fn run(args: &[impl AsRef<OsStr>], expected: &[u8]) {
let mut cmd = new_ucmd!();
let mut child = cmd.args(args).set_stdout(Stdio::piped()).run_no_wait();
let buf = child.stdout_exact_bytes(expected.len());
@ -34,7 +37,7 @@ fn test_invalid_arg() {
#[test]
fn test_simple() {
run(&[], b"y\ny\ny\ny\n");
run(NO_ARGS, b"y\ny\ny\ny\n");
}
#[test]
@ -44,7 +47,7 @@ fn test_args() {
#[test]
fn test_long_output() {
run(&[], "y\n".repeat(512 * 1024).as_bytes());
run(NO_ARGS, "y\n".repeat(512 * 1024).as_bytes());
}
/// Test with an output that seems likely to get mangled in case of incomplete writes.
@ -88,3 +91,20 @@ fn test_piped_to_dev_full() {
}
}
}
#[test]
#[cfg(any(unix, target_os = "wasi"))]
fn test_non_utf8() {
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::OsStrExt;
run(
&[
OsStr::from_bytes(b"\xbf\xff\xee"),
OsStr::from_bytes(b"bar"),
],
&b"\xbf\xff\xee bar\n".repeat(5000),
);
}