2019-11-27 01:24:29 -07:00
|
|
|
# -*- sh -*-
|
2022-04-25 05:45:19 -07:00
|
|
|
# shellcheck source=lib/functions/versions.bash
|
|
|
|
. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash"
|
|
|
|
# shellcheck source=lib/functions/plugins.bash
|
|
|
|
. "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash"
|
|
|
|
# shellcheck source=lib/commands/reshim.bash
|
|
|
|
. "$(dirname "$ASDF_CMD_FILE")/reshim.bash"
|
|
|
|
# shellcheck source=lib/functions/installs.bash
|
|
|
|
. "$(dirname "$(dirname "$0")")/lib/functions/installs.bash"
|
2019-11-27 01:24:29 -07:00
|
|
|
|
2016-05-13 00:04:01 -07:00
|
|
|
plugin_test_command() {
|
|
|
|
|
2021-11-02 06:48:26 -07:00
|
|
|
local plugin_name="$1"
|
|
|
|
local plugin_url="$2"
|
|
|
|
shift 2
|
|
|
|
|
2024-01-09 05:47:27 -07:00
|
|
|
local exit_code
|
|
|
|
local TEST_DIR
|
|
|
|
|
|
|
|
fail_test() {
|
|
|
|
printf "FAILED: %s\n" "$1"
|
|
|
|
rm -rf "$TEST_DIR"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -z "$plugin_name" ] || [ -z "$plugin_url" ]; then
|
|
|
|
fail_test "please provide a plugin name and url"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local plugin_gitref
|
2018-10-20 08:13:55 -07:00
|
|
|
local tool_version
|
2021-11-02 06:48:26 -07:00
|
|
|
local interpret_args_literally
|
2021-11-07 15:40:53 -07:00
|
|
|
local skip_next_arg
|
2021-11-02 06:48:26 -07:00
|
|
|
|
|
|
|
for arg; do
|
|
|
|
shift
|
2021-11-07 15:40:53 -07:00
|
|
|
if [ -n "${skip_next_arg}" ]; then
|
|
|
|
skip_next_arg=
|
|
|
|
elif [ -n "${interpret_args_literally}" ]; then
|
2021-11-02 06:48:26 -07:00
|
|
|
set -- "$@" "${arg}"
|
|
|
|
else
|
|
|
|
case "${arg}" in
|
|
|
|
--asdf-plugin-gitref)
|
2021-11-07 15:40:53 -07:00
|
|
|
plugin_gitref="$1"
|
|
|
|
skip_next_arg=true
|
2021-11-02 06:48:26 -07:00
|
|
|
;;
|
|
|
|
--asdf-tool-version)
|
2021-11-07 15:40:53 -07:00
|
|
|
tool_version="$1"
|
|
|
|
skip_next_arg=true
|
2021-11-02 06:48:26 -07:00
|
|
|
;;
|
|
|
|
--)
|
|
|
|
interpret_args_literally=true
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
set -- "$@" "${arg}"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2018-10-20 08:13:55 -07:00
|
|
|
done
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2024-01-09 05:47:27 -07:00
|
|
|
if [ -z "$plugin_gitref" ]; then
|
|
|
|
plugin_remote_default_branch=$(git ls-remote --symref "$plugin_url" HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }')
|
|
|
|
plugin_gitref=${3:-${plugin_remote_default_branch}}
|
2021-11-02 06:48:26 -07:00
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2024-01-09 05:47:27 -07:00
|
|
|
if [ "$#" -eq 1 ]; then
|
|
|
|
set -- "${SHELL:-sh}" -c "$1"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
2016-05-13 00:04:01 -07:00
|
|
|
|
2024-12-16 12:39:03 -07:00
|
|
|
TEST_DIR=$(mktemp -dt asdf.XXXXXX)
|
2019-11-30 14:00:58 -07:00
|
|
|
cp -R "$(asdf_dir)/bin" "$TEST_DIR"
|
|
|
|
cp -R "$(asdf_dir)/lib" "$TEST_DIR"
|
|
|
|
cp "$(asdf_dir)/asdf.sh" "$TEST_DIR"
|
2016-05-13 00:04:01 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
plugin_test() {
|
|
|
|
export ASDF_DIR=$TEST_DIR
|
|
|
|
export ASDF_DATA_DIR=$TEST_DIR
|
2016-05-13 00:04:01 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
# shellcheck disable=SC1090
|
2021-05-21 09:03:06 -07:00
|
|
|
. "$ASDF_DIR/asdf.sh"
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (plugin_add_command "$plugin_name" "$plugin_url"); then
|
2018-10-19 19:46:15 -07:00
|
|
|
fail_test "could not install $plugin_name from $plugin_url"
|
|
|
|
fi
|
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
# shellcheck disable=SC2119
|
2024-01-09 05:39:18 -07:00
|
|
|
if ! (plugin_list_command | grep -q "^$plugin_name$"); then
|
2018-08-29 23:16:01 -07:00
|
|
|
fail_test "$plugin_name was not properly installed"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (plugin_update_command "$plugin_name" "$plugin_gitref"); then
|
2019-11-30 13:08:52 -07:00
|
|
|
fail_test "failed to checkout $plugin_name gitref: $plugin_gitref"
|
|
|
|
fi
|
|
|
|
|
2018-10-19 19:46:15 -07:00
|
|
|
local plugin_path
|
2018-08-29 23:16:01 -07:00
|
|
|
plugin_path=$(get_plugin_path "$plugin_name")
|
|
|
|
local list_all="$plugin_path/bin/list-all"
|
2024-01-09 05:39:18 -07:00
|
|
|
if grep -q api.github.com "$list_all"; then
|
|
|
|
if ! grep -q Authorization "$list_all"; then
|
2022-12-23 02:53:22 -07:00
|
|
|
printf "\nLooks like %s/bin/list-all relies on GitHub releases\n" "$plugin_name"
|
|
|
|
printf "but it does not properly sets an Authorization header to prevent\n"
|
|
|
|
printf "GitHub API rate limiting.\n\n"
|
|
|
|
printf "See https://github.com/asdf-vm/asdf/blob/master/docs/creating-plugins.md#github-api-rate-limiting\n"
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-08-29 23:19:30 -07:00
|
|
|
fail_test "$plugin_name/bin/list-all does not set GitHub Authorization token"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
|
|
|
|
2018-08-29 23:19:30 -07:00
|
|
|
# test for most common token names we have on plugins. If both are empty show this warning
|
|
|
|
if [ -z "$OAUTH_TOKEN" ] && [ -z "$GITHUB_API_TOKEN" ]; then
|
2022-12-23 02:53:22 -07:00
|
|
|
printf "%s/bin/list-all is using GitHub API, just be sure you provide an API Authorization token\n" "$plugin_name"
|
|
|
|
printf "via your CI env GITHUB_API_TOKEN. This is the current rate_limit:\n\n"
|
2018-08-29 23:19:30 -07:00
|
|
|
curl -s https://api.github.com/rate_limit
|
2022-12-23 02:53:22 -07:00
|
|
|
printf "\n"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
2018-08-29 23:19:30 -07:00
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
local versions
|
2018-08-29 23:19:30 -07:00
|
|
|
# shellcheck disable=SC2046
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! read -r -a versions <<<$(list_all_command "$plugin_name"); then
|
2018-08-29 23:16:01 -07:00
|
|
|
fail_test "list-all exited with an error"
|
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
if [ ${#versions} -eq 0 ]; then
|
|
|
|
fail_test "list-all did not return any version"
|
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-10-20 08:13:55 -07:00
|
|
|
local version
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-10-20 08:13:55 -07:00
|
|
|
# Use the version passed in if it was set. Otherwise grab the latest
|
|
|
|
# version from the versions list
|
2019-11-30 14:13:33 -07:00
|
|
|
if [ -z "$tool_version" ] || [[ "$tool_version" == *"latest"* ]]; then
|
2022-04-25 05:45:19 -07:00
|
|
|
version="$(latest_command "$plugin_name" "$(sed -e 's#latest##;s#^:##' <<<"$tool_version")")"
|
2020-08-17 15:38:36 -07:00
|
|
|
if [ -z "$version" ]; then
|
2020-08-09 20:27:35 -07:00
|
|
|
fail_test "could not get latest version"
|
|
|
|
fi
|
2018-10-20 08:13:55 -07:00
|
|
|
else
|
2019-11-30 14:13:33 -07:00
|
|
|
version="$tool_version"
|
2018-10-20 08:13:55 -07:00
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (install_command "$plugin_name" "$version"); then
|
2018-08-29 23:16:01 -07:00
|
|
|
fail_test "install exited with an error"
|
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
cd "$TEST_DIR" || fail_test "could not cd $TEST_DIR"
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (local_command "$plugin_name" "$version"); then
|
2018-08-29 23:16:01 -07:00
|
|
|
fail_test "install did not add the requested version"
|
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (reshim_command "$plugin_name"); then
|
2018-08-29 23:16:01 -07:00
|
|
|
fail_test "could not reshim plugin"
|
2016-06-15 22:06:00 -07:00
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2021-11-02 06:48:26 -07:00
|
|
|
if [ "$#" -gt 0 ]; then
|
|
|
|
"$@"
|
2018-08-29 23:16:01 -07:00
|
|
|
exit_code=$?
|
|
|
|
if [ $exit_code != 0 ]; then
|
2022-12-29 13:24:24 -07:00
|
|
|
fail_test "$* failed with exit code $exit_code"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
2018-08-29 23:16:01 -07:00
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
# Assert the scripts in bin are executable by asdf
|
|
|
|
for filename in "$ASDF_DIR/plugins/$plugin_name/bin"/*; do
|
|
|
|
if [ ! -x "$filename" ]; then
|
|
|
|
fail_test "Incorrect permissions on $filename. Must be executable by asdf"
|
|
|
|
fi
|
|
|
|
done
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
# Assert that a license file exists in the plugin repo and is not empty
|
|
|
|
license_file="$ASDF_DIR/plugins/$plugin_name/LICENSE"
|
|
|
|
if [ -f "$license_file" ]; then
|
|
|
|
if [ ! -s "$license_file" ]; then
|
|
|
|
fail_test "LICENSE file in the plugin repository must not be empty"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
fail_test "LICENSE file must be present in the plugin repository"
|
2017-03-07 15:33:41 -07:00
|
|
|
fi
|
2018-08-29 23:16:01 -07:00
|
|
|
}
|
2019-11-29 09:15:38 -07:00
|
|
|
|
2018-08-29 23:16:01 -07:00
|
|
|
# run test in a subshell
|
2021-11-02 06:48:26 -07:00
|
|
|
(plugin_test "$@")
|
2018-08-29 23:16:01 -07:00
|
|
|
exit_code=$?
|
|
|
|
rm -rf "$TEST_DIR"
|
|
|
|
exit $exit_code
|
2016-05-13 00:04:01 -07:00
|
|
|
}
|
2019-11-27 01:24:29 -07:00
|
|
|
|
|
|
|
plugin_test_command "$@"
|