Skip to content

Commit 54665dd

Browse files
committed
uutests: Move command specific tests in their respective tests
1 parent cffec40 commit 54665dd

File tree

12 files changed

+608
-714
lines changed

12 files changed

+608
-714
lines changed

.vscode/cspell.dictionaries/workspace.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ libc
324324
libstdbuf
325325
musl
326326
tmpd
327+
uchild
327328
ucmd
328329
ucommand
329330
utmpx

tests/by-util/test_cat.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
#[cfg(any(target_os = "linux", target_os = "android"))]
88
use rlimit::Resource;
9-
#[cfg(target_os = "linux")]
9+
#[cfg(unix)]
1010
use std::fs::File;
1111
use std::fs::OpenOptions;
12-
#[cfg(not(windows))]
1312
use std::process::Stdio;
1413
use uutests::at_and_ucmd;
1514
use uutests::new_ucmd;
@@ -671,3 +670,40 @@ fn test_appending_same_input_output() {
671670
.no_stdout()
672671
.stderr_contains("input file is output file");
673672
}
673+
674+
#[cfg(unix)]
675+
#[test]
676+
fn test_uchild_when_no_capture_reading_from_infinite_source() {
677+
use regex::Regex;
678+
679+
let ts = TestScenario::new("cat");
680+
681+
let expected_stdout = b"\0".repeat(12345);
682+
let mut child = ts
683+
.ucmd()
684+
.set_stdin(Stdio::from(File::open("/dev/zero").unwrap()))
685+
.set_stdout(Stdio::piped())
686+
.run_no_wait();
687+
688+
child
689+
.make_assertion()
690+
.with_exact_output(12345, 0)
691+
.stdout_only_bytes(expected_stdout);
692+
693+
child
694+
.kill()
695+
.make_assertion()
696+
.with_current_output()
697+
.stdout_matches(&Regex::new("[\0].*").unwrap())
698+
.no_stderr();
699+
}
700+
701+
#[test]
702+
fn test_child_when_pipe_in() {
703+
let ts = TestScenario::new("cat");
704+
let mut child = ts.ucmd().set_stdin(Stdio::piped()).run_no_wait();
705+
child.pipe_in("content");
706+
child.wait().unwrap().stdout_only("content").success();
707+
708+
ts.ucmd().pipe_in("content").run().stdout_is("content");
709+
}

tests/by-util/test_chcon.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use std::ffi::CString;
1010
use std::path::Path;
1111
use std::{io, iter, str};
1212

13-
use uutests::util::*;
13+
use uutests::at_and_ucmd;
14+
use uutests::new_ucmd;
15+
use uutests::util::TestScenario;
16+
use uutests::util_name;
1417

1518
#[test]
1619
fn version() {

tests/by-util/test_echo.rs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5-
// spell-checker:ignore (words) araba merci
5+
// spell-checker:ignore (words) araba merci mright
66

77
use uutests::new_ucmd;
88
use uutests::util::TestScenario;
9+
use uutests::util::UCommand;
910
use uutests::util_name;
1011

1112
#[test]
@@ -393,6 +394,64 @@ fn slash_eight_off_by_one() {
393394
.stdout_only(r"\8");
394395
}
395396

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+
396455
mod posixly_correct {
397456
use super::*;
398457

@@ -444,3 +503,56 @@ mod posixly_correct {
444503
.stdout_only("foo");
445504
}
446505
}
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

Comments
 (0)