Skip to content

Commit

Permalink
cp: add tests for -p --parents
Browse files Browse the repository at this point in the history
  • Loading branch information
shinhs0506 committed May 10, 2023
1 parent 2f4afc5 commit 8bd9272
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `\\?`.
Expand Down Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 8bd9272

Please sign in to comment.