Skip to content

Commit 83a52bf

Browse files
committed
cp: Added test for permissions copying to an existing file
1 parent 84e6f03 commit 83a52bf

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

tests/by-util/test_cp.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7444,3 +7444,28 @@ fn test_cp_archive_deref_flag_ordering() {
74447444
assert_eq!(at.is_symlink(&dest), expect_symlink, "failed for {flags}");
74457445
}
74467446
}
7447+
7448+
/// Test that copying to an existing file maintains its permissions, unix only because .mode() only
7449+
/// works on Unix
7450+
#[test]
7451+
#[cfg(unix)]
7452+
fn test_cp_to_existing_file_permissions() {
7453+
let (at, mut ucmd) = at_and_ucmd!();
7454+
7455+
at.touch("src");
7456+
at.touch("dst");
7457+
7458+
let src_path = at.plus("src");
7459+
let dst_path = at.plus("dst");
7460+
7461+
let mut src_permissions = std::fs::metadata(&src_path).unwrap().permissions();
7462+
src_permissions.set_readonly(true);
7463+
std::fs::set_permissions(&src_path, src_permissions).unwrap();
7464+
7465+
let dst_mode = std::fs::metadata(&dst_path).unwrap().permissions().mode();
7466+
7467+
ucmd.args(&["src", "dst"]).succeeds();
7468+
7469+
let new_dst_mode = std::fs::metadata(&dst_path).unwrap().permissions().mode();
7470+
assert_eq!(dst_mode, new_dst_mode);
7471+
}

0 commit comments

Comments
 (0)