From 830db83fdbf591033fa0108752f51a635ee7f3ce Mon Sep 17 00:00:00 2001 From: Martin Bruzina Date: Mon, 15 Aug 2022 18:14:18 +0200 Subject: [PATCH] fix: skip pre-commit hook variable doesn't work without gitlab token --- scripts/pre-commit | 9 ++++-- scripts/pre-push | 6 +++- tests/pre-commit.bats | 67 +++++++++++++++++++++++++++++++++++++++---- tests/pre-push.bats | 64 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 131 insertions(+), 15 deletions(-) diff --git a/scripts/pre-commit b/scripts/pre-commit index 8aac544..5da35db 100755 --- a/scripts/pre-commit +++ b/scripts/pre-commit @@ -10,11 +10,16 @@ LANG=C function run_pre_commit() { # Skip GitLab CI Linter if GitLab Personal Access Token is not set if [ -z "${GL_TOKEN:-}" ]; then - SKIP=gitlab-ci-linter pre-commit + if [ -z "${SKIP:-}" ]; then + export SKIP=gitlab-ci-linter + else + export SKIP="$SKIP,gitlab-ci-linter" + fi else export GITLAB_PRIVATE_TOKEN="$GL_TOKEN" - pre-commit fi + + pre-commit } # Skip execution under test diff --git a/scripts/pre-push b/scripts/pre-push index c0a1168..249d635 100755 --- a/scripts/pre-push +++ b/scripts/pre-push @@ -10,7 +10,11 @@ LANG=C function run_pre_commit() { # Skip GitLab CI Linter if GitLab Personal Access Token is not set if [ -z "${GL_TOKEN:-}" ]; then - export SKIP=gitlab-ci-linter + if [ -z "${SKIP:-}" ]; then + export SKIP=gitlab-ci-linter + else + export SKIP="$SKIP,gitlab-ci-linter" + fi else export GITLAB_PRIVATE_TOKEN="$GL_TOKEN" fi diff --git a/tests/pre-commit.bats b/tests/pre-commit.bats index c1df4b3..b50204c 100644 --- a/tests/pre-commit.bats +++ b/tests/pre-commit.bats @@ -14,33 +14,88 @@ setup() { @test 'scripts/pre-commit run_pre_commit test' { function pre-commit() { - if [ -z "${GITLAB_PRIVATE_TOKEN:-}" ]; then + # shellcheck disable=SC2030,SC2031 + if [ -z "${GL_TOKEN:-}" ]; then echo 'Error' fi + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + echo "Skipping $SKIP" + fi echo 'OK' } export -f pre-commit - export GITLAB_PRIVATE_TOKEN='secret' + # shellcheck disable=SC2030,SC2031 + export GL_TOKEN='secret' + unset SKIP run run_pre_commit assert_success assert_output 'OK' } -@test 'scripts/pre-commit run_pre_commit with skip hook test' { +@test 'scripts/pre-commit run_pre_commit skipped hook test' { function pre-commit() { - if [ "${SKIP_HOOK:-}" == 'skipped-hook' ]; then - echo 'Skipping skipped-hook' + # shellcheck disable=SC2030,SC2031 + if [ -z "${GL_TOKEN:-}" ]; then + echo 'Error' + fi + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + echo "Skipping $SKIP" fi echo 'OK' } export -f pre-commit - export SKIP_HOOK='skipped-hook' + # shellcheck disable=SC2030,SC2031 + export GL_TOKEN='secret' + # shellcheck disable=SC2030,SC2031 + export SKIP='skipped-hook' run run_pre_commit assert_success assert_line -n 0 'Skipping skipped-hook' assert_line -n 1 'OK' } + +@test 'scripts/pre-commit run_pre_commit without GitLab token should skip gitlab-ci-linter test' { + function pre-commit() { + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + echo "Skipping $SKIP" + fi + echo 'OK' + } + export -f pre-commit + + unset GL_TOKEN + unset SKIP + run run_pre_commit + + assert_success + assert_line -n 0 'Skipping gitlab-ci-linter' + assert_line -n 1 'OK' +} + +@test 'scripts/pre-commit run_pre_commit without GitLab token and skipped hook should skip both test' { + function pre-commit() { + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + # shellcheck disable=SC2030,SC2031 + echo "Skipping $SKIP" + fi + echo 'OK' + } + export -f pre-commit + + unset GL_TOKEN + # shellcheck disable=SC2030,SC2031 + export SKIP='skipped-hook' + run run_pre_commit + + assert_success + assert_line -n 0 'Skipping skipped-hook,gitlab-ci-linter' + assert_line -n 1 'OK' +} diff --git a/tests/pre-push.bats b/tests/pre-push.bats index e159d8a..4737869 100644 --- a/tests/pre-push.bats +++ b/tests/pre-push.bats @@ -12,34 +12,47 @@ setup() { . scripts/pre-push } -@test 'scripts/pre-push run_pre_commit success test' { +@test 'scripts/pre-push run_pre_commit test' { function pre-commit() { # shellcheck disable=SC2030,SC2031 - if [ -z "${GITLAB_PRIVATE_TOKEN-}" ]; then + if [ -z "${GL_TOKEN-}" ]; then echo 'Error' fi + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + echo "Skipping $SKIP" + fi echo 'OK' } export -f pre-commit + # shellcheck disable=SC2030,SC2031 export GL_TOKEN='secret' + unset SKIP run run_pre_commit assert_success assert_output 'OK' } -@test 'scripts/pre-push run_pre_commit with skip hook test' { +@test 'scripts/pre-push run_pre_commit skipped hook test' { function pre-commit() { # shellcheck disable=SC2030,SC2031 - if [ -z "${GITLAB_PRIVATE_TOKEN-}" ]; then - echo 'Skipping skipped-hook' + if [ -z "${GL_TOKEN:-}" ]; then + echo 'Error' + fi + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + echo "Skipping $SKIP" fi echo 'OK' } export -f pre-commit - unset GL_TOKEN + # shellcheck disable=SC2030,SC2031 + export GL_TOKEN='secret' + # shellcheck disable=SC2030,SC2031 + export SKIP='skipped-hook' run run_pre_commit assert_success @@ -47,6 +60,45 @@ setup() { assert_line -n 1 'OK' } +@test 'scripts/pre-push run_pre_commit without GitLab token should skip gitlab-ci-linter test' { + function pre-commit() { + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + echo "Skipping $SKIP" + fi + echo 'OK' + } + export -f pre-commit + + unset GL_TOKEN + unset SKIP + run run_pre_commit + + assert_success + assert_line -n 0 'Skipping gitlab-ci-linter' + assert_line -n 1 'OK' +} + +@test 'scripts/pre-push run_pre_commit without GitLab token and skipped hook should skip both test' { + function pre-commit() { + # shellcheck disable=SC2030,SC2031 + if [ -n "${SKIP:-}" ]; then + echo "Skipping $SKIP" + fi + echo 'OK' + } + export -f pre-commit + + unset GL_TOKEN + # shellcheck disable=SC2030,SC2031 + export SKIP='skipped-hook' + run run_pre_commit + + assert_success + assert_line -n 0 'Skipping skipped-hook,gitlab-ci-linter' + assert_line -n 1 'OK' +} + @test 'scripts/pre-push run_pre_commit with arguments and stdin test' { function pre-commit() { cat <&0