Skip to content

Commit

Permalink
fix: skip pre-commit hook variable doesn't work without gitlab token
Browse files Browse the repository at this point in the history
  • Loading branch information
bruzina committed Aug 15, 2022
1 parent 1e5d834 commit 830db83
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 15 deletions.
9 changes: 7 additions & 2 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion scripts/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 61 additions & 6 deletions tests/pre-commit.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
64 changes: 58 additions & 6 deletions tests/pre-push.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,93 @@ 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
assert_line -n 0 'Skipping skipped-hook'
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
Expand Down

0 comments on commit 830db83

Please sign in to comment.