diff --git a/install.sh b/install.sh index 65c457f8..7582c4ee 100755 --- a/install.sh +++ b/install.sh @@ -2,10 +2,24 @@ # shellcheck disable=SC2155 # shellcheck disable=SC2164 +function is_git_installed() { + command -v git > /dev/null 2>&1 +} + +function get_latest_tag() { + local repository_url=$1 + + git ls-remote --tags "$repository_url" | + awk '{print $2}' | + sed 's|^refs/tags/||' | + sort -Vr | + head -n 1 +} + function build_and_install_beta() { echo "> Downloading non-stable version: 'beta'" - if ! command -v git >/dev/null 2>&1; then + if ! is_git_installed; then echo "Error: git is not installed." >&2 exit 1 fi @@ -20,8 +34,7 @@ function build_and_install_beta() { local beta_version=$(printf "(non-stable) beta after %s [%s] 🐍 #%s" \ "$LATEST_BASHUNIT_VERSION" \ "$(date +'%Y-%m-%d')" \ - "$latest_commit" - ) + "$latest_commit") sed -i -e 's/BASHUNIT_VERSION=".*"/BASHUNIT_VERSION="'"$beta_version"'"/g' temp_bashunit/bin/bashunit cp temp_bashunit/bin/bashunit ./ @@ -53,8 +66,12 @@ function install() { DIR=${1-lib} VERSION=${2-latest} -LATEST_BASHUNIT_VERSION="0.17.0" BASHUNIT_GIT_REPO="https://github.com/TypedDevs/bashunit" +if is_git_installed; then + LATEST_BASHUNIT_VERSION="$(get_latest_tag "$BASHUNIT_GIT_REPO")" +else + LATEST_BASHUNIT_VERSION="0.17.0" +fi TAG="$LATEST_BASHUNIT_VERSION" cd "$(dirname "$0")" diff --git a/tests/acceptance/install_test.sh b/tests/acceptance/install_test.sh index 25a1132b..c5f51449 100644 --- a/tests/acceptance/install_test.sh +++ b/tests/acceptance/install_test.sh @@ -5,6 +5,11 @@ function set_up_before_script() { TEST_ENV_FILE="./tests/acceptance/fixtures/.env.default" } +function set_up() { + rm -f ./lib/bashunit + rm -f ./deps/bashunit +} + function tear_down() { rm -f ./lib/bashunit rm -f ./deps/bashunit @@ -19,6 +24,7 @@ function test_install_downloads_the_latest_version() { assert_string_starts_with "$(printf "> Downloading the latest version: '")" "$output" assert_string_ends_with "$(printf "\n> bashunit has been installed in the 'lib' folder")" "$output" assert_file_exists "$installed_bashunit" + assert_string_starts_with\ "$(printf "\e[1m\e[32mbashunit\e[0m - ")"\ "$("$installed_bashunit" --env "$TEST_ENV_FILE" --version)" @@ -33,6 +39,7 @@ function test_install_downloads_in_given_folder() { assert_string_starts_with "$(printf "> Downloading the latest version: '")" "$output" assert_string_ends_with "$(printf "\n> bashunit has been installed in the 'deps' folder")" "$output" assert_file_exists "$installed_bashunit" + assert_string_starts_with\ "$(printf "\e[1m\e[32mbashunit\e[0m - ")"\ "$("$installed_bashunit" --env "$TEST_ENV_FILE" --version)" @@ -47,6 +54,7 @@ function test_install_downloads_the_given_version() { assert_same\ "$(printf "> Downloading a concrete version: '0.9.0'\n> bashunit has been installed in the 'lib' folder")"\ "$output" + assert_file_exists "$installed_bashunit" assert_same\ @@ -65,11 +73,15 @@ function test_install_downloads_the_non_stable_beta_version() { assert_contains\ "$(printf "> Downloading non-stable version: 'beta'\n> bashunit has been installed in the 'deps' folder")"\ "$output" + assert_file_exists "$installed_bashunit" + assert_matches\ "$(printf "\(non-stable\) beta after ([0-9]+\.[0-9]+\.[0-9]+) \[2023-11-13\] 🐍 \#[a-fA-F0-9]{7}")"\ "$("$installed_bashunit" --env "$TEST_ENV_FILE" --version)" + assert_directory_not_exists "./deps/temp_bashunit" + file_count_of_deps_directory=$(find ./deps -mindepth 1 -maxdepth 1 -print | wc -l | tr -d ' ') assert_same "$file_count_of_deps_directory" "1" assert_same "$(find ./deps -name 'bashunit')" "./deps/bashunit"