test.sh 12.8 KB
Newer Older
1
#!/bin/bash -e
2 3 4 5 6 7 8 9 10
# Copyright 2017 Emeric Verschuur <emeric@mbedsys.org>
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
11
#
12
#   http://www.apache.org/licenses/LICENSE-2.0
13
#
14 15 16 17 18 19
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
Emeric Verschuur's avatar
Emeric Verschuur committed
20

21
if [ "$GITLAB_CI" != "true" ]; then
22
    exec docker run --rm -i -e GITLAB_CI=true -v $PWD:$PWD -w $PWD ubuntu:16.04 ./test.sh "$@"
23 24
fi

25 26
. bashopts.sh
trap 'bashopts_exit_handle' ERR
Emeric Verschuur's avatar
Emeric Verschuur committed
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41
req_test_eq() {
    echo -n "\"$1\"" == "\"$2\""
    if [ "$1" == "$2" ]; then
        echo " [OK]"
        return 0
    fi
    echo " [KO]"
    exit 1
}

_test_case_1() {
    local dest1="This is a simple desc"
    local dest2="This is a desc with a '\"' and a \$test"

42 43
    bashopts_declare -n foo -l foo -o t -d "$dest1"
    bashopts_declare -n bar -l bar -o u -d "$dest2"
44

Emeric Verschuur's avatar
Emeric Verschuur committed
45 46
    req_test_eq "${bashopts_optprop_description[foo]}" "$dest1"
    req_test_eq "${bashopts_optprop_description[bar]}" "$dest2"
47 48 49
}

_test_case_2() {
50
    rm -f /tmp/bashopts_testrc
51

52
    bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
53

54
    bashopts_declare -n base_path -l base -o b -d "Base path" -t string -v "/opt/test" -i -s
55
    bashopts_declare -n config_path -l config -o c -d "Configuration path" -t string -e "\$base_path/myconf.cfg" -s
56

57
    bashopts_parse_args --base /tmp -n
58
    bashopts_process_opts
59

60
    req_test_eq "$config_path" "/tmp/myconf.cfg"
61
    req_test_eq "$(cat /tmp/bashopts_testrc)" "base_path=/tmp"
62 63
}

64
_test_case_3() {
65
    rm -f /tmp/bashopts_testrc
66

67
    bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
68
    bashopts_declare -n process_file_list -l input-file -o f -d "Input file" -t string -m add -s
69

Emeric Verschuur's avatar
Emeric Verschuur committed
70
    bashopts_parse_args --input-file foobar --input-file=/foo/bar
71
    bashopts_process_opts
72

Emeric Verschuur's avatar
Emeric Verschuur committed
73
    req_test_eq "$(bashopts_join_by , ${process_file_list[@]})" "foobar,/foo/bar"
74
    eval declare -x test="$(grep -E "^process_file_list=" /tmp/bashopts_testrc | sed -E "s/^[^=]+=//g")"
Emeric Verschuur's avatar
Emeric Verschuur committed
75
    req_test_eq "$(bashopts_join_by , ${test[@]})" "foobar,/foo/bar"
76 77
}

78
_test_case_4() {
79
    rm -f /tmp/bashopts_testrc
80

81
    bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
82

83
    bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
84

85
    bashopts_parse_args --value "value1" --value "value 2" --value "value '\"' \$3"
86
    bashopts_process_opts
87

88 89 90
    req_test_eq "${array_value[0]}" "value1"
    req_test_eq "${array_value[1]}" "value 2"
    req_test_eq "${array_value[2]}" "value '\"' \$3"
91
    req_test_eq "$(cat /tmp/bashopts_testrc)" "array_value=([0]=\"value1\" [1]=\"value 2\" [2]=\"value '\\\"' \\\$3\")"
92 93 94
    req_test_eq "$(bashopts_diplay_help | grep -E -o "^\\s+\-\-value \[\"value1\", \"value 2\", \"value '\\\\\"' \\\$3\"]\s+")" "    --value [\"value1\", \"value 2\", \"value '\\\"' \$3\"]        "
}

Emeric Verschuur's avatar
Emeric Verschuur committed
95
_test_case_5() {
96
    rm -f /tmp/bashopts_testrc
97

98
    bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
99

Emeric Verschuur's avatar
Emeric Verschuur committed
100
    bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
101

Emeric Verschuur's avatar
Emeric Verschuur committed
102
    bashopts_parse_args
103
    bashopts_process_opts
104

Emeric Verschuur's avatar
Emeric Verschuur committed
105 106 107
    req_test_eq "${#array_value[@]}" "0"
}

108
_test_case_6() {
109
    bashopts_setup -n "$0" -d "Test case $0"
110 111 112 113

    bashopts_declare -n computed_value -e '$(( 2 + 3 * 4 ))' -t string

    bashopts_parse_args
114
    bashopts_process_opts
115 116 117 118

    req_test_eq "$computed_value" "14"
}

119 120 121 122 123 124
_test_case_7() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n array_value -l value -m add -d "Array value" -t string

    bashopts_parse_args --value "value 1" --value "value 1" --value "" --value "value 3" --value "value 4"
125
    bashopts_process_opts
126 127 128 129 130 131 132 133 134 135 136 137

    req_test_eq "${#array_value[@]}" "2"
    req_test_eq "${array_value[0]}" "value 3"
    req_test_eq "${array_value[1]}" "value 4"
}

