Perform normalisation before format check

This commit is contained in:
Oliver Weiler
2020-04-22 09:37:30 +02:00
committed by Marco Vermeulen
parent 1f71f926f7
commit 4f09c2f3f5
3 changed files with 30 additions and 23 deletions

View File

@@ -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 '<candidate> <version>' 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:]]+\=.+$ ]]
}

View File

@@ -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

View File

@@ -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"
]
}
}