|
2 | 2 | // |
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | | -// spell-checker:ignore (words) araba merci |
| 5 | +// spell-checker:ignore (words) araba merci mright |
6 | 6 |
|
7 | 7 | use uutests::new_ucmd; |
8 | 8 | use uutests::util::TestScenario; |
| 9 | +use uutests::util::UCommand; |
9 | 10 | use uutests::util_name; |
10 | 11 |
|
11 | 12 | #[test] |
@@ -393,6 +394,64 @@ fn slash_eight_off_by_one() { |
393 | 394 | .stdout_only(r"\8"); |
394 | 395 | } |
395 | 396 |
|
| 397 | +#[test] |
| 398 | +fn test_normalized_newlines_stdout_is() { |
| 399 | + let res = new_ucmd!().args(&["-ne", "A\r\nB\nC"]).run(); |
| 400 | + |
| 401 | + res.normalized_newlines_stdout_is("A\r\nB\nC"); |
| 402 | + res.normalized_newlines_stdout_is("A\nB\nC"); |
| 403 | + res.normalized_newlines_stdout_is("A\nB\r\nC"); |
| 404 | +} |
| 405 | + |
| 406 | +#[test] |
| 407 | +fn test_normalized_newlines_stdout_is_fail() { |
| 408 | + new_ucmd!() |
| 409 | + .args(&["-ne", "A\r\nB\nC"]) |
| 410 | + .run() |
| 411 | + .stdout_is("A\r\nB\nC"); |
| 412 | +} |
| 413 | + |
| 414 | +#[test] |
| 415 | +fn test_cmd_result_stdout_check_and_stdout_str_check() { |
| 416 | + let result = new_ucmd!().arg("Hello world").run(); |
| 417 | + |
| 418 | + result.stdout_str_check(|stdout| stdout.ends_with("world\n")); |
| 419 | + result.stdout_check(|stdout| stdout.get(0..2).unwrap().eq(b"He")); |
| 420 | + result.no_stderr(); |
| 421 | +} |
| 422 | + |
| 423 | +#[test] |
| 424 | +fn test_cmd_result_stderr_check_and_stderr_str_check() { |
| 425 | + let ts = TestScenario::new("echo"); |
| 426 | + |
| 427 | + let result = UCommand::new() |
| 428 | + .arg(format!( |
| 429 | + "{} {} Hello world >&2", |
| 430 | + ts.bin_path.display(), |
| 431 | + ts.util_name |
| 432 | + )) |
| 433 | + .run(); |
| 434 | + |
| 435 | + result.stderr_str_check(|stderr| stderr.ends_with("world\n")); |
| 436 | + result.stderr_check(|stdout| stdout.get(0..2).unwrap().eq(b"He")); |
| 437 | + result.no_stdout(); |
| 438 | +} |
| 439 | + |
| 440 | +#[test] |
| 441 | +fn test_cmd_result_stdout_str_check_when_false_then_panics() { |
| 442 | + new_ucmd!() |
| 443 | + .args(&["-e", "\\f"]) |
| 444 | + .succeeds() |
| 445 | + .stdout_only("\x0C\n"); |
| 446 | +} |
| 447 | + |
| 448 | +#[cfg(unix)] |
| 449 | +#[test] |
| 450 | +fn test_cmd_result_signal_when_normal_exit_then_no_signal() { |
| 451 | + let result = TestScenario::new("echo").ucmd().run(); |
| 452 | + assert!(result.signal().is_none()); |
| 453 | +} |
| 454 | + |
396 | 455 | mod posixly_correct { |
397 | 456 | use super::*; |
398 | 457 |
|
@@ -444,3 +503,56 @@ mod posixly_correct { |
444 | 503 | .stdout_only("foo"); |
445 | 504 | } |
446 | 505 | } |
| 506 | + |
| 507 | +#[test] |
| 508 | +fn test_child_when_run_with_a_non_blocking_util() { |
| 509 | + new_ucmd!() |
| 510 | + .arg("hello world") |
| 511 | + .run() |
| 512 | + .success() |
| 513 | + .stdout_only("hello world\n"); |
| 514 | +} |
| 515 | + |
| 516 | +// Test basically that most of the methods of UChild are working |
| 517 | +#[test] |
| 518 | +fn test_uchild_when_run_no_wait_with_a_non_blocking_util() { |
| 519 | + let mut child = new_ucmd!().arg("hello world").run_no_wait(); |
| 520 | + |
| 521 | + // check `child.is_alive()` and `child.delay()` is working |
| 522 | + let mut trials = 10; |
| 523 | + while child.is_alive() { |
| 524 | + assert!( |
| 525 | + trials > 0, |
| 526 | + "Assertion failed: child process is still alive." |
| 527 | + ); |
| 528 | + |
| 529 | + child.delay(500); |
| 530 | + trials -= 1; |
| 531 | + } |
| 532 | + |
| 533 | + assert!(!child.is_alive()); |
| 534 | + |
| 535 | + // check `child.is_not_alive()` is working |
| 536 | + assert!(child.is_not_alive()); |
| 537 | + |
| 538 | + // check the current output is correct |
| 539 | + std::assert_eq!(child.stdout(), "hello world\n"); |
| 540 | + assert!(child.stderr().is_empty()); |
| 541 | + |
| 542 | + // check the current output of echo is empty. We already called `child.stdout()` and `echo` |
| 543 | + // exited so there's no additional output after the first call of `child.stdout()` |
| 544 | + assert!(child.stdout().is_empty()); |
| 545 | + assert!(child.stderr().is_empty()); |
| 546 | + |
| 547 | + // check that we're still able to access all output of the child process, even after exit |
| 548 | + // and call to `child.stdout()` |
| 549 | + std::assert_eq!(child.stdout_all(), "hello world\n"); |
| 550 | + assert!(child.stderr_all().is_empty()); |
| 551 | + |
| 552 | + // we should be able to call kill without panics, even if the process already exited |
| 553 | + child.make_assertion().is_not_alive(); |
| 554 | + child.kill(); |
| 555 | + |
| 556 | + // we should be able to call wait without panics and apply some assertions |
| 557 | + child.wait().unwrap().code_is(0).no_stdout().no_stderr(); |
| 558 | +} |
0 commit comments