#!/bin/sh # Self-test for the three gate scripts. It builds small fake trees in a temporary # directory and checks that each script passes the good tree and fails the bad ones. # Do not edit: this file defines the required behaviour of the scripts. set -eu here=$(cd "$(dirname "$0")" && pwd) tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT fails=0 expect() { # expect pass|fail NAME SCRIPT ROOT want="$1"; name="$2"; script="$3"; root="$4" if sh "$here/$script" "$root" >/dev/null 2>&1; then got=pass; else got=fail; fi if [ "$got" != "$want" ]; then echo "test-gate-scripts: $name: expected $want, got $got" >&2 fails=$((fails + 1)) fi } manifest() { # manifest DIR NAME [DEPENDENCY-LINES...] dir="$1"; name="$2"; shift 2 mkdir -p "$dir/src" { printf '[package]\nname = "%s"\nversion = "0.1.0"\n\n[dependencies]\n' "$name" for line in "$@"; do printf '%s\n' "$line"; done } > "$dir/Cargo.toml" } tree() { # tree ROOT: a good workspace with proto, loopd and brokerd root="$1" mkdir -p "$root/docs" printf '[workspace]\nmembers = ["crates/*"]\n\n[workspace.dependencies]\nproto = { path = "crates/proto" }\nserde = { version = "1", features = ["derive"] }\n' > "$root/Cargo.toml" printf '# Dependencies\n\n| Crate | Why |\n|---|---|\n| `serde` | types |\n' > "$root/docs/dependencies.md" manifest "$root/crates/proto" proto 'serde.workspace = true' manifest "$root/crates/loopd" loopd 'proto.workspace = true' manifest "$root/crates/brokerd" brokerd 'proto = { workspace = true }' 'serde.workspace = true' } lines() { # lines N FILE mkdir -p "$(dirname "$2")" i=0; : > "$2" while [ "$i" -lt "$1" ]; do echo "// line" >> "$2"; i=$((i + 1)); done } # check-lines.sh tree "$tmp/l-ok"; lines 500 "$tmp/l-ok/crates/loopd/src/lib.rs" expect pass "500 lines is allowed" check-lines.sh "$tmp/l-ok" tree "$tmp/l-bad"; lines 501 "$tmp/l-bad/crates/loopd/src/deep/mod.rs" expect fail "501 lines in a nested file" check-lines.sh "$tmp/l-bad" tree "$tmp/l-test"; lines 501 "$tmp/l-test/crates/proto/tests/big.rs" expect fail "501 lines in a test file" check-lines.sh "$tmp/l-test" tree "$tmp/l-tgt"; lines 501 "$tmp/l-tgt/crates/proto/target/debug/gen.rs" expect pass "files under target/ are ignored" check-lines.sh "$tmp/l-tgt" # check-crate-deps.sh tree "$tmp/c-ok" expect pass "roles depend only on proto" check-crate-deps.sh "$tmp/c-ok" tree "$tmp/c-role"; manifest "$tmp/c-role/crates/loopd" loopd 'proto.workspace = true' 'brokerd.workspace = true' expect fail "role depends on another role" check-crate-deps.sh "$tmp/c-role" tree "$tmp/c-tbl"; manifest "$tmp/c-tbl/crates/loopd" loopd 'brokerd = { path = "../brokerd" }' expect fail "role depends on another role by path" check-crate-deps.sh "$tmp/c-tbl" tree "$tmp/c-dev"; printf '\n[dev-dependencies]\nbrokerd.workspace = true\n' >> "$tmp/c-dev/crates/loopd/Cargo.toml" expect fail "role dev-depends on another role" check-crate-deps.sh "$tmp/c-dev" tree "$tmp/c-proto"; manifest "$tmp/c-proto/crates/proto" proto 'loopd.workspace = true' expect fail "proto depends on a role" check-crate-deps.sh "$tmp/c-proto" # check-dep-docs.sh tree "$tmp/d-ok" expect pass "every dependency is documented" check-dep-docs.sh "$tmp/d-ok" tree "$tmp/d-miss"; printf 'rand = "0.9"\n' >> "$tmp/d-miss/Cargo.toml" expect fail "workspace dependency without a docs row" check-dep-docs.sh "$tmp/d-miss" tree "$tmp/d-prose"; printf 'rand = "0.9"\n' >> "$tmp/d-prose/Cargo.toml"; printf '\nWe do not use rand.\n' >> "$tmp/d-prose/docs/dependencies.md" expect fail "a mention in prose is not a table row" check-dep-docs.sh "$tmp/d-prose" tree "$tmp/d-loose"; manifest "$tmp/d-loose/crates/loopd" loopd 'proto.workspace = true' 'rand = "0.9"' expect fail "crate declares a dependency outside the workspace table" check-dep-docs.sh "$tmp/d-loose" if [ "$fails" -ne 0 ]; then echo "test-gate-scripts: $fails failure(s)" >&2 exit 1 fi echo "test-gate-scripts: ok"