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
|
|
|
|
|
2019-11-30 14:00:58 -07:00
|
|
|
local plugin_gitref="master"
|
2019-11-29 09:15:38 -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
|
2019-11-29 09:15:38 -07:00
|
|
|
done
|
|
|
|
|
2021-11-02 06:48:26 -07:00
|
|
|
if [ "$#" -eq 1 ]; then
|
|
|
|
set -- "${SHELL:-sh}" -c "$1"
|
|
|
|
fi
|
2019-11-29 09:15:38 -07:00
|
|
|
|
|
|
|
local exit_code
|
|
|
|
local TEST_DIR
|
|
|
|
|
|
|
|
fail_test() {
|
2020-09-21 15:27:52 -07:00
|
|
|
printf "FAILED: %s\\n" "$1"
|
2019-11-29 09:15:38 -07:00
|
|
|
rm -rf "$TEST_DIR"
|
|
|
|
exit 1
|
|
|
|
}
|
2018-10-20 08:13:55 -07:00
|
|
|
|
2019-11-29 09:15:38 -07:00
|
|
|
if [ -z "$plugin_name" ] || [ -z "$plugin_url" ]; then
|
|
|
|
fail_test "please provide a plugin name and url"
|
|
|
|
fi
|
2016-05-13 00:04:01 -07:00
|
|
|
|
2019-11-29 09:15:38 -07:00
|
|
|
TEST_DIR=$(mktemp -dt asdf.XXXX)
|
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
|
|
|
|
2019-11-29 09:15:38 -07:00
|
|
|
plugin_test() {
|
|
|
|
export ASDF_DIR=$TEST_DIR
|
|
|
|
export ASDF_DATA_DIR=$TEST_DIR
|
2016-05-13 00:04:01 -07:00
|
|
|
|
2019-11-29 09:15:38 -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
|
2019-11-29 09:15:38 -07:00
|
|
|
fail_test "could not install $plugin_name from $plugin_url"
|
2018-10-19 19:46:15 -07:00
|
|
|
fi
|
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
# shellcheck disable=SC2119
|
|
|
|
if ! (plugin_list_command | grep "^$plugin_name$" >/dev/null); then
|
2019-11-29 09:15:38 -07:00
|
|
|
fail_test "$plugin_name was not properly installed"
|
|
|
|
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
|
|
|
|
|
2019-11-29 09:15:38 -07:00
|
|
|
local plugin_path
|
|
|
|
plugin_path=$(get_plugin_path "$plugin_name")
|
|
|
|
local list_all="$plugin_path/bin/list-all"
|
|
|
|
if grep api.github.com "$list_all" >/dev/null; then
|
|
|
|
if ! grep Authorization "$list_all" >/dev/null; then
|
2020-09-21 15:27:52 -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
|
|
|
|
|
|
|
fail_test "$plugin_name/bin/list-all does not set GitHub Authorization token"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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
|
2020-09-21 15:27:52 -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"
|
2019-11-29 09:15:38 -07:00
|
|
|
curl -s https://api.github.com/rate_limit
|
2020-09-21 15:27:52 -07:00
|
|
|
printf "\\n"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
local versions
|
|
|
|
# shellcheck disable=SC2046
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! read -r -a versions <<<$(list_all_command "$plugin_name"); then
|
2019-11-29 09:15:38 -07:00
|
|
|
fail_test "list-all exited with an error"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ${#versions} -eq 0 ]; then
|
|
|
|
fail_test "list-all did not return any version"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local version
|
|
|
|
|
|
|
|
# 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
|
2019-11-29 09:15:38 -07:00
|
|
|
else
|
2019-11-30 14:13:33 -07:00
|
|
|
version="$tool_version"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (install_command "$plugin_name" "$version"); then
|
2019-11-29 09:15:38 -07:00
|
|
|
fail_test "install exited with an error"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd "$TEST_DIR" || fail_test "could not cd $TEST_DIR"
|
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (local_command "$plugin_name" "$version"); then
|
2019-11-29 09:15:38 -07:00
|
|
|
fail_test "install did not add the requested version"
|
|
|
|
fi
|
|
|
|
|
2022-04-25 05:45:19 -07:00
|
|
|
if ! (reshim_command "$plugin_name"); then
|
2019-11-29 09:15:38 -07:00
|
|
|
fail_test "could not reshim plugin"
|
|
|
|
fi
|
|
|
|
|
2021-11-02 06:48:26 -07:00
|
|
|
if [ "$#" -gt 0 ]; then
|
|
|
|
"$@"
|
2019-11-29 09:15:38 -07:00
|
|
|
exit_code=$?
|
|
|
|
if [ $exit_code != 0 ]; then
|
2021-11-02 06:48:26 -07:00
|
|
|
fail_test "$* failed with exit code $?"
|
2019-11-29 09:15:38 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# run test in a subshell
|
2021-11-02 06:48:26 -07:00
|
|
|
(plugin_test "$@")
|
2019-11-29 09:15:38 -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 "$@"
|