1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #3027 from shoriminimoe/2986-cp

cp: only allow directory for -t
This commit is contained in:
Terts Diepraam 2022-03-22 23:08:54 +01:00 committed by GitHub
commit fc1fa8d1f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View file

@ -315,6 +315,12 @@ pub fn uu_app<'a>() -> Command<'a> {
.long(options::TARGET_DIRECTORY)
.value_name(options::TARGET_DIRECTORY)
.takes_value(true)
.validator(|s| {
if Path::new(s).is_dir() {
return Ok(());
}
Err(format!("'{}' is not a directory", s))
})
.help("copy all SOURCE arguments into target-directory"))
.arg(Arg::new(options::NO_TARGET_DIRECTORY)
.short('T')
@ -469,7 +475,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
LONG_HELP,
backup_control::BACKUP_CONTROL_LONG_HELP
))
.get_matches_from(args);
.try_get_matches_from(args)?;
let options = Options::from_matches(&matches)?;

View file

@ -50,6 +50,7 @@
// spell-checker:ignore uioerror
use clap;
use std::{
error::Error,
fmt::{Display, Formatter},
@ -615,3 +616,13 @@ impl From<i32> for Box<dyn UError> {
ExitCode::new(i)
}
}
/// Implementations for clap::Error
impl UError for clap::Error {
fn code(&self) -> i32 {
match self.kind {
clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => 0,
_ => 1,
}
}
}

View file

@ -183,6 +183,16 @@ fn test_cp_arg_no_target_directory() {
.stderr_contains("cannot overwrite directory");
}
#[test]
fn test_cp_target_directory_is_file() {
new_ucmd!()
.arg("-t")
.arg(TEST_HOW_ARE_YOU_SOURCE)
.arg(TEST_HELLO_WORLD_SOURCE)
.fails()
.stderr_contains(format!("'{}' is not a directory", TEST_HOW_ARE_YOU_SOURCE));
}
#[test]
fn test_cp_arg_interactive() {
new_ucmd!()