Skip to content

Commit

Permalink
cp: -i prompts in the right place
Browse files Browse the repository at this point in the history
Should fix tests/cp/cp-i.sh
  • Loading branch information
sylvestre committed May 31, 2023
1 parent 044daf8 commit 968ec80
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
26 changes: 18 additions & 8 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ use uucore::fs::{
};
use uucore::update_control::{self, UpdateMode};
use uucore::{
crash, format_usage, help_about, help_section, help_usage, prompt_yes, show_error, show_warning,
crash, format_usage, help_about, help_section, help_usage, prompt_yes, show_error,
show_warning, util_name,
};

use crate::copydir::copy_directory;
Expand Down Expand Up @@ -1256,13 +1257,23 @@ fn copy_source(
}

impl OverwriteMode {
fn verify(&self, path: &Path) -> CopyResult<()> {
fn verify(&self, path: &Path, verbose: bool) -> CopyResult<()> {
match *self {
Self::NoClobber => Err(Error::NotAllFilesCopied),
Self::NoClobber => {
if verbose {
println!("skipped {}", path.quote());
} else {
eprintln!("{}: not replacing {}", util_name(), path.quote());
}
Err(Error::NotAllFilesCopied)
}
Self::Interactive(_) => {
if prompt_yes!("overwrite {}?", path.quote()) {
Ok(())
} else {
if verbose {
println!("skipped {}", path.quote());
}
Err(Error::Skipped)
}
}
Expand Down Expand Up @@ -1470,7 +1481,7 @@ fn handle_existing_dest(
return Err(format!("{} and {} are the same file", source.quote(), dest.quote()).into());
}

options.overwrite.verify(dest)?;
options.overwrite.verify(dest, options.verbose)?;

let backup_path = backup_control::get_backup_path(options.backup, dest, &options.backup_suffix);
if let Some(backup_path) = backup_path {
Expand Down Expand Up @@ -1828,7 +1839,7 @@ fn copy_helper(
File::create(dest).context(dest.display().to_string())?;
} else if source_is_fifo && options.recursive && !options.copy_contents {
#[cfg(unix)]
copy_fifo(dest, options.overwrite)?;
copy_fifo(dest, options.overwrite, options.verbose)?;
} else if source_is_symlink {
copy_link(source, dest, symlinked_files)?;
} else {
Expand All @@ -1853,9 +1864,9 @@ fn copy_helper(
// "Copies" a FIFO by creating a new one. This workaround is because Rust's
// built-in fs::copy does not handle FIFOs (see rust-lang/rust/issues/79390).
#[cfg(unix)]
fn copy_fifo(dest: &Path, overwrite: OverwriteMode) -> CopyResult<()> {
fn copy_fifo(dest: &Path, overwrite: OverwriteMode, verbose: bool) -> CopyResult<()> {
if dest.exists() {
overwrite.verify(dest)?;
overwrite.verify(dest, verbose)?;
fs::remove_file(dest)?;
}

Expand Down Expand Up @@ -1963,7 +1974,6 @@ fn disk_usage_directory(p: &Path) -> io::Result<u64> {

#[cfg(test)]
mod tests {

use crate::{aligned_ancestors, localize_to_target};
use std::path::Path;

Expand Down
25 changes: 18 additions & 7 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,26 @@ fn test_cp_arg_interactive() {

#[test]
#[cfg(not(target_os = "android"))]
fn test_cp_arg_interactive_update() {
// -u -i won't show the prompt to validate the override or not
// Therefore, the error code will be 0
fn test_cp_arg_interactive_verbose() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("a");
at.touch("b");
ucmd.args(&["-i", "-u", "a", "b"])
ucmd.args(&["-vi", "a", "b"])
.pipe_in("N\n")
.succeeds()
.no_stdout();
.fails()
.stdout_is("skipped 'b'\n");
}

#[test]
#[cfg(not(target_os = "android"))]
fn test_cp_arg_interactive_verbose_clobber() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("a");
at.touch("b");
ucmd.args(&["-vin", "a", "b"])
.pipe_in("y\n")
.fails()
.stdout_is("skipped 'b'\n");
}

#[test]
Expand Down Expand Up @@ -487,7 +497,8 @@ fn test_cp_arg_no_clobber() {
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HOW_ARE_YOU_SOURCE)
.arg("--no-clobber")
.fails();
.fails()
.stderr_contains("not replacing");

assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n");
}
Expand Down

0 comments on commit 968ec80

Please sign in to comment.