_test_case_8() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n array_value -l value -m add -d "Array value" -t string -e '("value 1" "value 2")'

    bashopts_parse_args
138
    bashopts_process_opts
139 140 141 142 143 144

    req_test_eq "${#array_value[@]}" "2"
    req_test_eq "${array_value[0]}" "value 1"
    req_test_eq "${array_value[1]}" "value 2"
}

145 146 147 148
_test_case_9() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_parse_args command1 "command 2"
149
    bashopts_process_opts
150 151 152 153 154 155 156 157 158 159 160 161

    req_test_eq "${#bashopts_commands[@]}" "2"
    req_test_eq "${bashopts_commands[0]}" "command1"
    req_test_eq "${bashopts_commands[1]}" "command 2"
}

_test_case_10() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'

    bashopts_parse_args --value=test
162
    req_test_eq "$(bashopts_process_opts 2>&1)" "[WARNING] No settings file specified"
163 164 165
}

_test_case_11() {
166
    bashopts_setup -n "$0" -d "Test case $0" -s /dev/null/invalid
167 168 169 170

    bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'

    bashopts_parse_args --value=test
171
    req_test_eq "$(bashopts_process_opts 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file"
172 173
}

174 175 176 177 178 179
_test_case_12() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n array_value -l value -m add -d "Array value" -t string -i

    bashopts_parse_args
180
    bashopts_process_opts > /dev/null <<< "( \"value 1\" \"value 2\" )"
181 182 183 184 185 186 187 188 189 190 191 192

    req_test_eq "${#array_value[@]}" "2"
    req_test_eq "${array_value[0]}" "value 1"
    req_test_eq "${array_value[1]}" "value 2"
}

_test_case_13() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n array_value -l value -m add -d "Array value" -t number -i

    bashopts_parse_args
193
    bashopts_process_opts > /dev/null <<< "( 3 -8 )"
194 195 196 197 198 199 200 201 202 203 204 205

    req_test_eq "${#array_value[@]}" "2"
    req_test_eq "${array_value[0]}" "3"
    req_test_eq "${array_value[1]}" "-8"
}

_test_case_14() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n array_value -l value -m add -d "Array value" -t boolean -i

    bashopts_parse_args
206
    bashopts_process_opts > /dev/null <<< "( true false 1 True TRUE t T 0 False FALSE f F )"
207

208
    req_test_eq "${#array_value[@]}" "12"
209 210
    req_test_eq "${array_value[0]}" "true"
    req_test_eq "${array_value[1]}" "false"
211 212 213 214 215 216 217 218 219 220
    req_test_eq "${array_value[2]}" "true"
    req_test_eq "${array_value[3]}" "true"
    req_test_eq "${array_value[4]}" "true"
    req_test_eq "${array_value[5]}" "true"
    req_test_eq "${array_value[6]}" "true"
    req_test_eq "${array_value[7]}" "false"
    req_test_eq "${array_value[8]}" "false"
    req_test_eq "${array_value[9]}" "false"
    req_test_eq "${array_value[10]}" "false"
    req_test_eq "${array_value[11]}" "false"
221 222
}

Emeric Verschuur's avatar
Emeric Verschuur committed
223 224 225 226 227 228 229
_test_case_15() {
    rm -f /tmp/bashopts_testrc
    bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc

    bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -e '("value 1" "value 2")'

    bashopts_parse_args --value ""
230
    bashopts_process_opts > /dev/null <<< '( "value 1" "value 3" )'
Emeric Verschuur's avatar
Emeric Verschuur committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

    req_test_eq "${#array_value[@]}" "2"
    req_test_eq "${array_value[0]}" "value 1"
    req_test_eq "${array_value[1]}" "value 3"
    if [ ! -f /tmp/bashopts_testrc ]; then
        echo "file /tmp/bashopts_testrc should be present"
        exit 1
    fi
    req_test_eq "$(cat /tmp/bashopts_testrc)" 'array_value=([0]="value 1" [1]="value 3")'
}

_test_case_16() {
    rm -f /tmp/bashopts_testrc
    bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc

    bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -e '("value 1" "value 2")'

    bashopts_parse_args --value ""
249
    bashopts_process_opts > /dev/null <<< '( "value 1" "value 2" )'
Emeric Verschuur's avatar
Emeric Verschuur committed
250 251 252 253 254 255 256 257 258 259

    req_test_eq "${#array_value[@]}" "2"
    req_test_eq "${array_value[0]}" "value 1"
    req_test_eq "${array_value[1]}" "value 2"
    if [ -f /tmp/bashopts_testrc ]; then
        echo "file /tmp/bashopts_testrc should not be present"
        exit 1
    fi
}

