Commit 23dc2e8d authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

fix: fix unscope_variables

Don't use pattern matching to test pure strings (messes up with files on Alpine)
parent 146c556b
Loading
Loading
Loading
Loading
+36 −15
Original line number Diff line number Diff line
@@ -72,16 +72,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__abcde: "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__ab: "applied"
    scoped__TESTVAR7__if__CI_JOB_STAGE__endswith__st: "applied"
    scoped__TESTVAR8__if__CI_JOB_STAGE__endswith__de: "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__abcde: "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__ab: "applied"
    scoped__TESTVAR17__ifnot__CI_JOB_STAGE__endswith__st: "applied"
    scoped__TESTVAR18__ifnot__CI_JOB_STAGE__endswith__de: "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:
@@ -100,17 +110,28 @@ e2e:
    - maybe_install_packages wget ca-certificates 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"
+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