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
38 changes: 38 additions & 0 deletions ci/test-comment-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ pull_request_ci() {
cd "$root"
}

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")

workflow_step() {
repo="$tmp/$1"
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')"
code=missing
else
out=$(GITHUB_SHA=$(git rev-parse HEAD) bash -e -c "$step_run" 2>&1)
code=$?
fi
set -e
cd "$root"
}

expect() {
case=$1 want_code=$2 want_text=$3
if [ "$code" != "$want_code" ] || { [ -n "$want_text" ] && ! printf '%s' "$out" | grep -q -F -- "$want_text"; }; then
Expand All @@ -40,11 +72,17 @@ 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
expect 'catches: the pull request workflow passes a disallowed comment' 1 'catalog/x/check.sh:2: comment:'

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
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
expect 'catches: the pull request workflow fails a format fixture comment' 0 ''

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