Commit f961c387 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'fix/unscope_vars' into 'main'

fix: fix unscope_variables

Closes #3

See merge request to-be-continuous/tools/tbc-commons!8
parents 146c556b b2e6bfeb
Loading
Loading
Loading
Loading
+43 −19
Original line number Diff line number Diff line
@@ -22,7 +22,8 @@ include:
  }

  function assert_defined() {
    if [[ -z "${!1}" ]]
    val=$(eval echo "\$$1")
    if [[ -z "$val" ]]
    then
      log_error "Variable '$1' should be defined"
      TBC_TEST_FAILS=$((TBC_TEST_FAILS + 1))
@@ -30,7 +31,8 @@ include:
  }

  function refute_defined() {
    if [[ "${!1}" ]]
    val=$(eval echo "\$$1")
    if [[ "$val" ]]
    then
      log_error "Variable '$1' should not be defined"
      TBC_TEST_FAILS=$((TBC_TEST_FAILS + 1))
@@ -61,7 +63,7 @@ e2e:
  image: $IMAGE
  parallel:
    matrix:
      # - IMAGE: "fedora"
      - IMAGE: "fedora"
      - IMAGE: "debian"
      - IMAGE: "alpine"
  variables:
@@ -72,16 +74,26 @@ e2e:
    HEX_SECRET: "@hex@6865786120697320746865206B6579"
    URL_SECRET: "@url@$CI_PROJECT_URL/-/raw/$CI_COMMIT_REF_NAME/tests/fixtures/secret.txt"
    # scoped variables
    scoped__TESTVAR1__if__CI_JOB_STAGE__equals__test: "stage is 'test'"
    scoped__TESTVAR2__if__CI_JOB_STAGE__equals__abcde: "stage is 'abcde'"
    scoped__TESTVAR3__if__CI_JOB_STAGE__in__build__test: "stage is 'build' or 'test'"
    scoped__TESTVAR4__if__CI_JOB_STAGE__in__package__package_test: "stage is 'package' or 'package-test'"
    scoped__TESTVAR5__if__CI_JOB_STAGE__startswith__te: "stage starts with 'te'"
    scoped__TESTVAR6__if__CI_JOB_STAGE__startswith__ab: "stage starts with 'ab'"
    scoped__TESTVAR7__if__CI_JOB_STAGE__endswith__st: "stage ends with 'st'"
    scoped__TESTVAR8__if__CI_JOB_STAGE__endswith__de: "stage ends with 'de'"
    scoped__TESTVAR9__if__CI_JOB_STAGE__contains__es: "stage contains 'es'"
    scoped__TESTVAR10__if__CI_JOB_STAGE__contains__bcd: "stage contains 'bcd'"
    scoped__TESTVAR1__if__CI_JOB_STAGE__equals__test: "applied"
    scoped__TESTVAR2__if__CI_JOB_STAGE__equals__es: "applied"
    scoped__TESTVAR3__if__CI_JOB_STAGE__in__build__test: "applied"
    scoped__TESTVAR4__if__CI_JOB_STAGE__in__package__package_test: "applied"
    scoped__TESTVAR5__if__CI_JOB_STAGE__startswith__te: "applied"
    scoped__TESTVAR6__if__CI_JOB_STAGE__startswith__es: "applied"
    scoped__TESTVAR7__if__CI_JOB_STAGE__endswith__st: "applied"
    scoped__TESTVAR8__if__CI_JOB_STAGE__endswith__es: "applied"
    scoped__TESTVAR9__if__CI_JOB_STAGE__contains__es: "applied"
    scoped__TESTVAR10__ifnot__CI_JOB_STAGE__contains__bcd: "applied"
    scoped__TESTVAR11__ifnot__CI_JOB_STAGE__equals__test: "applied"
    scoped__TESTVAR12__ifnot__CI_JOB_STAGE__equals__es: "applied"
    scoped__TESTVAR13__ifnot__CI_JOB_STAGE__in__build__test: "applied"
    scoped__TESTVAR14__ifnot__CI_JOB_STAGE__in__package__package_test: "applied"
    scoped__TESTVAR15__ifnot__CI_JOB_STAGE__startswith__te: "applied"
    scoped__TESTVAR16__ifnot__CI_JOB_STAGE__startswith__es: "applied"
    scoped__TESTVAR17__ifnot__CI_JOB_STAGE__endswith__st: "applied"
    scoped__TESTVAR18__ifnot__CI_JOB_STAGE__endswith__es: "applied"
    scoped__TESTVAR19__ifnot__CI_JOB_STAGE__contains__es: "applied"
    scoped__TESTVAR20__ifnot__CI_JOB_STAGE__contains__bcd: "applied"
  cache:
    key: "$IMAGE-$CI_COMMIT_REF_SLUG"
    paths:
@@ -97,20 +109,32 @@ e2e:
    - source tbc_unscope_vars.sh
    - !reference [.e2e-scripts]
  script:
    - maybe_install_packages wget ca-certificates git
    - maybe_install_packages ca-certificates
    - maybe_install_packages wget git

    - unscope_variables
    - assert_equals "stage is 'test'" "$TESTVAR1"
    - assert_defined TESTVAR1
    - refute_defined TESTVAR2
    - assert_equals "stage is 'build' or 'test'" "$TESTVAR3"
    - assert_defined TESTVAR3
    - refute_defined TESTVAR4
    - assert_equals "stage starts with 'te'" "$TESTVAR5"
    - assert_defined TESTVAR5
    - refute_defined TESTVAR6
    - assert_equals "stage ends with 'st'" "$TESTVAR7"
    - assert_defined TESTVAR7
    - refute_defined TESTVAR8
    - assert_equals "stage contains 'es'" "$TESTVAR9"
    - assert_defined TESTVAR9
    - refute_defined TESTVAR10

    - refute_defined TESTVAR11
    - assert_defined TESTVAR12
    - refute_defined TESTVAR13
    - assert_defined TESTVAR14
    - refute_defined TESTVAR15
    - assert_defined TESTVAR16
    - refute_defined TESTVAR17
    - assert_defined TESTVAR18
    - refute_defined TESTVAR19
    - assert_defined TESTVAR20

    - eval_all_secrets
    - assert_equals "$B64_SECRET" "base64 rocks!"
    - assert_equals "$HEX_SECRET" "hexa is the key"
+7 −0
Original line number Diff line number Diff line
@@ -22,6 +22,13 @@ set -e
      then
        apk add --no-cache "$@"
      fi
    elif command -v dnf > /dev/null
    then
      # RPM based
      if ! dnf info --installed "$@" > /dev/null
      then
        dnf install --assumeyes --quiet "$@"
      fi
    else
      log_error "... didn't find any supported package manager to install $*"
      exit 1
+10 −9
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ function unscope_variables() {
      _cmp_val_prefix="scoped__${_target_var}__${_condition}__${_cond_var}__${_test_op}__"
      _cmp_val=${_scoped_var#"$_cmp_val_prefix"}
      # manage 'ignore case'
      if [[ "$_test_op" == *_ic ]]
      if [[ "$_test_op" =~ _ic$ ]]
      then
        # lowercase everything
        _cond_val=$(echo "$_cond_val" | tr '[:upper:]' '[:lower:]')
@@ -49,23 +49,24 @@ function unscope_variables() {
        fi
        ;;
      startswith*)
        if [[ -z "$_not" ]] && [[ "$_cond_val" != "$_cmp_val"* ]]; then continue;
        elif [[ "$_not" ]] && [[ "$_cond_val" == "$_cmp_val"* ]]; then continue;
        if [[ -z "$_not" ]] && [[ ! "$_cond_val" =~ ^"$_cmp_val" ]]; then continue;
        elif [[ "$_not" ]] && [[ "$_cond_val" =~ ^"$_cmp_val" ]]; then continue;
        fi
        ;;
      endswith*)
        if [[ -z "$_not" ]] && [[ "$_cond_val" != *"$_cmp_val" ]]; then continue;
        elif [[ "$_not" ]] && [[ "$_cond_val" == *"$_cmp_val" ]]; then continue;
        if [[ -z "$_not" ]] && [[ ! "$_cond_val" =~ "$_cmp_val"$ ]]; then continue;
        elif [[ "$_not" ]] && [[ "$_cond_val" =~ "$_cmp_val"$ ]]; then continue;
        fi
        ;;
      contains*)
        if [[ -z "$_not" ]] && [[ "$_cond_val" != *"$_cmp_val"* ]]; then continue;
        elif [[ "$_not" ]] && [[ "$_cond_val" == *"$_cmp_val"* ]]; then continue;
        # shellcheck disable=SC2076
        if [[ -z "$_not" ]] && [[ ! "$_cond_val" =~ "$_cmp_val" ]]; then continue;
        elif [[ "$_not" ]] && [[ "$_cond_val" =~ "$_cmp_val" ]]; then continue;
        fi
        ;;
      in*)
        if [[ -z "$_not" ]] && [[ "__${_cmp_val}__" != *"__${_cond_val}__"* ]]; then continue;
        elif [[ "$_not" ]] && [[ "__${_cmp_val}__" == *"__${_cond_val}__"* ]]; then continue;
        if [[ -z "$_not" ]] && [[ ! __"$_cmp_val"__ =~ __"$_cond_val"__ ]]; then continue;
        elif [[ "$_not" ]] && [[ __"$_cmp_val"__ =~ __"$_cond_val"__ ]]; then continue;
        fi
        ;;
      esac