lib/helpers: first `shellcheck` pass

Quote things, SC2268, SC2143, SC2181, SC2162, SC2016, SC2013, &c.

Rewrite globbing per `shellcheck`’s SC2013, and alsö s/typeset/local/g. Eliminate `compgen` where possible.

Alsö: use the existing utility functions `_bash-it-get-component-type-from-path` and `_bash-it-get-component-name-from-path`, which just use parameter substitution anyway. Why was `sed` here?

Alsö, don't add not-existing directories to `$PATH` in `pathmunge()`.

Finally, merge PR #1865 from NoahGorny...and clean it a bit...
pull/1934/head
John D Pell 2022-01-03 15:57:18 -08:00 committed by John D Pell
parent 9b51dc0b5f
commit 5eab3bd288
6 changed files with 401 additions and 366 deletions

720
lib/helpers.bash 100755 → 100644

File diff suppressed because it is too large Load Diff

View File

View File

View File

View File

@ -3,6 +3,22 @@
load ../test_helper load ../test_helper
load ../test_helper_libs load ../test_helper_libs
function local_setup()
{
setup_test_fixture
}
function setup_go_path()
{
local go_path="$1"
# Make sure that the requested GO folder is available
assert_dir_exist "$go_path/bin"
# Make sure that the requested GO folder is on the path
export GOPATH="$go_path:${GOPATH:-}"
}
# We test `go version` in each test to account for users with goenv and no system go. # We test `go version` in each test to account for users with goenv and no system go.
@test 'ensure _bash-it-gopath-pathmunge is defined' { @test 'ensure _bash-it-gopath-pathmunge is defined' {
@ -14,42 +30,47 @@ load ../test_helper_libs
@test 'plugins go: single entry in GOPATH' { @test 'plugins go: single entry in GOPATH' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo/bin" assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath/bin"
} }
@test 'plugins go: single entry in GOPATH, with space' { @test 'plugins go: single entry in GOPATH, with space' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo bar" setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo bar/bin" assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin"
} }
@test 'plugins go: single entry in GOPATH, with escaped space' { @test 'plugins go: single entry in GOPATH, with escaped space' {
skip 'huh?'
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo\ bar" setup_go_path "$BASH_IT/test/fixtures/go/go\ path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo\ bar/bin" assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin"
} }
@test 'plugins go: multiple entries in GOPATH' { @test 'plugins go: multiple entries in GOPATH' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo:/bar" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/gopath2"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/bar/bin" assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath2/bin:$BASH_IT/test/fixtures/go/gopath/bin"
} }
@test 'plugins go: multiple entries in GOPATH, with space' { @test 'plugins go: multiple entries in GOPATH, with space' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo:/foo bar" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/foo bar/bin" assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin:$BASH_IT/test/fixtures/go/gopath/bin"
} }
@test 'plugins go: multiple entries in GOPATH, with escaped space' { @test 'plugins go: multiple entries in GOPATH, with escaped space' {
skip 'huh?'
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found' { _command_exists go && go version &>/dev/null; } || skip 'golang not found'
export GOPATH="/foo:/foo\ bar" setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/foo\ bar/bin" assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin:$BASH_IT/test/fixtures/go/gopath/bin"
} }

View File

@ -6,6 +6,8 @@ load ../test_helper_libs
function local_setup { function local_setup {
setup_test_fixture setup_test_fixture
_command_exists "ruby" && mkdir -p "$(ruby -e 'print Gem.user_dir')/bin"
export OLD_PATH="$PATH" export OLD_PATH="$PATH"
export PATH="/usr/bin:/bin:/usr/sbin" export PATH="/usr/bin:/bin:/usr/sbin"
} }