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.
] {
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 {

View File

@ -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)
}
}