| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
#![allow(clippy::borrow_as_ptr)] |
| 7 |
|
| 8 |
use uutests::{at_and_ucmd, new_ucmd}; |
| 9 |
|
| 10 |
use regex::Regex; |
| 11 |
use std::process::Stdio; |
| 12 |
use std::time::Duration; |
| 13 |
|
| 14 |
// tests for basic tee functionality. |
| 15 |
// inspired by: |
| 16 |
// https://github.com/coreutils/coreutils/tests/misc/tee.sh |
| 17 |
|
| 18 |
// spell-checker:ignore nopipe |
| 19 |
|
| 20 |
#[test] |
| 21 |
#[cfg(unix)] |
| 22 |
fn test_error_stdin_directory() { |
| 23 |
new_ucmd!() |
| 24 |
.set_stdin(std::fs::File::open(".").unwrap()) |
| 25 |
.fails_with_code(1) |
| 26 |
.stderr_is("tee: read error: Is a directory\n"); |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn test_invalid_arg() { |
| 31 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn test_short_help_is_long_help() { |
| 36 |
// I can't believe that this test is necessary. |
| 37 |
let help_short = new_ucmd!() |
| 38 |
.arg("-h") |
| 39 |
.succeeds() |
| 40 |
.no_stderr() |
| 41 |
.stdout_str() |
| 42 |
.to_owned(); |
| 43 |
new_ucmd!() |
| 44 |
.arg("--help") |
| 45 |
.succeeds() |
| 46 |
.no_stderr() |
| 47 |
.stdout_is(help_short); |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn test_tee_processing_multiple_operands() { |
| 52 |
// POSIX says: "Processing of at least 13 file operands shall be supported." |
| 53 |
|
| 54 |
let content = "tee_sample_content"; |
| 55 |
for n in [1, 2, 12, 13] { |
| 56 |
let files = (1..=n).map(|x| x.to_string()).collect::<Vec<_>>(); |
| 57 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 58 |
|
| 59 |
ucmd.args(&files) |
| 60 |
.pipe_in(content) |
| 61 |
.succeeds() |
| 62 |
.stdout_is(content); |
| 63 |
|
| 64 |
for file in &files { |
| 65 |
assert!(at.file_exists(file)); |
| 66 |
assert_eq!(at.read(file), content); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn test_tee_treat_minus_as_filename() { |
| 73 |
// Ensure tee treats '-' as the name of a file, as mandated by POSIX. |
| 74 |
|
| 75 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 76 |
let content = "tee_sample_content"; |
| 77 |
let file = "-"; |
| 78 |
|
| 79 |
ucmd.arg("-").pipe_in(content).succeeds().stdout_is(content); |
| 80 |
|
| 81 |
assert!(at.file_exists(file)); |
| 82 |
assert_eq!(at.read(file), content); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn test_tee_append() { |
| 87 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 88 |
let content = "tee_sample_content"; |
| 89 |
let file = "tee_out"; |
| 90 |
|
| 91 |
at.touch(file); |
| 92 |
at.write(file, content); |
| 93 |
assert_eq!(at.read(file), content); |
| 94 |
|
| 95 |
ucmd.arg("-a") |
| 96 |
.arg(file) |
| 97 |
.pipe_in(content) |
| 98 |
.succeeds() |
| 99 |
.stdout_is(content); |
| 100 |
assert!(at.file_exists(file)); |
| 101 |
assert_eq!(at.read(file), content.repeat(2)); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn test_tee_multiple_append_flags() { |
| 106 |
// Test for bug: https://bugs.launchpad.net/ubuntu/+source/rust-coreutils/+bug/2134578 |
| 107 |
// The command should accept multiple -a flags for different files |
| 108 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 109 |
let content = "don't fail me now rust"; |
| 110 |
let file1 = "log1"; |
| 111 |
let file2 = "log2"; |
| 112 |
|
| 113 |
// Pre-populate files with some content to verify append behavior |
| 114 |
at.write(file1, "existing1\n"); |
| 115 |
at.write(file2, "existing2\n"); |
| 116 |
|
| 117 |
ucmd.args(&["-a", file1, "-a", file2]) |
| 118 |
.pipe_in(content) |
| 119 |
.succeeds() |
| 120 |
.stdout_is(content); |
| 121 |
|
| 122 |
assert!(at.file_exists(file1)); |
| 123 |
assert!(at.file_exists(file2)); |
| 124 |
assert_eq!(at.read(file1), format!("existing1\n{content}")); |
| 125 |
assert_eq!(at.read(file2), format!("existing2\n{content}")); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 130 |
fn test_readonly() { |
| 131 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 132 |
let content_tee = "hello"; |
| 133 |
let content_file = "world"; |
| 134 |
let file_out = "tee_file_out"; |
| 135 |
let writable_file = "tee_file_out2"; |
| 136 |
at.write(file_out, content_file); |
| 137 |
at.set_readonly(file_out); |
| 138 |
ucmd.arg(file_out) |
| 139 |
.arg(writable_file) |
| 140 |
.pipe_in(content_tee) |
| 141 |
.ignore_stdin_write_error() |
| 142 |
.fails() |
| 143 |
.stdout_is(content_tee) |
| 144 |
// Windows says "Access is denied" for some reason. |
| 145 |
.stderr_matches(&Regex::new("(Permission|Access is) denied").unwrap()); |
| 146 |
assert_eq!(at.read(file_out), content_file); |
| 147 |
assert_eq!(at.read(writable_file), content_tee); |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
#[cfg_attr(wasi_runner, ignore = "WASI: no pipe/signal support")] |
| 152 |
fn test_tee_output_not_buffered() { |
| 153 |
// POSIX says: The tee utility shall not buffer output |
| 154 |
|
| 155 |
// If the output is buffered, the test will hang, so we run it in |
| 156 |
// a separate thread to stop execution by timeout. |
| 157 |
let handle = std::thread::spawn(move || { |
| 158 |
let content = "a"; |
| 159 |
let file_out = "tee_file_out"; |
| 160 |
|
| 161 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 162 |
let mut child = ucmd |
| 163 |
.arg(file_out) |
| 164 |
.set_stdin(Stdio::piped()) |
| 165 |
.set_stdout(Stdio::piped()) |
| 166 |
.run_no_wait(); |
| 167 |
|
| 168 |
// We write to the input pipe, but do not close it. If the output is |
| 169 |
// buffered, reading from output pipe will hang indefinitely, as we |
| 170 |
// will never write anything else to it. |
| 171 |
child.write_in(content.as_bytes()); |
| 172 |
|
| 173 |
let out = String::from_utf8(child.stdout_exact_bytes(1)).unwrap(); |
| 174 |
assert_eq!(&out, content); |
| 175 |
|
| 176 |
// Writing to a file may take a couple hundreds nanoseconds |
| 177 |
child.delay(1); |
| 178 |
assert_eq!(at.read(file_out), content); |
| 179 |
}); |
| 180 |
|
| 181 |
// Give some time for the `tee` to create an output file. Some platforms |
| 182 |
// take a lot of time to spin up the process and create the output file |
| 183 |
for _ in 0..100 { |
| 184 |
std::thread::sleep(Duration::from_millis(1)); |
| 185 |
if handle.is_finished() { |
| 186 |
break; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
assert!( |
| 191 |
handle.is_finished(), |
| 192 |
"Nothing was received through output pipe" |
| 193 |
); |
| 194 |
handle.join().unwrap(); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
fn test_tee_continues_after_short_read() { |
| 199 |
// Regression test: `tee` must keep reading until EOF even when the |
| 200 |
// first `read(2)` returns fewer bytes than its internal buffer. This |
| 201 |
// happens in any pipeline where the upstream writer pauses between |
| 202 |
// writes (e.g. a slow producer, a `sleep` in a shell pipeline, or a |
| 203 |
// service emitting log lines in bursts). Treating a short read as |
| 204 |
// end-of-file caused tee to exit prematurely and downstream producers |
| 205 |
// to die with SIGPIPE on their next write. |
| 206 |
// |
| 207 |
// Run in a separate thread so that the test fails via timeout rather |
| 208 |
// than hanging if a regression reintroduces the bug in a form where |
| 209 |
// tee blocks on read instead of exiting early. |
| 210 |
let handle = std::thread::spawn(move || { |
| 211 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 212 |
let file_out = "tee_short_read_out"; |
| 213 |
|
| 214 |
let mut child = ucmd |
| 215 |
.arg(file_out) |
| 216 |
.set_stdin(Stdio::piped()) |
| 217 |
.set_stdout(Stdio::piped()) |
| 218 |
.run_no_wait(); |
| 219 |
|
| 220 |
// First chunk — deliberately much smaller than tee's internal |
| 221 |
// buffer so that `read(2)` returns a short count. |
| 222 |
child.write_in(b"first\n"); |
| 223 |
assert_eq!(&child.stdout_exact_bytes(6), b"first\n"); |
| 224 |
|
| 225 |
// Give a buggy implementation time to exit before we try to |
| 226 |
// write again. |
| 227 |
child.delay(50); |
| 228 |
|
| 229 |
// Second chunk. A correctly-implemented tee is still reading |
| 230 |
// from stdin; a buggy one has already exited and this write |
| 231 |
// will either fail with EPIPE or never reach the output file. |
| 232 |
child.write_in(b"second\n"); |
| 233 |
assert_eq!(&child.stdout_exact_bytes(7), b"second\n"); |
| 234 |
|
| 235 |
// `wait` closes stdin for us before waiting on the child. |
| 236 |
child.wait().unwrap().success(); |
| 237 |
|
| 238 |
assert_eq!(at.read(file_out), "first\nsecond\n"); |
| 239 |
}); |
| 240 |
|
| 241 |
for _ in 0..500 { |
| 242 |
std::thread::sleep(Duration::from_millis(10)); |
| 243 |
if handle.is_finished() { |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
assert!( |
| 249 |
handle.is_finished(), |
| 250 |
"tee did not complete within the timeout" |
| 251 |
); |
| 252 |
handle.join().unwrap(); |
| 253 |
} |
| 254 |
|
| 255 |
#[cfg(all(target_os = "linux", not(wasi_runner)))] |
| 256 |
mod linux_only { |
| 257 |
use uutests::util::{AtPath, CmdResult, UCommand}; |
| 258 |
|
| 259 |
use std::fmt::Write; |
| 260 |
use std::process::Stdio; |
| 261 |
use std::time::Duration; |
| 262 |
use uutests::at_and_ucmd; |
| 263 |
use uutests::new_ucmd; |
| 264 |
|
| 265 |
fn make_broken_pipe() -> std::io::PipeWriter { |
| 266 |
let (read, write) = std::io::pipe().expect("Failed to create pipe"); |
| 267 |
// Drop the read end of the pipe |
| 268 |
drop(read); |
| 269 |
// Return the write end of the pipe |
| 270 |
write |
| 271 |
} |
| 272 |
|
| 273 |
fn make_hanging_read() -> std::io::PipeReader { |
| 274 |
let (read, write) = std::io::pipe().expect("Failed to create pipe"); |
| 275 |
// PURPOSELY leak the write end of the pipe, so the read end hangs. |
| 276 |
std::mem::forget(write); |
| 277 |
// Return the read end of the pipe |
| 278 |
read |
| 279 |
} |
| 280 |
|
| 281 |
fn run_tee(proc: &mut UCommand) -> (String, CmdResult) { |
| 282 |
let content = (1..=100_000).fold(String::new(), |mut output, x| { |
| 283 |
let _ = writeln!(output, "{x}"); |
| 284 |
output |
| 285 |
}); |
| 286 |
|
| 287 |
let result = proc |
| 288 |
.ignore_stdin_write_error() |
| 289 |
.set_stdin(Stdio::piped()) |
| 290 |
.run_no_wait() |
| 291 |
.pipe_in_and_wait(content.as_bytes()); |
| 292 |
|
| 293 |
(content, result) |
| 294 |
} |
| 295 |
|
| 296 |
fn expect_success(result: &CmdResult) { |
| 297 |
assert!( |
| 298 |
result.succeeded(), |
| 299 |
"Command was expected to succeed.\nstdout = {}\n stderr = {}", |
| 300 |
std::str::from_utf8(result.stdout()).unwrap(), |
| 301 |
std::str::from_utf8(result.stderr()).unwrap(), |
| 302 |
); |
| 303 |
assert!( |
| 304 |
result.stderr_str().is_empty(), |
| 305 |
"Unexpected data on stderr.\n stderr = {}", |
| 306 |
std::str::from_utf8(result.stderr()).unwrap(), |
| 307 |
); |
| 308 |
} |
| 309 |
|
| 310 |
fn expect_failure(result: &CmdResult, message: &str) { |
| 311 |
assert!( |
| 312 |
!result.succeeded(), |
| 313 |
"Command was expected to fail.\nstdout = {}\n stderr = {}", |
| 314 |
std::str::from_utf8(result.stdout()).unwrap(), |
| 315 |
std::str::from_utf8(result.stderr()).unwrap(), |
| 316 |
); |
| 317 |
assert!( |
| 318 |
result.stderr_str().contains(message), |
| 319 |
"Expected to see error message fragment {message} in stderr, but did not.\n stderr = {}", |
| 320 |
std::str::from_utf8(result.stderr()).unwrap(), |
| 321 |
); |
| 322 |
} |
| 323 |
|
| 324 |
fn expect_silent_failure(result: &CmdResult) { |
| 325 |
assert!( |
| 326 |
!result.succeeded(), |
| 327 |
"Command was expected to fail.\nstdout = {}\n stderr = {}", |
| 328 |
std::str::from_utf8(result.stdout()).unwrap(), |
| 329 |
std::str::from_utf8(result.stderr()).unwrap(), |
| 330 |
); |
| 331 |
assert!( |
| 332 |
result.stderr_str().is_empty(), |
| 333 |
"Unexpected data on stderr.\n stderr = {}", |
| 334 |
std::str::from_utf8(result.stderr()).unwrap(), |
| 335 |
); |
| 336 |
} |
| 337 |
|
| 338 |
fn expect_correct(name: &str, at: &AtPath, contents: &str) { |
| 339 |
assert!(at.file_exists(name)); |
| 340 |
let compare = at.read(name); |
| 341 |
assert_eq!(compare, contents); |
| 342 |
} |
| 343 |
|
| 344 |
fn expect_short(name: &str, at: &AtPath, contents: &str) { |
| 345 |
assert!(at.file_exists(name)); |
| 346 |
let compare = at.read(name); |
| 347 |
assert!( |
| 348 |
compare.len() < contents.len(), |
| 349 |
"Too many bytes ({}) written to {name} (should be a short count from {})", |
| 350 |
compare.len(), |
| 351 |
contents.len() |
| 352 |
); |
| 353 |
assert!( |
| 354 |
contents.starts_with(&compare), |
| 355 |
"Expected truncated output to be a prefix of the correct output, but it isn't.\n Correct: {contents}\n Compare: {compare}" |
| 356 |
); |
| 357 |
} |
| 358 |
|
| 359 |
#[test] |
| 360 |
fn test_pipe_error_default() { |
| 361 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 362 |
|
| 363 |
let file_out_a = "tee_file_out_a"; |
| 364 |
|
| 365 |
let proc = ucmd.arg(file_out_a).set_stdout(make_broken_pipe()); |
| 366 |
|
| 367 |
let (content, output) = run_tee(proc); |
| 368 |
|
| 369 |
expect_silent_failure(&output); |
| 370 |
expect_short(file_out_a, &at, content.as_str()); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn test_pipe_error_warn_nopipe_1() { |
| 375 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 376 |
|
| 377 |
let file_out_a = "tee_file_out_a"; |
| 378 |
|
| 379 |
let proc = ucmd |
| 380 |
.arg("-p") |
| 381 |
.arg(file_out_a) |
| 382 |
.set_stdout(make_broken_pipe()); |
| 383 |
|
| 384 |
let (content, output) = run_tee(proc); |
| 385 |
|
| 386 |
expect_success(&output); |
| 387 |
expect_correct(file_out_a, &at, content.as_str()); |
| 388 |
} |
| 389 |
|
| 390 |
#[test] |
| 391 |
fn test_pipe_error_warn_nopipe_2() { |
| 392 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 393 |
|
| 394 |
let file_out_a = "tee_file_out_a"; |
| 395 |
|
| 396 |
let proc = ucmd |
| 397 |
.arg("--output-error") |
| 398 |
.arg(file_out_a) |
| 399 |
.set_stdout(make_broken_pipe()); |
| 400 |
|
| 401 |
let (content, output) = run_tee(proc); |
| 402 |
|
| 403 |
expect_success(&output); |
| 404 |
expect_correct(file_out_a, &at, content.as_str()); |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn test_pipe_error_warn_nopipe_3() { |
| 409 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 410 |
|
| 411 |
let file_out_a = "tee_file_out_a"; |
| 412 |
|
| 413 |
let proc = ucmd |
| 414 |
.arg("--output-error=warn-nopipe") |
| 415 |
.arg(file_out_a) |
| 416 |
.set_stdout(make_broken_pipe()); |
| 417 |
|
| 418 |
let (content, output) = run_tee(proc); |
| 419 |
|
| 420 |
expect_success(&output); |
| 421 |
expect_correct(file_out_a, &at, content.as_str()); |
| 422 |
} |
| 423 |
|
| 424 |
#[test] |
| 425 |
fn test_pipe_error_warn_nopipe_3_shortcut() { |
| 426 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 427 |
|
| 428 |
let file_out_a = "tee_file_out_a"; |
| 429 |
|
| 430 |
let proc = ucmd |
| 431 |
.arg("--output-error=warn-") |
| 432 |
.arg(file_out_a) |
| 433 |
.set_stdout(make_broken_pipe()); |
| 434 |
|
| 435 |
let (content, output) = run_tee(proc); |
| 436 |
|
| 437 |
expect_success(&output); |
| 438 |
expect_correct(file_out_a, &at, content.as_str()); |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn test_pipe_error_warn() { |
| 443 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 444 |
|
| 445 |
let file_out_a = "tee_file_out_a"; |
| 446 |
|
| 447 |
let proc = ucmd |
| 448 |
.arg("--output-error=warn") |
| 449 |
.arg(file_out_a) |
| 450 |
.set_stdout(make_broken_pipe()); |
| 451 |
|
| 452 |
let (content, output) = run_tee(proc); |
| 453 |
|
| 454 |
expect_failure(&output, "Broken pipe"); |
| 455 |
expect_correct(file_out_a, &at, content.as_str()); |
| 456 |
} |
| 457 |
|
| 458 |
#[test] |
| 459 |
fn test_pipe_error_exit() { |
| 460 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 461 |
|
| 462 |
let file_out_a = "tee_file_out_a"; |
| 463 |
|
| 464 |
let proc = ucmd |
| 465 |
.arg("--output-error=exit") |
| 466 |
.arg(file_out_a) |
| 467 |
.set_stdout(make_broken_pipe()); |
| 468 |
|
| 469 |
let (content, output) = run_tee(proc); |
| 470 |
|
| 471 |
expect_failure(&output, "Broken pipe"); |
| 472 |
expect_short(file_out_a, &at, content.as_str()); |
| 473 |
} |
| 474 |
|
| 475 |
#[test] |
| 476 |
fn test_pipe_error_exit_nopipe() { |
| 477 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 478 |
|
| 479 |
let file_out_a = "tee_file_out_a"; |
| 480 |
|
| 481 |
let proc = ucmd |
| 482 |
.arg("--output-error=exit-nopipe") |
| 483 |
.arg(file_out_a) |
| 484 |
.set_stdout(make_broken_pipe()); |
| 485 |
|
| 486 |
let (content, output) = run_tee(proc); |
| 487 |
|
| 488 |
expect_success(&output); |
| 489 |
expect_correct(file_out_a, &at, content.as_str()); |
| 490 |
} |
| 491 |
|
| 492 |
#[test] |
| 493 |
fn test_pipe_error_exit_nopipe_shortcut() { |
| 494 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 495 |
|
| 496 |
let file_out_a = "tee_file_out_a"; |
| 497 |
|
| 498 |
let proc = ucmd |
| 499 |
.arg("--output-error=exit-nop") |
| 500 |
.arg(file_out_a) |
| 501 |
.set_stdout(make_broken_pipe()); |
| 502 |
|
| 503 |
let (content, output) = run_tee(proc); |
| 504 |
|
| 505 |
expect_success(&output); |
| 506 |
expect_correct(file_out_a, &at, content.as_str()); |
| 507 |
} |
| 508 |
|
| 509 |
#[test] |
| 510 |
fn test_space_error_default() { |
| 511 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 512 |
|
| 513 |
let file_out_a = "tee_file_out_a"; |
| 514 |
|
| 515 |
let proc = ucmd.arg(file_out_a).arg("/dev/full"); |
| 516 |
|
| 517 |
let (content, output) = run_tee(proc); |
| 518 |
|
| 519 |
expect_failure(&output, "No space left"); |
| 520 |
expect_correct(file_out_a, &at, content.as_str()); |
| 521 |
} |
| 522 |
|
| 523 |
#[test] |
| 524 |
fn test_space_error_warn_nopipe_1() { |
| 525 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 526 |
|
| 527 |
let file_out_a = "tee_file_out_a"; |
| 528 |
|
| 529 |
let proc = ucmd |
| 530 |
.arg("-p") |
| 531 |
.arg(file_out_a) |
| 532 |
.arg("/dev/full") |
| 533 |
.set_stdout(make_broken_pipe()); |
| 534 |
|
| 535 |
let (content, output) = run_tee(proc); |
| 536 |
|
| 537 |
expect_failure(&output, "No space left"); |
| 538 |
expect_correct(file_out_a, &at, content.as_str()); |
| 539 |
} |
| 540 |
|
| 541 |
#[test] |
| 542 |
fn test_space_error_warn_nopipe_2() { |
| 543 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 544 |
|
| 545 |
let file_out_a = "tee_file_out_a"; |
| 546 |
|
| 547 |
let proc = ucmd |
| 548 |
.arg("--output-error") |
| 549 |
.arg(file_out_a) |
| 550 |
.arg("/dev/full") |
| 551 |
.set_stdout(make_broken_pipe()); |
| 552 |
|
| 553 |
let (content, output) = run_tee(proc); |
| 554 |
|
| 555 |
expect_failure(&output, "No space left"); |
| 556 |
expect_correct(file_out_a, &at, content.as_str()); |
| 557 |
} |
| 558 |
|
| 559 |
#[test] |
| 560 |
fn test_space_error_warn_nopipe_3() { |
| 561 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 562 |
|
| 563 |
let file_out_a = "tee_file_out_a"; |
| 564 |
|
| 565 |
let proc = ucmd |
| 566 |
.arg("--output-error=warn-nopipe") |
| 567 |
.arg(file_out_a) |
| 568 |
.arg("/dev/full") |
| 569 |
.set_stdout(make_broken_pipe()); |
| 570 |
|
| 571 |
let (content, output) = run_tee(proc); |
| 572 |
|
| 573 |
expect_failure(&output, "No space left"); |
| 574 |
expect_correct(file_out_a, &at, content.as_str()); |
| 575 |
} |
| 576 |
|
| 577 |
#[test] |
| 578 |
fn test_space_error_warn() { |
| 579 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 580 |
|
| 581 |
let file_out_a = "tee_file_out_a"; |
| 582 |
|
| 583 |
let proc = ucmd |
| 584 |
.arg("--output-error=warn") |
| 585 |
.arg(file_out_a) |
| 586 |
.arg("/dev/full") |
| 587 |
.set_stdout(make_broken_pipe()); |
| 588 |
|
| 589 |
let (content, output) = run_tee(proc); |
| 590 |
|
| 591 |
expect_failure(&output, "No space left"); |
| 592 |
expect_correct(file_out_a, &at, content.as_str()); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn test_space_error_exit() { |
| 597 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 598 |
|
| 599 |
let file_out_a = "tee_file_out_a"; |
| 600 |
|
| 601 |
let proc = ucmd |
| 602 |
.arg("--output-error=exit") |
| 603 |
.arg(file_out_a) |
| 604 |
.arg("/dev/full") |
| 605 |
.set_stdout(make_broken_pipe()); |
| 606 |
|
| 607 |
let (content, output) = run_tee(proc); |
| 608 |
|
| 609 |
expect_failure(&output, "No space left"); |
| 610 |
expect_short(file_out_a, &at, content.as_str()); |
| 611 |
} |
| 612 |
|
| 613 |
#[test] |
| 614 |
fn test_space_error_exit_nopipe() { |
| 615 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 616 |
|
| 617 |
let file_out_a = "tee_file_out_a"; |
| 618 |
|
| 619 |
let proc = ucmd |
| 620 |
.arg("--output-error=exit-nopipe") |
| 621 |
.arg(file_out_a) |
| 622 |
.arg("/dev/full") |
| 623 |
.set_stdout(make_broken_pipe()); |
| 624 |
|
| 625 |
let (content, output) = run_tee(proc); |
| 626 |
|
| 627 |
expect_failure(&output, "No space left"); |
| 628 |
expect_short(file_out_a, &at, content.as_str()); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn test_pipe_mode_broken_pipe_only() { |
| 633 |
new_ucmd!() |
| 634 |
.timeout(Duration::from_secs(1)) |
| 635 |
.arg("-p") |
| 636 |
.set_stdin(make_hanging_read()) |
| 637 |
.set_stdout(make_broken_pipe()) |
| 638 |
.succeeds(); |
| 639 |
} |
| 640 |
|
| 641 |
#[test] |
| 642 |
fn test_pipe_mode_broken_pipe_file() { |
| 643 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 644 |
|
| 645 |
let file_out_a = "tee_file_out_a"; |
| 646 |
|
| 647 |
let proc = ucmd |
| 648 |
.arg("-p") |
| 649 |
.arg(file_out_a) |
| 650 |
.set_stdout(make_broken_pipe()); |
| 651 |
|
| 652 |
let (content, output) = run_tee(proc); |
| 653 |
|
| 654 |
expect_success(&output); |
| 655 |
expect_correct(file_out_a, &at, content.as_str()); |
| 656 |
} |
| 657 |
|
| 658 |
#[test] |
| 659 |
fn test_tee_no_more_writeable_1() { |
| 660 |
// equals to 'tee /dev/full out2 <multi_read' call |
| 661 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 662 |
let content = (1..=10).fold(String::new(), |mut output, x| { |
| 663 |
writeln!(output, "{x}").unwrap(); |
| 664 |
output |
| 665 |
}); |
| 666 |
let file_out = "tee_file_out"; |
| 667 |
|
| 668 |
ucmd.arg("/dev/full") |
| 669 |
.arg(file_out) |
| 670 |
.pipe_in(&content[..]) |
| 671 |
.fails() |
| 672 |
.stdout_contains(&content) |
| 673 |
.stderr_is("tee: /dev/full: No space left on device\n"); |
| 674 |
|
| 675 |
assert_eq!(at.read(file_out), content); |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn test_tee_no_more_writeable_2() { |
| 680 |
use std::fs::File; |
| 681 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 682 |
let content = (1..=10).fold(String::new(), |mut output, x| { |
| 683 |
let _ = writeln!(output, "{x}"); |
| 684 |
output |
| 685 |
}); |
| 686 |
let file_out_a = "tee_file_out_a"; |
| 687 |
let file_out_b = "tee_file_out_b"; |
| 688 |
let dev_full = File::options().append(true).open("/dev/full").unwrap(); |
| 689 |
|
| 690 |
let result = ucmd |
| 691 |
.arg(file_out_a) |
| 692 |
.arg(file_out_b) |
| 693 |
.set_stdout(dev_full) |
| 694 |
.pipe_in(content.as_bytes()) |
| 695 |
.fails(); |
| 696 |
|
| 697 |
assert_eq!(at.read(file_out_a), content); |
| 698 |
assert_eq!(at.read(file_out_b), content); |
| 699 |
assert!(result.stderr_str().contains("No space left on device")); |
| 700 |
} |
| 701 |
|
| 702 |
#[test] |
| 703 |
fn test_permission_denied_clean() { |
| 704 |
new_ucmd!() |
| 705 |
.arg("/dev/mem") |
| 706 |
.fails_with_code(1) |
| 707 |
.stderr_is("tee: /dev/mem: Permission denied\n"); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
// Additional cross-platform tee tests to cover GNU compatibility around --output-error |
| 712 |
#[test] |
| 713 |
fn test_output_error_flag_without_value_defaults_warn_nopipe() { |
| 714 |
// When --output-error is present without an explicit value, it should default to warn-nopipe |
| 715 |
// We can't easily simulate a broken pipe across all platforms here, but we can ensure |
| 716 |
// the flag is accepted without error and basic tee functionality still works. |
| 717 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 718 |
let file_out = "tee_output_error_default.txt"; |
| 719 |
let content = "abc"; |
| 720 |
|
| 721 |
let result = ucmd |
| 722 |
.arg("--output-error") |
| 723 |
.arg(file_out) |
| 724 |
.pipe_in(content) |
| 725 |
.succeeds(); |
| 726 |
|
| 727 |
result.stdout_is(content); |
| 728 |
assert!(at.file_exists(file_out)); |
| 729 |
assert_eq!(at.read(file_out), content); |
| 730 |
} |
| 731 |
// Unix-only: presence-only --output-error should not crash on broken pipe. |
| 732 |
// Current implementation may exit zero; we only assert the process exits to avoid flakiness. |
| 733 |
// TODO: When semantics are aligned with GNU warn-nopipe, strengthen assertions here. |
| 734 |
#[cfg(all(unix, not(target_os = "freebsd")))] |
| 735 |
#[test] |
| 736 |
fn test_output_error_presence_only_broken_pipe_unix() { |
| 737 |
let (read, write) = std::io::pipe().expect("Failed to create pipe"); |
| 738 |
// Close the read end to simulate a broken pipe on stdout |
| 739 |
drop(read); |
| 740 |
let content = (0..10_000).map(|_| "x").collect::<String>(); |
| 741 |
let result = new_ucmd!() |
| 742 |
.arg("--output-error") // presence-only flag |
| 743 |
.set_stdout(write) |
| 744 |
.pipe_in(content.as_bytes()) |
| 745 |
.run(); |
| 746 |
|
| 747 |
// Assert that a status was produced (i.e., process exited) and no crash occurred. |
| 748 |
assert!(result.try_exit_status().is_some(), "process did not exit"); |
| 749 |
} |
| 750 |
|
| 751 |
// Skip on FreeBSD due to repeated CI hangs in FreeBSD VM (see PR #8684) |
| 752 |
#[cfg(all(unix, not(target_os = "freebsd")))] |
| 753 |
#[test] |
| 754 |
fn test_broken_pipe_early_termination_stdout_only() { |
| 755 |
let (read, write) = std::io::pipe().expect("Failed to create pipe"); |
| 756 |
// Create a broken stdout |
| 757 |
drop(read); |
| 758 |
let content = (0..10_000).map(|_| "x").collect::<String>(); |
| 759 |
let mut proc = new_ucmd!(); |
| 760 |
let result = proc |
| 761 |
.set_stdout(write) |
| 762 |
.ignore_stdin_write_error() |
| 763 |
.pipe_in(content.as_bytes()) |
| 764 |
.run(); |
| 765 |
// GNU tee exits nonzero on broken pipe unless configured otherwise; implementation |
| 766 |
// details vary by mode, but we should not panic and should return an exit status. |
| 767 |
// Assert that a status was produced (i.e., process exited) and no crash occurred. |
| 768 |
assert!(result.try_exit_status().is_some(), "process did not exit"); |
| 769 |
} |
| 770 |
|
| 771 |
#[test] |
| 772 |
fn test_write_failure_reports_error_and_nonzero_exit() { |
| 773 |
// Simulate a file open failure which should be reported via show_error and cause a failure |
| 774 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 775 |
// Create a directory and try to use it as an output file (open will fail) |
| 776 |
at.mkdir("out_dir"); |
| 777 |
|
| 778 |
let result = ucmd.arg("out_dir").pipe_in("data").fails(); |
| 779 |
|
| 780 |
assert!(!result.stderr_str().is_empty()); |
| 781 |
} |