diff --git a/src/main/bash/sdkman-env.sh b/src/main/bash/sdkman-env.sh index 54b5aa3d..afa163c7 100644 --- a/src/main/bash/sdkman-env.sh +++ b/src/main/bash/sdkman-env.sh @@ -17,7 +17,7 @@ # function __sdk_env() { - readonly sdkmanrc='.sdkmanrc' + readonly sdkmanrc=".sdkmanrc" if [[ $1 == 'init' ]]; then cat <<- EOF > "$sdkmanrc" @@ -28,33 +28,38 @@ function __sdk_env() { fi if [[ ! -f "$sdkmanrc" ]]; then - __sdkman_echo_red "SDKMAN can't find an .sdkmanrc file in your current directory." + __sdkman_echo_red "Could not find .sdkmanrc file in current directory." echo "" - __sdkman_echo_yellow "We recommend creating one by entering 'sdk env init'." + __sdkman_echo_yellow "Run 'sdk env init' to create it." return 1 fi while IFS= read -r line || [[ -n $line ]]; do - __sdkman_is_blank_line_or_comment "$line" && continue + local normalised_line=$(__sdkman_normalise "$line") - if ! __sdkman_matches_candidate_format "$line"; then - __sdkman_echo_red "Invalid candidate format! Expected ' ' but found '$line'" + [[ -z $normalised_line ]] && continue + + if ! __sdkman_matches_candidate_format "$normalised_line"; then + __sdkman_echo_red 'Invalid candidate format!' + echo "" + __sdkman_echo_yellow "Expected 'candidate=version' but found '$normalised_line'" return 1 fi - local candidate version rest - IFS=$' \t' read -r candidate version rest <<< "$line" - - __sdk_use "$candidate" "$version" + __sdk_use "${normalised_line%=*}" "${normalised_line#*=}" done < "$sdkmanrc" } -function __sdkman_is_blank_line_or_comment() { - [[ $1 =~ ^[[:blank:]]*(\#|$) ]] +function __sdkman_normalise() { + # strip comments + local result="${1/\#*/}" + + # strip whitespace + printf '%s\n' "${result//[[:space:]]/}" } function __sdkman_matches_candidate_format() { - [[ $1 =~ ^[[:blank:]]*[[:lower:]]+[[:blank:]]+[^[:blank:]]+ ]] + [[ $1 =~ ^[[:lower:]]+\=.+$ ]] } \ No newline at end of file diff --git a/src/test/cucumber/per_project_configuration.feature b/src/test/cucumber/per_project_configuration.feature index 793bfdcb..8b6c2860 100644 --- a/src/test/cucumber/per_project_configuration.feature +++ b/src/test/cucumber/per_project_configuration.feature @@ -12,7 +12,7 @@ Feature: Per-project configuration And the exit code is 1 Scenario: The env command is issued with an sdkman project configuration present - Given the file ".sdkmanrc" exists and contains "groovy 2.4.1" + Given the file ".sdkmanrc" exists and contains "groovy=2.4.1" And the candidate "groovy" version "2.0.5" is already installed and default And the candidate "groovy" version "2.4.1" is a valid candidate version And the candidate "groovy" version "2.4.1" is already installed but not default diff --git a/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy b/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy index 3d55e1f5..6956754e 100644 --- a/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy +++ b/src/test/groovy/sdkman/specs/EnvCommandSpec.groovy @@ -1,7 +1,9 @@ package sdkman.specs import sdkman.support.SdkmanEnvSpecification +import spock.lang.Unroll +@Unroll class EnvCommandSpec extends SdkmanEnvSpecification { def setup() { @@ -37,17 +39,17 @@ class EnvCommandSpec extends SdkmanEnvSpecification { where: sdkrc << [ - "grails 2.1.0\ngroovy 2.4.1", - "grails 2.1.0\ngroovy 2.4.1\n", - " grails 2.1.0\ngroovy 2.4.1\n", - "grails 2.1.0 \ngroovy 2.4.1\n", - "grails 2.1.0\ngroovy 2.4.1\n", + "grails=2.1.0\ngroovy=2.4.1", + "grails=2.1.0\ngroovy=2.4.1\n", + " grails=2.1.0\ngroovy=2.4.1\n", + "grails=2.1.0 \ngroovy=2.4.1\n", + "grails=2.1.0\ngroovy = 2.4.1\n", ] } def "should issue an error if .sdkmanrc contains malformed candidate entries"() { given: - new File(bash.workDir, ".sdkmanrc").text = "Groovy 2.4.1" + new File(bash.workDir, ".sdkmanrc").text = "groovy 2.4.1" when: bash.execute("sdk env") @@ -77,9 +79,9 @@ class EnvCommandSpec extends SdkmanEnvSpecification { where: sdkrc << [ - "\ngroovy 2.4.1\n", - "# this is a comment\ngroovy 2.4.1\n", - "groovy 2.4.1 # this is a comment too\n" + "\ngroovy=2.4.1\n", + "# this is a comment\ngroovy=2.4.1\n", + "groovy=2.4.1 # this is a comment too\n" ] } } \ No newline at end of file