Skip to content

Commit dd7b454

Browse files
authored
cp: print verbose msg after prompt (#7287)
* cp: fix verbose output order after prompt Fixes: #7285 * cp: add test for verbose message order * cp: fix test for interactive prompt ordering * cp: update test for verbose output order * cp: fix test cases to use update option
1 parent 6415651 commit dd7b454

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/uu/cp/src/cp.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,10 +2271,6 @@ fn copy_file(
22712271
.into());
22722272
}
22732273

2274-
if options.verbose {
2275-
print_verbose_output(options.parents, progress_bar, source, dest);
2276-
}
2277-
22782274
if options.preserve_hard_links() {
22792275
// if we encounter a matching device/inode pair in the source tree
22802276
// we can arrange to create a hard link between the corresponding names
@@ -2284,6 +2280,11 @@ fn copy_file(
22842280
.context(format!("cannot stat {}", source.quote()))?,
22852281
) {
22862282
std::fs::hard_link(new_source, dest)?;
2283+
2284+
if options.verbose {
2285+
print_verbose_output(options.parents, progress_bar, source, dest);
2286+
}
2287+
22872288
return Ok(());
22882289
};
22892290
}
@@ -2334,6 +2335,10 @@ fn copy_file(
23342335
source_is_stream,
23352336
)?;
23362337

2338+
if options.verbose {
2339+
print_verbose_output(options.parents, progress_bar, source, dest);
2340+
}
2341+
23372342
// TODO: implement something similar to gnu's lchown
23382343
if !dest_is_symlink {
23392344
// Here, to match GNU semantics, we quietly ignore an error

tests/by-util/test_cp.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6041,3 +6041,37 @@ fn test_cp_from_stdin() {
60416041
assert!(at.file_exists(target));
60426042
assert_eq!(at.read(target), test_string);
60436043
}
6044+
6045+
#[test]
6046+
fn test_cp_update_older_interactive_prompt_yes() {
6047+
let (at, mut ucmd) = at_and_ucmd!();
6048+
let old_file = "old";
6049+
let new_file = "new";
6050+
6051+
let f = at.make_file(old_file);
6052+
f.set_modified(std::time::UNIX_EPOCH).unwrap();
6053+
at.touch(new_file);
6054+
6055+
ucmd.args(&["-i", "-v", "--update=older", new_file, old_file])
6056+
.pipe_in("Y\n")
6057+
.stderr_to_stdout()
6058+
.succeeds()
6059+
.stdout_is("cp: overwrite 'old'? 'new' -> 'old'\n");
6060+
}
6061+
6062+
#[test]
6063+
fn test_cp_update_older_interactive_prompt_no() {
6064+
let (at, mut ucmd) = at_and_ucmd!();
6065+
let old_file = "old";
6066+
let new_file = "new";
6067+
6068+
let f = at.make_file(old_file);
6069+
f.set_modified(std::time::UNIX_EPOCH).unwrap();
6070+
at.touch(new_file);
6071+
6072+
ucmd.args(&["-i", "-v", "--update=older", new_file, old_file])
6073+
.pipe_in("N\n")
6074+
.stderr_to_stdout()
6075+
.fails()
6076+
.stdout_is("cp: overwrite 'old'? ");
6077+
}

0 commit comments

Comments
 (0)