From 6fb9ee6fc42e0b6317fb1608863e2fe17dbe74cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20L=C3=BCdtke?= Date: Wed, 25 Dec 2019 14:21:37 +0100 Subject: [PATCH] implement and use true/false helpers --- gitlab.sh | 2 +- industrial_ci/src/builders/catkin_make.sh | 2 +- .../src/builders/catkin_make_isolated.sh | 2 +- industrial_ci/src/builders/catkin_tools.sh | 8 ++-- industrial_ci/src/builders/colcon.sh | 4 +- industrial_ci/src/ci_main.sh | 2 +- industrial_ci/src/docker.sh | 4 +- industrial_ci/src/env.sh | 10 ++--- industrial_ci/src/tests/abi_check.sh | 2 +- industrial_ci/src/tests/source_tests.sh | 12 +++--- industrial_ci/src/util.sh | 38 +++++++++++++++++-- 11 files changed, 58 insertions(+), 28 deletions(-) diff --git a/gitlab.sh b/gitlab.sh index 83067c98a..6f5b7ed1a 100755 --- a/gitlab.sh +++ b/gitlab.sh @@ -26,7 +26,7 @@ export TARGET_REPO_NAME=$CI_PROJECT_NAME export _FOLDING_TYPE=gitlab if [ -n "$SSH_PRIVATE_KEY" ]; then - if [ "$CI_DISPOSABLE_ENVIRONMENT" != true ] && ! [ -f /.dockerenv ] ; then + if ici_is_false_or_unset "$CI_DISPOSABLE_ENVIRONMENT" && ! [ -f /.dockerenv ] ; then echo "SSH auto set-up cannot be used in non-disposable environments" exit 1 fi diff --git a/industrial_ci/src/builders/catkin_make.sh b/industrial_ci/src/builders/catkin_make.sh index f080c667e..1c30f4884 100644 --- a/industrial_ci/src/builders/catkin_make.sh +++ b/industrial_ci/src/builders/catkin_make.sh @@ -29,7 +29,7 @@ function builder_run_tests { local extend=$1; shift local ws=$1; shift local -a opts - if [ "$PARALLEL_TESTS" == false ]; then + if ici_is_false_or_unset "$PARALLEL_TESTS"; then opts+=(-j1) fi ici_exec_in_workspace "$extend" "$ws" catkin_make --make-args run_tests "${opts[@]}" "$@" diff --git a/industrial_ci/src/builders/catkin_make_isolated.sh b/industrial_ci/src/builders/catkin_make_isolated.sh index 82fd06ce5..234f62a87 100644 --- a/industrial_ci/src/builders/catkin_make_isolated.sh +++ b/industrial_ci/src/builders/catkin_make_isolated.sh @@ -32,7 +32,7 @@ function builder_run_build { function builder_run_tests { local -a opts - if [ "$PARALLEL_TESTS" == false ]; then + if ici_is_false_or_unset "$PARALLEL_TESTS"; then opts+=(-j1) fi _run_catkin_make_isolated run_tests "$1" "$2" "${opts[@]}" diff --git a/industrial_ci/src/builders/catkin_tools.sh b/industrial_ci/src/builders/catkin_tools.sh index 8db534488..ec5315de1 100644 --- a/industrial_ci/src/builders/catkin_tools.sh +++ b/industrial_ci/src/builders/catkin_tools.sh @@ -23,7 +23,7 @@ function builder_run_build { local extend=$1; shift local ws=$1; shift local -a opts - if [ "$VERBOSE_OUTPUT" != false ]; then + if ici_is_true"$VERBOSE_OUTPUT"; then opts+=("-vi") fi ici_exec_in_workspace "$extend" "$ws" catkin config --install @@ -34,13 +34,13 @@ function builder_run_tests { local extend=$1; shift local ws=$1; shift local -a opts - if [ "$VERBOSE_TESTS" != false ]; then + if ici_is_true"$VERBOSE_TESTS"; then opts+=(-v) fi - if [ "$IMMEDIATE_TEST_OUTPUT" == true ]; then + if ici_is_true"$IMMEDIATE_TEST_OUTPUT"; then opts+=(-i) fi - if [ "$PARALLEL_TESTS" == false ]; then + if ici_is_false_or_unset "$PARALLEL_TESTS"; then opts+=(-j1 -p1) fi ici_exec_in_workspace "$extend" "$ws" catkin build --catkin-make-args run_tests -- "${opts[@]}" --no-status diff --git a/industrial_ci/src/builders/colcon.sh b/industrial_ci/src/builders/colcon.sh index b9716b223..afd2973c9 100644 --- a/industrial_ci/src/builders/colcon.sh +++ b/industrial_ci/src/builders/colcon.sh @@ -35,13 +35,13 @@ function builder_run_tests { local extend=$1; shift local ws=$1; shift local output_handler - if [ "$IMMEDIATE_TEST_OUTPUT" == true ]; then + if ici_is_true "$IMMEDIATE_TEST_OUTPUT"; then output_handler="console_direct+" else output_handler="console_cohesion+" fi local opts=(--event-handlers "${_colcon_event_handlers[@]}" "${output_handler}") - if [ "$PARALLEL_TESTS" == false ]; then + if ici_is_false_or_unset "$PARALLEL_TESTS"; then opts+=(--executor sequential --ctest-args -j1) else opts+=(--executor parallel) diff --git a/industrial_ci/src/ci_main.sh b/industrial_ci/src/ci_main.sh index e2c2f4fcb..b299ec338 100644 --- a/industrial_ci/src/ci_main.sh +++ b/industrial_ci/src/ci_main.sh @@ -38,7 +38,7 @@ source "${ICI_SRC_PATH}/env.sh" trap ici_exit EXIT # install industrial_ci exit handler # Start prerelease, and once it finishs then finish this script too. -if [ "$PRERELEASE" == true ]; then +if ici_is_true "$PRERELEASE"; then # shellcheck source=industrial_ci/src/tests/ros_prerelease.sh source "${ICI_SRC_PATH}/tests/ros_prerelease.sh" run_ros_prerelease diff --git a/industrial_ci/src/docker.sh b/industrial_ci/src/docker.sh index 89aff5609..bd2ddf94c 100644 --- a/industrial_ci/src/docker.sh +++ b/industrial_ci/src/docker.sh @@ -145,7 +145,7 @@ function docker_cp { function ici_docker_build() { local -a build_opts ici_parse_env_array build_opts DOCKER_BUILD_OPTS - if [ "$DOCKER_PULL" != false ]; then + if ici_is_true_or_unset "$DOCKER_PULL"; then build_opts+=("--pull") fi docker build -t "$DOCKER_IMAGE" "${build_opts[@]}" "$@" @@ -153,7 +153,7 @@ function ici_docker_build() { function ici_docker_try_pull { local image=$1 - if [ "$DOCKER_PULL" != false ]; then + if ici_is_true_or_unset "$DOCKER_PULL"; then echo "Pulling Docker image '$image'..." ici_quiet docker pull "$image" fi diff --git a/industrial_ci/src/env.sh b/industrial_ci/src/env.sh index 283ca3efb..5ce1bde39 100644 --- a/industrial_ci/src/env.sh +++ b/industrial_ci/src/env.sh @@ -21,7 +21,7 @@ ici_enforce_deprecated CATKIN_CONFIG "Explicit catkin configuration is not avail ici_enforce_deprecated INJECT_QEMU "Please check https://github.com/ros-industrial/industrial_ci/blob/master/doc/migration_guide.md#inject_qemu" if [ -n "$NOT_TEST_INSTALL" ]; then - if [ "$NOT_TEST_INSTALL" != true ]; then + if ici_is_false "$NOT_TEST_INSTALL"; then ici_enforce_deprecated NOT_TEST_INSTALL "testing installed test files has been removed." else ici_mark_deprecated NOT_TEST_INSTALL "testing installed test files has been removed, NOT_TEST_INSTALL=false is superfluous" @@ -80,7 +80,7 @@ function use_snapshot() { } function use_repo_or_final_snapshot() { - if [ "$ROS_VERSION_EOL" = true ]; then + if ici_is_true "$ROS_VERSION_EOL"; then use_snapshot final if [ -n "$ROS_REPO" ]; then ici_warn "'$ROS_DISTRO' is in end-of-life state, ROS_REPO='$ROS_REPO' gets ignored" @@ -242,20 +242,20 @@ if [ "$UPSTREAM_WORKSPACE" = "debian" ]; then unset UPSTREAM_WORKSPACE fi -if [ "$USE_DEB" = true ]; then +if ici_is_true "$USE_DEB"; then if [ "${UPSTREAM_WORKSPACE:-debian}" != "debian" ]; then ici_error "USE_DEB and UPSTREAM_WORKSPACE are in conflict" fi ici_warn "Setting 'USE_DEB=true' is superfluous" fi -if [ "$UPSTREAM_WORKSPACE" = "file" ] || [ "${USE_DEB:-true}" != true ]; then +if [ "$UPSTREAM_WORKSPACE" = "file" ] || ici_is_false "$USE_DEB"; then ROSINSTALL_FILENAME="${ROSINSTALL_FILENAME:-.travis.rosinstall}" if [ -f "$TARGET_REPO_PATH/$ROSINSTALL_FILENAME.$ROS_DISTRO" ]; then ROSINSTALL_FILENAME="$ROSINSTALL_FILENAME.$ROS_DISTRO" fi - if [ "${USE_DEB:-true}" != true ]; then # means UPSTREAM_WORKSPACE=file + if ici_is_false "$USE_DEB"; then # means UPSTREAM_WORKSPACE=file if [ "${UPSTREAM_WORKSPACE:-file}" != "file" ]; then ici_error "USE_DEB and UPSTREAM_WORKSPACE are in conflict" fi diff --git a/industrial_ci/src/tests/abi_check.sh b/industrial_ci/src/tests/abi_check.sh index 56352bb7f..a8e83d617 100644 --- a/industrial_ci/src/tests/abi_check.sh +++ b/industrial_ci/src/tests/abi_check.sh @@ -89,7 +89,7 @@ function abi_process_workspace() { } function abi_configure() { - if [ "$ABICHECK_MERGE" = true ]; then + if ici_is_true "$ABICHECK_MERGE"; then local ref_list if ici_split_array ref_list "$(cd "$TARGET_REPO_PATH" && git rev-list --parents -n 1 HEAD)" && [ "${#ref_list[@]}" -gt 2 ]; then ABICHECK_VERSION="${ref_list[1]}" diff --git a/industrial_ci/src/tests/source_tests.sh b/industrial_ci/src/tests/source_tests.sh index 42e2e7cde..25001c1e1 100644 --- a/industrial_ci/src/tests/source_tests.sh +++ b/industrial_ci/src/tests/source_tests.sh @@ -106,7 +106,7 @@ function run_source_tests { target_ws=~/target_ws downstream_ws=~/downstream_ws - if [ "$CCACHE_DIR" ]; then + if [ -n "$CCACHE_DIR" ]; then ici_run "setup_ccache" ici_asroot apt-get install -qq -y ccache export PATH="/usr/lib/ccache:$PATH" fi @@ -122,16 +122,16 @@ function run_source_tests { extend="$upstream_ws/install" fi - if [ "${CLANG_TIDY:-false}" != false ]; then + if ! ici_is_false_or_unset "$CLANG_TIDY"; then TARGET_CMAKE_ARGS="$TARGET_CMAKE_ARGS -DCMAKE_EXPORT_COMPILE_COMMANDS=ON" fi ici_with_ws "$target_ws" ici_build_workspace "target" "$extend" "$target_ws" - if [ "$NOT_TEST_BUILD" != "true" ]; then + if ici_is_false_or_unset "$NOT_TEST_BUILD"; then ici_with_ws "$target_ws" ici_test_workspace "target" "$extend" "$target_ws" fi - if [ "$CATKIN_LINT" == "true" ] || [ "$CATKIN_LINT" == "pedantic" ]; then + if ici_is_true "$CATKIN_LINT" || [ "$CATKIN_LINT" == "pedantic" ]; then ici_run "install_catkin_lint" install_catkin_lint local -a catkin_lint_args ici_parse_env_array catkin_lint_args CATKIN_LINT_ARGS @@ -141,7 +141,7 @@ function run_source_tests { ici_with_ws "$target_ws" ici_run "catkin_lint" ici_exec_in_workspace "$extend" "$target_ws" catkin_lint --explain "${catkin_lint_args[@]}" src fi - if [ "${CLANG_TIDY:-false}" != false ]; then + if ! ici_is_false_or_unset "$CLANG_TIDY"; then run_clang_tidy_check "$target_ws" fi @@ -150,7 +150,7 @@ function run_source_tests { ici_with_ws "$downstream_ws" ici_build_workspace "downstream" "$extend" "$downstream_ws" #extend="$downstream_ws/install" - if [ "$NOT_TEST_DOWNSTREAM" != "true" ]; then + if ici_is_true_or_unset "$NOT_TEST_DOWNSTREAM"; then ici_with_ws "$downstream_ws" ici_test_workspace "downstream" "$extend" "$downstream_ws" fi fi diff --git a/industrial_ci/src/util.sh b/industrial_ci/src/util.sh index 5efa286f9..152d06c79 100644 --- a/industrial_ci/src/util.sh +++ b/industrial_ci/src/util.sh @@ -35,6 +35,36 @@ function ici_ansi_cleared_line { echo -en "$*\r\e[0K" } +function ici_is_true { + local v=$1; shift + case "${v,,}" in + "true" | "on" | "enabled" | "1") + return 0 + ;; + esac + return 1 +} + +function ici_is_false { + local v=$1; shift + case "${v,,}" in + "false" | "off" | "disabled" | "0") + return 0 + ;; + esac + return 1 +} + +function ici_is_true_or_unset { + local v=$1; shift + [ -z "" ] || ici_is_true "$v" +} + +function ici_is_false_or_unset { + local v=$1; shift + [ -z "" ] || ici_is_false "$v" +} + function ici_source_setup { local u_set=1 [[ $- =~ u ]] || u_set=0 @@ -105,7 +135,7 @@ source "${ICI_SRC_PATH}/folding/${_FOLDING_TYPE:-none}.sh" || ici_error "Folding function ici_time_start { ici_hook "before_${1}" - if [ "$DEBUG_BASH" ] && [ "$DEBUG_BASH" == true ]; then set +x; fi + if ici_is_true "$DEBUG_BASH"; then set +x; fi ICI_START_TIME=$(date -u +%s%N) ICI_TIME_ID="$(printf %08x $((RANDOM * RANDOM)))" ICI_FOLD_NAME=$1 @@ -116,7 +146,7 @@ function ici_time_start { ici_color_output $ANSI_BLUE ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ici_color_output $ANSI_BLUE "Starting function '$ICI_FOLD_NAME'" - if [ "$DEBUG_BASH" ] && [ "$DEBUG_BASH" == true ]; then set -x; fi + if ici_is_true "$DEBUG_BASH"; then set -x; fi } ####################################### @@ -135,7 +165,7 @@ function ici_time_start { # (None) ####################################### function ici_time_end { - if [ "$DEBUG_BASH" ] && [ "$DEBUG_BASH" == true ]; then set +x; fi + if ici_is_true "$DEBUG_BASH"; then set +x; fi local color_wrap=${1:-${ANSI_GREEN}} local exit_code=${2:-$?} local name=$ICI_FOLD_NAME @@ -150,7 +180,7 @@ function ici_time_end { ici_color_output "$color_wrap" "Function '$name' returned with code '${exit_code}' after $(( elapsed_seconds / 60 )) min $(( elapsed_seconds % 60 )) sec" unset ICI_FOLD_NAME - if [ "$DEBUG_BASH" ] && [ "$DEBUG_BASH" == true ]; then set -x; fi + if ici_is_true "$DEBUG_BASH"; then set -x; fi ici_hook "after_${name}" }