Test program versions

This commit is contained in:
Tiago Programmer 2019-12-19 21:00:38 +00:00 committed by GitHub
parent c40ab81504
commit f064485bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,53 +2,53 @@
# Shellcheck the script
# Note: using bash for more power in testing (the final user doesn't need it).
# Global Variables
G_NANO_VERSION=""
G_SHELLCHECK_VERSION=""
# Functions
# Test Version
# Receives two parameters in x.x.x format.
# Returns true or false
# Compare Version
# Compare the first version (x.x.x format) against second one.
# Returns true if $1 => $2, false otherwise.
# 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_version (){
currentver="$(gcc -dumpversion)"
requiredver="5.0.0"
if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
echo "Greater than or equal to 5.0.0"
else
echo "Less than 5.0.0"
fi
###################
verlte() {
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
# 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
f_compare_version(){
local required_v="$1"
local getted_v="$2"
# First: check if equal
if [ "$required_v" = "$getted_v" ]; then
return true;
fi
# Second: check if greater or lesser
local test_v=$(printf "%s\n%s" "$greater_v" "$lesser_v" | sort -V | head -n 1)
case $test_v in
$getted_v) return true;
$required_v) return false;
*) return false;
esac
}
verlt() {
[ "$1" = "$2" ] && return 1 || verlte $1 $2
# Test Functions
f_test_nano_version() {
local version="nano --version | cut -d ' ' -f 4"
return f_compare_version $G_NANO_VERSION $version
}
##################
f_test_nano_version() {
local version="nano --version | cut -d ' ' -f 8"
return f_compare_version $G_SHELLCHECK_VERSION $version
}
# Tests
get_nano_version () {
local tested_version=""
# x.x.x
local actual_version="nano --version | cut -d ' ' -f 4"
}
nano --version
shellcheck --version
# ....shellcheck -f diff *.sh | git apply | git commit -a -m "Shellcheck fast corrections"
shellcheck *.sh