From 5ca750edb2239e1df602008d35dc924ee34145ac Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 11 Mar 2021 16:38:34 -0500 Subject: [PATCH 1/6] Add `source` to list of banned commands --- test/banned_commands.bats | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/banned_commands.bats b/test/banned_commands.bats index 1e41c601..85a618b6 100644 --- a/test/banned_commands.bats +++ b/test/banned_commands.bats @@ -22,6 +22,8 @@ banned_commands=( echo # Process substitution isn't POSIX compliant and cause trouble "<(" + # source isn't POSIX compliant. . behaves the same and is POSIX compliant + source ) setup() { From f125741fb5d1cdbb9700fc1b857a77cce22d404d Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Fri, 21 May 2021 12:03:06 -0400 Subject: [PATCH 2/6] Update code to use . instead of source --- asdf.fish | 4 ++-- bin/asdf | 6 +++--- bin/private/asdf-exec | 2 +- lib/asdf.fish | 2 +- lib/commands/command-global.bash | 2 +- lib/commands/command-local.bash | 2 +- lib/commands/command-plugin-test.bash | 2 +- lib/commands/command-reshim.bash | 2 +- lib/commands/command-uninstall.bash | 2 +- lib/utils.bash | 4 ++-- test/asdf_fish.bats | 10 +++++----- test/version_commands.bats | 8 ++++---- 12 files changed, 23 insertions(+), 23 deletions(-) diff --git a/asdf.fish b/asdf.fish index eb33f090..15736301 100644 --- a/asdf.fish +++ b/asdf.fish @@ -9,7 +9,7 @@ set -l asdf_user_shims ( ) # Add asdf to PATH -set -l asdf_bin_dirs $ASDF_DIR/bin $asdf_user_shims +set -l asdf_bin_dirs $ASDF_DIR/bin $asdf_user_shims for x in $asdf_bin_dirs if test -d $x @@ -24,4 +24,4 @@ for x in $asdf_bin_dirs end # Load the asdf wrapper function -source $ASDF_DIR/lib/asdf.fish +. $ASDF_DIR/lib/asdf.fish diff --git a/bin/asdf b/bin/asdf index a5020436..0693133d 100755 --- a/bin/asdf +++ b/bin/asdf @@ -1,7 +1,7 @@ #!/usr/bin/env bash # shellcheck source=lib/utils.bash -source "$(dirname "$(dirname "$0")")/lib/utils.bash" +. "$(dirname "$(dirname "$0")")/lib/utils.bash" find_cmd() { local cmd_dir="$1" @@ -74,12 +74,12 @@ asdf_cmd() { exec "$ASDF_CMD_FILE" "${@:${args_offset}}" elif [ -f "$ASDF_CMD_FILE" ]; then set -- "${@:${args_offset}}" - source "$ASDF_CMD_FILE" + . "$ASDF_CMD_FILE" else local asdf_cmd_dir asdf_cmd_dir="$(asdf_dir)/lib/commands" printf "%s\\n" "Unknown command: \`asdf ${*}\`" >&2 - source "$asdf_cmd_dir/command-help.bash" >&2 + . "$asdf_cmd_dir/command-help.bash" >&2 return 127 fi } diff --git a/bin/private/asdf-exec b/bin/private/asdf-exec index d24830cc..eec8b74e 100755 --- a/bin/private/asdf-exec +++ b/bin/private/asdf-exec @@ -5,7 +5,7 @@ printf "asdf is self upgrading shims to new asdf exec ...\\n" asdf_dir="$(dirname "$(dirname "$(dirname "$0")")")" # shellcheck source=lib/utils.bash -source "$asdf_dir/lib/utils.bash" +. "$asdf_dir/lib/utils.bash" rm "$(asdf_data_dir)"/shims/* "$asdf_dir"/bin/asdf reshim shim_name=$(basename "$2") diff --git a/lib/asdf.fish b/lib/asdf.fish index 34a67886..698b5444 100644 --- a/lib/asdf.fish +++ b/lib/asdf.fish @@ -7,7 +7,7 @@ function asdf switch "$command" case "shell" # source commands that need to export variables - source (asdf export-shell-version fish $argv | psub) + . (asdf export-shell-version fish $argv | psub) case '*' # forward other commands to asdf script command asdf "$command" $argv diff --git a/lib/commands/command-global.bash b/lib/commands/command-global.bash index 0bfaa06f..89f52518 100644 --- a/lib/commands/command-global.bash +++ b/lib/commands/command-global.bash @@ -1,5 +1,5 @@ # -*- sh -*- # shellcheck source=lib/commands/version_commands.bash -source "$(dirname "$ASDF_CMD_FILE")/version_commands.bash" +. "$(dirname "$ASDF_CMD_FILE")/version_commands.bash" version_command global "$@" diff --git a/lib/commands/command-local.bash b/lib/commands/command-local.bash index 50bf390b..78443d49 100644 --- a/lib/commands/command-local.bash +++ b/lib/commands/command-local.bash @@ -1,7 +1,7 @@ # -*- sh -*- # shellcheck source=lib/commands/version_commands.bash -source "$(dirname "$ASDF_CMD_FILE")/version_commands.bash" +. "$(dirname "$ASDF_CMD_FILE")/version_commands.bash" local_command() { local parent=false diff --git a/lib/commands/command-plugin-test.bash b/lib/commands/command-plugin-test.bash index 32cff4a0..e95e63a7 100644 --- a/lib/commands/command-plugin-test.bash +++ b/lib/commands/command-plugin-test.bash @@ -55,7 +55,7 @@ plugin_test_command() { export ASDF_DATA_DIR=$TEST_DIR # shellcheck disable=SC1090 - source "$ASDF_DIR/asdf.sh" + . "$ASDF_DIR/asdf.sh" if ! (asdf plugin-add "$plugin_name" "$plugin_url"); then fail_test "could not install $plugin_name from $plugin_url" diff --git a/lib/commands/command-reshim.bash b/lib/commands/command-reshim.bash index b54b68c9..92720e34 100644 --- a/lib/commands/command-reshim.bash +++ b/lib/commands/command-reshim.bash @@ -1,7 +1,7 @@ # -*- sh -*- # shellcheck source=lib/commands/reshim.bash -source "$(dirname "$ASDF_CMD_FILE")/reshim.bash" +. "$(dirname "$ASDF_CMD_FILE")/reshim.bash" reshim_command() { local plugin_name=$1 diff --git a/lib/commands/command-uninstall.bash b/lib/commands/command-uninstall.bash index a4553cd7..37ae44dd 100644 --- a/lib/commands/command-uninstall.bash +++ b/lib/commands/command-uninstall.bash @@ -1,7 +1,7 @@ # -*- sh -*- # shellcheck source=lib/commands/reshim.bash -source "$(dirname "$ASDF_CMD_FILE")/reshim.bash" +. "$(dirname "$ASDF_CMD_FILE")/reshim.bash" uninstall_command() { local plugin_name=$1 diff --git a/lib/utils.bash b/lib/utils.bash index 51d35888..58757d66 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -566,7 +566,7 @@ with_plugin_env() { ASDF_INSTALL_TYPE=$install_type \ ASDF_INSTALL_VERSION=$version \ ASDF_INSTALL_PATH=$install_path \ - source "${plugin_path}/bin/exec-env" + . "${plugin_path}/bin/exec-env" PATH=$path "$callback" } @@ -658,7 +658,7 @@ get_shim_versions() { preset_versions() { shim_name=$1 - shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c "source $(asdf_dir)/lib/utils.bash; printf \"%s %s\\n\" PLUGIN \$(get_preset_version_for PLUGIN)" + shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\\n\" PLUGIN \$(get_preset_version_for PLUGIN)" } select_from_preset_version() { diff --git a/test/asdf_fish.bats b/test/asdf_fish.bats index 16f07492..745f52e7 100644 --- a/test/asdf_fish.bats +++ b/test/asdf_fish.bats @@ -17,7 +17,7 @@ cleaned_path() { set -e ASDF_DATA_DIR set PATH $(cleaned_path) - source asdf.fish + . asdf.fish echo \$ASDF_DIR ") @@ -32,7 +32,7 @@ cleaned_path() { set -e ASDF_DATA_DIR set PATH $(cleaned_path) - source (pwd)/asdf.fish # if the full path is not passed, status -f will return the relative path + . (pwd)/asdf.fish # if the full path is not passed, status -f will return the relative path echo \$PATH ") @@ -48,8 +48,8 @@ cleaned_path() { set -e ASDF_DATA_DIR set PATH $(cleaned_path) - source asdf.fish - source asdf.fish + . asdf.fish + . asdf.fish echo \$PATH ") @@ -64,7 +64,7 @@ cleaned_path() { set -e ASDF_DIR set PATH $(cleaned_path) - source asdf.fish + . asdf.fish type asdf ") diff --git a/test/version_commands.bats b/test/version_commands.bats index f0cbb360..94b53770 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -231,14 +231,14 @@ teardown() { } @test "shell wrapper function should export ENV var" { - source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh + . $(dirname "$BATS_TEST_DIRNAME")/asdf.sh asdf shell "dummy" "1.1.0" [ $(echo $ASDF_DUMMY_VERSION) = "1.1.0" ] unset ASDF_DUMMY_VERSION } @test "shell wrapper function with --unset should unset ENV var" { - source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh + . $(dirname "$BATS_TEST_DIRNAME")/asdf.sh asdf shell "dummy" "1.1.0" [ $(echo $ASDF_DUMMY_VERSION) = "1.1.0" ] asdf shell "dummy" --unset @@ -247,7 +247,7 @@ teardown() { } @test "shell wrapper function should return an error for missing plugins" { - source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh + . $(dirname "$BATS_TEST_DIRNAME")/asdf.sh expected="No such plugin: nonexistent version 1.0.0 is not installed for nonexistent" @@ -306,7 +306,7 @@ false" } @test "shell wrapper function should support latest" { - source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh + . $(dirname "$BATS_TEST_DIRNAME")/asdf.sh asdf shell "dummy" "latest" [ $(echo $ASDF_DUMMY_VERSION) = "2.0.0" ] unset ASDF_DUMMY_VERSION From e121a9371679635cb51a7ec0e899cb625315b5a3 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Fri, 21 May 2021 12:40:20 -0400 Subject: [PATCH 3/6] Update banned_commands.bats Update banned_commands.bats so commands in comments are ignored and output is only printed when a banned command is found. --- test/banned_commands.bats | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/banned_commands.bats b/test/banned_commands.bats index 85a618b6..1bd278a0 100644 --- a/test/banned_commands.bats +++ b/test/banned_commands.bats @@ -38,8 +38,18 @@ teardown() { for cmd in "${banned_commands[@]}"; do # Assert command is not used in the lib and bin dirs # or expect an explicit comment at end of line, allowing it. - run bash -c "grep -nHR '$cmd' lib bin | grep -v '# asdf_allow: $cmd'" - echo "banned command $cmd: $output" + # Also ignore matches that are contained in comments or followed by an + # underscore (indicating it's a variable and not a command). + run bash -c "grep -nHR '$cmd' lib bin\ + | grep -v '#.*$cmd'\ + | grep -v '${cmd}_'\ + | grep -v '# asdf_allow: $cmd'" + + # Only print output if we've found a banned command + if [ "$status" -ne 1 ]; then + echo "banned command $cmd: $output" + fi + [ "$status" -eq 1 ] [ "" == "$output" ] done From 4dbe88a62adeccef98d2347bbc5ff4ffc70596ee Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Mon, 24 May 2021 17:06:17 -0400 Subject: [PATCH 4/6] Remove process substitution from command-list-all.bash file --- lib/commands/command-list-all.bash | 36 +++++++++++++----------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/lib/commands/command-list-all.bash b/lib/commands/command-list-all.bash index 06a938d6..74d7397c 100644 --- a/lib/commands/command-list-all.bash +++ b/lib/commands/command-list-all.bash @@ -4,48 +4,42 @@ list_all_command() { local plugin_name=$1 local query=$2 local plugin_path - local std_out - local std_err + local std_out_file + local std_err_file + local output plugin_path=$(get_plugin_path "$plugin_name") check_if_plugin_exists "$plugin_name" # Capture return code to allow error handling - return_code=0 && split_outputs std_out std_err "bash ${plugin_path}/bin/list-all" || return_code=$? + std_out_file="$(mktemp "/tmp/asdf-command-list-all-${plugin_name}.stdout.XXXXXX")" + std_err_file="$(mktemp "/tmp/asdf-command-list-all-${plugin_name}.stderr.XXXXXX")" + return_code=0 && $(bash ${plugin_path}/bin/list-all > "$std_out_file" 2> "$std_err_file") || return_code=$? if [[ $return_code -ne 0 ]]; then # Printing all output to allow plugin to handle error formatting printf "Plugin %s's list-all callback script failed with output:\\n" "${plugin_name}" >&2 - printf "%s\\n" "${std_err}" >&2 - printf "%s\\n" "${std_out}" >&2 + printf "%s\\n" "$(cat "$std_err_file")" >&2 + printf "%s\\n" "$(cat "$std_out_file")" >&2 + rm "$std_out_file" "$std_err_file" exit 1 fi if [[ $query ]]; then - std_out=$(tr ' ' '\n' <<<"$std_out" | + output=$(cat "$std_out_file" | tr ' ' '\n' | grep -E "^\\s*$query" | tr '\n' ' ') + else + output=$(cat "$std_out_file") fi - IFS=' ' read -r -a versions_list <<<"$std_out" + IFS=' ' read -r -a versions_list <<<"$output" for version in "${versions_list[@]}"; do printf "%s\\n" "${version}" done -} -# This function splits stdout from std error, whilst preserving the return core -function split_outputs() { - { - IFS=$'\n' read -r -d '' "${1}" - IFS=$'\n' read -r -d '' "${2}" - ( - IFS=$'\n' read -r -d '' _ERRNO_ - return "${_ERRNO_}" - ) - } < <((printf '\0%s\0%d\0' "$( ( ( ({ - ${3} - printf "%s\n" ${?} 1>&3- - } | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1) + # Remove temp files if they still exist + rm "$std_out_file" "$std_err_file" || true } list_all_command "$@" From 79eca42572998867c1b9e9b9ae96b8ffa952c666 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Mon, 24 May 2021 17:30:51 -0400 Subject: [PATCH 5/6] Update banned_commands.bats tests to ignore string literals --- test/banned_commands.bats | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/banned_commands.bats b/test/banned_commands.bats index 1bd278a0..f1fee138 100644 --- a/test/banned_commands.bats +++ b/test/banned_commands.bats @@ -38,10 +38,12 @@ teardown() { for cmd in "${banned_commands[@]}"; do # Assert command is not used in the lib and bin dirs # or expect an explicit comment at end of line, allowing it. - # Also ignore matches that are contained in comments or followed by an - # underscore (indicating it's a variable and not a command). + # Also ignore matches that are contained in comments or a string or + # followed by an underscore (indicating it's a variable and not a + # command). run bash -c "grep -nHR '$cmd' lib bin\ | grep -v '#.*$cmd'\ + | grep -v '\".*$cmd.*\"' \ | grep -v '${cmd}_'\ | grep -v '# asdf_allow: $cmd'" From 0f508418e42cfc3a6ee54a12e3cf50b57ee677c0 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Mon, 24 May 2021 17:54:04 -0400 Subject: [PATCH 6/6] Address shellcheck and shfmt warnings --- lib/commands/command-list-all.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/commands/command-list-all.bash b/lib/commands/command-list-all.bash index 74d7397c..d1660297 100644 --- a/lib/commands/command-list-all.bash +++ b/lib/commands/command-list-all.bash @@ -13,7 +13,7 @@ list_all_command() { # Capture return code to allow error handling std_out_file="$(mktemp "/tmp/asdf-command-list-all-${plugin_name}.stdout.XXXXXX")" std_err_file="$(mktemp "/tmp/asdf-command-list-all-${plugin_name}.stderr.XXXXXX")" - return_code=0 && $(bash ${plugin_path}/bin/list-all > "$std_out_file" 2> "$std_err_file") || return_code=$? + return_code=0 && bash "${plugin_path}/bin/list-all" >"$std_out_file" 2>"$std_err_file" || return_code=$? if [[ $return_code -ne 0 ]]; then # Printing all output to allow plugin to handle error formatting @@ -25,7 +25,7 @@ list_all_command() { fi if [[ $query ]]; then - output=$(cat "$std_out_file" | tr ' ' '\n' | + output=$(tr ' ' '\n' <"$std_out_file" | grep -E "^\\s*$query" | tr '\n' ' ') else