Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions ci/test-comment-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,42 @@ pull_request_ci() {
}

workflow=$root/.github/workflows/checks.yml
step_if=$(awk '
/^ *- name: comment-check *$/ { s = 1; ind = index($0, "-"); next }
s && /^ *- / && index($0, "-") <= ind { exit }
s && /^ *if: / { sub(/^ *if: */, ""); print; exit }
' "$workflow")
step_run=$(awk '
/^ *- name: comment-check *$/ { s = 1; ind = index($0, "-"); next }
s && /^ *- / && index($0, "-") <= ind { exit }
r { match($0, /^ */); if ($0 !~ /^ *$/ && RLENGTH < k) exit; print; next }
s && /^ *run: *\| *$/ { r = 1; k = index($0, "r"); next }
' "$workflow")
no_fetch=$tmp/no-fetch.yml
sed '/git fetch/d' "$workflow" > "$no_fetch"
with_step_setting() {
awk -v setting="$1" '{ print } /^ *- name: comment-check *$/ { match($0, /^ */); print substr($0, 1, RLENGTH) " " setting }' "$workflow" > "$2"
}
tolerant=$tmp/tolerant.yml
with_step_setting 'continue-on-error: true' "$tolerant"
other_shell=$tmp/other-shell.yml
with_step_setting 'shell: bash {0}' "$other_shell"

workflow_step() {
repo="$tmp/$1"
origin="$tmp/$1" wf=$2
repo="$tmp/$1-$(basename "$wf" .yml)-checkout"
git clone -q --depth 1 --no-tags "file://$origin" "$repo"
tolerates=$(grep -n -E "^ *[\"']?(continue-on-error|shell)[\"']? *:" "$wf" | grep -v -E 'continue-on-error: *false *$' || true)
step_if=$(awk '
/^ *- name: comment-check *$/ { s = 1; ind = index($0, "-"); next }
s && /^ *- / && index($0, "-") <= ind { exit }
s && /^ *if: / { sub(/^ *if: */, ""); print; exit }
' "$wf")
step_run=$(awk '
/^ *- name: comment-check *$/ { s = 1; ind = index($0, "-"); next }
s && /^ *- / && index($0, "-") <= ind { exit }
r { match($0, /^ */); if ($0 !~ /^ *$/ && RLENGTH < k) exit; print; next }
s && /^ *run: *\| *$/ { r = 1; k = index($0, "r"); next }
' "$wf")
cd "$repo"
mkdir -p ci tools/comment-check
cp "$root/ci/comment-check.sh" ci/
cp "$root/tools/comment-check/check.py" tools/comment-check/
git remote add origin "file://$repo"
set +e
if [ "$step_if" != "github.event_name == 'pull_request'" ] || [ -z "$step_run" ]; then
out="no comment-check step that runs on pull requests in $workflow (if: '$step_if')"
if [ -n "$tolerates" ]; then
out="$wf sets what tolerates a failing comment-check, or runs it in another shell than the bash -e this proof models: $tolerates"
code=tolerated
elif [ "$step_if" != "github.event_name == 'pull_request'" ] || [ -z "$step_run" ]; then
out="no comment-check step that runs on pull requests in $wf (if: '$step_if')"
code=missing
else
out=$(GITHUB_SHA=$(git rev-parse HEAD) bash -e -c "$step_run" 2>&1)
Expand All @@ -72,17 +86,23 @@ expect() {

pull_request_ci disallowed catalog/x/check.sh '# a plain comment'
expect 'catches: a pull request adding a disallowed comment passes CI' 1 'catalog/x/check.sh:2: comment:'
workflow_step disallowed
workflow_step disallowed "$workflow"
expect 'catches: the pull request workflow passes a disallowed comment' 1 'catalog/x/check.sh:2: comment:'
workflow_step disallowed "$no_fetch"
expect 'catches: the workflow proof passes a comment-check step that never fetches the merge parents' 2 'HEAD^1'
workflow_step disallowed "$tolerant"
expect 'catches: the workflow proof passes a comment-check step that tolerates its own failure' tolerated 'continue-on-error: true'
workflow_step disallowed "$other_shell"
expect 'catches: the workflow proof passes a comment-check step run in a shell that ignores a failing command' tolerated 'shell: bash {0}'

pull_request_ci escaped catalog/x/check.sh '# comment-ok: the runner reads this line'
expect 'catches: a reasoned escape tag still fails CI' 0 ''
workflow_step escaped
workflow_step escaped "$workflow"
expect 'catches: the pull request workflow fails a reasoned escape tag' 0 ''

pull_request_ci fixture format/fixtures/valid/x/check.sh '# Prints nothing: nothing is due.'
expect 'catches: a format fixture planting a comment the validator reads fails CI' 0 ''
workflow_step fixture
workflow_step fixture "$workflow"
expect 'catches: the pull request workflow fails a format fixture comment' 0 ''

[ "$failures" -eq 0 ] || exit 1
Loading