From 54259467bdd4946eafae21b6b9fd684453f0b904 Mon Sep 17 00:00:00 2001 From: fistach Date: Mon, 22 Apr 2024 21:47:51 +0200 Subject: [PATCH 1/4] add tests --- .../features/use_major_version.feature | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/resources/features/use_major_version.feature diff --git a/src/test/resources/features/use_major_version.feature b/src/test/resources/features/use_major_version.feature new file mode 100644 index 000000000..caef1052d --- /dev/null +++ b/src/test/resources/features/use_major_version.feature @@ -0,0 +1,36 @@ +Feature: Use Version Giving Only The Major Version Number + + Background: + Given the internet is reachable + And an initialised environment + + Scenario: Use a grails version that is installed giving only major version + Given the candidate "grails" version "1.3.0" is already installed and default + And the candidate "grails" version "2.1.0" is already installed but not default + And the system is bootstrapped + When I enter "sdk use grails 2" + Then I see "Using grails version 2.1.0 in this shell." + Then the candidate "grails" version "2.1.0" should be in use + + Scenario: Use a java version that is installed giving only major version + Given the candidate "java" version "21.0.3-blah" is already installed and default + And the candidate "java" version "17.0.11-graal" is already installed but not default + And the system is bootstrapped + When I enter "sdk use java 17" + Then I see "Using java version 17.0.11-graal in this shell." + Then the candidate "java" version "17.0.11-graal" should be in use + + Scenario: Fail to use a major version because there are multiple matching versions + Given the candidate "grails" version "1.3.0" is already installed and default + And the candidate "grails" version "2.1.0" is already installed but not default + And the candidate "grails" version "2.3.0" is already installed but not default + And the system is bootstrapped + When I enter "sdk use grails 2" + Then I see "Stop! Cannot decide which version to use." + + Scenario: Fail to use a major version because there is no matching version + Given the candidate "grails" version "1.3.0" is already installed and default + And the system is bootstrapped + When I enter "sdk use grails 2" + Then I see "Stop! No matching version found." + \ No newline at end of file From 3e8fa1c04567e9aec07c58388eb1b23be38f4f82 Mon Sep 17 00:00:00 2001 From: fistach Date: Wed, 24 Apr 2024 21:33:20 +0200 Subject: [PATCH 2/4] fix all tests for the new feature --- src/main/bash/sdkman-use.sh | 96 ++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 29 deletions(-) diff --git a/src/main/bash/sdkman-use.sh b/src/main/bash/sdkman-use.sh index 3bfe1445d..5b9e4b9b3 100644 --- a/src/main/bash/sdkman-use.sh +++ b/src/main/bash/sdkman-use.sh @@ -24,36 +24,74 @@ function __sdk_use() { __sdkman_check_version_present "$version" || return 1 __sdkman_check_candidate_present "$candidate" || return 1 - if [[ ! -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}" ]]; then - echo "" - __sdkman_echo_red "Stop! Candidate version is not installed." - echo "" - __sdkman_echo_yellow "Tip: Run the following to install this version" - echo "" - __sdkman_echo_yellow "$ sdk install ${candidate} ${version}" - return 1 - fi - - # Just update the *_HOME and PATH for this shell. - __sdkman_set_candidate_home "$candidate" "$version" - - if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then - local matched_version - - if [[ "$zsh_shell" == "true" ]]; then - matched_version=${match[1]} + major_version=$(echo $version | cut -d. -f1) + if [[ "$major_version" == "$version" ]]; then + count=0 + + for dir in "${SDKMAN_CANDIDATES_DIR}/${candidate}"/*; do + if [ -d "$dir" ] && [[ "$(basename "$dir")" == "${major_version}"* ]]; + then + ((count++)) + fi + done + if [[ ${count} -eq 0 ]] + then + echo "" + __sdkman_echo_red "Stop! No matching version found." + echo "" + elif [[ ${count} -eq 1 ]] + then + version=$(basename $(ls -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${major_version}"*)) + if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then + local matched_version + + if [[ "$zsh_shell" == "true" ]]; then + matched_version=${match[1]} + else + matched_version=${BASH_REMATCH[1]} + fi + + export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}} + fi + __sdkman_echo_green "Using ${candidate} version ${version} in this shell." else - matched_version=${BASH_REMATCH[1]} + echo "" + __sdkman_echo_red "Stop! Cannot decide which version to use." + echo "" + __sdkman_echo_yellow + fi + else + if [[ ! -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${version}" ]]; then + echo "" + __sdkman_echo_red "Stop! Candidate version is not installed." + echo "" + __sdkman_echo_yellow "Tip: Run the following to install this version" + echo "" + __sdkman_echo_yellow "$ sdk install ${candidate} ${version}" + return 1 fi - - export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}} - fi - - if [[ ! (-L "${SDKMAN_CANDIDATES_DIR}/${candidate}/current" || -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/current") ]]; then - __sdkman_echo_green "Setting ${candidate} version ${version} as default." - __sdkman_link_candidate_version "$candidate" "$version" + + # Just update the *_HOME and PATH for this shell. + __sdkman_set_candidate_home "$candidate" "$version" + + if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then + local matched_version + + if [[ "$zsh_shell" == "true" ]]; then + matched_version=${match[1]} + else + matched_version=${BASH_REMATCH[1]} + fi + + export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}} + fi + + if [[ ! (-L "${SDKMAN_CANDIDATES_DIR}/${candidate}/current" || -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/current") ]]; then + __sdkman_echo_green "Setting ${candidate} version ${version} as default." + __sdkman_link_candidate_version "$candidate" "$version" + fi + + echo "" + __sdkman_echo_green "Using ${candidate} version ${version} in this shell." fi - - echo "" - __sdkman_echo_green "Using ${candidate} version ${version} in this shell." } From f90d907272da9d74ed5482ee7278f2d55825457a Mon Sep 17 00:00:00 2001 From: fistach Date: Wed, 24 Apr 2024 21:46:35 +0200 Subject: [PATCH 3/4] refactor - deduplicate code --- src/main/bash/sdkman-use.sh | 39 +++++++++++++++---------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/bash/sdkman-use.sh b/src/main/bash/sdkman-use.sh index 5b9e4b9b3..afdeef5dc 100644 --- a/src/main/bash/sdkman-use.sh +++ b/src/main/bash/sdkman-use.sh @@ -42,17 +42,7 @@ function __sdk_use() { elif [[ ${count} -eq 1 ]] then version=$(basename $(ls -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/${major_version}"*)) - if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then - local matched_version - - if [[ "$zsh_shell" == "true" ]]; then - matched_version=${match[1]} - else - matched_version=${BASH_REMATCH[1]} - fi - - export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}} - fi + __sdkman_change_candidate_in_path "$candidate" __sdkman_echo_green "Using ${candidate} version ${version} in this shell." else echo "" @@ -74,18 +64,7 @@ function __sdk_use() { # Just update the *_HOME and PATH for this shell. __sdkman_set_candidate_home "$candidate" "$version" - if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${candidate}/([^/]+) ]]; then - local matched_version - - if [[ "$zsh_shell" == "true" ]]; then - matched_version=${match[1]} - else - matched_version=${BASH_REMATCH[1]} - fi - - export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}} - fi - + __sdkman_change_candidate_in_path "$candidate" if [[ ! (-L "${SDKMAN_CANDIDATES_DIR}/${candidate}/current" || -d "${SDKMAN_CANDIDATES_DIR}/${candidate}/current") ]]; then __sdkman_echo_green "Setting ${candidate} version ${version} as default." __sdkman_link_candidate_version "$candidate" "$version" @@ -95,3 +74,17 @@ function __sdk_use() { __sdkman_echo_green "Using ${candidate} version ${version} in this shell." fi } + +function __sdkman_change_candidate_in_path() { + if [[ $PATH =~ ${SDKMAN_CANDIDATES_DIR}/${1}/([^/]+) ]]; then + local matched_version + + if [[ "$zsh_shell" == "true" ]]; then + matched_version=${match[1]} + else + matched_version=${BASH_REMATCH[1]} + fi + + export PATH=${PATH//${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${matched_version}/${SDKMAN_CANDIDATES_DIR}\/${candidate}\/${version}} + fi +} From 86d771b87679f09b2cc6e28ccc6f407e00658245 Mon Sep 17 00:00:00 2001 From: fistach Date: Thu, 25 Apr 2024 16:25:35 +0200 Subject: [PATCH 4/4] remove specific test for java which is actually a duplicate of the previous grails test because the test framework is sensitive to "java" name --- src/test/resources/features/use_major_version.feature | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/test/resources/features/use_major_version.feature b/src/test/resources/features/use_major_version.feature index caef1052d..b2c57d722 100644 --- a/src/test/resources/features/use_major_version.feature +++ b/src/test/resources/features/use_major_version.feature @@ -12,14 +12,6 @@ Feature: Use Version Giving Only The Major Version Number Then I see "Using grails version 2.1.0 in this shell." Then the candidate "grails" version "2.1.0" should be in use - Scenario: Use a java version that is installed giving only major version - Given the candidate "java" version "21.0.3-blah" is already installed and default - And the candidate "java" version "17.0.11-graal" is already installed but not default - And the system is bootstrapped - When I enter "sdk use java 17" - Then I see "Using java version 17.0.11-graal in this shell." - Then the candidate "java" version "17.0.11-graal" should be in use - Scenario: Fail to use a major version because there are multiple matching versions Given the candidate "grails" version "1.3.0" is already installed and default And the candidate "grails" version "2.1.0" is already installed but not default