|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# Contract for the two label-writing workflows: pr-labeler.yaml (which labels |
| 4 | +# it may apply and from what input) and stale.yaml (which labels drive the |
| 5 | +# auto-close policy and who is exempt). Every label name either workflow can |
| 6 | +# write or match must resolve against .github/labels.yml — a mistyped name is |
| 7 | +# not an error at runtime, it is a rule that silently stops matching. |
| 8 | +# |
| 9 | +# These tests intentionally pin executable/structural lines, not prose: a |
| 10 | +# mapping demoted to a YAML `#` or JavaScript `//` comment must never satisfy |
| 11 | +# the contract. See hack/promote-gate-contract.bats for the pattern. |
| 12 | + |
| 13 | +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" |
| 14 | +PR_LABELER="$REPO_ROOT/.github/workflows/pr-labeler.yaml" |
| 15 | +STALE="$REPO_ROOT/.github/workflows/stale.yaml" |
| 16 | +LABELS="$REPO_ROOT/.github/labels.yml" |
| 17 | + |
| 18 | +# Comment-stripping filters, one per language in these files. grep exits 1 on |
| 19 | +# an empty selection (legitimate for an empty block) and 2 on a real error, so |
| 20 | +# only the latter propagates. |
| 21 | +code_lines() { |
| 22 | + local rc=0 |
| 23 | + grep -v '^[[:space:]]*#' || rc=$? |
| 24 | + [ "$rc" -le 1 ] |
| 25 | +} |
| 26 | + |
| 27 | +script_lines() { |
| 28 | + local rc=0 |
| 29 | + grep -v '^[[:space:]]*//' || rc=$? |
| 30 | + [ "$rc" -le 1 ] |
| 31 | +} |
| 32 | + |
| 33 | +# Body of a `const <name> = {` ... `};` object literal inside the |
| 34 | +# github-script block, comment lines already stripped. |
| 35 | +js_object_block() { |
| 36 | + awk -v name="const $1 = {" ' |
| 37 | + index($0, name) { inside = 1; next } |
| 38 | + inside && /^[[:space:]]*};[[:space:]]*$/ { exit } |
| 39 | + inside' "$2" | script_lines |
| 40 | +} |
| 41 | + |
| 42 | +# Every label defined in .github/labels.yml, one per line. |
| 43 | +defined_labels() { |
| 44 | + code_lines < "$LABELS" | awk '/^- name: / { print $3 }' |
| 45 | +} |
| 46 | + |
| 47 | +# `key: value` inputs of the stale action, comment lines stripped. |
| 48 | +stale_input() { |
| 49 | + code_lines < "$STALE" | awk -v key=" $1: " ' |
| 50 | + index($0, key) == 1 { sub(key, ""); print; exit }' |
| 51 | +} |
| 52 | + |
| 53 | +# A `>-` folded list input of the stale action, flattened to one label per line. |
| 54 | +stale_exempt_list() { |
| 55 | + code_lines < "$STALE" | awk -v key=" $1: >-" ' |
| 56 | + $0 == key { inside = 1; next } |
| 57 | + inside && !/^ / { exit } |
| 58 | + inside { print }' | tr -d ' ' | tr ',' '\n' | grep -v '^$' |
| 59 | +} |
| 60 | + |
| 61 | +assert_labels_defined() { |
| 62 | + # $1: newline-separated label names. Fails naming the missing ones. |
| 63 | + # POSIX-only on purpose: hack/cozytest.sh runs these files in plain sh. |
| 64 | + local all missing |
| 65 | + all="$(defined_labels)" |
| 66 | + missing="$(printf '%s\n' "$1" | sort -u | while read -r l; do |
| 67 | + [ -n "$l" ] || continue |
| 68 | + printf '%s\n' "$all" | grep -qxF "$l" || printf '%s\n' "$l" |
| 69 | + done)" |
| 70 | + if [ -n "$missing" ]; then |
| 71 | + echo "labels not defined in .github/labels.yml: $missing" >&2 |
| 72 | + return 1 |
| 73 | + fi |
| 74 | +} |
| 75 | +
|
| 76 | +@test "every label pr-labeler can apply resolves against labels.yml" { |
| 77 | + kind_labels="$(js_object_block typeToKind "$PR_LABELER" | grep -o "'kind/[^']*'" | tr -d "'")" |
| 78 | + [ -n "$kind_labels" ] |
| 79 | + assert_labels_defined "$kind_labels" |
| 80 | +
|
| 81 | + area_labels="$(js_object_block scopeToArea "$PR_LABELER" | grep -o "'area/[^']*'" | tr -d "'")" |
| 82 | + [ -n "$area_labels" ] |
| 83 | + assert_labels_defined "$area_labels" |
| 84 | +
|
| 85 | + # Labels added outside the two tables: backport prefix, breaking-change |
| 86 | + # footer, and the no-area fallback. |
| 87 | + literal_labels="$(code_lines < "$PR_LABELER" | script_lines \ |
| 88 | + | grep -o "toAdd.add('[^']*')" | sed "s/toAdd.add('//; s/')//" \ |
| 89 | + | grep -v '^typeToKind\|^scopeToArea' || true)" |
| 90 | + for l in area/release kind/breaking-change area/uncategorized; do |
| 91 | + printf '%s\n' "$literal_labels" | grep -qxF "$l" |
| 92 | + done |
| 93 | + assert_labels_defined "$literal_labels" |
| 94 | +} |
| 95 | +
|
| 96 | +@test "pr-labeler applies exactly the documented type mappings" { |
| 97 | + block="$(js_object_block typeToKind "$PR_LABELER")" |
| 98 | + count="$(printf '%s\n' "$block" | grep -c ":" || true)" |
| 99 | + [ "${count:-0}" -eq 5 ] |
| 100 | + for t in feat fix docs chore refactor; do |
| 101 | + printf '%s\n' "$block" | grep -qE "^[[:space:]]*$t:" |
| 102 | + done |
| 103 | + # Commented-out types must not count as mappings. |
| 104 | + for t in style perf test build ci revert; do |
| 105 | + ! printf '%s\n' "$block" | grep -qE "^[[:space:]]*$t:" |
| 106 | + done |
| 107 | +} |
| 108 | +
|
| 109 | +@test "pr-labeler subscribes to opened/reopened/synchronize and not edited" { |
| 110 | + count="$(code_lines < "$PR_LABELER" | grep -cF ' types: [opened, reopened, synchronize]' || true)" |
| 111 | + [ "${count:-0}" -eq 1 ] |
| 112 | + # 'edited' is deliberately omitted (see the comment above the trigger); it |
| 113 | + # must not creep back in as an executable line. |
| 114 | + ! code_lines < "$PR_LABELER" | grep -E '^[[:space:]]*types:' | grep -q 'edited' |
| 115 | +} |
| 116 | +
|
| 117 | +@test "pr-labeler is additive only and falls back to area/uncategorized" { |
| 118 | + lines="$(code_lines < "$PR_LABELER" | script_lines)" |
| 119 | +
|
| 120 | + count="$(printf '%s\n' "$lines" | grep -cF "toAdd.add('area/uncategorized')" || true)" |
| 121 | + [ "${count:-0}" -eq 1 ] |
| 122 | +
|
| 123 | + # Only labels not already present are sent, and only via addLabels: a |
| 124 | + # labeler that starts removing or replacing labels is a different tool. |
| 125 | + count="$(printf '%s\n' "$lines" | grep -cF '!existing.has(l)' || true)" |
| 126 | + [ "${count:-0}" -eq 1 ] |
| 127 | + count="$(printf '%s\n' "$lines" | grep -cF 'addLabels' || true)" |
| 128 | + [ "${count:-0}" -eq 1 ] |
| 129 | + ! printf '%s\n' "$lines" | grep -q 'removeLabel\|setLabels' |
| 130 | +} |
| 131 | +
|
| 132 | +@test "every label stale.yaml writes or matches resolves against labels.yml" { |
| 133 | + for key in stale-issue-label stale-pr-label close-issue-label close-pr-label; do |
| 134 | + value="$(stale_input "$key")" |
| 135 | + [ -n "$value" ] |
| 136 | + assert_labels_defined "$value" |
| 137 | + done |
| 138 | + assert_labels_defined "$(stale_exempt_list exempt-issue-labels)" |
| 139 | + assert_labels_defined "$(stale_exempt_list exempt-pr-labels)" |
| 140 | +} |
| 141 | +
|
| 142 | +@test "stale policy pins: label choices, exempt list sizes, day counts, write cap" { |
| 143 | + [ "$(stale_input stale-issue-label)" = "lifecycle/stale" ] |
| 144 | + [ "$(stale_input stale-pr-label)" = "lifecycle/stale" ] |
| 145 | + [ "$(stale_input close-issue-label)" = "lifecycle/rotten" ] |
| 146 | + [ "$(stale_input close-pr-label)" = "lifecycle/rotten" ] |
| 147 | +
|
| 148 | + count="$(stale_exempt_list exempt-issue-labels | wc -l | tr -d ' ')" |
| 149 | + [ "$count" -eq 12 ] |
| 150 | + count="$(stale_exempt_list exempt-pr-labels | wc -l | tr -d ' ')" |
| 151 | + [ "$count" -eq 4 ] |
| 152 | +
|
| 153 | + # The renewal path: frozen exempts both; an accepted issue or a held PR is |
| 154 | + # deliberate, not stale. |
| 155 | + stale_exempt_list exempt-issue-labels | grep -qxF 'lifecycle/frozen' |
| 156 | + stale_exempt_list exempt-issue-labels | grep -qxF 'triage/accepted' |
| 157 | + stale_exempt_list exempt-pr-labels | grep -qxF 'lifecycle/frozen' |
| 158 | + stale_exempt_list exempt-pr-labels | grep -qxF 'do-not-merge/hold' |
| 159 | +
|
| 160 | + [ "$(stale_input days-before-stale)" = "60" ] |
| 161 | + [ "$(stale_input days-before-close)" = "14" ] |
| 162 | + [ "$(stale_input operations-per-run)" = "100" ] |
| 163 | + [ "$(stale_input remove-stale-when-updated)" = "true" ] |
| 164 | +} |
0 commit comments