260 261 262 263 264 265 266
_test_case_17() {
    rm -f /tmp/bashopts_testrc
    bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc -p

    bashopts_declare -n a_value -l value -d "A value" -s -i -t string -v "default value"

    bashopts_parse_args
267
    bashopts_process_opts > /dev/null <<< ""
268 269 270 271 272

    req_test_eq "$a_value" "default value"
    req_test_eq "$(cat /tmp/bashopts_testrc)" "a_value='default value'"
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
_test_case_18() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n a_boolean_value -l boolean -m set -d "A boolean value" -t boolean -i

    bashopts_parse_args
    req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru")" "[ERROR] 'tru' is not a valid boolean value"
    req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "fa")" "[ERROR] 'fa' is not a valid boolean value"
    req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "5")" "[ERROR] '5' is not a valid boolean value"
}

_test_case_19() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n a_boolean_value -o b -m set -d "A boolean value" -t boolean

    bashopts_parse_args -b
    bashopts_process_opts
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    req_test_eq "$a_boolean_value" "true"
}

is_eq_to_null() {
    if [ "$1" == "NULL" ];then
        echo "$1"
        return 0
    fi
    bashopts_error "Not equal to NULL"
    return 1
}

_test_case_20() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -m add -d "A str value" -t string

    bashopts_parse_args
    bashopts_process_opts
    req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "na")" \
        | grep '\[ERROR\] Not equal to NULL' \
        | head -n 1 || true)" '[ERROR] Not equal to NULL'
    req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "()")" \
        | grep '\[ERROR\] At least one value required' \
        | head -n 1 || true)" '[ERROR] At least one value required'
    bashopts_process_option -n value -k is_eq_to_null -r > /dev/null 2>&1 <<< "NULL"
}

_test_case_21() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -m add -d "A str value" -t string

    bashopts_parse_args -n
    bashopts_process_opts
    req_test_eq "$(echo "$(bashopts_process_option -n value -k is_not_empty -r 2>&1 > /dev/null)" \
        | grep '\[ERROR\] At least one value required' \
        | head -n 1)" '[ERROR] At least one value required'
}

_test_case_22() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -m add -d "A str value" -t string -i

    bashopts_parse_args --value "NULL" -n
    bashopts_process_opts
    bashopts_process_option -n value -k is_eq_to_null -r
}

342 343 344 345 346 347 348 349 350
_test_case_23() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -m add -d "A str value" -t string -k is_eq_to_null

    bashopts_parse_args
    bashopts_process_opts
}

351 352 353 354 355 356 357 358 359 360 361 362
_test_case_24() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -d "A str value" -t string

    bashopts_parse_args
    bashopts_process_opts
    # NOTE: Fix answer again after a is_eq_to_null check failure
    req_test_eq "$( (bashopts_process_option -n value -k is_eq_to_null -r 2>&1 <<< "NUL" \
        || true) | grep -E '^* A str value$' | wc -l )" "2"
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
_test_case_25() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -d "A str value" -t string -r

    req_test_eq "$( (bashopts_process_opts 2>&1 <<< "" \
        || true) | grep -E '^* A str value$' | wc -l )" "2"
}

_test_case_26() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -d "A str value" -t string -r -k is_eq_to_null

    req_test_eq "$( (bashopts_process_opts 2>&1 <<< "

NUL
NULL
")  | sed 's/At least one value required/###CASE1###/g' \
        | sed 's/Not equal to NULL/###CASE2###/g' \
        | grep -o -E '###CASE(1|2)###' | tr '\n' ' ' )" \
        "###CASE1### ###CASE1### ###CASE1### ###CASE2### "
}

_test_case_27() {
    bashopts_setup -n "$0" -d "Test case $0"

    req_test_eq "$(bashopts_declare -n value -l value -d "A str value" -t string -r -v "test" 2>&1)" \
    "[ERROR] bashopts_declare: -r and -v options cannot be activated at the same time"
}

394 395 396 397 398 399 400 401 402 403
_test() {
    bashopts_setup -n "$0" -d "Test case $0"

    bashopts_declare -n value -l value -m add -d "A str value" -t string -i

    bashopts_parse_args --value "NULL" -n
    bashopts_process_opts
    bashopts_process_option -n value -k is_eq_to_null -r
}

Emeric Verschuur's avatar
Emeric Verschuur committed
404
if [ ${#} -eq 0 ]; then
405
    echo
Emeric Verschuur's avatar
Emeric Verschuur committed
406
    for t in $(grep -E -o '_test_case_\w+\>' $0); do
407
        echo "### Test case $t ###"
Emeric Verschuur's avatar
Emeric Verschuur committed
408
        $0 $t
409
        echo
Emeric Verschuur's avatar
Emeric Verschuur committed
410 411
    done
fi
412

Emeric Verschuur's avatar
Emeric Verschuur committed
413
$@