Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/babel/blob/main/tests/run.sh # babel aien-dev/babel Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `babel / tests / run.sh` 296 lines ยท 17951 bytes Raw Blame History 1 #!/bin/sh 2 # babel end-to-end tests. Coordination script only: every check here inspects babel's 3 # output with independent tools (cmp, python3 -m json.tool); it implements nothing. 4 # usage: BABEL_ADAPTERS= tests/run.sh [targets...] 5 # Optional: ZIG=, GO= when those toolchains are not on PATH. 6 set -u 7 here=$(cd "$(dirname "$0")/.." && pwd) 8 B="$here/build/babel" 9 : "${BABEL_ADAPTERS:?set BABEL_ADAPTERS to a built omega-mv adapter clone}" 10 targets=${*:-rust c python cpp zig go} 11 work=$(mktemp -d) 12 trap 'rm -rf "$work"' EXIT 13 fails=0 14 ok() { echo "PASS $*"; } 15 bad() { echo "FAIL $*"; fails=$((fails + 1)); } 16 17 # 1. lint: a statement starting with an unknown name silently becomes an assignment in 18 # OM-2, so every bare statement word must be a defined function or a host word. 19 awk '/^fn /{print $2}' "$here"/build/babel.om | sort -u > "$work/fns" 20 cat "$here"/src/*.om | awk '$1 ~ /^[a-z][a-z0-9]*$/ && $2 != "=" {print $1}' | sort -u \ 21 | grep -vxF -f "$work/fns" \ 22 | grep -vxE 'if|while|for|end|else|ret|put|break|continue|note|text|global|fn|elif|exit|reset' > "$work/lint" 23 if [ -s "$work/lint" ]; then bad "lint: unknown statement words: $(tr '\n' ' ' < "$work/lint")"; else ok "lint"; fi 24 25 # 2. usage paths 26 "$B" --version > /dev/null; [ $? -eq 0 ] && ok "--version" || bad "--version" 27 "$B" > /dev/null 2>&1; [ $? -eq 2 ] && ok "no path exits 2" || bad "no path exits 2" 28 "$B" "$here/examples/sample" --adapters "$BABEL_ADAPTERS" < /dev/null > /dev/null 2>&1; r=$? 29 [ $r -eq 64 ] && ok "no --to without a terminal exits 64" || bad "no --to without a terminal exits 64 (got $r)" 30 31 # 3. every target on the sample: exit status, verdict counts, JSON validity, independent cmp 32 cp -r "$here/examples/sample" "$work/sample" 33 for t in $targets; do 34 (cd "$work" && "$B" sample --to "$t" --adapters "$BABEL_ADAPTERS" --json > "$work/$t.json" 2> "$work/$t.err") 35 r=$? 36 [ $r -eq 0 ] && ok "$t: exit 0" || { bad "$t: exit $r"; cat "$work/$t.err"; } 37 if python3 -m json.tool "$work/$t.json" > /dev/null && python3 -m json.tool "$work/sample.babel/report.json" > /dev/null; then 38 ok "$t: --json and report.json parse" 39 else 40 bad "$t: JSON does not parse" 41 fi 42 s=$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1]))["summary"]; print(d["translated"],d["unsupported"],d["missing_implementation"],d["fail"],d["not_run"],d["absorbed"])' "$work/sample.babel/report.json") 43 [ "$s" = "5 7 5 0 0 2" ] && ok "$t: verdicts 5 TRANSLATED / 7 UNSUPPORTED / 5 MISSING / 0 FAIL / 0 NOT_RUN / 2 ABSORBED" || bad "$t: verdicts $s" 44 f0=$fails 45 for u in fold square add3 poly mix6; do 46 d="$work/sample.babel/diff/$u" 47 n=$(head -1 "$d/vectors.txt" | cut -d' ' -f2) 48 if [ -s "$d/c-original.out" ] && cmp -s "$d/c-original.out" "$d/omega.out" && cmp -s "$d/c-original.out" "$d/target.out" \ 49 && [ "$(wc -l < "$d/c-original.out")" -eq "$n" ]; then 50 : 51 else 52 bad "$t: $u three legs not byte-identical (independent cmp)" 53 fi 54 done 55 [ $fails -eq $f0 ] && ok "$t: independent cmp: 3 legs byte-identical, one line per vector, 5 units" 56 done 57 58 # 4. negative control: a cc wrapper that changes fold's original C (- k becomes + k) 59 # when it compiles the differential oracle. babel must report FAIL, never TRANSLATED. 60 mkdir -p "$work/bin" 61 realcc=$(command -v cc) 62 cat > "$work/bin/cc" < "$work/mut.c" 68 grep -q 'h \* k + h + k' "$work/mut.c" || { echo "mutation did not apply" >&2; exit 99; } 69 set -- \$(for b in "\$@"; do [ "\$b" = "\$a" ] && echo "$work/mut.c" || echo "\$b"; done) 70 ;; 71 esac 72 done 73 exec "$realcc" "\$@" 74 EOF 75 chmod +x "$work/bin/cc" 76 (cd "$work" && PATH="$work/bin:$PATH" "$B" sample --to c --adapters "$BABEL_ADAPTERS" > "$work/neg.out" 2>&1) 77 r=$? 78 v=$(python3 -c 'import json,sys 79 for u in json.load(open(sys.argv[1]))["units"]: 80 if u.get("name") == "fold": print(u["verdict"], u.get("reason",""))' "$work/sample.babel/report.json") 81 case "$v" in 82 FAIL*differs*) ok "negative control: mutated oracle gives FAIL (exit $r): $v" ;; 83 *) bad "negative control: expected FAIL, got exit $r: $v" ;; 84 esac 85 [ $r -eq 1 ] && ok "negative control: exit 1" || bad "negative control: exit $r, expected 1" 86 87 # 5. completion record (policy complete-v1) and --complete exit status. 88 # comp DIR prints "state denominator covered n_open" from DIR/report.json and validates the JSON; 89 # openl DIR prints one "kind|item|name|verdict|reason" line per open item. 90 comp() { 91 python3 -m json.tool "$1/report.json" > /dev/null || { echo "JSON-INVALID"; return; } 92 python3 -c 'import json,sys; c=json.load(open(sys.argv[1]))["completion"]; assert c["policy"]=="complete-v1"; print(c["state"],c["denominator"],c["covered"],len(c["open"]))' "$1/report.json" 93 } 94 openl() { 95 python3 -c 'import json,sys 96 for o in json.load(open(sys.argv[1]))["completion"]["open"]: print("|".join(str(o[k]) for k in ("kind","item","name","verdict","reason")))' "$1/report.json" 97 } 98 # (f) legacy: no --complete, the sample still exits 0; the record is computed but not enforced 99 rm -rf "$work/sample.babel" 100 (cd "$work" && "$B" sample --to c --adapters "$BABEL_ADAPTERS" > "$work/cf.out" 2>&1); r=$? 101 s=$(comp "$work/sample.babel") 102 e=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["completion"]["enforced"])' "$work/sample.babel/report.json") 103 [ $r -eq 0 ] && [ "${s%% *}" = PARTIAL ] && [ "$e" = False ] && ok "complete (f): sample without --complete exits 0, record $s, enforced $e" \ 104 || bad "complete (f): exit $r, record $s, enforced $e" 105 # (a) the sample under --complete is PARTIAL (exit 3) and names its open items 106 rm -rf "$work/sample.babel" 107 (cd "$work" && "$B" sample --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/ca.out" 2>&1); r=$? 108 s=$(comp "$work/sample.babel") 109 openl "$work/sample.babel" > "$work/ca.open" 110 if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "${s##* }" -gt 0 ] \ 111 && grep -q '^unit|src/mix.c:15-17|zero|UNSUPPORTED(68)|' "$work/ca.open" \ 112 && grep -q '^unit|tools/report.py:1|None|MISSING_IMPLEMENTATION|' "$work/ca.open" \ 113 && grep -q '^file|README.txt|None|MISSING_IMPLEMENTATION|' "$work/ca.open" \ 114 && grep -q '^## Completion' "$work/sample.babel/REPORT.md" && grep -q 'State: \*\*PARTIAL\*\*' "$work/sample.babel/REPORT.md"; then 115 ok "complete (a): sample --complete exits 3, record $s (state den covered open), open list names zero, tools/report.py, README.txt" 116 else 117 bad "complete (a): exit $r, record $s"; cat "$work/ca.open" 118 fi 119 # (d) negative control: the mutated oracle under --complete is FAILED (exit 1) 120 rm -rf "$work/sample.babel" 121 (cd "$work" && PATH="$work/bin:$PATH" "$B" sample --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cd.out" 2>&1); r=$? 122 s=$(comp "$work/sample.babel") 123 openl "$work/sample.babel" > "$work/cd.open" 124 if [ $r -eq 1 ] && [ "${s%% *}" = FAILED ] && grep -q '^unit|src/hash.c:3-5|fold|FAIL|' "$work/cd.open" \ 125 && grep -q '^check|src/hash.c:3-5|fold|FAIL|differential leg: Omega' "$work/cd.open"; then 126 ok "complete (d) negative control: mutated oracle under --complete exits 1, record $s, fold unit and Omega leg FAIL" 127 else 128 bad "complete (d): exit $r, record $s"; cat "$work/cd.open" 129 fi 130 # (b) examples/complete (three S1 functions + the stdint include) is COMPLETE (exit 0) 131 cp -r "$here/examples/complete" "$work/complete" 132 for t in c rust; do 133 rm -rf "$work/complete.babel" 134 (cd "$work" && "$B" complete --to "$t" --adapters "$BABEL_ADAPTERS" --complete > "$work/cb.out" 2>&1); r=$? 135 s=$(comp "$work/complete.babel") 136 set -- $s 137 if [ $r -eq 0 ] && [ "$1" = COMPLETE ] && [ "$2" -eq "$3" ] && [ "$4" -eq 0 ] && grep -q 'State: \*\*COMPLETE\*\*' "$work/complete.babel/REPORT.md"; then 138 ok "complete (b) $t: examples/complete --complete exits 0, record $s, covered == denominator" 139 else 140 bad "complete (b) $t: exit $r, record $s"; openl "$work/complete.babel" 141 fi 142 done 143 # (c) negative controls: the same input plus one hidden file, or plus one symbolic link, is PARTIAL (exit 3) 144 for kind in hidden symlink; do 145 rm -rf "$work/cc" "$work/cc.babel" 146 cp -r "$here/examples/complete" "$work/cc" 147 if [ $kind = hidden ]; then 148 echo 'uint64_t ghost(uint64_t x) { return x; }' > "$work/cc/src/.ghost.c"; want='^skipped|src/.ghost.c|None|SKIPPED|hidden' 149 else 150 ln -s arith.c "$work/cc/src/alias.c"; want='^skipped|src/alias.c|None|SKIPPED|symbolic link' 151 fi 152 (cd "$work" && "$B" cc --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/ccc.out" 2>&1); r=$? 153 s=$(comp "$work/cc.babel") 154 openl "$work/cc.babel" > "$work/cc.open" 155 if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "$(wc -l < "$work/cc.open")" -eq 1 ] && grep -q "$want" "$work/cc.open"; then 156 ok "complete (c) negative control ($kind): exits 3, record $s, the only open item: $(cut -d'|' -f2,4 "$work/cc.open")" 157 else 158 bad "complete (c) $kind: exit $r, record $s"; cat "$work/cc.open" 159 fi 160 done 161 # (e) a missing target toolchain is BLOCKED (exit 4) 162 rm -rf "$work/complete.babel" 163 (cd "$work" && ZIG=/nonexistent "$B" complete --to zig --adapters "$BABEL_ADAPTERS" --complete > "$work/ce.out" 2>&1); r=$? 164 s=$(comp "$work/complete.babel") 165 openl "$work/complete.babel" > "$work/ce.open" 166 if [ $r -eq 4 ] && [ "${s%% *}" = BLOCKED ] && grep -q '^toolchain|zig|None|NOT_RUN|' "$work/ce.open" \ 167 && grep -q '^check|src/arith.c:3-5|blend|NOT_RUN|differential leg: target zig did not run' "$work/ce.open"; then 168 ok "complete (e): ZIG=/nonexistent --to zig --complete exits 4, record $s" 169 else 170 bad "complete (e): exit $r, record $s"; cat "$work/ce.open" 171 fi 172 173 # 6. --exclude manifest. exl DIR prints "excluded_count walked_as" and one "kind|item|reason|matched_by" 174 # line per excluded item. Manifests live in $work, outside the walked tree, unless a test says so. 175 exl() { 176 python3 -c 'import json,sys 177 c=json.load(open(sys.argv[1]))["completion"]; m=c["manifest"] 178 assert c["excluded_count"]==len(c["excluded"]) 179 print(c["excluded_count"], m["walked_as"] if m else "-") 180 for x in c["excluded"]: assert x["verdict"]=="EXCLUDED"; print("|".join(str(x[k]) for k in ("kind","item","reason","matched_by")))' "$1/report.json" 181 } 182 rm -rf "$work/cg" "$work/cg.babel" 183 cp -r "$here/examples/complete" "$work/cg" 184 echo 'notes for people' > "$work/cg/README.txt" 185 # (g) README.txt makes it PARTIAL (exit 3); a manifest excluding it with a reason makes it COMPLETE (exit 0) 186 (cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cg0.out" 2>&1); r0=$? 187 s0=$(comp "$work/cg.babel") 188 printf '# documentation is not translated\n\nREADME.txt human notes, no code \n' > "$work/g.exclude" 189 (cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete --exclude g.exclude --no-color > "$work/cg1.out" 2>&1); r1=$? 190 s1=$(comp "$work/cg.babel") 191 exl "$work/cg.babel" > "$work/cg1.exl" 192 sha8=$(sha256sum "$work/g.exclude" | cut -c1-8) 193 set -- $s0; d0=$2 194 set -- $s1 195 if [ $r0 -eq 3 ] && [ "${s0%% *}" = PARTIAL ] && [ $r1 -eq 0 ] && [ "$1" = COMPLETE ] && [ "$2" -eq "$d0" ] && [ "$4" -eq 0 ] \ 196 && [ "$(head -1 "$work/cg1.exl")" = "1 None" ] && grep -qx 'file|README.txt|human notes, no code|README.txt' "$work/cg1.exl" \ 197 && grep -q "COMPLETE $3 of $2 items covered, 1 excluded by manifest $sha8" "$work/cg1.out" \ 198 && grep -qF '| file | `README.txt` | EXCLUDED | `README.txt` (line 3) | human notes, no code |' "$work/cg.babel/REPORT.md"; then 199 ok "complete (g): +README.txt exits $r0 ($s0); --exclude README.txt exits 0, record $s1, denominator unchanged $d0, excluded_count 1, manifest $sha8" 200 else 201 bad "complete (g): exits $r0/$r1, records $s0 / $s1"; cat "$work/cg1.exl" "$work/cg1.out" 202 fi 203 # (g2) the manifest inside the walked tree is counted (covered); a hidden non-source entry may be excluded 204 printf 'x\n' > "$work/cg/.gitignore" 205 printf 'README.txt human notes\n.gitignore VCS metadata, no code\n' > "$work/cg/babel.exclude" 206 (cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete --exclude cg/babel.exclude > "$work/cg2.out" 2>&1); r=$? 207 s=$(comp "$work/cg.babel") 208 exl "$work/cg.babel" > "$work/cg2.exl" 209 set -- $s 210 if [ $r -eq 0 ] && [ "$1" = COMPLETE ] && [ "$2" -eq $((d0 + 2)) ] && [ "$3" -eq "$d0" ] && [ "$(head -1 "$work/cg2.exl")" = "2 babel.exclude" ] \ 211 && grep -qx 'skipped|.gitignore|VCS metadata, no code|.gitignore' "$work/cg2.exl"; then 212 ok "complete (g2): in-tree manifest covered, .gitignore excluded: exits 0, record $s, excluded_count 2, walked_as babel.exclude" 213 else 214 bad "complete (g2): exit $r, record $s"; cat "$work/cg2.exl"; openl "$work/cg.babel" 215 fi 216 rm -f "$work/cg/.gitignore" "$work/cg/babel.exclude" 217 # (h)-(k) negative controls: each bad manifest exits 2 with a line diagnostic and writes no output folder 218 xneg() { # name manifest-text expected-stderr 219 rm -rf "$work/cg.babel" 220 printf "$2" > "$work/$1.exclude" 221 (cd "$work" && "$B" cg --to c --adapters "$BABEL_ADAPTERS" --complete --exclude "$1.exclude" > "$work/$1.out" 2>&1); r=$? 222 if [ $r -eq 2 ] && grep -qF "$3" "$work/$1.out" && [ ! -e "$work/cg.babel" ]; then 223 ok "complete ($1) negative control: exits 2, no output folder: $(grep -- '--exclude' "$work/$1.out" | head -1)" 224 else 225 bad "complete ($1): exit $r, expected 2 and /$3/"; cat "$work/$1.out" 226 fi 227 } 228 xneg h 'src/arith.c not wanted\n' "line 1: entry names source babel splits, which cannot be excluded: src/arith.c -> src/arith.c" 229 xneg h2 'src/ the whole tree\n' "line 1: entry names source babel splits, which cannot be excluded: src -> src/arith.c" 230 xneg i 'README.txt human notes\nnothere.txt gone\n' "line 2: entry matches nothing in the walked tree (stale manifest): nothere.txt" 231 xneg j '# no reason below\nREADME.txt\n' "line 2: entry has no reason (each line is ): README.txt" 232 xneg j2 'src/*.txt glob\n' "line 1: globs are not supported" 233 echo 'uint64_t ghost(uint64_t x) { return x; }' > "$work/cg/src/.ghost.c" 234 xneg k 'README.txt human notes\nsrc/.ghost.c hidden scratch\n' "line 2: entry names source babel splits, which cannot be excluded: src/.ghost.c -> src/.ghost.c" 235 rm -f "$work/cg/src/.ghost.c" 236 ln -s arith.c "$work/cg/src/alias.c" 237 xneg k2 'README.txt human notes\nsrc/alias.c a link\n' "line 2: entry names source babel splits, which cannot be excluded: src/alias.c -> src/alias.c" 238 rm -f "$work/cg/src/alias.c" 239 240 # 7. completion gaps: other non-regular file, unreadable directory, missing cc, failing generated test 241 rm -rf "$work/cf" "$work/cf.babel" 242 cp -r "$here/examples/complete" "$work/cf" 243 mkfifo "$work/cf/src/pipe" 244 (cd "$work" && "$B" cf --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cf7.out" 2>&1); r=$? 245 s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/cf7.open" 246 if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "$(wc -l < "$work/cf7.open")" -eq 1 ] && grep -q '^skipped|src/pipe|None|SKIPPED|not a regular file' "$work/cf7.open"; then 247 ok "complete gap (fifo): exits 3, record $s, the only open item: $(cut -d'|' -f2,4,5 "$work/cf7.open")" 248 else 249 bad "complete gap (fifo): exit $r, record $s"; cat "$work/cf7.open" 250 fi 251 rm -f "$work/cf/src/pipe" 252 if [ "$(id -u)" = 0 ]; then 253 echo "NOT_RUN (root) complete gap (unreadable directory): root reads a mode-000 directory" 254 else 255 mkdir "$work/cf/locked"; echo x > "$work/cf/locked/a.txt"; chmod 000 "$work/cf/locked" 256 trap 'chmod 755 "$work/cf/locked" 2>/dev/null; rm -rf "$work"' EXIT 257 (cd "$work" && "$B" cf --to c --adapters "$BABEL_ADAPTERS" --complete > "$work/cu.out" 2>&1); r=$? 258 chmod 755 "$work/cf/locked"; rm -rf "$work/cf/locked" 259 s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/cu.open" 260 if [ $r -eq 3 ] && [ "${s%% *}" = PARTIAL ] && [ "$(wc -l < "$work/cu.open")" -eq 1 ] && grep -q '^skipped|locked|None|SKIPPED|directory could not be read' "$work/cu.open"; then 261 ok "complete gap (unreadable directory): exits 3, record $s, the only open item: $(cut -d'|' -f2,4,5 "$work/cu.open")" 262 else 263 bad "complete gap (unreadable directory): exit $r, record $s"; cat "$work/cu.open" 264 fi 265 fi 266 # a PATH with every tool but cc: the oracle cannot be built, so the run is BLOCKED (exit 4) 267 mkdir "$work/nocc" 268 for d in $(echo "$PATH" | tr ':' ' '); do 269 [ -d "$d" ] || continue 270 for f in "$d"/*; do 271 n=${f##*/}; [ "$n" = cc ] && continue; [ -e "$work/nocc/$n" ] || [ -L "$work/nocc/$n" ] || ln -s "$f" "$work/nocc/$n" 272 done 273 done 274 (cd "$work" && PATH="$work/nocc" "$B" cf --to python --adapters "$BABEL_ADAPTERS" --complete > "$work/cn.out" 2>&1); r=$? 275 s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/cn.open" 276 if [ $r -eq 4 ] && [ "${s%% *}" = BLOCKED ] && grep -q '^toolchain|cc|None|NOT_RUN|cc was not found' "$work/cn.open" && ! grep -q '|FAIL|' "$work/cn.open"; then 277 ok "complete gap (no cc): exits 4, record $s, $(grep -c NOT_RUN "$work/cn.open") NOT_RUN items incl. toolchain cc" 278 else 279 bad "complete gap (no cc): exit $r, record $s"; cat "$work/cn.open" 280 fi 281 # a cargo that fails only "cargo test": the generated target test is the one FAIL (exit 1) 282 realcargo=$(command -v cargo) 283 mkdir "$work/cbin" 284 printf '#!/bin/sh\nif [ "$1" = test ]; then echo "shim: cargo test forced to fail" >&2; exit 101; fi\nexec "%s" "$@"\n' "$realcargo" > "$work/cbin/cargo" 285 chmod +x "$work/cbin/cargo" 286 (cd "$work" && PATH="$work/cbin:$PATH" "$B" cf --to rust --adapters "$BABEL_ADAPTERS" --complete > "$work/ct.out" 2>&1); r=$? 287 s=$(comp "$work/cf.babel"); openl "$work/cf.babel" > "$work/ct.open" 288 if [ $r -eq 1 ] && [ "${s%% *}" = FAILED ] && [ "$(wc -l < "$work/ct.open")" -eq 1 ] && grep -q '^check|diff/test.log|None|FAIL|generated target test failed (see diff/test.log), exit 101' "$work/ct.open"; then 289 ok "complete gap (generated test): cargo test fails, exits 1, record $s, the only open item: $(cut -d'|' -f2,4,5 "$work/ct.open")" 290 else 291 bad "complete gap (generated test): exit $r, record $s"; cat "$work/ct.open" 292 fi 293 echo "NOT_RUN complete gap (lstat failure, skip reason 6): no unprivileged way to make lstat fail on an entry getdents returned" 294 295 echo "failures: $fails" 296 [ $fails -eq 0 ]