Skip to content

Commit

Permalink
tests/util: Do not trim stderr in CmdResult::stderr_is. Add method st…
Browse files Browse the repository at this point in the history
…derr_trimmed_is.

Adjust tests to use `stderr_trimmed_is` instead of `stderr_is` when appropriate. Prepend call of
`stderr_only` with `stderr_str_apply(str:trim)`.
  • Loading branch information
Joining7943 committed Jan 6, 2023
1 parent 19a1175 commit b2e4a22
Show file tree
Hide file tree
Showing 39 changed files with 266 additions and 158 deletions.
1 change: 1 addition & 0 deletions tests/by-util/test_base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,6 @@ fn test_base32_file_not_found() {
new_ucmd!()
.arg("a.txt")
.fails()
.stderr_str_apply(str::trim)
.stderr_only("base32: a.txt: No such file or directory");
}
1 change: 1 addition & 0 deletions tests/by-util/test_base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ fn test_base64_file_not_found() {
new_ucmd!()
.arg("a.txt")
.fails()
.stderr_str_apply(str::trim)
.stderr_only("base64: a.txt: No such file or directory");
}
2 changes: 2 additions & 0 deletions tests/by-util/test_basenc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ fn test_z85_not_padded() {
.args(&["--z85", "-d"])
.pipe_in("##########")
.fails()
.stderr_str_apply(str::trim)
.stderr_only("basenc: error: invalid input");
new_ucmd!()
.args(&["--z85"])
.pipe_in("123")
.fails()
.stderr_str_apply(str::trim)
.stderr_only("basenc: error: invalid input (length must be multiple of 4 characters)");
}
5 changes: 3 additions & 2 deletions tests/by-util/test_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn test_directory() {
s.ucmd()
.args(&["test_directory"])
.fails()
.stderr_is("cat: test_directory: Is a directory");
.stderr_trimmed_is("cat: test_directory: Is a directory");
}

#[test]
Expand All @@ -194,7 +194,7 @@ fn test_directory_and_file() {
s.ucmd()
.args(&["test_directory2", fixture])
.fails()
.stderr_is("cat: test_directory2: Is a directory")
.stderr_trimmed_is("cat: test_directory2: Is a directory")
.stdout_is_fixture(fixture);
}
}
Expand Down Expand Up @@ -527,6 +527,7 @@ fn test_write_to_self() {
.arg("second_file")
.fails()
.code_is(2)
.stderr_str_apply(str::trim)
.stderr_only("cat: first_file: input file is output file\ncat: first_file: input file is output file");

assert_eq!(
Expand Down
14 changes: 8 additions & 6 deletions tests/by-util/test_chgrp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn test_invalid_group() {
.arg("__nosuchgroup__")
.arg("/")
.fails()
.stderr_is("chgrp: invalid group: '__nosuchgroup__'");
.stderr_trimmed_is("chgrp: invalid group: '__nosuchgroup__'");
}

#[test]
Expand Down Expand Up @@ -92,7 +92,7 @@ fn test_preserve_root() {
.arg("-R")
.arg("bin").arg(d)
.fails()
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
.stderr_trimmed_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
}
}

Expand All @@ -111,7 +111,7 @@ fn test_preserve_root_symlink() {
.arg("-HR")
.arg("bin").arg(file)
.fails()
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
.stderr_trimmed_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
}

let (at, mut ucmd) = at_and_ucmd!();
Expand All @@ -120,15 +120,15 @@ fn test_preserve_root_symlink() {
.arg("-HR")
.arg("bin").arg(format!(".//{}/..//..//../../", file))
.fails()
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
.stderr_trimmed_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");

let (at, mut ucmd) = at_and_ucmd!();
at.symlink_file("/", "__root__");
ucmd.arg("--preserve-root")
.arg("-R")
.arg("bin").arg("__root__/.")
.fails()
.stderr_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
.stderr_trimmed_is("chgrp: it is dangerous to operate recursively on '/'\nchgrp: use --no-preserve-root to override this failsafe");
}

#[test]
Expand All @@ -143,7 +143,7 @@ fn test_reference() {
.arg("--reference=/etc/passwd")
.arg("/etc")
.fails()
.stderr_is("chgrp: changing group of '/etc': Operation not permitted (os error 1)\nfailed to change group of '/etc' from root to root");
.stderr_trimmed_is("chgrp: changing group of '/etc': Operation not permitted (os error 1)\nfailed to change group of '/etc' from root to root");
}
}

