1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

cp: copy dir if source path ends with dot (#7874)

This commit is contained in:
Daniel Hofstetter 2025-05-04 09:31:34 +02:00 committed by GitHub
parent f49e120877
commit cd3c921d1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 48 deletions

View file

@ -201,27 +201,6 @@ impl Entry {
}
}
/// Decide whether the given path ends with `/.`.
///
/// # Examples
///
/// ```rust,ignore
/// assert!(ends_with_slash_dot("/."));
/// assert!(ends_with_slash_dot("./."));
/// assert!(ends_with_slash_dot("a/."));
///
/// assert!(!ends_with_slash_dot("."));
/// assert!(!ends_with_slash_dot("./"));
/// assert!(!ends_with_slash_dot("a/.."));
/// ```
fn ends_with_slash_dot<P>(path: P) -> bool
where
P: AsRef<Path>,
{
// `path.ends_with(".")` does not seem to work
path.as_ref().display().to_string().ends_with("/.")
}
#[allow(clippy::too_many_arguments)]
/// Copy a single entry during a directory traversal.
fn copy_direntry(
@ -248,10 +227,7 @@ fn copy_direntry(
// If the source is a directory and the destination does not
// exist, ...
if source_absolute.is_dir()
&& !ends_with_slash_dot(&source_absolute)
&& !local_to_target.exists()
{
if source_absolute.is_dir() && !local_to_target.exists() {
return if target_is_file {
Err("cannot overwrite non-directory with directory".into())
} else {
@ -590,26 +566,3 @@ fn build_dir(
builder.create(path)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::ends_with_slash_dot;
#[test]
#[allow(clippy::cognitive_complexity)]
fn test_ends_with_slash_dot() {
assert!(ends_with_slash_dot("/."));
assert!(ends_with_slash_dot("./."));
assert!(ends_with_slash_dot("../."));
assert!(ends_with_slash_dot("a/."));
assert!(ends_with_slash_dot("/a/."));
assert!(!ends_with_slash_dot(""));
assert!(!ends_with_slash_dot("."));
assert!(!ends_with_slash_dot("./"));
assert!(!ends_with_slash_dot(".."));
assert!(!ends_with_slash_dot("/.."));
assert!(!ends_with_slash_dot("a/.."));
assert!(!ends_with_slash_dot("/a/.."));
}
}

View file

@ -291,6 +291,24 @@ fn test_cp_recurse_several() {
assert_eq!(at.read(TEST_COPY_TO_FOLDER_NEW_FILE), "Hello, World!\n");
}
#[test]
fn test_cp_recurse_source_path_ends_with_slash_dot() {
let source_dir = "source_dir";
let target_dir = "target_dir";
let file = "file";
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir(source_dir);
at.touch(format!("{source_dir}/{file}"));
ucmd.arg("-r")
.arg(format!("{source_dir}/."))
.arg(target_dir)
.succeeds()
.no_output();
assert!(at.file_exists(format!("{target_dir}/{file}")));
}
#[test]
fn test_cp_with_dirs_t() {
let (at, mut ucmd) = at_and_ucmd!();