Skip to content

Commit 2d02d29

Browse files
Jenkinsopenstack-gerrit
Jenkins
authored andcommitted
Merge "Add vercmp function"
2 parents 8c71ffa + 2ba36cd commit 2d02d29

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

functions

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,58 @@ function vercmp_numbers {
529529
typeset v1=$1 v2=$2 sep
530530
typeset -a ver1 ver2
531531

532+
deprecated "vercmp_numbers is deprecated for more generic vercmp"
533+
532534
IFS=. read -ra ver1 <<< "$v1"
533535
IFS=. read -ra ver2 <<< "$v2"
534536

535537
_vercmp_r "${#ver1[@]}" "${ver1[@]}" "${ver2[@]}"
536538
}
537539

540+
# vercmp ver1 op ver2
541+
# Compare VER1 to VER2
542+
# - op is one of < <= == >= >
543+
# - returns true if satisified
544+
# e.g.
545+
# if vercmp 1.0 "<" 2.0; then
546+
# ...
547+
# fi
548+
function vercmp {
549+
local v1=$1
550+
local op=$2
551+
local v2=$3
552+
local result
553+
554+
# sort the two numbers with sort's "-V" argument. Based on if v2
555+
# swapped places with v1, we can determine ordering.
556+
result=$(echo -e "$v1\n$v2" | sort -V | head -1)
557+
558+
case $op in
559+
"==")
560+
[ "$v1" = "$v2" ]
561+
return
562+
;;
563+
">")
564+
[ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
565+
return
566+
;;
567+
"<")
568+
[ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
569+
return
570+
;;
571+
">=")
572+
[ "$result" = "$v2" ]
573+
return
574+
;;
575+
"<=")
576+
[ "$result" = "$v1" ]
577+
return
578+
;;
579+
*)
580+
die $LINENO "unrecognised op: $op"
581+
;;
582+
esac
583+
}
538584

539585
# This function sets log formatting options for colorizing log
540586
# output to stdout. It is meant to be called by lib modules.

tests/test_vercmp.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
# Tests for DevStack vercmp functionality
4+
5+
TOP=$(cd $(dirname "$0")/.. && pwd)
6+
7+
# Import common functions
8+
source $TOP/functions
9+
source $TOP/tests/unittest.sh
10+
11+
assert_true "numeric gt" vercmp 2.0 ">" 1.0
12+
assert_true "numeric gte" vercmp 2.0 ">=" 1.0
13+
assert_true "numeric gt" vercmp 1.0.1 ">" 1.0
14+
assert_true "numeric gte" vercmp 1.0.1 ">=" 1.0
15+
assert_true "alpha gt" vercmp 1.0.1b ">" 1.0.1a
16+
assert_true "alpha gte" vercmp 1.0.1b ">=" 1.0.1a
17+
assert_true "alpha gt" vercmp b ">" a
18+
assert_true "alpha gte" vercmp b ">=" a
19+
assert_true "alpha gt" vercmp 2.0-rc3 ">" 2.0-rc1
20+
assert_true "alpha gte" vercmp 2.0-rc3 ">=" 2.0-rc1
21+
22+
assert_false "numeric gt fail" vercmp 1.0 ">" 1.0
23+
assert_true "numeric gte" vercmp 1.0 ">=" 1.0
24+
assert_false "numeric gt fail" vercmp 0.9 ">" 1.0
25+
assert_false "numeric gte fail" vercmp 0.9 ">=" 1.0
26+
assert_false "numeric gt fail" vercmp 0.9.9 ">" 1.0
27+
assert_false "numeric gte fail" vercmp 0.9.9 ">=" 1.0
28+
assert_false "numeric gt fail" vercmp 0.9a.9 ">" 1.0.1
29+
assert_false "numeric gte fail" vercmp 0.9a.9 ">=" 1.0.1
30+
31+
assert_false "numeric lt" vercmp 1.0 "<" 1.0
32+
assert_true "numeric lte" vercmp 1.0 "<=" 1.0
33+
assert_true "numeric lt" vercmp 1.0 "<" 1.0.1
34+
assert_true "numeric lte" vercmp 1.0 "<=" 1.0.1
35+
assert_true "alpha lt" vercmp 1.0.1a "<" 1.0.1b
36+
assert_true "alpha lte" vercmp 1.0.1a "<=" 1.0.1b
37+
assert_true "alpha lt" vercmp a "<" b
38+
assert_true "alpha lte" vercmp a "<=" b
39+
assert_true "alpha lt" vercmp 2.0-rc1 "<" 2.0-rc3
40+
assert_true "alpha lte" vercmp 2.0-rc1 "<=" 2.0-rc3
41+
42+
assert_true "eq" vercmp 1.0 "==" 1.0
43+
assert_true "eq" vercmp 1.0.1 "==" 1.0.1
44+
assert_false "eq fail" vercmp 1.0.1 "==" 1.0.2
45+
assert_false "eq fail" vercmp 2.0-rc1 "==" 2.0-rc2
46+
47+
report_results

tests/unittest.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,51 @@ function assert_empty {
9292
fi
9393
}
9494

95+
# assert the arguments evaluate to true
96+
# assert_true "message" arg1 arg2
97+
function assert_true {
98+
local lineno
99+
lineno=`caller 0 | awk '{print $1}'`
100+
local function
101+
function=`caller 0 | awk '{print $2}'`
102+
local msg=$1
103+
shift
104+
105+
$@
106+
if [ $? -eq 0 ]; then
107+
PASS=$((PASS+1))
108+
echo "PASS: $function:L$lineno - $msg"
109+
else
110+
FAILED_FUNCS+="$function:L$lineno\n"
111+
echo "ERROR: test failed in $function:L$lineno!"
112+
echo " $msg"
113+
ERROR=$((ERROR+1))
114+
fi
115+
}
116+
117+
# assert the arguments evaluate to false
118+
# assert_false "message" arg1 arg2
119+
function assert_false {
120+
local lineno
121+
lineno=`caller 0 | awk '{print $1}'`
122+
local function
123+
function=`caller 0 | awk '{print $2}'`
124+
local msg=$1
125+
shift
126+
127+
$@
128+
if [ $? -eq 0 ]; then
129+
FAILED_FUNCS+="$function:L$lineno\n"
130+
echo "ERROR: test failed in $function:L$lineno!"
131+
echo " $msg"
132+
ERROR=$((ERROR+1))
133+
else
134+
PASS=$((PASS+1))
135+
echo "PASS: $function:L$lineno - $msg"
136+
fi
137+
}
138+
139+
95140
# Print a summary of passing and failing tests and exit
96141
# (with an error if we have failed tests)
97142
# usage: report_results

0 commit comments

Comments
 (0)