Skip to content

Commit

Permalink
tests/util: Improve UChild. Cleanup redundant functions in util.rs.
Browse files Browse the repository at this point in the history
A short summary of changes:

* Add some basic tests for UChild and the run methods.
* Try more often in a fixed interval to create the tempfile for CapturedOutput.
* Fix drop order of struct fields for better cleanup of temporary files/dirs.
* Mark UChild::wait_with_output and UChild::pipe_in_and_wait_with_output deprecated
* Make CapturedOutput private
* Panic in stdout_all, stdout_all_bytes etc. if output is not captured.
* Rename some methods, refactor, clean up, fix documentation, add try_... methods
  • Loading branch information
Joining7943 committed Nov 29, 2022
1 parent b3a8a23 commit c1f9bb8
Show file tree
Hide file tree
Showing 5 changed files with 550 additions and 276 deletions.
16 changes: 8 additions & 8 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,12 @@ fn test_rm_descend_directory() {
at.touch(file_2);

let mut child = scene.ucmd().arg("-ri").arg("a").run_no_wait();
child.write_in(yes.as_bytes()).unwrap();
child.write_in(yes.as_bytes()).unwrap();
child.write_in(yes.as_bytes()).unwrap();
child.write_in(yes.as_bytes()).unwrap();
child.write_in(yes.as_bytes()).unwrap();
child.write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();

child.wait().unwrap();

Expand Down Expand Up @@ -447,7 +447,7 @@ fn test_rm_prompts() {

let mut child = scene.ucmd().arg("-ri").arg("a").run_no_wait();
for _ in 0..9 {
child.write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();
}

let result = child.wait().unwrap();
Expand Down Expand Up @@ -487,7 +487,7 @@ fn test_rm_force_prompts_order() {

// This should cause rm to prompt to remove regular empty file
let mut child = scene.ucmd().arg("-fi").arg(empty_file).run_no_wait();
child.write_in(yes.as_bytes()).unwrap();
child.try_write_in(yes.as_bytes()).unwrap();

let result = child.wait().unwrap();
let string_output = result.stderr_str();
Expand Down
19 changes: 8 additions & 11 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn test_follow_non_utf8_bytes() {
.run_no_wait();

child
.make_assertion_with_delay(100)
.make_assertion_with_delay(500)
.is_alive()
.with_current_output()
.stdout_only_fixture("foobar_single_default.expected");
Expand Down Expand Up @@ -712,7 +712,7 @@ fn test_follow_with_pid() {
.run_no_wait();

child
.make_assertion_with_delay(100)
.make_assertion_with_delay(500)
.is_alive()
.with_current_output()
.stdout_only_fixture("foobar_follow_multiple.expected");
Expand Down Expand Up @@ -1392,9 +1392,8 @@ fn test_retry5() {
at.mkdir(missing);
p.delay(delay);

p.make_assertion().is_not_alive();
p.kill()
.make_assertion()
p.make_assertion()
.is_not_alive()
.with_all_output()
.stderr_only(expected_stderr)
.failure();
Expand Down Expand Up @@ -1895,9 +1894,8 @@ fn test_follow_name_remove() {
p.delay(delay);

if i == 0 {
p.make_assertion().is_not_alive();
p.kill()
.make_assertion()
p.make_assertion()
.is_not_alive()
.with_all_output()
.stdout_is(&expected_stdout)
.stderr_is(&expected_stderr[i])
Expand Down Expand Up @@ -2299,9 +2297,8 @@ fn test_follow_name_move1() {
.stderr_is(&expected_stderr[i])
.stdout_is(&expected_stdout);
} else {
p.make_assertion().is_not_alive();
p.kill()
.make_assertion()
p.make_assertion()
.is_not_alive()
.with_all_output()
.stderr_is(&expected_stderr[i])
.stdout_is(&expected_stdout)
Expand Down
2 changes: 2 additions & 0 deletions tests/by-util/test_tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ mod linux_only {

fn run_tee(proc: &mut UCommand) -> (String, Output) {
let content = (1..=100000).map(|x| format!("{}\n", x)).collect::<String>();

#[allow(deprecated)]
let output = proc
.run_no_wait()
.pipe_in_and_wait_with_output(content.as_bytes());
Expand Down
2 changes: 2 additions & 0 deletions tests/by-util/test_yes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fn run(args: &[&str], expected: &[u8]) {
let mut child = cmd.args(args).set_stdout(Stdio::piped()).run_no_wait();
let buf = child.stdout_exact_bytes(expected.len());
child.close_stdout();

#[allow(deprecated)]
check_termination(&child.wait_with_output().unwrap().status);
assert_eq!(buf.as_slice(), expected);
}
Expand Down
Loading

0 comments on commit c1f9bb8

Please sign in to comment.