Skip to content

Commit

Permalink
Send only absolute paths to uu_cp (nushell#11080)
Browse files Browse the repository at this point in the history
# Description
Fixes nushell#10832

Replaces: nushell#10843
  • Loading branch information
kubouch authored and hardfau1t committed Dec 14, 2023
1 parent 2122f58 commit 471084e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
8 changes: 8 additions & 0 deletions crates/nu-command/src/filesystem/ucp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ impl Command for UCp {
sources.append(&mut app_vals);
}

// Make sure to send absolute paths to avoid uu_cp looking for cwd in std::env which is not
// supported in Nushell
for src in sources.iter_mut() {
if !src.is_absolute() {
*src = nu_path::expand_path_with(&src, &cwd);
}
}

let options = uu_cp::Options {
overwrite,
reflink_mode,
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/filesystem/umkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ impl Command for UMkdir {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let path = current_dir(engine_state, stack)?;
let cwd = current_dir(engine_state, stack)?;
let mut directories = call
.rest::<String>(engine_state, stack, 0)?
.into_iter()
.map(|dir| path.join(dir))
.map(|dir| nu_path::expand_path_with(dir, &cwd))
.peekable();

let is_verbose = call.has_flag("verbose");
Expand Down
34 changes: 25 additions & 9 deletions crates/nu-command/tests/commands/cp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::path::Path;

use nu_test_support::fs::file_contents;
use nu_test_support::fs::{
files_exist_at, AbsoluteFile,
Stub::{EmptyFile, FileWithContent, FileWithPermission},
};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
use std::path::Path;

fn get_file_hash<T: std::fmt::Display>(file: T) -> String {
nu!("open -r {} | to text | hash md5", file).out
Expand Down Expand Up @@ -127,9 +128,9 @@ fn copies_the_directory_inside_directory_if_path_to_copy_is_directory_and_with_r
vec![
Path::new("yehuda.txt"),
Path::new("jttxt"),
Path::new("andres.txt")
Path::new("andres.txt"),
],
&expected_dir
&expected_dir,
));
})
}
Expand Down Expand Up @@ -175,15 +176,15 @@ fn deep_copies_with_recursive_flag_impl(progress: bool) {
assert!(expected_dir.exists());
assert!(files_exist_at(
vec![Path::new("errors.txt"), Path::new("multishells.txt")],
jts_expected_copied_dir
jts_expected_copied_dir,
));
assert!(files_exist_at(
vec![Path::new("coverage.txt"), Path::new("commands.txt")],
andres_expected_copied_dir
andres_expected_copied_dir,
));
assert!(files_exist_at(
vec![Path::new("defer-evaluation.txt")],
yehudas_expected_copied_dir
yehudas_expected_copied_dir,
));
})
}
Expand Down Expand Up @@ -221,7 +222,7 @@ fn copies_using_path_with_wildcard_impl(progress: bool) {
Path::new("sgml_description.json"),
Path::new("utf16.ini"),
],
dirs.test()
dirs.test(),
));

// Check integrity after the copy is done
Expand Down Expand Up @@ -266,7 +267,7 @@ fn copies_using_a_glob_impl(progress: bool) {
Path::new("sgml_description.json"),
Path::new("utf16.ini"),
],
dirs.test()
dirs.test(),
));

// Check integrity after the copy is done
Expand Down Expand Up @@ -340,7 +341,7 @@ fn copy_files_using_glob_two_parents_up_using_multiple_dots_imp(progress: bool)
"kevin.txt",
"many_more.ppl",
],
dirs.test()
dirs.test(),
));
})
}
Expand Down Expand Up @@ -615,3 +616,18 @@ fn copy_file_with_update_flag_impl(progress: bool) {
assert_eq!(actual.out, "newest_body");
});
}

#[test]
fn cp_with_cd() {
Playground::setup("cp_test_20", |_dirs, sandbox| {
sandbox
.mkdir("tmp_dir")
.with_files(vec![FileWithContent("tmp_dir/file.txt", "body")]);

let actual = nu!(
cwd: sandbox.cwd(),
r#"do { cd tmp_dir; let f = 'file.txt'; cp $f .. }; open file.txt"#,
);
assert!(actual.out.contains("body"));
});
}

0 comments on commit 471084e

Please sign in to comment.