nanorc/tests.sh

66 lines
1.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2019-11-23 22:31:28 +00:00
# Shellcheck the script
# Note: using bash for more power in testing (the final user doesn't need it).
2019-12-19 21:00:38 +00:00
# Global Variables
2019-12-20 18:47:45 +00:00
readonly G_NANO_VERSION="4.6.0"
readonly G_SHELLCHECK_VERSION="0.6.0"
2019-12-19 21:00:38 +00:00
# Functions
2019-12-19 21:00:38 +00:00
# Compare Version
# Compare the first version (x.x.x format) against second one.
2019-12-20 18:47:45 +00:00
# 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
2019-12-19 21:00:38 +00:00
# Test table:
2019-12-20 18:47:45 +00:00
# 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
2019-12-19 21:00:38 +00:00
f_compare_version(){
local required_v="$1"
local getted_v="$2"
# First: check if equal
if [ "$required_v" = "$getted_v" ]; then
2019-12-20 18:47:45 +00:00
return 0;
2019-12-19 21:00:38 +00:00
fi
# Second: check if greater or lesser
2019-12-20 18:47:45 +00:00
local test_v
2019-12-20 18:52:36 +00:00
test_v="$(printf "%s\n%s" "$required_v" "$getted_v" | sort -V | head -n 1)"
2019-12-19 21:00:38 +00:00
case $test_v in
2019-12-20 18:47:45 +00:00
$getted_v) return 0 ;;
$required_v) return 1 ;;
*) return 2 ;;
2019-12-19 21:00:38 +00:00
esac
}
2019-12-19 21:00:38 +00:00
# Test Functions
2019-12-19 21:00:38 +00:00
f_test_nano_version() {
2019-12-20 18:47:45 +00:00
local version
2019-12-20 18:52:36 +00:00
version="$(nano --version | cut -d ' ' -f 4)"
2019-12-20 20:14:36 +00:00
f_compare_version "$G_NANO_VERSION" "$version"
2019-12-20 19:41:30 +00:00
return $?
}
2019-12-19 21:04:33 +00:00
f_test_shellcheck_version() {
2019-12-20 18:47:45 +00:00
local version
2019-12-20 18:52:36 +00:00
version="$(nano --version | cut -d ' ' -f 8)"
2019-12-20 19:41:30 +00:00
f_compare_version "$G_SHELLCHECK_VERSION" "$version"
return $?
}
2019-11-23 22:31:28 +00:00
2019-12-20 20:08:05 +00:00
printf "=================\n"
2019-12-20 19:10:39 +00:00
printf "TESTS\n"
2019-12-20 20:08:05 +00:00
printf "=================\n"
2019-12-20 19:10:39 +00:00
printf "Nano Version ok? %s\n" "$(f_test_nano_version)"
printf "Shellcheck Version ok? %s\n" "$(f_test_shellcheck_version)"
# ....shellcheck -f diff *.sh | git apply | git commit -a -m "Shellcheck fast corrections"
2019-12-20 18:47:45 +00:00
shellcheck -- *.sh