Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow for custom global tool-versions location #1295

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/functions/versions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ version_command() {
file_name="$(version_file_name)"

if [ "$cmd" = "global" ]; then
file="$HOME/$file_name"
file="$(asdf_config_dir)/$file_name"
elif [ "$cmd" = "local-tree" ]; then
file=$(find_tool_versions)
else # cmd = local
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ asdf_data_dir() {
printf "%s\\n" "$data_dir"
}

asdf_config_dir() {
local config_dir="${HOME}"

if [ -n "${ASDF_CONFIG_DIR}" ]; then
config_dir="${ASDF_CONFIG_DIR}"
elif [ -n "${XDG_CONFIG_HOME}" ]; then
config_dir="${XDG_CONFIG_HOME}/asdf"
fi

printf "%s\\n" "${config_dir}"
}

get_install_path() {
local plugin=$1
local install_type=$2
Expand Down
29 changes: 29 additions & 0 deletions test/utils.bats
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,32 @@ EOF
[ "$status" -eq 0 ]
[ "$output" = "$message" ]
}

@test "asdf_config_dir should return user dir when ASDF_CONFIG_DIR is set and XDG_CONFIG_HOME is not set" {
export ASDF_CONFIG_DIR="/tmp/test1"
export XDG_CONFIG_HOME="/tmp/test2"

run asdf_config_dir

echo "${ASDF_CONFIG_DIR}:${XDG_CONFIG_HOME}:$(asdf_config_dir)"
[ "$status" -eq 0 ]
[ "$output" = "$ASDF_CONFIG_DIR" ]
}

@test "asdf_config_dir should return user dir when XDG_CONFIG_HOME is set and ASDF_CONFIG_DIR is not set" {
unset ASDF_CONFIG_DIR
export XDG_CONFIG_HOME="/tmp/test2"

run asdf_config_dir
[ "$status" -eq 0 ]
[ "$output" = "$XDG_CONFIG_HOME/asdf" ]
}

@test "asdf_config_dir should return HOME when ASDF_CONFIG_DIR and XDG_CONFIG_HOME are not set" {
unset ASDF_CONFIG_DIR
unset XDG_CONFIG_HOME

run asdf_config_dir
[ "$status" -eq 0 ]
[ "$output" = "$HOME" ]
}
17 changes: 17 additions & 0 deletions test/version_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
load test_helpers

setup() {
# Unset XDG_CONFIG_HOME to ensure it uses HOME for determine location
unset XDG_CONFIG_HOME

setup_asdf_dir
install_dummy_plugin
install_dummy_version "1.0.0"
Expand Down Expand Up @@ -187,6 +190,20 @@ teardown() {
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
}

@test "global should create a global .tool-versions in ASDF_CONFIG_DIR if it doesn't exist" {
export ASDF_CONFIG_DIR="/tmp/test"
run asdf global "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $ASDF_CONFIG_DIR/.tool-versions)" = "dummy 1.1.0" ]
}

@test "global should create a global .tool-versions in XDG_CONFIG_HOME if it doesn't exist" {
export XDG_CONFIG_HOME="/tmp/test"
run asdf global "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $XDG_CONFIG_HOME/asdf/.tool-versions)" = "dummy 1.1.0" ]
}

@test "[global - dummy_plugin] with latest should use the latest installed version" {
run asdf global "dummy" "latest"
[ "$status" -eq 0 ]
Expand Down