From 8bd9272c9904036030f3a95a8706afe2e5f10b09 Mon Sep 17 00:00:00 2001 From: John Shin Date: Wed, 10 May 2023 16:31:08 -0700 Subject: [PATCH] cp: add tests for -p --parents --- src/uu/cp/src/copydir.rs | 8 +------- tests/by-util/test_cp.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/uu/cp/src/copydir.rs b/src/uu/cp/src/copydir.rs index ad4d069b8e2..c312b7cbb4a 100644 --- a/src/uu/cp/src/copydir.rs +++ b/src/uu/cp/src/copydir.rs @@ -25,7 +25,7 @@ use walkdir::{DirEntry, WalkDir}; use crate::{ aligned_ancestors, context_for, copy_attributes, copy_file, copy_link, preserve_hardlinks, - should_preserve_attribute, CopyResult, Error, Options, TargetSlice, + CopyResult, Error, Options, TargetSlice, }; /// Ensure a Windows path starts with a `\\?`. @@ -363,12 +363,6 @@ pub(crate) fn copy_directory( } } - if should_preserve_attribute(options) { - for (x, y) in aligned_ancestors(root, &target.join(root)) { - copy_attributes(x, y, &options.attributes)?; - } - } - new_target } else { target.to_path_buf() diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 0a6e5804973..fb208f51156 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -1075,6 +1075,40 @@ fn test_cp_parents_dest_not_directory() { .stderr_contains("with --parents, the destination must be a directory"); } +#[test] +fn test_cp_parents_with_permissions_copy_file() { + let (at, mut ucmd) = at_and_ucmd!(); + + let dir = "dir"; + let file = "p1/p2/file"; + + at.mkdir(dir); + at.mkdir_all("p1/p2"); + at.touch(file); + + let pdir1_mode = 0o0777; + let pdir2_mode = 0o0711; + let file_mode = 0o0702; + + at.set_mode("p1", pdir1_mode); + at.set_mode("p1/p2", pdir2_mode); + at.set_mode(file, file_mode); + + ucmd.arg("-p") + .arg("--parents") + .arg(file) + .arg(dir) + .succeeds(); + + let pdir1_metadata = at.metadata("p1"); + let pdir2_metadata = at.metadata("p1/p2"); + let file_metadata = at.metadata(file); + + assert_metadata_eq!(pdir1_metadata, at.metadata("dir/p1")); + assert_metadata_eq!(pdir2_metadata, at.metadata("dir/p1/p2")); + assert_metadata_eq!(file_metadata, at.metadata("dir/p1/p2/file")); +} + #[test] #[cfg(unix)] fn test_cp_writable_special_file_permissions() {