From 177730de962418ba00c3a514e083fef4b8526013 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Sat, 8 Dec 2018 00:45:44 -0500 Subject: [PATCH] simplify wrapped pathmunge logic. update tests to account for cases with spaces in $PATH. --- plugins/available/go.plugin.bash | 22 +++----- test/plugins/go.plugin.bats | 86 ++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 43 deletions(-) diff --git a/plugins/available/go.plugin.bash b/plugins/available/go.plugin.bash index 910194e5..04a39251 100755 --- a/plugins/available/go.plugin.bash +++ b/plugins/available/go.plugin.bash @@ -5,25 +5,15 @@ about-plugin 'go environment variables & path configuration' [ ! command -v go &>/dev/null ] && return -function _split_path_reverse() { - local a=( ${@//:/ } ) +function _go_pathmunge_wrap() { + IFS=':' local -a 'a=($1)' local i=${#a[@]} - local r= while [ $i -gt 0 ] ; do i=$(( i - 1 )) - if [ $(( i + 1 )) -eq ${#a[@]} ] ; then - r="${a[i]}" - else - r="${r} ${a[i]}" - fi + pathmunge "${a[i]}/bin" done - echo "$r" } -export GOROOT=${GOROOT:-$(go env GOROOT)} -pathmunge "${GOROOT}/bin" - -export GOPATH=${GOPATH:-$(go env GOPATH)} -for p in $( _split_path_reverse ${GOPATH} ) ; do - pathmunge "${p}/bin" -done +export GOROOT="${GOROOT:-$(go env GOROOT)}" +export GOPATH="${GOPATH:-$(go env GOPATH)}" +_go_pathmunge_wrap "${GOPATH}:${GOROOT}" diff --git a/test/plugins/go.plugin.bats b/test/plugins/go.plugin.bats index e2b27e02..a27a90e8 100644 --- a/test/plugins/go.plugin.bats +++ b/test/plugins/go.plugin.bats @@ -1,44 +1,76 @@ #!/usr/bin/env bats -#load ../test_helper load ../../lib/helpers load ../../lib/composure -load ../../plugins/available/go.plugin - -@test 'plugins go: reverse path: single entry' { - run _split_path_reverse '/foo' - echo "output = ${output}" - [ "$output" = "/foo" ] -} - -@test 'plugins go: reverse path: single entry, colon empty' { - run _split_path_reverse '/foo:' - echo "output = ${output}" - [ "$output" = "/foo" ] -} - -@test 'plugins go: reverse path: single entry, colon whitespace' { - run _split_path_reverse '/foo: ' - echo "output = ${output}" - [ "$output" = "/foo" ] -} - -@test 'plugins go: reverse path: multiple entries' { - run _split_path_reverse '/foo:/bar' - echo "output = ${output}" - [ "$output" = "/bar /foo" ] -} @test 'plugins go: single entry in GOPATH' { + export GOROOT="/baz" export GOPATH="/foo" load ../../plugins/available/go.plugin - echo "$(echo $PATH | cut -d':' -f1,2)" + + echo "$(echo $PATH | cut -d':' -f1)" [ "$(echo $PATH | cut -d':' -f1)" = "/foo/bin" ] + + echo "$(echo $PATH | cut -d':' -f2)" + [ "$(echo $PATH | cut -d':' -f2)" = "/baz/bin" ] +} + +@test 'plugins go: single entry in GOPATH, with space' { + export GOROOT="/baz" + export GOPATH="/foo bar" + load ../../plugins/available/go.plugin + + echo "$(echo $PATH | cut -d':' -f1)" + [ "$(echo $PATH | cut -d':' -f1)" = "/foo bar/bin" ] + + echo "$(echo $PATH | cut -d':' -f2)" + [ "$(echo $PATH | cut -d':' -f2)" = "/baz/bin" ] +} + +@test 'plugins go: single entry in GOPATH, with escaped space' { + export GOROOT="/baz" + export GOPATH="/foo\ bar" + load ../../plugins/available/go.plugin + + echo "$(echo $PATH | cut -d':' -f1)" + [ "$(echo $PATH | cut -d':' -f1)" = "/foo\ bar/bin" ] + + echo "$(echo $PATH | cut -d':' -f2)" + [ "$(echo $PATH | cut -d':' -f2)" = "/baz/bin" ] } @test 'plugins go: multiple entries in GOPATH' { + export GOROOT="/baz" export GOPATH="/foo:/bar" load ../../plugins/available/go.plugin + echo "$(echo $PATH | cut -d':' -f1,2)" [ "$(echo $PATH | cut -d':' -f1,2)" = "/foo/bin:/bar/bin" ] + + echo "$(echo $PATH | cut -d':' -f3)" + [ "$(echo $PATH | cut -d':' -f3)" = "/baz/bin" ] +} + +@test 'plugins go: multiple entries in GOPATH, with space' { + export GOROOT="/baz" + export GOPATH="/foo:/foo bar" + load ../../plugins/available/go.plugin + + echo "$(echo $PATH | cut -d':' -f1,2)" + [ "$(echo $PATH | cut -d':' -f1,2)" = "/foo/bin:/foo bar/bin" ] + + echo "$(echo $PATH | cut -d':' -f3)" + [ "$(echo $PATH | cut -d':' -f3)" = "/baz/bin" ] +} + +@test 'plugins go: multiple entries in GOPATH, with escaped space' { + export GOROOT="/baz" + export GOPATH="/foo:/foo\ bar" + load ../../plugins/available/go.plugin + + echo "$(echo $PATH | cut -d':' -f1,2)" + [ "$(echo $PATH | cut -d':' -f1,2)" = "/foo/bin:/foo\ bar/bin" ] + + echo "$(echo $PATH | cut -d':' -f3)" + [ "$(echo $PATH | cut -d':' -f3)" = "/baz/bin" ] }