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
This commit is contained in:
Antoine Stevan 2023-10-25 16:43:27 +02:00 committed by GitHub
parent f3656f7822
commit a11e41332c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 16 deletions

View File

@ -40,7 +40,7 @@ export def --env "path add" [
...paths # the paths to add to $env.PATH. ...paths # the paths to add to $env.PATH.
] { ] {
let span = (metadata $paths).span let span = (metadata $paths).span
let paths = ($paths | flatten) let paths = $paths | flatten
if ($paths | is-empty) or ($paths | length) == 0 { if ($paths | is-empty) or ($paths | length) == 0 {
error make {msg: "Empty input", label: { 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 path_name = if "PATH" in $env { "PATH" } else { "Path" }
let paths = ($paths | each {|p| let paths = $paths | each {|p|
if ($p | describe) == "string" { let p = match ($p | describe | str replace --regex '<.*' '') {
$p "string" => $p,
} else if ($p | describe | str starts-with "record") { "record" => { $p | get --ignore-errors $nu.os-info.name },
$p | get -i $nu.os-info.name
} }
})
$p | path expand
}
if null in $paths or ($paths | is-empty) { if null in $paths or ($paths | is-empty) {
error make {msg: "Empty input", label: { error make {msg: "Empty input", label: {
@ -70,9 +71,10 @@ export def --env "path add" [
load-env {$path_name: ( load-env {$path_name: (
$env $env
| get $path_name | get $path_name
| if $append { append $paths } | split row (char esep)
else { prepend $paths } | path expand
| if $append { append $paths } else { prepend $paths }
)} )}
if $ret { if $ret {

View File

@ -12,19 +12,19 @@ def path_add [] {
assert equal (get_path) [] assert equal (get_path) []
std path add "/foo/" std path add "/foo/"
assert equal (get_path) ["/foo/"] assert equal (get_path) (["/foo/"] | path expand)
std path add "/bar/" "/baz/" 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: []} load-env {$path_name: []}
std path add "foo" std path add "foo"
std path add "bar" "baz" --append 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 (std path add "fooooo" --ret) (["fooooo", "foo", "bar", "baz"] | path expand)
assert equal (get_path) ["fooooo", "foo", "bar", "baz"] assert equal (get_path) (["fooooo", "foo", "bar", "baz"] | path expand)
load-env {$path_name: []} load-env {$path_name: []}
@ -36,7 +36,11 @@ def path_add [] {
} }
std path add $target_paths 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)
} }
} }