From f9b0c70b2b4da6f0144934ac3c186b9cc95ab83b Mon Sep 17 00:00:00 2001 From: Owen Nelson Date: Wed, 24 Jan 2024 15:02:41 -0600 Subject: [PATCH] Test install script in pipeline Authored-by: Owen Nelson --- .github/workflows/test-and-coverage.yml | 8 ++- install.sh | 2 +- test-install.sh | 81 +++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 test-install.sh diff --git a/.github/workflows/test-and-coverage.yml b/.github/workflows/test-and-coverage.yml index f82776d..085cea2 100644 --- a/.github/workflows/test-and-coverage.yml +++ b/.github/workflows/test-and-coverage.yml @@ -1,4 +1,4 @@ -name: Build +name: Test on: push: @@ -27,3 +27,9 @@ jobs: # You may pin to the exact commit or the version. # uses: codecov/codecov-action@e156083f13aff6830c92fc5faa23505779fbf649 uses: codecov/codecov-action@v1.2.1 + + - name: Install bash_unit + run: bash <(curl -s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh) + + - name: Test install script + run: ./bash_unit test-install.sh diff --git a/install.sh b/install.sh index dc1fb7b..76faa1e 100755 --- a/install.sh +++ b/install.sh @@ -80,7 +80,7 @@ function set_binary_name() { function download() { ASSETS=$(curl -Ls https://api.github.com/repos/"$INSTALL_ORG_REPO"/releases/latest | - grep download_url | awk '{print $2}' | tr -d '"') + grep download_url | awk '{print $2}' | tr -d '"') BINARY_URL=$(echo "$ASSETS" | grep "$BINARY_NAME") CHECKSUM_URL=$(echo "$ASSETS" | grep $CHECKSUM_FILE_NAME) echo_debug "Downloading $BINARY_NAME and from $BINARY_URL" diff --git a/test-install.sh b/test-install.sh new file mode 100644 index 0000000..322c423 --- /dev/null +++ b/test-install.sh @@ -0,0 +1,81 @@ +_linux_uname() { + if [ "${FAKE_PARAMS[0]}" = "-m" ]; then + echo "x86_64" + else + echo "Linux" + fi +} +export -f _linux_uname + +_windows_uname() { + if [ "${FAKE_PARAMS[0]}" = "-m" ]; then + echo "i686" + else + echo "MINGW64_NT-10.0-19045" + fi +} +export -f _windows_uname + +_mac_uname() { + if [ "${FAKE_PARAMS[0]}" = "-m" ]; then + echo "aarch64" + else + echo "Darwin" + fi +} +export -f _mac_uname + +setup() { + temp=$(mktemp -d) + fake uname _linux_uname + fake curl 'echo "download_url: talisman_linux_amd64checksums"' + fake shasum true + fake tput true +} + +teardown() { + rm -rf "$temp" +} + +test_installs_without_sudo() { + fake sudo 'echo "expected no sudo" && exit 1' + INSTALL_LOCATION=$temp ./install.sh + assert "test -x $temp/talisman_linux_amd64" "Should install file with executable mode" + assert_matches "$temp/talisman_linux_amd64" "$(readlink "$temp/talisman")" "Should create a link" +} + +test_installs_with_sudo_if_available() { + fake touch 'echo "Permission denied" && exit 1' + fake which 'echo "sudo installed" && exit 0' + # shellcheck disable=SC2016 + fake sudo 'bash -c "${FAKE_PARAMS[*]}"' + INSTALL_LOCATION=$temp ./install.sh + assert "test -x $temp/talisman_linux_amd64" "Should install file with executable mode" + assert_matches "$temp/talisman_linux_amd64" "$(readlink "$temp/talisman")" "Should create a link" +} + +test_errors_if_unable_to_install() { + fake touch 'echo "Permission denied" && exit 1' + fake which 'echo "sudo not installed" && exit 1' + assert_status_code 126 "INSTALL_LOCATION=$temp ./install.sh" +} + +test_errors_if_no_install_location() { + assert_status_code 1 "INSTALL_LOCATION=/does/not/exist ./install.sh" +} + +test_mac_arm_binary_name() { + fake uname _mac_uname + fake curl 'echo "download_url: talisman_darwin_arm64checksums"' + INSTALL_LOCATION=$temp ./install.sh + assert "test -x $temp/talisman_darwin_arm64" "Should install file with executable mode" + assert_matches "$temp/talisman_darwin_arm64" "$(readlink "$temp/talisman")" "Should create a link" +} + +test_windows_binary_name() { + fake uname _windows_uname + fake curl 'echo "download_url: talisman_windows_386.exechecksums"' + INSTALL_LOCATION=$temp ./install.sh + assert "test -x $temp/talisman_windows_386.exe" "Should install file with executable mode" + assert_matches "$temp/talisman_windows_386.exe" "$(readlink "$temp/talisman")" "Should create a link" +}