-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_semver
executable file
·175 lines (163 loc) · 5.73 KB
/
test_semver
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
echo Testing with bash version ${BASH_VERSION}
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
set -eu
PATH="${DIR}:${PATH}"
. shl_semver
SEP=$'\t'
test_vn_parse() {
local _parts
[[ "$#" -ne 5 ]] && {
echo wrong number of parameters "$#"
return
}
local _input="$1"
local _version="$2"
local _prerelease="$3"
local _build="$4"
local _expect="$5"
[[ ${_version} == "''" ]] && _version=""
[[ ${_prerelease} == "''" ]] && _prerelease=""
[[ ${_build} == "''" ]] && _build=""
set +e
local -a _parts
local _result _array
_array="$(vn_Parse "${_input}" "_parts")"
_result=$?
eval "${_array}"
# _array=$(vn_Parse "${_input}" "_parts")
# _result=$?
# eval "${_array}"
set -e
compare_parts() {
[[ ${#_parts[@]} -eq 3 && $# -eq 3 && "${_parts[0]}" == "$1" && "${_parts[1]}" == "$2" && "${_parts[2]}" == "$3" ]]
}
case "${_expect}" in
pass)
[[ "${_result}" -eq 0 ]] && compare_parts "${_version}" "${_prerelease}" "${_build}" &&
echo "Pass: '${_input}'" || echo "Fail: '$1 expect ${_expect}'"
;;
fail)
[[ "${_result}" -ne 0 ]] && echo "Pass: Negative '${_input}'" || echo "Fail: '$1 expect ${_expect}'"
;;
*)
echo Invaild expect value for test_vn_parse "${@}"
;;
esac
}
echo -e "\n\nvn_parse tests"
while IFS=$'\t' read -r _input _version _prerelease _build _expect _remaining
do
[[ -n "${_remaining}" ]] && {
echo test data error
continue
}
[[ "${_input}" =~ ^# ]] && continue
test_vn_parse "${_input}" "${_version}" "${_prerelease}" "${_build}" "${_expect}"
done << EOF
1.0.0-prerelease+build${SEP}1.0.0${SEP}prerelease${SEP}build${SEP}pass
1.0.0${SEP}1.0.0${SEP}''${SEP}''${SEP}pass
1.0.0+build${SEP}${SEP}${SEP}${SEP}1.0.0${SEP}''${SEP}${SEP}${SEP}build${SEP}pass
1.0.0-prerelease${SEP}${SEP}1.0.0${SEP}prerelease${SEP}''${SEP}${SEP}pass
9.10.10-a.b.c+....${SEP}${SEP}9.10.10${SEP}a.b.c${SEP}${SEP}....${SEP}pass
invalid${SEP}''${SEP}''${SEP}''${SEP}fail
-prelease${SEP}''${SEP}''${SEP}''${SEP}fail
invalid-prelease${SEP}''${SEP}''${SEP}''${SEP}fail
0.1.0invalid${SEP}''${SEP}''${SEP}''${SEP}fail
1.0.0-alpha${SEP}1.0.0${SEP}alpha${SEP}''${SEP}pass
1.0.0-alpha.1${SEP}1.0.0${SEP}alpha.1${SEP}''${SEP}pass
1.0.0-0.3.7${SEP}1.0.0${SEP}0.3.7${SEP}''${SEP}pass
1.0.0-alpha.beta${SEP}1.0.0${SEP}alpha.beta${SEP}''${SEP}pass
1.0.0-beta${SEP}1.0.0${SEP}beta${SEP}''${SEP}pass
1.0.0-beta.2${SEP}1.0.0${SEP}beta.2${SEP}''${SEP}pass
1.0.0-beta.11${SEP}1.0.0${SEP}beta.11${SEP}''${SEP}pass
1.0.0-rc.1${SEP}1.0.0${SEP}rc.1${SEP}''${SEP}pass
1.0.0-x.7.z.92${SEP}1.0.0${SEP}x.7.z.92${SEP}''${SEP}pass
...${SEP}...${SEP}''${SEP}''${SEP}pass
1.2.3.4.5-6-1+2 1.2.3.4.5 6-1 2 pass
3.4-a b c+d e f${SEP}3.4${SEP}a b c${SEP}d e f${SEP}pass
EOF
test_vn_Compare () {
local op
local sensitivity=""
[[ "$3" == "insensitive" ]] && {
sensitivity=" $3"
}
set +e
vn_Compare "$1" "$2" "$3"
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
254) op='~';;
255) op='!';;
esac
set -e
if [[ "$op" != "$4" ]]
then
if [[ $# == 4 ]]
then
echo "FAIL: Expected '$4', Actual '${op}${sensitivity}', Arg1 '$1', Arg2 '$2'"
else
echo "Pass: Negative '$1 ${op}${sensitivity} $2' Requested '$4'"
fi
else
echo "Pass: '$1 ${op}${sensitivity} $2'"
fi
}
# Run tests
# argument table format:
# testarg1 testarg2 expected_relationship
# echo "The following tests should pass"
echo -e "\n\nvn_Compare tests"
while IFS=$'\t' read -r _semver1 _semver2 _sensitivity _expect _remaining
do
[[ -n "${_remaining}" ]] && {
echo test data error
continue
}
[[ "${_semver1}" =~ ^# ]] && continue
test_vn_Compare "${_semver1}" "${_semver2}" "${_sensitivity}" "${_expect}"
done << EOF
1${SEP}1${SEP}sensitive${SEP}=
2.1${SEP}2.2${SEP}sensitive${SEP}<
3.0.4.10${SEP}3.0.4.2${SEP}sensitive${SEP}>
4.08${SEP}4.08.01${SEP}sensitive${SEP}<
3.2.1.9.8144${SEP}3.2${SEP}sensitive${SEP}>
3.2.1.9.8144${SEP}3.2${SEP}sensitive${SEP}>
3.2${SEP}3.2.1.9.8144${SEP}sensitive${SEP}<
1.2${SEP}2.1${SEP}sensitive${SEP}<
2.1${SEP}1.2${SEP}sensitive${SEP}>
5.6.7${SEP}5.6.7${SEP}sensitive${SEP}=
1.01.1${SEP}1.1.1${SEP}sensitive${SEP}=
1.1.1${SEP}1.01.1${SEP}sensitive${SEP}=
1${SEP}1.0${SEP}sensitive${SEP}=
1.0${SEP}1${SEP}sensitive${SEP}=
1.0.2.0${SEP}1.0.2${SEP}sensitive${SEP}=
1..0${SEP}1.0${SEP}sensitive${SEP}=
1.0${SEP}1..0${SEP}sensitive${SEP}=
1.0.1${SEP}1..1${SEP}sensitive${SEP}=
1${SEP}1${SEP}sensitive${SEP}=
a.b.c${SEP}1.1.1${SEP}sensitive${SEP}~
a${SEP}b${SEP}sensitive${SEP}~
1.1.1${SEP}a.b.c${SEP}sensitive${SEP}~
1.0.0-alpha${SEP}1.0.0-alpha.1${SEP}sensitive${SEP}<
1.0.0-alpha.1${SEP}1.0.0-alpha.beta${SEP}sensitive${SEP}<
1.0.0-alpha.beta${SEP}1.0.0-beta${SEP}sensitive${SEP}<
1.0.0-beta${SEP}1.0.0-beta.2${SEP}sensitive${SEP}<
1.0.0-beta.2${SEP}1.0.0-beta.11${SEP}sensitive${SEP}<
1.0.0-beta.11${SEP}1.0.0-rc.1${SEP}sensitive${SEP}<
1.0.0-beta.11${SEP}1.0.0${SEP}sensitive${SEP}<
1.0.0-beta.9${SEP}1.0.0-beta.12${SEP}sensitive${SEP}<
1.0.0-beta.12${SEP}1.0.0-beta.12.14${SEP}sensitive${SEP}<
1.0.0-beta.11.12${SEP}1.0.0${SEP}sensitive${SEP}<
1.0.0-alpha.1${SEP}1.0.0-alpha${SEP}sensitive${SEP}>
1.0.0-beta${SEP}1.0.0-alpha.beta${SEP}sensitive${SEP}>
1.0.0${SEP}1.0.0-beta.11${SEP}sensitive${SEP}>
1.0.0-beta.12.14${SEP}1.0.0-beta.12${SEP}sensitive${SEP}>
1.0.0${SEP}1.0.0+build.11${SEP}sensitive${SEP}=
3.4-a b. c+d \$e f${SEP}3.4-a b. b+d \$e f${SEP}sensitive${SEP}>
1.0.0-beta.2${SEP}1.0.0-Beta.11${SEP}sensitive${SEP}>
1.0.0-beta.2${SEP}1.0.0-Beta.11${SEP}insensitive${SEP}<
1.0.0-beta${SEP}1.0.0-Beta${SEP}insensitive${SEP}=
EOF