summaryrefslogtreecommitdiff
path: root/tests/conventional-commits.test.sh
blob: 042b8191e7a7e9ad6865696e8c7e726bf82bafc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env bash
# Enforce Conventional Commits 1.0.0 on every commit subject in this repo.
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "ok: $*"; }

# Same rule as scripts/lib/note-helpers.mjs isConventionalCommitSubject
RE='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-zA-Z0-9._/-]+\))?!?: .+'

mapfile -t SUBJECTS < <(git log --format=%s)
[[ "${#SUBJECTS[@]}" -gt 0 ]] || fail "no commits"

bad=0
for s in "${SUBJECTS[@]}"; do
  if [[ ! "$s" =~ $RE ]]; then
    echo "non-conventional: $s" >&2
    bad=1
  else
    pass "commit: $s"
  fi
done
[[ "$bad" -eq 0 ]] || fail "one or more commits are not Conventional Commits"

# Unit helper agrees with git history
node --input-type=module -e '
import { isConventionalCommitSubject } from "./scripts/lib/note-helpers.mjs";
import { execSync } from "node:child_process";
const subjects = execSync("git log --format=%s", { encoding: "utf8" })
  .trim().split("\n").filter(Boolean);
for (const s of subjects) {
  if (!isConventionalCommitSubject(s)) {
    console.error("helper rejected:", s);
    process.exit(1);
  }
}
'

echo "All conventional-commit checks passed."