diff --git a/scripts/build-binaries b/scripts/build-binaries index c7806b64..f43a0ec1 100755 --- a/scripts/build-binaries +++ b/scripts/build-binaries @@ -8,7 +8,7 @@ MAKE_FILE_PATH="${REPO_ROOT_PATH}/Makefile" BIN_DIR="${SCRIPTPATH}/../build/bin" mkdir -p "${BIN_DIR}" -VERSION=$(make -s -f ${MAKE_FILE_PATH} version) +VERSION=$(make -s -f "${MAKE_FILE_PATH}" version) PLATFORMS=("linux/amd64") USAGE=$(cat << 'EOM' @@ -23,7 +23,7 @@ EOM ) # Process our input arguments -while getopts "dp:v:" opt; do +while getopts "p:v:" opt; do case ${opt} in p ) # Platform Pairs IFS=',' read -ra PLATFORMS <<< "$OPTARG" @@ -39,19 +39,19 @@ while getopts "dp:v:" opt; do done for os_arch in "${PLATFORMS[@]}"; do - os=$(echo $os_arch | cut -d'/' -f1) - arch=$(echo $os_arch | cut -d'/' -f2) + os=$(echo "$os_arch" | cut -d'/' -f1) + arch=$(echo "$os_arch" | cut -d'/' -f2) container_name="extract-aeis-$os-$arch" repo_name="aeis-bin" base_bin_name="ec2-instance-selector" bin_name="${base_bin_name}-${os}-${arch}" - docker container rm $container_name || : - $SCRIPTPATH/build-docker-images -p $os_arch -v $VERSION -r $repo_name - docker container create --rm --name $container_name "$repo_name:$VERSION-$os-$arch" - docker container cp $container_name:/ec2-instance-selector $BIN_DIR/$bin_name + docker container rm "$container_name" || : + "$SCRIPTPATH/build-docker-images" -p "$os_arch" -v "$VERSION" -r "$repo_name" + docker container create --rm --name "$container_name" "$repo_name:$VERSION-$os-$arch" + docker container cp "$container_name":/ec2-instance-selector "$BIN_DIR/$bin_name" - cp ${BIN_DIR}/${bin_name} ${BIN_DIR}/${base_bin_name} - tar -zcvf ${BIN_DIR}/${bin_name}.tar.gz -C ${BIN_DIR} ${base_bin_name} - rm -f ${BIN_DIR}/${base_bin_name} + cp "${BIN_DIR}/${bin_name}" "${BIN_DIR}/${base_bin_name}" + tar -zcvf "${BIN_DIR}/${bin_name}.tar.gz" -C "${BIN_DIR}" "${base_bin_name}" + rm -f "${BIN_DIR}/${base_bin_name}" done diff --git a/scripts/build-docker-images b/scripts/build-docker-images index 3f3a7fa6..4ffe201e 100755 --- a/scripts/build-docker-images +++ b/scripts/build-docker-images @@ -6,7 +6,7 @@ SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" REPO_ROOT_PATH=$SCRIPTPATH/../ MAKE_FILE_PATH=$REPO_ROOT_PATH/Makefile -VERSION=$(make -s -f $MAKE_FILE_PATH version) +VERSION=$(make -s -f "$MAKE_FILE_PATH" version) PLATFORMS=("linux/amd64") GOPROXY="direct|https://proxy.golang.org" @@ -24,7 +24,7 @@ EOM ) # Process our input arguments -while getopts "dp:r:v:" opt; do +while getopts "p:r:v:" opt; do case ${opt} in p ) # Platform Pairs IFS=',' read -ra PLATFORMS <<< "$OPTARG" @@ -43,15 +43,15 @@ while getopts "dp:r:v:" opt; do done for os_arch in "${PLATFORMS[@]}"; do - os=$(echo $os_arch | cut -d'/' -f1) - arch=$(echo $os_arch | cut -d'/' -f2) + os=$(echo "$os_arch" | cut -d'/' -f1) + arch=$(echo "$os_arch" | cut -d'/' -f2) img_tag="$IMAGE_REPO:$VERSION-$os-$arch" docker build \ - --build-arg GOOS=${os} \ - --build-arg GOARCH=${arch} \ - --build-arg GOPROXY=${GOPROXY} \ - -t ${img_tag} \ - ${REPO_ROOT_PATH} + --build-arg GOOS="${os}" \ + --build-arg GOARCH="${arch}" \ + --build-arg GOPROXY="${GOPROXY}" \ + -t "${img_tag}" \ + "${REPO_ROOT_PATH}" done \ No newline at end of file diff --git a/scripts/create-local-tag-for-release b/scripts/create-local-tag-for-release index 8ca5023a..b933eb5b 100755 --- a/scripts/create-local-tag-for-release +++ b/scripts/create-local-tag-for-release @@ -107,13 +107,13 @@ sync_local_tags_from_remote() { } create_tag() { - git tag $NEW_TAG + git tag "$NEW_TAG" echo -e "\nāœ… Created new tag $NEW_TAG (Current latest release tag in remote: v$CURR_REMOTE_RELEASE_TAG)\n" exit 0 } get_latest_tag() { - make -s -f $MAKEFILE_PATH latest-release-tag | cut -b 2- + make -s -f "$MAKEFILE_PATH" latest-release-tag | cut -b 2- } main() { @@ -129,19 +129,19 @@ main() { # increment version if $MAJOR_INC || $MINOR_INC || $PATCH_INC; then - curr_major_v=$(echo $CURR_REMOTE_RELEASE_TAG | tr '.' '\n' | head -1) - curr_minor_v=$(echo $CURR_REMOTE_RELEASE_TAG | tr '.' '\n' | head -2 | tail -1) - curr_patch_v=$(echo $CURR_REMOTE_RELEASE_TAG | tr '.' '\n' | tail -1) + curr_major_v=$(echo "$CURR_REMOTE_RELEASE_TAG" | tr '.' '\n' | head -1) + curr_minor_v=$(echo "$CURR_REMOTE_RELEASE_TAG" | tr '.' '\n' | head -2 | tail -1) + curr_patch_v=$(echo "$CURR_REMOTE_RELEASE_TAG" | tr '.' '\n' | tail -1) if [[ $MAJOR_INC == true ]]; then - new_major_v=$(echo $(($curr_major_v + 1))) - NEW_TAG=$(echo v$new_major_v.0.0) + new_major_v=$((curr_major_v + 1)) + NEW_TAG="v$new_major_v.0.0" elif [[ $MINOR_INC == true ]]; then - new_minor_v=$(echo $(($curr_minor_v + 1))) - NEW_TAG=$(echo v$curr_major_v.$new_minor_v.0) + new_minor_v=$((curr_minor_v + 1)) + NEW_TAG="v$curr_major_v.$new_minor_v.0" elif [[ $PATCH_INC == true ]]; then - new_patch_v=$(echo $(($curr_patch_v + 1))) - NEW_TAG=$(echo v$curr_major_v.$curr_minor_v.$new_patch_v) + new_patch_v=$((curr_patch_v + 1)) + NEW_TAG="v$curr_major_v.$curr_minor_v.$new_patch_v" fi create_tag fi diff --git a/scripts/prepare-for-release b/scripts/prepare-for-release index 27825c22..132e3b03 100755 --- a/scripts/prepare-for-release +++ b/scripts/prepare-for-release @@ -10,8 +10,8 @@ set -euo pipefail REPO_ROOT_PATH="$( cd "$(dirname "$0")"; cd ../; pwd -P )" MAKEFILE_PATH=$REPO_ROOT_PATH/Makefile -LATEST_VERSION=$(make -s -f $MAKEFILE_PATH latest-release-tag | cut -b 2- ) -PREVIOUS_VERSION=$(make -s -f $MAKEFILE_PATH previous-release-tag | cut -b 2- ) +LATEST_VERSION=$(make -s -f "$MAKEFILE_PATH" latest-release-tag | cut -b 2- ) +PREVIOUS_VERSION=$(make -s -f "$MAKEFILE_PATH" previous-release-tag | cut -b 2- ) # files with versions, to update REPO_README=$REPO_ROOT_PATH/README.md @@ -24,7 +24,7 @@ NEW_BRANCH="pr/$LATEST_TAG-release" COMMIT_MESSAGE="šŸ„‘šŸ¤– $LATEST_TAG release prep šŸ¤–šŸ„‘" # PR details -DEFAULT_REPO_FULL_NAME=$(make -s -f $MAKEFILE_PATH repo-full-name) +DEFAULT_REPO_FULL_NAME=$(make -s -f "$MAKEFILE_PATH" repo-full-name) PR_BASE=main # target PR_TITLE="šŸ„‘šŸ¤– $LATEST_TAG release prep" PR_BODY="šŸ„‘šŸ¤– Auto-generated PR for $LATEST_TAG release. Updating release versions in repo." @@ -76,7 +76,7 @@ process_args() { done # set repo full name to the default value if unset - if [ -z $REPO_FULL_NAME ]; then + if [ -z "$REPO_FULL_NAME" ]; then REPO_FULL_NAME=$DEFAULT_REPO_FULL_NAME fi } @@ -100,7 +100,7 @@ verify_origin_tracking() { } create_release_branch() { - exists=$(git checkout -b $NEW_BRANCH 2>&1) || true + exists=$(git checkout -b "$NEW_BRANCH" 2>&1) || true if [[ $exists == "fatal: A branch named '$NEW_BRANCH' already exists." ]]; then echo -e "āŒ ${RED}$exists${RESET_FMT}" @@ -115,9 +115,13 @@ update_versions() { echo -e "šŸ„‘ Attempting to update instance-selector release version in preparation for a new release." for f in "${FILES[@]}"; do - has_incorrect_version=$(cat $f | grep $PREVIOUS_VERSION) + has_incorrect_version=$(cat "$f" | grep "$PREVIOUS_VERSION") if [[ ! -z $has_incorrect_version ]]; then - sed -i '' "s/$PREVIOUS_VERSION/$LATEST_VERSION/g" $f + if [[ "$OSTYPE" == "darwin"* ]]; then + sed -i '' "s/$PREVIOUS_VERSION/$LATEST_VERSION/g" "$f" + else + sed -i "s/$PREVIOUS_VERSION/$LATEST_VERSION/g" "$f" + fi FILES_CHANGED+=("$f") fi done @@ -139,7 +143,7 @@ commit_changes() { } confirm_with_user_and_create_pr(){ - git checkout $NEW_BRANCH # checkout new branch before printing git diff + git checkout "$NEW_BRANCH" # checkout new branch before printing git diff echo -e "\nšŸ„‘${BOLD}The following PR will be created:\n" cat << EOM @@ -155,7 +159,7 @@ confirm_with_user_and_create_pr(){ EOM while true; do echo -e "šŸ„‘${BOLD}Do you wish to create the release prep PR? Enter y/n" - read -p "" yn + read -r -p "" yn case $yn in [Yy]* ) create_pr; break;; [Nn]* ) rollback; exit;; @@ -166,9 +170,10 @@ EOM } create_pr() { - git push -u origin $NEW_BRANCH # sets source branch for PR to NEW_BRANCH on the fork or origin - git checkout $NEW_BRANCH # checkout new branch before creating a pr + git push -u origin "$NEW_BRANCH" # sets source branch for PR to NEW_BRANCH on the fork or origin + git checkout "$NEW_BRANCH" # checkout new branch before creating a pr + local pr_result=0 if [[ $DRAFT == true ]]; then gh pr create \ --repo "$REPO_FULL_NAME" \ @@ -176,17 +181,17 @@ create_pr() { --title "$PR_TITLE" \ --body "$PR_BODY" \ --label "$PR_LABEL_1" --label "$PR_LABEL_2" \ - --draft + --draft || pr_result=$? else gh pr create \ --repo "$REPO_FULL_NAME" \ --base "$PR_BASE" \ --title "$PR_TITLE" \ --body "$PR_BODY" \ - --label "$PR_LABEL_1" --label "$PR_LABEL_2" + --label "$PR_LABEL_1" --label "$PR_LABEL_2" || pr_result=$? fi - if [[ $? == 0 ]]; then + if [[ $pr_result == 0 ]]; then echo -e "āœ…āœ…āœ…āœ… ${BOLD}Created $LATEST_TAG release prep PR\n${RESET_FMT}" else echo -e "āŒ ${RED}PR creation failed.${RESET_FMT}āŒ" @@ -203,21 +208,21 @@ rollback() { git checkout main # delete local and remote release branch only if current execution of the script created them - git branch -D $NEW_BRANCH - git push origin --delete $NEW_BRANCH + git branch -D "$NEW_BRANCH" + git push origin --delete "$NEW_BRANCH" fi echo "${RESET_FMT}" } handle_errors() { # error handling - if [ $1 != "0" ]; then + if [ "$1" != "0" ]; then FAILED_COMMAND=${*:2} echo -e "\nāŒ ${RED}Error occurred while running command '$FAILED_COMMAND'.${RESET_FMT}āŒ" rollback exit 1 fi - exit $1 + exit "$1" } main() { diff --git a/scripts/push-docker-images b/scripts/push-docker-images index 500e40ab..d806ad81 100755 --- a/scripts/push-docker-images +++ b/scripts/push-docker-images @@ -6,7 +6,7 @@ SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" REPO_ROOT_PATH=$SCRIPTPATH/../ MAKE_FILE_PATH=$REPO_ROOT_PATH/Makefile -VERSION=$(make -s -f $MAKE_FILE_PATH version) +VERSION=$(make -s -f "$MAKE_FILE_PATH" version) PLATFORMS=("linux/amd64") MANIFEST_IMAGES="" MANIFEST="" @@ -54,8 +54,8 @@ if [[ ${#PLATFORMS[@]} -gt 1 && $MANIFEST != "true" ]]; then fi for os_arch in "${PLATFORMS[@]}"; do - os=$(echo $os_arch | cut -d'/' -f1) - arch=$(echo $os_arch | cut -d'/' -f2) + os=$(echo "$os_arch" | cut -d'/' -f1) + arch=$(echo "$os_arch" | cut -d'/' -f2) img_tag_w_platform="$IMAGE_REPO:$VERSION-$os-$arch" @@ -63,30 +63,32 @@ for os_arch in "${PLATFORMS[@]}"; do img_tag=$img_tag_w_platform else img_tag="$IMAGE_REPO:$VERSION" - docker tag $img_tag_w_platform $img_tag + docker tag "$img_tag_w_platform" "$img_tag" fi - docker push $img_tag + docker push "$img_tag" MANIFEST_IMAGES="$MANIFEST_IMAGES $img_tag" done if [[ $MANIFEST == "true" ]]; then - if [[ ! -f $DOCKER_CLI_CONFIG ]]; then - echo '{"experimental":"enabled"}' > $DOCKER_CLI_CONFIG + if [[ ! -f "$DOCKER_CLI_CONFIG" ]]; then + echo '{"experimental":"enabled"}' > "$DOCKER_CLI_CONFIG" echo "Created docker config file" fi - cat <<< $(jq '.+{"experimental":"enabled"}' $DOCKER_CLI_CONFIG) > $DOCKER_CLI_CONFIG + # shellcheck disable=SC2046,SC2094 # This pattern works despite shellcheck warnings + cat <<< "$(jq '.+{"experimental":"enabled"}' "$DOCKER_CLI_CONFIG")" > "$DOCKER_CLI_CONFIG" echo "Enabled experimental CLI features to create the docker manifest" - docker manifest create $IMAGE_REPO:$VERSION $MANIFEST_IMAGES + # shellcheck disable=SC2086 # MANIFEST_IMAGES intentionally contains space-separated values + docker manifest create "$IMAGE_REPO:$VERSION" $MANIFEST_IMAGES for os_arch in "${PLATFORMS[@]}"; do - os=$(echo $os_arch | cut -d'/' -f1) - arch=$(echo $os_arch | cut -d'/' -f2) + os=$(echo "$os_arch" | cut -d'/' -f1) + arch=$(echo "$os_arch" | cut -d'/' -f2) img_tag="$IMAGE_REPO:$VERSION-$os-$arch" - docker manifest annotate $IMAGE_REPO:$VERSION $img_tag --arch $arch --os $os + docker manifest annotate "$IMAGE_REPO:$VERSION" "$img_tag" --arch "$arch" --os "$os" done - docker manifest push $IMAGE_REPO:$VERSION + docker manifest push "$IMAGE_REPO:$VERSION" fi \ No newline at end of file diff --git a/scripts/sync-readme-to-dockerhub b/scripts/sync-readme-to-dockerhub index 6e5ff9ef..f6cdc05a 100755 --- a/scripts/sync-readme-to-dockerhub +++ b/scripts/sync-readme-to-dockerhub @@ -11,7 +11,7 @@ if git --no-pager diff --name-only HEAD^ HEAD | grep 'README.md'; then -d '{"username": "'"${DOCKERHUB_USERNAME}"'", "password": "'"${DOCKERHUB_PASSWORD}"'"}' \ https://hub.docker.com/v2/users/login/ | jq -r .token) - rcode=$(jq -n --arg msg "$(<$SCRIPTPATH/../README.md)" \ + rcode=$(jq -n --arg msg "$(<"$SCRIPTPATH"/../README.md)" \ '{"registry":"registry-1.docker.io","full_description": $msg }' | curl -s -o /dev/stderr -L -w "%{http_code}" \ https://hub.docker.com/v2/repositories/"${image}"/ \ diff --git a/scripts/sync-to-aws-homebrew-tap b/scripts/sync-to-aws-homebrew-tap index b6b85d55..90a5361d 100755 --- a/scripts/sync-to-aws-homebrew-tap +++ b/scripts/sync-to-aws-homebrew-tap @@ -85,9 +85,9 @@ if [[ -z "${REPO}" ]]; then fi if [[ -z $(command -v gh) ]] || [[ ! $(gh --version) =~ $GH_CLI_VERSION ]]; then - mkdir -p ${BUILD_DIR}/gh - curl -Lo ${BUILD_DIR}/gh/gh.tar.gz "https://github.com/cli/cli/releases/download/v${GH_CLI_VERSION}/gh_${GH_CLI_VERSION}_${OS}_amd64.tar.gz" - tar -C ${BUILD_DIR}/gh -xvf "${BUILD_DIR}/gh/gh.tar.gz" + mkdir -p "${BUILD_DIR}/gh" + curl -Lo "${BUILD_DIR}/gh/gh.tar.gz" "https://github.com/cli/cli/releases/download/v${GH_CLI_VERSION}/gh_${GH_CLI_VERSION}_${OS}_amd64.tar.gz" + tar -C "${BUILD_DIR}/gh" -xvf "${BUILD_DIR}/gh/gh.tar.gz" export PATH="${BUILD_DIR}/gh/gh_${GH_CLI_VERSION}_${OS}_amd64/bin:$PATH" if [[ ! $(gh --version) =~ $GH_CLI_VERSION ]]; then echo "āŒ Failed install of github cli" @@ -99,7 +99,7 @@ function restore_gh_config() { mv -f "${GH_CLI_CONFIG_PATH}.bkup" "${GH_CLI_CONFIG_PATH}" || : } -if [[ -n $(env | grep GITHUB_TOKEN) ]] && [[ -n "${GITHUB_TOKEN}" ]]; then +if env | grep -q GITHUB_TOKEN && [[ -n "${GITHUB_TOKEN}" ]]; then trap restore_gh_config EXIT INT TERM ERR mkdir -p "${HOME}/.config/gh" cp -f "${GH_CLI_CONFIG_PATH}" "${GH_CLI_CONFIG_PATH}.bkup" || : @@ -131,7 +131,7 @@ LINUX_ARM64_HASH="" function hash_file() { local file="${1}" - echo "$(openssl dgst -sha256 "${file}" | cut -d' ' -f2 | tr -d '\n')" + openssl dgst -sha256 "${file}" | cut -d' ' -f2 | tr -d '\n' } for os_arch in "${PLATFORMS[@]}"; do @@ -153,8 +153,8 @@ for os_arch in "${PLATFORMS[@]}"; do if [[ "${asset_file_size}" -lt 100 ]]; then ## If we cannot download and dry-run is set, build it locally if [[ "${DRY_RUN}" -eq 1 ]]; then - ${SCRIPTPATH}/build-binaries -d -v "${VERSION}" -p "${os_arch}" - cp "${BUILD_DIR}/bin/${asset_file}" ${asset_file_path} + "${SCRIPTPATH}/build-binaries" -v "${VERSION}" -p "${os_arch}" + cp "${BUILD_DIR}/bin/${asset_file}" "${asset_file_path}" else echo "ā—ļø${asset_file_path} is empty, skipping" continue @@ -198,7 +198,7 @@ if [[ "${DRY_RUN}" -eq 0 ]]; then cd "${SYNC_DIR}" gh repo fork $TAP_REPO --clone --remote cd "${FORK_DIR}" - git remote set-url origin https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${GITHUB_USERNAME}/${TAP_NAME}.git + git remote set-url origin https://"${GITHUB_USERNAME}":"${GITHUB_TOKEN}"@github.com/"${GITHUB_USERNAME}"/"${TAP_NAME}".git DEFAULT_BRANCH=$(git rev-parse --abbrev-ref HEAD | tr -d '\n') git config user.name "ec2-bot šŸ¤–" @@ -209,7 +209,7 @@ if [[ "${DRY_RUN}" -eq 0 ]]; then git push -u origin "${DEFAULT_BRANCH}" FORK_RELEASE_BRANCH="${BINARY_BASE}-${VERSION}-${BUILD_ID}" - git checkout -b "${FORK_RELEASE_BRANCH}" upstream/${DEFAULT_BRANCH} + git checkout -b "${FORK_RELEASE_BRANCH}" upstream/"${DEFAULT_BRANCH}" cp "${BREW_CONFIG_DIR}/${BINARY_BASE}.json" "${FORK_DIR}/bottle-configs/${BINARY_BASE}.json" @@ -217,11 +217,11 @@ if [[ "${DRY_RUN}" -eq 0 ]]; then git commit -m "${BINARY_BASE} update to version ${VERSION_NUM}" RELEASE_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - https://api.github.com/repos/${REPO}/releases | \ + https://api.github.com/repos/"${REPO}"/releases | \ jq --arg VERSION "$VERSION" '.[] | select(.tag_name==$VERSION) | .id') RELEASE_NOTES=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \ - https://api.github.com/repos/${REPO}/releases/${RELEASE_ID} | \ + https://api.github.com/repos/"${REPO}"/releases/"${RELEASE_ID}" | \ jq -r '.body') PR_BODY=$(cat << EOM diff --git a/scripts/upload-resources-to-github b/scripts/upload-resources-to-github index 723d2fc5..645dea52 100755 --- a/scripts/upload-resources-to-github +++ b/scripts/upload-resources-to-github @@ -2,17 +2,17 @@ set -euo pipefail SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" -VERSION=$(make -s -f $SCRIPTPATH/../Makefile version) +VERSION=$(make -s -f "$SCRIPTPATH"/../Makefile version) RELEASE_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ https://api.github.com/repos/aws/amazon-ec2-instance-selector/releases | \ jq --arg VERSION "$VERSION" '.[] | select(.tag_name==$VERSION) | .id') -for binary in $SCRIPTPATH/../build/bin/*; do +for binary in "$SCRIPTPATH"/../build/bin/*; do curl \ -H "Authorization: token $GITHUB_TOKEN" \ - -H "Content-Type: $(file -b --mime-type $binary)" \ - --data-binary @$binary \ - "https://uploads.github.com/repos/aws/amazon-ec2-instance-selector/releases/$RELEASE_ID/assets?name=$(basename $binary)" + -H "Content-Type: $(file -b --mime-type "$binary")" \ + --data-binary @"$binary" \ + "https://uploads.github.com/repos/aws/amazon-ec2-instance-selector/releases/$RELEASE_ID/assets?name=$(basename "$binary")" done diff --git a/test/e2e/run-test b/test/e2e/run-test index b5634e55..19288969 100755 --- a/test/e2e/run-test +++ b/test/e2e/run-test @@ -17,6 +17,7 @@ function echoerr() { function sort_array() { local input=( "$@" ) IFS=$'\n' + # shellcheck disable=SC2207 # Array assignment from command substitution is intentional here local sorted=($(sort <<<"${input[*]}")) unset IFS echo "${sorted[*]}" @@ -27,11 +28,14 @@ function sort_array() { function assert_contains_instance_types() { local expected=( "$@" ) local actual=() - while read actual_input; do + while read -r actual_input; do + # shellcheck disable=SC2206 # Word splitting is intentional here actual+=($actual_input) done [[ 0 -eq "${#actual[@]}" ]] && return 1 + # shellcheck disable=SC2207 # Array assignment from command substitution is intentional here local actual_sorted=($(sort_array "${actual[@]}")) + # shellcheck disable=SC2207 # Array assignment from command substitution is intentional here local expected_sorted=($(sort_array "${expected[@]}")) for expectation in "${expected_sorted[@]}"; do @@ -52,7 +56,8 @@ function execute_test() { shift local params=( "$@" ) local expected=() - while read expected_input; do + while read -r expected_input; do + # shellcheck disable=SC2206 # Word splitting is intentional here expected+=($expected_input) done [[ 0 -eq "${#expected[@]}" ]] && return 1 @@ -60,7 +65,7 @@ function execute_test() { echo "=========================== Test: ${test_name} ===========================" for p in "${params[@]}"; do - if $AEIS ${p} ${GLOBAL_PARAMS} | assert_contains_instance_types "${expected[*]}"; then + if $AEIS "${p}" ${GLOBAL_PARAMS} | assert_contains_instance_types "${expected[*]}"; then echo "āœ… ${test_name} \"$p\" passed!" else echo "āŒ Failed ${test_name} \"$p\"" | tee "${TEST_FAILURES_LOG}" diff --git a/test/license-test/check-licenses.sh b/test/license-test/check-licenses.sh index a54997d9..98e18ed8 100755 --- a/test/license-test/check-licenses.sh +++ b/test/license-test/check-licenses.sh @@ -4,10 +4,10 @@ set -euo pipefail ROOT_PATH="$( cd "$(dirname "$0")" ; pwd -P )/../../" EXIT_CODE=0 -while read pkg_license_tuple; do - pkg=$(echo $pkg_license_tuple | tr -s " " | cut -d" " -f1) - license=$(echo $pkg_license_tuple | tr -s " " | cut -d" " -f2-) - if [[ "$(grep -c ${pkg} ${ROOT_PATH}/THIRD_PARTY_LICENSES)" -ge 1 ]]; then +while read -r pkg_license_tuple; do + pkg=$(echo "$pkg_license_tuple" | tr -s " " | cut -d" " -f1) + license=$(echo "$pkg_license_tuple" | tr -s " " | cut -d" " -f2-) + if [[ "$(grep -c "${pkg}" "${ROOT_PATH}/THIRD_PARTY_LICENSES")" -ge 1 ]]; then echo "āœ… FOUND ${pkg} (${license})" else echo "šŸ”“ MISSING for ${pkg} (${license})" diff --git a/test/license-test/gen-license-report.sh b/test/license-test/gen-license-report.sh index a0db197e..e789ce9c 100755 --- a/test/license-test/gen-license-report.sh +++ b/test/license-test/gen-license-report.sh @@ -3,11 +3,11 @@ set -euo pipefail SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" BUILD_DIR="$SCRIPTPATH/../../build" -mkdir -p $BUILD_DIR +mkdir -p "$BUILD_DIR" GOBIN=$(go env GOPATH | sed 's+:+/bin+g')/bin export PATH="$PATH:$GOBIN" go install github.com/mitchellh/golicense@v0.2.0 -go build -o $BUILD_DIR/aeis $SCRIPTPATH/../../. -golicense -out-xlsx=$BUILD_DIR/report.xlsx $SCRIPTPATH/license-config.hcl $BUILD_DIR/aeis +go build -o "$BUILD_DIR/aeis" "$SCRIPTPATH/../../." +golicense -out-xlsx="$BUILD_DIR/report.xlsx" "$SCRIPTPATH/license-config.hcl" "$BUILD_DIR/aeis" diff --git a/test/license-test/run-license-test.sh b/test/license-test/run-license-test.sh index c675bd94..acc39610 100755 --- a/test/license-test/run-license-test.sh +++ b/test/license-test/run-license-test.sh @@ -10,7 +10,7 @@ LICENSE_TEST_TAG="aeis-license-test" LICENSE_REPORT_FILE="$BUILD_PATH/license-report" GOPROXY="direct|https://proxy.golang.org" -SUPPORTED_PLATFORMS_LINUX="linux/amd64" make -s -f $SCRIPTPATH/../../Makefile build-binaries -docker buildx build --load --build-arg=GOPROXY=${GOPROXY} -t $LICENSE_TEST_TAG $SCRIPTPATH/ -docker run -i -e GITHUB_TOKEN --rm -v $SCRIPTPATH/:/test -v $BUILD_BIN/:/aeis-bin $LICENSE_TEST_TAG golicense /test/license-config.hcl /aeis-bin/$BINARY_NAME | tee $LICENSE_REPORT_FILE -$SCRIPTPATH/check-licenses.sh $LICENSE_REPORT_FILE +SUPPORTED_PLATFORMS_LINUX="linux/amd64" make -s -f "$SCRIPTPATH/../../Makefile" build-binaries +docker buildx build --load --build-arg=GOPROXY=${GOPROXY} -t $LICENSE_TEST_TAG "$SCRIPTPATH/" +docker run -i -e GITHUB_TOKEN --rm -v "$SCRIPTPATH/":/test -v "$BUILD_BIN/":/aeis-bin $LICENSE_TEST_TAG golicense /test/license-config.hcl /aeis-bin/$BINARY_NAME | tee "$LICENSE_REPORT_FILE" +"$SCRIPTPATH/check-licenses.sh" "$LICENSE_REPORT_FILE" diff --git a/test/readme-test/run-readme-codeblocks b/test/readme-test/run-readme-codeblocks index ebdf1181..7d06b8f6 100755 --- a/test/readme-test/run-readme-codeblocks +++ b/test/readme-test/run-readme-codeblocks @@ -10,33 +10,35 @@ function exit_and_fail() { exit 1 } trap exit_and_fail INT ERR -docker build --target=builder -t codeblocks -f $SCRIPTPATH/../../Dockerfile $SCRIPTPATH/../../ -docker build -t rundoc -f $SCRIPTPATH/rundoc-Dockerfile $SCRIPTPATH/ +docker build --target=builder -t codeblocks -f "$SCRIPTPATH/../../Dockerfile" "$SCRIPTPATH/../../" +docker build -t rundoc -f "$SCRIPTPATH/rundoc-Dockerfile" "$SCRIPTPATH/" function rd() { - docker run -i --rm -v $SCRIPTPATH/../../:/aeis rundoc rundoc "$@" + docker run -i --rm -v "$SCRIPTPATH/../../":/aeis rundoc rundoc "$@" } rundoc_output=$(rd list-blocks /aeis/README.md) -example_files=($(echo $rundoc_output | jq -r '.code_blocks[] | .tags[1]')) -interpreters=($(echo $rundoc_output | jq -r '.code_blocks[] | .interpreter')) +# shellcheck disable=SC2207 # Array assignment from command substitution is intentional here +example_files=($(echo "$rundoc_output" | jq -r '.code_blocks[] | .tags[1]')) +# shellcheck disable=SC2207 # Array assignment from command substitution is intentional here +interpreters=($(echo "$rundoc_output" | jq -r '.code_blocks[] | .interpreter')) ## Execute --help check which compares the help codeblock in the README to the actual output of the binary -rd list-blocks -T "bash#help" /aeis/README.md | jq -r '.code_blocks[0] .code' > $BUILD_DIR/readme_help.out -docker run -t --rm codeblocks build/ec2-instance-selector --help | perl -pe 's/\e\[?.*?[a-zA-Z]//g' > $BUILD_DIR/actual_help.out +rd list-blocks -T "bash#help" /aeis/README.md | jq -r '.code_blocks[0] .code' > "$BUILD_DIR/readme_help.out" +docker run -t --rm codeblocks build/ec2-instance-selector --help | perl -pe 's/\e\[?.*?[a-zA-Z]//g' > "$BUILD_DIR/actual_help.out" diff --ignore-all-space --ignore-blank-lines --ignore-trailing-space "$BUILD_DIR/actual_help.out" "$BUILD_DIR/readme_help.out" echo "āœ… README help section matches actual binary output!" ## Execute go codeblocks example tests which checks the go codeblocks in the readme with a source file path -echo $rundoc_output | docker run -i --rm codeblocks go run test/readme-test/readme-codeblocks.go --current-dir /amazon-ec2-instance-selector/test/readme-test/ +echo "$rundoc_output" | docker run -i --rm codeblocks go run test/readme-test/readme-codeblocks.go --current-dir /amazon-ec2-instance-selector/test/readme-test/ echo "āœ… Codeblocks match source files" for i in "${!example_files[@]}"; do if [[ "${interpreters[$i]}" == "go" ]]; then example_file="${example_files[$i]}" - example_bin=$(echo $example_file | cut -d'.' -f1) - mkdir -p $BUILD_DIR/examples - docker run -i -e GOOS=$OS -e GOARCH=amd64 -e CGO_ENABLED=0 -v $BUILD_DIR:/amazon-ec2-instance-selector/build --rm codeblocks go build -o build/examples/$example_bin $example_file - $BUILD_DIR/examples/$example_bin + example_bin=$(echo "$example_file" | cut -d'.' -f1) + mkdir -p "$BUILD_DIR/examples" + docker run -i -e GOOS="$OS" -e GOARCH=amd64 -e CGO_ENABLED=0 -v "$BUILD_DIR":/amazon-ec2-instance-selector/build --rm codeblocks go build -o build/examples/"$example_bin" "$example_file" + "$BUILD_DIR/examples/$example_bin" echo "āœ… $example_file Executed Successfully!" fi done diff --git a/test/readme-test/run-readme-spellcheck b/test/readme-test/run-readme-spellcheck index b4a398d0..c6a8c616 100755 --- a/test/readme-test/run-readme-spellcheck +++ b/test/readme-test/run-readme-spellcheck @@ -9,6 +9,6 @@ function exit_and_fail() { } trap exit_and_fail INT ERR TERM -docker build -t misspell -f $SCRIPTPATH/spellcheck-Dockerfile $SCRIPTPATH/ -docker run -i --rm -v $SCRIPTPATH/../../:/aeis misspell /bin/bash -c 'find /aeis/ -type f -name "*.md" -not -path "build" | grep -v "/build/" | xargs misspell -error -debug' +docker build -t misspell -f "$SCRIPTPATH/spellcheck-Dockerfile" "$SCRIPTPATH/" +docker run -i --rm -v "$SCRIPTPATH/../../":/aeis misspell /bin/bash -c 'find /aeis/ -type f -name "*.md" -not -path "build" | grep -v "/build/" | xargs misspell -error -debug' echo "āœ… Markdown file spell check passed!" diff --git a/test/shellcheck/run-shellcheck b/test/shellcheck/run-shellcheck index 299e79e0..98aee171 100755 --- a/test/shellcheck/run-shellcheck +++ b/test/shellcheck/run-shellcheck @@ -6,7 +6,7 @@ SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" BUILD_DIR="${SCRIPTPATH}/../../build" KERNEL=$(uname -s | tr '[:upper:]' '[:lower:]') -SHELLCHECK_VERSION="0.7.1" +SHELLCHECK_VERSION="0.11.0" function exit_and_fail() { echo "āŒ Test Failed! Found a shell script with errors." @@ -14,10 +14,10 @@ function exit_and_fail() { } trap exit_and_fail INT ERR TERM -curl -Lo ${BUILD_DIR}/shellcheck.tar.xz https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${KERNEL}.x86_64.tar.xz -tar -C ${BUILD_DIR} -xvf "${BUILD_DIR}/shellcheck.tar.xz" +curl -sfLo "${BUILD_DIR}/shellcheck.tar.xz" https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}."${KERNEL}".x86_64.tar.xz +tar -C "${BUILD_DIR}" -xf "${BUILD_DIR}/shellcheck.tar.xz" export PATH="${BUILD_DIR}/shellcheck-v${SHELLCHECK_VERSION}:$PATH" -shellcheck -S error $(grep -Rn -e '#!.*/bin/bash' -e '#!.*/usr/bin/env bash' ${SCRIPTPATH}/../../ | grep ":[1-5]:" | cut -d':' -f1) +find "${SCRIPTPATH}/../../" -type f -not -path "*/.git/*" -exec file --mime-type {} \; | awk -F ':' '/text\/x-shellscript/{print$1}' | xargs -n 1 shellcheck echo "āœ… All shell scripts look good! šŸ˜Ž"