From 8545cac33d9cfc7a8310e8eb5e9cce37190ecd1b Mon Sep 17 00:00:00 2001 From: Ivan Dyachkov Date: Sun, 6 Aug 2023 13:45:33 +0200 Subject: [PATCH] ci(parse-git-ref): handle more than one tag on the same commit also add shelltest in CI --- .github/actions/pr-sanity-checks/action.yaml | 5 ++ scripts/parse-git-ref.sh | 18 ++-- scripts/shelltest/parse-git-ref.cleanup | 4 + scripts/shelltest/parse-git-ref.setup | 4 + scripts/shelltest/parse-git-ref.test | 94 ++++++++++++++++++++ scripts/shelltest/run_tests.sh | 17 ++++ 6 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 scripts/shelltest/parse-git-ref.cleanup create mode 100644 scripts/shelltest/parse-git-ref.setup create mode 100644 scripts/shelltest/parse-git-ref.test create mode 100755 scripts/shelltest/run_tests.sh diff --git a/.github/actions/pr-sanity-checks/action.yaml b/.github/actions/pr-sanity-checks/action.yaml index 70efae7a3..f19fe2c6d 100644 --- a/.github/actions/pr-sanity-checks/action.yaml +++ b/.github/actions/pr-sanity-checks/action.yaml @@ -20,6 +20,11 @@ runs: run: | DEBIAN_FRONTEND=noninteractive apt-get update -qy && apt-get install -qy shellcheck ./scripts/shellcheck.sh + - name: Run shell tests + shell: bash + run: | + DEBIAN_FRONTEND=noninteractive apt-get update -qy && apt-get install -qy shelltestrunner + scripts/shelltest/run_tests.sh - name: Check workflow files shell: bash env: diff --git a/scripts/parse-git-ref.sh b/scripts/parse-git-ref.sh index 407c7b21a..a486f2589 100755 --- a/scripts/parse-git-ref.sh +++ b/scripts/parse-git-ref.sh @@ -6,12 +6,14 @@ set -euo pipefail is_latest() { ref_name=$(basename "$1") - latest_ref_name=$(git describe --tags "$(git rev-list --tags --max-count=1)") - if [[ "$ref_name" == "$latest_ref_name" ]]; then - echo true; - else - echo false; - fi + # shellcheck disable=SC2046 + for t in $(git tag --points-at $(git rev-list --tags --max-count=1)); do + if [[ "$t" == "$ref_name" ]]; then + echo true; + return; + fi + done + echo false } if [[ $1 =~ ^refs/tags/v[5-9]+\.[0-9]+\.[0-9]+$ ]]; then @@ -45,7 +47,7 @@ elif [[ $1 =~ ^refs/tags/e[5-9]+\.[0-9]+\.[0-9]+-(alpha|beta|rc)\.[0-9]+$ ]]; th RELEASE=true LATEST=false elif [[ $1 =~ ^refs/tags/.+ ]]; then - echo "Unrecognized tag: $1" + echo "Unrecognized tag: $1" 1>&2 exit 1 elif [[ $1 =~ ^refs/heads/master$ ]]; then PROFILE=emqx @@ -63,7 +65,7 @@ elif [[ $1 =~ ^refs/heads/ci/.* ]]; then RELEASE=false LATEST=false else - echo "Unrecognized git ref: $1" + echo "Unrecognized git ref: $1" 1>&2 exit 1 fi diff --git a/scripts/shelltest/parse-git-ref.cleanup b/scripts/shelltest/parse-git-ref.cleanup new file mode 100644 index 000000000..8c6dcc027 --- /dev/null +++ b/scripts/shelltest/parse-git-ref.cleanup @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +git tag -d v5.1.99 >/dev/null +git tag -d e5.1.99 >/dev/null diff --git a/scripts/shelltest/parse-git-ref.setup b/scripts/shelltest/parse-git-ref.setup new file mode 100644 index 000000000..f8938787f --- /dev/null +++ b/scripts/shelltest/parse-git-ref.setup @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +git tag v5.1.99 +git tag e5.1.99 diff --git a/scripts/shelltest/parse-git-ref.test b/scripts/shelltest/parse-git-ref.test new file mode 100644 index 000000000..3f2ede0d1 --- /dev/null +++ b/scripts/shelltest/parse-git-ref.test @@ -0,0 +1,94 @@ +./parse-git-ref.sh refs/tags/v5.2.0-foobar.1 +>>>2 +Unrecognized tag: refs/tags/v5.2.0-foobar.1 +>>>= 1 + +./parse-git-ref.sh v5.2.0 +>>>2 +Unrecognized git ref: v5.2.0 +>>>= 1 + +./parse-git-ref.sh refs/tags/v5.1.0 +>>> +{"profile": "emqx", "edition": "Opensource", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/v5.1.5.1 +>>> +{"profile": "emqx", "edition": "Opensource", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/v5.2.0-alpha.1 +>>> +{"profile": "emqx", "edition": "Opensource", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/v5.2.0-alpha-1 +>>>2 +Unrecognized tag: refs/tags/v5.2.0-alpha-1 +>>>= 1 + +./parse-git-ref.sh refs/tags/v5.2.0-beta.1 +>>> +{"profile": "emqx", "edition": "Opensource", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/v5.2.0-rc.1 +>>> +{"profile": "emqx", "edition": "Opensource", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/e5.1.0 +>>> +{"profile": "emqx-enterprise", "edition": "Enterprise", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/e5.1.5.1 +>>> +{"profile": "emqx-enterprise", "edition": "Enterprise", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/e5.2.0-alpha.1 +>>> +{"profile": "emqx-enterprise", "edition": "Enterprise", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/e5.2.0-beta.1 +>>> +{"profile": "emqx-enterprise", "edition": "Enterprise", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/e5.2.0-rc.1 +>>> +{"profile": "emqx-enterprise", "edition": "Enterprise", "release": true, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/tags/e5.1.99 +>>> +{"profile": "emqx-enterprise", "edition": "Enterprise", "release": true, "latest": true} +>>>= 0 + +./parse-git-ref.sh refs/tags/v5.1.99 +>>> +{"profile": "emqx", "edition": "Opensource", "release": true, "latest": true} +>>>= 0 + +./parse-git-ref.sh refs/heads/master +>>> +{"profile": "emqx", "edition": "Opensource", "release": false, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/heads/release-51 +>>> +{"profile": "emqx-enterprise", "edition": "Enterprise", "release": false, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/heads/ci/foobar +>>> +{"profile": "emqx", "edition": "Opensource", "release": false, "latest": false} +>>>= 0 + +./parse-git-ref.sh refs/heads/release-44 +>>>2 +Unrecognized git ref: refs/heads/release-44 +>>>= 1 diff --git a/scripts/shelltest/run_tests.sh b/scripts/shelltest/run_tests.sh new file mode 100755 index 000000000..28f48eaee --- /dev/null +++ b/scripts/shelltest/run_tests.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +cd -P -- "$(dirname -- "$0")/.." + +exit_code=0 + +for test in shelltest/*.test; do + echo "Running $test" + /bin/sh "${test%.test}.setup" + shelltest -c --diff --all --precise -- "$test" + if [ $? -ne 0 ]; then + exit_code=1 + fi + /bin/sh "${test%.test}.cleanup" +done + +exit $exit_code