From fc4a8e12b9984650a35109beb334c08a35abcde4 Mon Sep 17 00:00:00 2001 From: Danny Kirchmeier Date: Thu, 30 Jul 2015 18:47:32 -0500 Subject: [PATCH 1/2] Fix init from missing the last few candidates In bash, `${#ARR}` returns the length of the string in the first slot. 'asciidoctorj' has a length of 12, which was working until more candidates were added. `${#ARR[*]}` is the correct way to return the length of an array in bash. Thankfully, this works in ZSH as well. --- src/main/bash/gvm-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/bash/gvm-init.sh b/src/main/bash/gvm-init.sh index d46b4fae..05ce996d 100644 --- a/src/main/bash/gvm-init.sh +++ b/src/main/bash/gvm-init.sh @@ -170,7 +170,7 @@ if [[ "${GVM_INIT}" != "true" ]]; then # The candidates are assigned to an array for zsh compliance, a list of words is not iterable # Arrays are the only way, but unfortunately zsh arrays are not backward compatible with bash # In bash arrays are zero index based, in zsh they are 1 based(!) - for (( i=0; i <= ${#GVM_CANDIDATES}; i++ )); do + for (( i=0; i <= ${#GVM_CANDIDATES[*]}; i++ )); do # Eliminate empty entries due to incompatibility if [[ -n ${GVM_CANDIDATES[${i}]} ]]; then CANDIDATE_NAME="${GVM_CANDIDATES[${i}]}" From 5ef508427a72d347b580b6fde490b3439b4162a5 Mon Sep 17 00:00:00 2001 From: Danny Kirchmeier Date: Mon, 3 Aug 2015 16:11:20 -0500 Subject: [PATCH 2/2] Add test to prevent missing candidates --- .../gvm/specs/InitialisationSpec.groovy | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/groovy/gvm/specs/InitialisationSpec.groovy diff --git a/src/test/groovy/gvm/specs/InitialisationSpec.groovy b/src/test/groovy/gvm/specs/InitialisationSpec.groovy new file mode 100644 index 00000000..fa7bb809 --- /dev/null +++ b/src/test/groovy/gvm/specs/InitialisationSpec.groovy @@ -0,0 +1,74 @@ +package gvm.specs + +import gvm.env.BashEnv +import gvm.env.GvmBashEnvBuilder +import gvm.stubs.CurlStub +import spock.lang.Specification + +import static gvm.utils.TestUtils.prepareBaseDir + +class InitialisationSpec extends Specification { + + CurlStub curlStub + BashEnv bash + + File gvmBaseDir + String gvmDotDir + String bootstrap + + void setup(){ + gvmBaseDir = prepareBaseDir() + gvmDotDir = "${gvmBaseDir.absolutePath}/.gvm" + bootstrap = "${gvmDotDir}/bin/gvm-init.sh" + curlStub = CurlStub.prepareIn(new File(gvmBaseDir, "bin")) + } + + void "should include all candidates in PATH"(){ + given: 'a working gvm installation with many candidates' + def candidates = [ + "asciidoctorj", + "crash", + "gaiden", + "glide", + "gradle", + "grails", + "griffon", + "groovy", + "groovyserv", + "jbake", + "jbossforge", + "lazybones", + "springboot", + "vertx", + ] + bash = GvmBashEnvBuilder + .create(gvmBaseDir) + .withAvailableCandidates(candidates) + .withCandidates(candidates) + .withCurlStub(curlStub) + .withVersionToken("x.y.z") + .build() + bash.start() + bash.execute("source $bootstrap") + bash.resetOutput() + + when: 'obtaining and parsing PATH' + bash.execute('echo $PATH') + def pathParts = bash.output.split(':') + def pathElementMatcher = ~/$gvmDotDir\/([^\/]+)\/.*/ + def includedCandidates = pathParts + .collect { it =~ pathElementMatcher } + .findAll { it } + .collect { it[0][1] } + + then: + def missingCandidates = (candidates - includedCandidates) + missingCandidates.empty + } + + void cleanup(){ + println bash.output + bash.stop() + assert gvmBaseDir.deleteDir() + } +}