Commit fa6fb3ca authored by Emeric Verschuur's avatar Emeric Verschuur

Fix fix bash eval + test add

parent 2c68ee36
......@@ -7,9 +7,9 @@
# 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
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# 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
......@@ -24,7 +24,7 @@ function bashopts_exit_handle() {
set +o xtrace
local code="${1:-1}"
echo "Error in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}' exited with status $err"
# Print out the stack trace described by $function_stack
# Print out the stack trace described by $function_stack
if [ ${#FUNCNAME[@]} -gt 2 ]
then
echo "Call tree:"
......@@ -249,7 +249,7 @@ bashopts_declare() {
bashopts_opt2name[--${options[long_opt]}]=${options[name]}
fi
for f in ${!options[@]}; do
eval bashopts_optprop_$f[${options[name]}]=\'${options[$f]//\'/\'\\\'\'}\'
eval "bashopts_optprop_$f[${options[name]}]='${options[$f]//\'/\'\\\'\'}'"
done
bashopts_optlist+=(${options[name]})
}
......@@ -351,7 +351,7 @@ bashopts_diplay_summary() {
# STEP 3: parse arg
bashopts_parse_args() {
local op arg val args is_arg short_opts long_opts
is_arg=1
args=()
for arg in "$@"; do
......@@ -374,13 +374,13 @@ bashopts_parse_args() {
fi
done
long_opts=$(bashopts_join_by , ${long_opts[@]})
if ! args=$(getopt -o $short_opts -l "$long_opts" -n "$bashopts_tool_name " -- "${args[@]}"); then
>&2 bashopts_diplay_help
exit 1
fi
eval set -- "$args";
while true; do
arg=$1
shift
......
......@@ -8,9 +8,9 @@
# 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
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# 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
......@@ -19,7 +19,7 @@
# under the License.
if [ "$GITLAB_CI" != "true" ]; then
exec docker run --rm -ti -e GITLAB_CI=true -v $PWD:$PWD -w $PWD ubuntu:16.04 ./test.sh
exec docker run --rm -ti -e GITLAB_CI=true -v $PWD:$PWD -w $PWD ubuntu:16.04 ./test.sh "$@"
fi
. bashopts.sh
......@@ -48,28 +48,28 @@ _test_case_1() {
_test_case_2() {
rm -f /tmp/myapp_case2rc
bashopts_setup -n "myapp_case2" -d "Test case 2" -s /tmp/myapp_case2rc
bashopts_declare -n base_path -l base -o b -d "Base path" -t string -v "/opt/test" -i -s
bashopts_declare -n config_path -l config -o c -d "Configuration path" -t string -e "\$base_path/myconf.cfg" -i -s
bashopts_parse_args --base /tmp -n
bashopts_process_args
req_test_eq "$config_path" "/tmp/myconf.cfg"
req_test_eq "$(cat /tmp/myapp_case2rc)" "base_path=/tmp"
}
_test_case_3() {
rm -f /tmp/myapp_case3rc
bashopts_setup -n "myapp_case3" -d "Test case 3" -s /tmp/myapp_case3rc
bashopts_declare -n process_file_list -l input-file -o f -d "Input file" -t string -m add -s
bashopts_parse_args --input-file foobar --input-file=/foo/bar
bashopts_process_args
req_test_eq "$(bashopts_join_by , ${process_file_list[@]})" "foobar,/foo/bar"
eval declare -x test="$(grep -E "^process_file_list=" /tmp/myapp_case3rc | sed -E "s/^[^=]+=//g")"
req_test_eq "$(bashopts_join_by , ${test[@]})" "foobar,/foo/bar"
......@@ -77,14 +77,14 @@ _test_case_3() {
_test_case_4() {
rm -f /tmp/myapp_case4rc
bashopts_setup -n "myapp_case4" -d "Test case 4" -s /tmp/myapp_case4rc
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
bashopts_parse_args --value "value1" --value "value 2" --value "value '\"' \$3"
bashopts_process_args
req_test_eq "${array_value[0]}" "value1"
req_test_eq "${array_value[1]}" "value 2"
req_test_eq "${array_value[2]}" "value '\"' \$3"
......@@ -93,21 +93,34 @@ _test_case_4() {
}
_test_case_5() {
rm -f /tmp/myapp_case4rc
bashopts_setup -n "myapp_case4" -d "Test case 4" -s /tmp/myapp_case4rc
rm -f /tmp/myapp_case5rc
bashopts_setup -n "myapp_case5" -d "Test case 5" -s /tmp/myapp_case5rc
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
bashopts_parse_args
bashopts_process_args
req_test_eq "${#array_value[@]}" "0"
}
_test_case_6() {
rm -f /tmp/myapp_case6rc
bashopts_setup -n "myapp_case6" -d "Test case 6"
bashopts_declare -n computed_value -e '$(( 2 + 3 * 4 ))' -t string
bashopts_parse_args
bashopts_process_args
req_test_eq "$computed_value" "14"
}
if [ ${#} -eq 0 ]; then
for t in $(grep -E -o '_test_case_\w+\>' $0); do
echo "=> Test case $t"
echo "=> Test case $t"
$0 $t
done
fi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment