From a11e41332c36da8910b7e8f16d72a2cfa0d807c4 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:43:27 +0200 Subject: [PATCH] expand paths and split PATH in `std path add` (#10710) related to - https://discord.com/channels/601130461678272522/614593951969574961/1162406310155923626 # Description this PR - does a bit of minor refactoring - makes sure the input paths get expanded - makes sure the input PATH gets split on ":" - adds a test - fixes the other tests # User-Facing Changes should give a better overall experience with `std path add` # Tests + Formatting adds a new test case to the `path_add` test and fixes the others. # After Submitting --- crates/nu-std/std/mod.nu | 22 ++++++++++++---------- crates/nu-std/tests/test_std.nu | 16 ++++++++++------ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/crates/nu-std/std/mod.nu b/crates/nu-std/std/mod.nu index 29f486bdd5..56c585cfc5 100644 --- a/crates/nu-std/std/mod.nu +++ b/crates/nu-std/std/mod.nu @@ -40,7 +40,7 @@ export def --env "path add" [ ...paths # the paths to add to $env.PATH. ] { let span = (metadata $paths).span - let paths = ($paths | flatten) + let paths = $paths | flatten if ($paths | is-empty) or ($paths | length) == 0 { error make {msg: "Empty input", label: { @@ -52,13 +52,14 @@ export def --env "path add" [ let path_name = if "PATH" in $env { "PATH" } else { "Path" } - let paths = ($paths | each {|p| - if ($p | describe) == "string" { - $p - } else if ($p | describe | str starts-with "record") { - $p | get -i $nu.os-info.name + let paths = $paths | each {|p| + let p = match ($p | describe | str replace --regex '<.*' '') { + "string" => $p, + "record" => { $p | get --ignore-errors $nu.os-info.name }, } - }) + + $p | path expand + } if null in $paths or ($paths | is-empty) { error make {msg: "Empty input", label: { @@ -70,9 +71,10 @@ export def --env "path add" [ load-env {$path_name: ( $env - | get $path_name - | if $append { append $paths } - else { prepend $paths } + | get $path_name + | split row (char esep) + | path expand + | if $append { append $paths } else { prepend $paths } )} if $ret { diff --git a/crates/nu-std/tests/test_std.nu b/crates/nu-std/tests/test_std.nu index 6e82335aeb..45f11e5729 100644 --- a/crates/nu-std/tests/test_std.nu +++ b/crates/nu-std/tests/test_std.nu @@ -12,19 +12,19 @@ def path_add [] { assert equal (get_path) [] std path add "/foo/" - assert equal (get_path) ["/foo/"] + assert equal (get_path) (["/foo/"] | path expand) std path add "/bar/" "/baz/" - assert equal (get_path) ["/bar/", "/baz/", "/foo/"] + assert equal (get_path) (["/bar/", "/baz/", "/foo/"] | path expand) load-env {$path_name: []} std path add "foo" std path add "bar" "baz" --append - assert equal (get_path) ["foo", "bar", "baz"] + assert equal (get_path) (["foo", "bar", "baz"] | path expand) - assert equal (std path add "fooooo" --ret) ["fooooo", "foo", "bar", "baz"] - assert equal (get_path) ["fooooo", "foo", "bar", "baz"] + assert equal (std path add "fooooo" --ret) (["fooooo", "foo", "bar", "baz"] | path expand) + assert equal (get_path) (["fooooo", "foo", "bar", "baz"] | path expand) load-env {$path_name: []} @@ -36,7 +36,11 @@ def path_add [] { } std path add $target_paths - assert equal (get_path) [($target_paths | get $nu.os-info.name)] + assert equal (get_path) ([($target_paths | get $nu.os-info.name)] | path expand) + + load-env {$path_name: [$"/foo(char esep)/bar"]} + std path add "~/foo" + assert equal (get_path) (["~/foo", "/foo", "/bar"] | path expand) } }