Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(ubicloud): lint awk print lists for the one-true-awk; usage runs under every awk on PATH
garrytanc5f99990814c
diff --git a/test/scripts/awk-portability.test.ts b/test/scripts/awk-portability.test.tsnew file mode 100644index 0000000000..3ffc45ec1b--- /dev/null+++ b/test/scripts/awk-portability.test.ts@@ -0,0 +1,54 @@+/**+ * Operator scripts run on macOS too (the interrupted-run check runs `ubi-runner.sh usage` on a Mac), where awk is the+ * one-true-awk (BWK). Its grammar rejects a comparison in an unparenthesized print or printf argument list: in+ * `printf "%s", o == me ? "y" : ""` the `==` is a syntax error, while gawk, mawk and busybox accept it. This lint+ * finds that construct in the awk programs of scripts/ubicloud, and `usage` runs under every awk on PATH, the+ * one-true-awk (`original-awk`) included where it is installed.+ */+import { describe, expect, it } from 'bun:test';+import { readdirSync, readFileSync } from 'node:fs';+import { join, resolve } from 'node:path';++const ROOT = resolve(import.meta.dir, '..', '..');+const DIR = join(ROOT, 'scripts/ubicloud');++/** Each print/printf whose unparenthesized argument list holds a comparison at paren depth 0, as `line: text`. */+function bwkPrintComparisons(source: string): string[] {+ const found: string[] = [];+ for (const match of source.matchAll(/\bprintf?\b/g)) {+ let i = match.index! + match[0].length;+ while (source[i] === ' ' || source[i] === '\t') i++;+ if (source[i] === '(' || source[i] === "'") continue;+ let depth = 0;+ for (; i < source.length; i++) {+ const c = source[i]!;+ const two = source.slice(i, i + 2);+ if (c === '"') { for (i++; i < source.length && source[i] !== '"'; i++) if (source[i] === '\\') i++; continue; }+ if (c === '(') { depth++; continue; }+ if (c === ')') { if (depth === 0) break; depth--; continue; }+ if (depth > 0) continue;+ if (['==', '!=', '<=', '>=', '!~'].includes(two) || c === '<' || c === '~') {+ const line = source.slice(0, match.index).split('\n').length;+ found.push(`${line}: ${source.split('\n')[line - 1]!.trim()}`);+ break;+ }+ if (c === ';' || c === '}' || c === '\n' || c === '|' || c === '>' || c === "'") break;+ }+ }+ return found;+}++describe('awk portability (one-true-awk) in scripts/ubicloud', () => {+ it('flags a comparison in an unparenthesized print list and passes the parenthesized form', () => {+ expect(bwkPrintComparisons(`awk '{ printf "%s%s\\n", o, o == me ? " (you)" : "" | "sort" }'`)).toHaveLength(1);+ expect(bwkPrintComparisons(`awk '{ print $1, $2 != "" }'`)).toHaveLength(1);+ expect(bwkPrintComparisons(`awk '{ printf("%s%s\\n", o, (o == me) ? " (you)" : "") | "sort" }'`)).toEqual([]);+ expect(bwkPrintComparisons(`awk '$2==n {print $1}'`)).toEqual([]);+ expect(bwkPrintComparisons(`printf '%s\\n' "$rows" | awk -v o="$OWNER" 'NF && $3!=o {print $3}'`)).toEqual([]);+ });++ it('no awk program in scripts/ubicloud puts a comparison in an unparenthesized print list', () => {+ const hits = readdirSync(DIR).filter(f => f.endsWith('.sh')).flatMap(f => bwkPrintComparisons(readFileSync(join(DIR, f), 'utf8')).map(h => `${f}:${h}`));+ expect(hits).toEqual([]);+ });+});diff --git a/test/scripts/ubi-runner.test.ts b/test/scripts/ubi-runner.test.tsindex d51ad2310e..1515c0f9c4 100644--- a/test/scripts/ubi-runner.test.ts+++ b/test/scripts/ubi-runner.test.ts@@ -222,6 +222,25 @@ describe("stale sweep and ownership", () => {expect(usage).toMatch(/^\(other\)\s+1\s+2$/m);expect(usage).toMatch(/^total\s+5\s+46$/m);});++ // macOS runs the one-true-awk, which rejected usage's vCPU loop (a comparison in an unparenthesized printf list).+ it("usage prints the same table under every awk on PATH, the one-true-awk included where installed", async () => {+ const awks = [["original-awk"], ["mawk"], ["gawk"], ["busybox", "awk"]].filter(([bin]) => Bun.which(bin!));+ expect(awks.length).toBeGreaterThan(0);+ const tables: string[] = [];+ for (const awk of awks) {+ serve({ seed });+ const shim = join(dir, `awk-${awk[0]}`);+ execFileSync("mkdir", ["-p", shim]);+ writeFileSync(join(shim, "awk"), `#!/bin/sh\nexec ${awk.map((a) => Bun.which(a) ?? a).join(" ")} "$@"\n`, { mode: 0o755 });+ const usage = await run(["usage"], { UBI_OWNER: "t1", PATH: `${shim}:${process.env.PATH ?? "/usr/bin:/bin"}` });+ expect({ awk: awk.join(" "), code: usage.code, stderr: usage.stderr }).toEqual({ awk: awk.join(" "), code: 0, stderr: "" });+ tables.push(usage.stdout);+ mock?.stop();+ }+ expect(tables[0]).toMatch(/^t1\s+2\s+24\s+\(you\)$/m);+ expect(new Set(tables).size).toBe(1);+ });});// `pack` from a checkout on a branch named like `git init`'s default branch