From 05a7446619244278ab060d4fa272f7406e1d874b Mon Sep 17 00:00:00 2001 From: DustinChaloupka Date: Tue, 10 Oct 2017 12:02:42 -0500 Subject: [PATCH] Add installing from a parent directory's .tool-versions --- lib/commands/install.sh | 8 ++++---- lib/utils.sh | 11 +++++++++++ test/install_command.bats | 16 ++++++++++++++++ test/utils.bats | 19 +++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/commands/install.sh b/lib/commands/install.sh index 9ea36f66..a7e5ccd0 100644 --- a/lib/commands/install.sh +++ b/lib/commands/install.sh @@ -37,10 +37,9 @@ get_concurrency() { } install_local_tool_versions() { - if [ -f "$(pwd)/.tool-versions" ]; then - local asdf_versions_path - asdf_versions_path="$(pwd)/.tool-versions" - + local asdf_versions_path + asdf_versions_path=$(find_tool_versions) + if [ -f "${asdf_versions_path}" ]; then while read -r tool_line; do IFS=' ' read -r -a tool_info <<< "$tool_line" local tool_name @@ -55,6 +54,7 @@ install_local_tool_versions() { else echo "Either specify a tool & version in the command" echo "OR add .tool-versions file in this directory" + echo "or in a parent directory" exit 1 fi } diff --git a/lib/utils.sh b/lib/utils.sh index 55f99789..7a89f909 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -323,3 +323,14 @@ get_plugin_source_url() { grep "repository" "$plugin_config" | awk -F'=' '{print $2}' | sed 's/ //' fi } + +find_tool_versions() { + local search_path=$(pwd) + while [ "$search_path" != "/" ]; do + if [ -f "$search_path/.tool-versions" ]; then + echo "${search_path}/.tool-versions" + return 0 + fi + search_path=$(dirname "$search_path") + done +} diff --git a/test/install_command.bats b/test/install_command.bats index 7d41ff4e..942db233 100644 --- a/test/install_command.bats +++ b/test/install_command.bats @@ -97,3 +97,19 @@ teardown() { [ "$output" = "You must specify a name and a version to install" ] [ ! -f $ASDF_DIR/installs/dummy/1.1/version ] } + +@test "install_command uses a parent directory .tool-versions file if present" { + # asdf lib needed to run generated shims + cp -rf $BATS_TEST_DIRNAME/../{bin,lib} $ASDF_DIR/ + + echo 'dummy 1.0' > $PROJECT_DIR/.tool-versions + mkdir -p $PROJECT_DIR/child + + cd $PROJECT_DIR/child + + run install_command + + # execute the generated shim + [ "$($ASDF_DIR/shims/dummy world hello)" == "This is Dummy 1.0! hello world" ] + [ "$status" -eq 0 ] +} diff --git a/test/utils.bats b/test/utils.bats index c1e2dba9..77dd82d6 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -207,3 +207,22 @@ teardown() { [ "$status" -eq 0 ] [ "$output" = "$executable_path" ] } + +@test "find_tool_versions will find a .tool-versions path if it exists in current directory" { + echo "dummy 0.1.0" > $PROJECT_DIR/.tool-versions + cd $PROJECT_DIR + + run find_tool_versions + [ "$status" -eq 0 ] + [ "$output" = "$PROJECT_DIR/.tool-versions" ] +} + +@test "find_tool_versions will find a .tool-versions path if it exists in parent directory" { + echo "dummy 0.1.0" > $PROJECT_DIR/.tool-versions + mkdir -p $PROJECT_DIR/child + cd $PROJECT_DIR/child + + run find_tool_versions + [ "$status" -eq 0 ] + [ "$output" = "$PROJECT_DIR/.tool-versions" ] +}