Expand Down Expand Up @@ -270,6 +270,7 @@ fn test_permission_denied() {
.arg(group.as_raw().to_string())
.arg("dir")
.fails()
.stderr_str_apply(str::trim)
.stderr_only("chgrp: cannot access 'dir': Permission denied");
}
}
Expand All @@ -289,6 +290,7 @@ fn test_subdir_permission_denied() {
.arg(group.as_raw().to_string())
.arg("dir")
.fails()
.stderr_str_apply(str::trim)
.stderr_only("chgrp: cannot access 'dir/subdir': Permission denied");
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/by-util/test_chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn test_chmod_ugoa() {
.fails()
.code_is(1)
// spell-checker:disable-next-line
.stderr_is("chmod: file: new permissions are r-xrwxrwx, not r-xr-xr-x");
.stderr_trimmed_is("chmod: file: new permissions are r-xrwxrwx, not r-xr-xr-x");
assert_eq!(
metadata(at.plus("file")).unwrap().permissions().mode(),
0o100577
Expand Down Expand Up @@ -314,7 +314,7 @@ fn test_permission_denied() {
.arg("o=r")
.arg("d")
.fails()
.stderr_is("chmod: 'd/no-x/y': Permission denied");
.stderr_trimmed_is("chmod: 'd/no-x/y': Permission denied");
}

#[test]
Expand All @@ -341,7 +341,7 @@ fn test_chmod_recursive() {
.arg("a")
.arg("z")
.fails()
.stderr_is("chmod: Permission denied");
.stderr_trimmed_is("chmod: Permission denied");

assert_eq!(at.metadata("z/y").permissions().mode(), 0o100444);
assert_eq!(at.metadata("a/a").permissions().mode(), 0o100444);
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_chroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_no_such_directory() {

ucmd.arg("a")
.fails()
.stderr_is("chroot: cannot change root directory to 'a': no such directory")
.stderr_trimmed_is("chroot: cannot change root directory to 'a': no such directory")
.code_is(125);
}

Expand Down
1 change: 1 addition & 0 deletions tests/by-util/test_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,6 @@ fn test_no_such_file() {
new_ucmd!()
.args(&["bogus_file_1", "bogus_file_2"])
.fails()
.stderr_str_apply(str::trim)
.stderr_only("comm: bogus_file_1: No such file or directory");
}
17 changes: 16 additions & 1 deletion tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ fn test_cp_backup_simple_protect_source() {
.arg(&source)
.arg(TEST_HELLO_WORLD_SOURCE)
.fails()
.stderr_str_apply(str::trim)
.stderr_only(format!(
"cp: backing up '{}' might destroy source; '{}' not copied",
TEST_HELLO_WORLD_SOURCE, source,
Expand Down Expand Up @@ -1441,6 +1442,7 @@ fn test_cp_reflink_insufficient_permission() {
.arg("unreadable")
.arg(TEST_EXISTING_FILE)
.fails()
.stderr_str_apply(str::trim)
.stderr_only("cp: 'unreadable' -> 'existing_file.txt': Permission denied (os error 13)");
}

Expand Down Expand Up @@ -1779,6 +1781,7 @@ fn test_copy_through_just_created_symlink() {
.arg("b/1")
.arg("c")
.fails()
.stderr_str_apply(str::trim)
.stderr_only(if cfg!(not(target_os = "windows")) {
"cp: will not copy 'b/1' through just-created symlink 'c/1'"
} else {
Expand All @@ -1798,6 +1801,7 @@ fn test_copy_through_dangling_symlink() {
ucmd.arg("file")
.arg("target")
.fails()
.stderr_str_apply(str::trim)
.stderr_only("cp: not writing through dangling symlink 'target'");
}

Expand Down Expand Up @@ -1850,6 +1854,7 @@ fn test_copy_through_dangling_symlink_no_dereference_2() {
at.symlink_file("nonexistent", "target");
ucmd.args(&["-P", "file", "target"])
.fails()
.stderr_str_apply(str::trim)
.stderr_only("cp: not writing through dangling symlink 'target'");
}

Expand All @@ -1862,6 +1867,7 @@ fn test_copy_through_dangling_symlink_force() {
at.symlink_file("no-such-file", "dest");
ucmd.args(&["--force", "src", "dest"])
.fails()
.stderr_str_apply(str::trim)
.stderr_only("cp: not writing through dangling symlink 'dest'");
assert!(!at.file_exists("dest"));
}
Expand All @@ -1874,6 +1880,7 @@ fn test_cp_archive_on_nonexistent_file() {
.arg(TEST_NONEXISTENT_FILE)
.arg(TEST_EXISTING_FILE)
.fails()
.stderr_str_apply(str::trim)
.stderr_only(
"cp: cannot stat 'nonexistent_file.txt': No such file or directory (os error 2)",
);
Expand Down Expand Up @@ -1960,6 +1967,7 @@ fn test_cp_dir_vs_file() {
.arg(TEST_COPY_FROM_FOLDER)
.arg(TEST_EXISTING_FILE)
.fails()
.stderr_str_apply(str::trim)
.stderr_only("cp: cannot overwrite non-directory with directory");
}

Expand Down Expand Up @@ -2205,7 +2213,10 @@ fn test_copy_directory_to_itself_disallowed() {
let expected = "cp: cannot copy a directory, 'd', into itself, 'd/d'";
#[cfg(windows)]
let expected = "cp: cannot copy a directory, 'd', into itself, 'd\\d'";
ucmd.args(&["-R", "d", "d"]).fails().stderr_only(expected);
ucmd.args(&["-R", "d", "d"])
.fails()
.stderr_str_apply(str::trim)
.stderr_only(expected);
}

/// Test that copying a nested directory to itself is disallowed.
Expand All @@ -2221,6 +2232,7 @@ fn test_copy_nested_directory_to_itself_disallowed() {
let expected = "cp: cannot copy a directory, 'a/b', into itself, 'a/b/c\\b'";
ucmd.args(&["-R", "a/b", "a/b/c"])
.fails()
.stderr_str_apply(str::trim)
.stderr_only(expected);
}

Expand Down Expand Up @@ -2278,6 +2290,7 @@ fn test_copy_dir_preserve_permissions_inaccessible_file() {
ucmd.args(&["-p", "-R", "d1", "d2"])
.fails()
.code_is(1)
.stderr_str_apply(str::trim)
.stderr_only("cp: cannot open 'd1/f' for reading: Permission denied");
assert!(at.dir_exists("d2"));
assert!(!at.file_exists("d2/f"));
Expand All @@ -2295,6 +2308,7 @@ fn test_same_file_backup() {
at.touch("f");
ucmd.args(&["--backup", "f", "f"])
.fails()
.stderr_str_apply(str::trim)
.stderr_only("cp: 'f' and 'f' are the same file");
assert!(!at.file_exists("f~"));
}
Expand All @@ -2307,6 +2321,7 @@ fn test_same_file_force() {
at.touch("f");
ucmd.args(&["--force", "f", "f"])
.fails()
.stderr_str_apply(str::trim)
.stderr_only("cp: 'f' and 'f' are the same file");
assert!(!at.file_exists("f~"));
}
Expand Down
Loading

0 comments on commit b2e4a22

Please sign in to comment.