diff --git a/tests.sh b/tests.sh index f3d2eb9..8e35078 100755 --- a/tests.sh +++ b/tests.sh @@ -3,54 +3,58 @@ # Note: using bash for more power in testing (the final user doesn't need it). # Global Variables -G_NANO_VERSION="4.6.0" -G_SHELLCHECK_VERSION="0.6.0" +readonly G_NANO_VERSION="4.6.0" +readonly G_SHELLCHECK_VERSION="0.6.0" # Functions # Compare Version # Compare the first version (x.x.x format) against second one. -# Returns true if $1 => $2, false otherwise. +# Returns 0 if $1 => $2, 1 otherwise. +# In error returns 2. # Sources: # https://unix.stackexchange.com/questions/285924/how-to-compare-a-programs-version-in-a-shell-script # https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash # Test table: -# req | get | test | res -# ------+-------+-------+------- -# 2.0.0 | 1.0.0 | 1.0.0 | true -# 1.0.0 | 2.0.0 | 1.0.0 | false -# 0.5.3 | 0.5.3 | 0.5.3 | true +# req | get | test | res +# -------+-------+-------+----------- +# 2.0.0 | 1.0.0 | 1.0.0 | true = 0 +# 1.0.0 | 2.0.0 | 1.0.0 | false = 1 +# 0.5.3 | 0.5.3 | 0.5.3 | true = 0 f_compare_version(){ local required_v="$1" local getted_v="$2" # First: check if equal if [ "$required_v" = "$getted_v" ]; then - return true; + return 0; fi # Second: check if greater or lesser - local test_v=$(printf "%s\n%s" "$greater_v" "$lesser_v" | sort -V | head -n 1) + local test_v + $test_v=$(printf "%s\n%s" "$required_v" "$getted_v" | sort -V | head -n 1) case $test_v in - $getted_v) return true ;; - $required_v) return false ;; - *) return false ;; + $getted_v) return 0 ;; + $required_v) return 1 ;; + *) return 2 ;; esac } # Test Functions f_test_nano_version() { - local version="nano --version | cut -d ' ' -f 4" - return f_compare_version $G_NANO_VERSION $version + local version + $version=$(nano --version | cut -d ' ' -f 4) + return $(f_compare_version $G_NANO_VERSION $version) } f_test_shellcheck_version() { - local version="nano --version | cut -d ' ' -f 8" - return f_compare_version $G_SHELLCHECK_VERSION $version + local version + $version=$(nano --version | cut -d ' ' -f 8) + return $(f_compare_version $G_SHELLCHECK_VERSION $version) } f_test_nano_version f_test_shellcheck_version # ....shellcheck -f diff *.sh | git apply | git commit -a -m "Shellcheck fast corrections" -shellcheck *.sh +shellcheck -- *.sh