Skip to content

Commit

Permalink
fix: Nushell plugin list --urls (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
acidghost authored Mar 23, 2023
1 parent 49e541a commit 9363fb2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 57 deletions.
11 changes: 6 additions & 5 deletions asdf.nu
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ module asdf {
] {

let params = [
{name: 'urls', enabled: $urls, template: '\s+?(?P<repository>git@.+\.git)', flag: '--urls'}
{name: 'refs', enabled: $refs, template: '\s+?(?P<branch>\w+)\s+(?P<ref>\w+)', flag: '--refs'}
{name: 'urls', enabled: $urls, flag: '--urls',
template: '\s+?(?P<repository>(?:http[s]?|git).+\.git|/.+)'}
{name: 'refs', enabled: $refs, flag: '--refs',
template: '\s+?(?P<branch>\w+)\s+(?P<ref>\w+)'}
]

let template = '(?P<name>.+)' + (
Expand All @@ -91,10 +93,9 @@ module asdf {
str trim
)

let parsed_urls_flag = ($params | where enabled and name == 'urls' | get --ignore-errors flag | default '' )
let parsed_refs_flag = ($params | where enabled and name == 'refs' | get --ignore-errors flag | default '' )
let flags = ($params | where enabled | get --ignore-errors flag | default '' )

^asdf plugin list $parsed_urls_flag $parsed_refs_flag | lines | parse -r $template | str trim
^asdf plugin list $flags | lines | parse -r $template | str trim
}

# list all available plugins
Expand Down
10 changes: 4 additions & 6 deletions lib/functions/plugins.bash
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ plugin_list_command() {
printf "%s" "$plugin_name"

if [ -n "$show_repo" ]; then
printf "\t%s" "$(git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null)"
printf "\t%s" "$(get_plugin_remote_url "$plugin_name")"
fi

if [ -n "$show_ref" ]; then
local branch
local gitref
branch=$(git --git-dir "$plugin_path/.git" rev-parse --abbrev-ref HEAD 2>/dev/null)
gitref=$(git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null)
printf "\t%s\t%s" "$branch" "$gitref"
printf "\t%s\t%s" \
"$(get_plugin_remote_branch "$plugin_name")" \
"$(get_plugin_remote_gitref "$plugin_name")"
fi

printf "\n"
Expand Down
21 changes: 21 additions & 0 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,24 @@ util_resolve_user_path() {
util_resolve_user_path_reply="$path"
fi
}

get_plugin_remote_url() {
local plugin_name="$1"
local plugin_path
plugin_path="$(get_plugin_path "$plugin_name")"
git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null
}

get_plugin_remote_branch() {
local plugin_name="$1"
local plugin_path
plugin_path="$(get_plugin_path "$plugin_name")"
git --git-dir "$plugin_path/.git" rev-parse --abbrev-ref HEAD 2>/dev/null
}

get_plugin_remote_gitref() {
local plugin_name="$1"
local plugin_path
plugin_path="$(get_plugin_path "$plugin_name")"
git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null
}
115 changes: 69 additions & 46 deletions test/asdf_nu.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ setup() {
fi

setup_asdf_dir
setup_repo
install_dummy_plugin
}

teardown() {
Expand All @@ -23,33 +21,27 @@ cleaned_path() {
echo "$PATH" | tr ':' '\n' | grep -v "asdf" | tr '\n' ':'
}

@test "exports ASDF_DIR" {
run_nushell() {
run nu -c "
hide-env -i asdf
hide-env -i ASDF_DIR
let-env PATH = ( '$(cleaned_path)' | split row ':' )
let-env ASDF_NU_DIR = '$PWD'
source asdf.nu
$1"
}

echo \$env.ASDF_DIR"
@test "exports ASDF_DIR" {
run_nushell "echo \$env.ASDF_DIR"

[ "$status" -eq 0 ]
result=$(echo "$output" | grep "asdf")
[ "$result" = "$PWD" ]
}

@test "adds asdf dirs to PATH" {
run nu -c "
hide-env -i asdf
hide-env -i ASDF_DIR
let-env PATH = ( '$(cleaned_path)' | split row ':' )
let-env ASDF_NU_DIR = '$PWD'
source asdf.nu
\$env.PATH | to text"
run_nushell "\$env.PATH | to text"

[ "$status" -eq 0 ]

Expand All @@ -58,15 +50,8 @@ cleaned_path() {
}

@test "does not add paths to PATH more than once" {
run nu -c "
hide-env -i asdf
hide-env -i ASDF_DIR
let-env PATH = ( '$(cleaned_path)' | split row ':' )
let-env ASDF_NU_DIR = '$PWD'
source asdf.nu
run_nushell "
source asdf.nu
echo \$env.PATH"

[ "$status" -eq 0 ]
Expand All @@ -91,43 +76,81 @@ cleaned_path() {
}

@test "defines the asdf or main function" {
run nu -c "
hide-env -i asdf
hide-env -i ASDF_DIR
let-env PATH = ( '$(cleaned_path)' | split row ':' )
let-env ASDF_NU_DIR = '$PWD'
source asdf.nu
which asdf | get path | to text"
run_nushell "which asdf | get path | to text"

[ "$status" -eq 0 ]
}

@test "function calls asdf command" {
run nu -c "
hide-env -i asdf
hide-env -i ASDF_DIR
let-env PATH = ( '$(cleaned_path)' | split row ':' )
let-env ASDF_NU_DIR = '$PWD'
source asdf.nu
asdf info"
run_nushell "asdf info"

[ "$status" -eq 0 ]

result=$(echo "$output" | grep "ASDF INSTALLED PLUGINS:")
[ "$result" != "" ]
}

@test "parses the output of asdf plugin list all" {
run nu -c "
hide-env -i asdf
hide-env -i ASDF_DIR
let-env PATH = ( '$(cleaned_path)' | split row ':' )
let-env ASDF_NU_DIR = '$PWD'
@test "parses the output of asdf plugin list" {
setup_repo
install_dummy_plugin
run_nushell "asdf plugin list | to csv -n"

source asdf.nu
asdf plugin list all | to csv -n"
[ "$status" -eq 0 ]
[ "$output" = "dummy" ]
}

@test "parses the output of asdf plugin list --urls" {
setup_repo
install_mock_plugin_repo "dummy"
asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"

run_nushell "asdf plugin list --urls | to csv -n"

[ "$status" -eq 0 ]

local repo_url
repo_url=$(get_plugin_remote_url "dummy")

[ "$output" = "dummy,$repo_url" ]
}

@test "parses the output of asdf plugin list --refs" {
setup_repo
install_mock_plugin_repo "dummy"
asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"

run_nushell "asdf plugin list --refs | to csv -n"

[ "$status" -eq 0 ]

local branch gitref
branch=$(get_plugin_remote_branch "dummy")
gitref=$(get_plugin_remote_gitref "dummy")

[ "$output" = "dummy,$branch,$gitref" ]
}

@test "parses the output of asdf plugin list --urls --refs" {
setup_repo
install_mock_plugin_repo "dummy"
asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"

run_nushell "asdf plugin list --urls --refs | to csv -n"

[ "$status" -eq 0 ]

local repo_url branch gitref
repo_url=$(get_plugin_remote_url "dummy")
branch=$(get_plugin_remote_branch "dummy")
gitref=$(get_plugin_remote_gitref "dummy")

[ "$output" = "dummy,$repo_url,$branch,$gitref" ]
}

@test "parses the output of asdf plugin list all" {
setup_repo
install_dummy_plugin
run_nushell "asdf plugin list all | to csv -n"

[ "$status" -eq 0 ]
[ "$output" = "\
Expand Down

0 comments on commit 9363fb2

Please sign in to comment.