From 1ff8c2d81dea68e90a6a61b68a50a2098b1d6a8a Mon Sep 17 00:00:00 2001 From: Artemiy Date: Sat, 25 Nov 2023 18:42:20 +0300 Subject: [PATCH] Cp target expansion (#11152) # Description This PR addresses issue with cp brough up on [discord](https://discord.com/channels/601130461678272522/614593951969574961/1177669443917189130) where target of cp is not correctly expanded. If one has directory `test` with file `file.txt` in it then the following command (in one line or inside a `do` block): ```nu cd test; let file = 'copy.txt'; cp file.txt $file ``` will create a `copy.txt` in `.` not in `test` instead. This happens because target of `cp` is a variable which is not expanded unlike a string literal # User-Facing Changes `cp` will correctly parse realative target paths # Tests + Formatting # After Submitting --- crates/nu-command/src/filesystem/ucp.rs | 2 ++ crates/nu-command/tests/commands/ucp.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/nu-command/src/filesystem/ucp.rs b/crates/nu-command/src/filesystem/ucp.rs index 82ab62dfa..3c3d81046 100644 --- a/crates/nu-command/src/filesystem/ucp.rs +++ b/crates/nu-command/src/filesystem/ucp.rs @@ -209,6 +209,8 @@ impl Command for UCp { } } + let target_path = nu_path::expand_path_with(&target_path, &cwd); + let options = uu_cp::Options { overwrite, reflink_mode, diff --git a/crates/nu-command/tests/commands/ucp.rs b/crates/nu-command/tests/commands/ucp.rs index abcf9fd64..e0f7faca8 100644 --- a/crates/nu-command/tests/commands/ucp.rs +++ b/crates/nu-command/tests/commands/ucp.rs @@ -970,6 +970,21 @@ fn test_cp_with_vars() { }); } +#[test] +fn test_cp_destination_after_cd() { + Playground::setup("ucp_test_34", |dirs, sandbox| { + sandbox.mkdir("test"); + sandbox.with_files(vec![EmptyFile("test/file.txt")]); + nu!( + cwd: dirs.test(), + // Defining variable avoid path expansion of cp argument. + // If argument was not expanded ucp wrapper should do it + "cd test; let file = 'copy.txt'; cp file.txt $file", + ); + assert!(dirs.test().join("test").join("copy.txt").exists()); + }); +} + #[rstest] #[case(r#"'a]c'"#)] #[case(r#"'a[c'"#)]