-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_010_version.sh
executable file
·99 lines (84 loc) · 3.6 KB
/
test_010_version.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
# Meant to be run from maketests.sh. See its exported variables.
# Tests BME_VERSION format and value
function main() {
source bash-magic-enviro || exit $?
check_version_format || exit $?
# check_test_version_function || exit $?
}
#--
# EVALUATES CURRENT VERSION's FORMAT
#--
function check_version_format() {
test_title "assert version variable"
if ! [[ -n ${BME_VERSION} ]]; then
test_log "${C_BOLD}'BME_VERSION'${C_NC} undefined!" error
return 1
else
test_log "${C_BOLD}'BME_VERSION'${C_NC} is defined: ${T_BOLD}'${BME_VERSION}'${T_NC}" ok
fi
# Strips version string into its (dot-separated) components: vNN.NN.NN[+|-optional]
# $BASH_REMATCH structure:
# ${BASH_REMATCH[0]} contains the complete match of the regular expression
# ${BASH_REMATCH[1]} contains the match of the 1st () capture group
# ${BASH_REMATCH[2]} contains the match of the 2nd () capture group, and so on.
test_title "check version pattern"
if [[ ${BME_VERSION} =~ ^(v[[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)(.*)?$ ]]; then
test_log "BME version ${C_BOLD}'${BME_VERSION}'${C_NC} follows the expected basic ${C_BOLD}'vNN.NN.NN[optional]'${C_NC} pattern." ok
declare -A version_components
version_components['major']=${BASH_REMATCH[1]}
version_components['minor']=${BASH_REMATCH[2]}
version_components['patch']=${BASH_REMATCH[3]}
version_components['optional']=${BASH_REMATCH[4]}
else
test_log "BME version ${C_BOLD}'${BME_VERSION}'${C_NC} doesn't match expected pattern ${C_BOLD}'vNN.NN.NN[optional]'${C_NC}." fail
return 1
fi
# As per Semantic version standard, "patch" level (defined as an integer), may be followed by a pre-release (-[something]) or build metadata (+[something]) suffix
test_title "check post-extraversion pattern"
if [[ -n ${version_components['optional']} ]]; then
if ! [[ ${version_components['optional']} =~ ^[-\+]([[:alnum:]]|\.|\-)+$ ]]; then
err_msg="Optional patch extension ${C_BOLD}'${version_components['optional']}'${C_NC} doesn't match expected pattern.\n"
err_msg+="\tFull version: ${C_BOLD}'${BME_VERSION}'${C_NC}."
test_log "${err_msg}" fail
return 1
else
test_log "Optional patch extension ${C_BOLD}'${version_components['optional']}'${C_NC} matches de expected pattern." ok
fi
else
test_log "Version '${C_BOLD}'${BME_VERSION}'${C_NC}' doesn't have optional patch extension." info
fi
test_log "Check ${C_BOLD}'BME version formatting'${C_NC}: ${C_GREEN}OK${C_NC}" info
}
#--
# EVALUATES bme_check_version()
#--
check_test_version_function() {
# Testing old version
BME_VERSION='v0.0.1'
local function_output=$(bme_check_version)
local stripped_output=$(strip_escape_codes "${function_output}")
if [[ "${stripped_output}" =~ .*"consider upgrading".* ]]; then
bme_log "Version ${C_BOLD}'${BME_VERSION}'${C_NC} is older than current." ok 1
else
bme_log "Version ${C_BOLD}'${BME_VERSION}'${C_NC} is older than current." fail 1
bme_log "${C_BOLD}bme_check_version() output follows:${C_NC}"
bme_log "${function_output}" '' 1
bme_log "${C_BOLD}end of bme_check_version() output.${C_NC}"
return 1
fi
# Testing unknown version
BME_VERSION='vasdf'
local function_output=$(bme_check_version)
local stripped_output=$(strip_escape_codes "${function_output}")
if [[ "${stripped_output}" =~ .*"version couldn't be found at your remote".* ]]; then
bme_log "Check ${C_BOLD}'BME unknown version'${C_NC}." ok 1
else
bme_log "Check ${C_BOLD}'BME older version'${C_NC}." fail 1
bme_log "${C_BOLD}bme_check_version() output follows:${C_NC}"
bme_log "${function_output}" '' 1
bme_log "${C_BOLD}end of bme_check_version() output.${C_NC}"
return 1
fi
}
main; exit $?