Files
gstack/test/bash32-case-in-substitution.test.ts
T
e5107cc7e3 fix(doctor): parse the launch probe under macOS /bin/bash 3.2 (#3601) (#3753)
* fix(doctor): parse the launch probe under macOS /bin/bash 3.2 (#3601)

bash 3.2 ends a $( ) at the ) of a case pattern that has no leading (, so
macOS /bin/bash could not parse bin/gstack-launch-probe.sh and gstack-doctor
reported the probe missing. The pattern takes its POSIX leading (. A static
scanner, which Linux CI runs, fails any tracked shell script with a bare case
pattern inside a substitution, and the probe test parses the file with
/bin/bash.

Co-authored-by: 김보경 <rlaqhrud6263@naver.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* changelog: fragment for the bash 3.2 launch probe fix (#3601)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(launch-probe): say the /bin/bash parse case discriminates only on macOS

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* changelog: the probe parse test catches #3601 only on a Mac

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test: move the /bin/bash parse case out of the windows-smoke probe test (#3601)

Main's windows-smoke runs test/gstack-launch-probe.test.ts, and a hardcoded
/bin/bash path takes a file out of the Windows-safe set, so the merged tree
failed windows-native-workflows. The case now lives in
test/bash32-case-in-substitution.test.ts with the Linux scan.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: 김보경 <rlaqhrud6263@naver.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-11-05 00:50:43 +00:00

63 lines
3.8 KiB
TypeScript

/**
* #3601: macOS /bin/bash 3.2 cannot parse a case pattern without its leading
* ( inside $( ), and Linux CI's bash 5 can, so bin/gstack-launch-probe.sh
* shipped a parse error that made gstack-doctor call the probe "missing".
* The repo-wide case is the guard Linux CI can run; the last case parses the
* probe with /bin/bash, which is bash 3.2 on macOS. It lives here, not in
* test/gstack-launch-probe.test.ts, because that file runs in windows-smoke and
* a hardcoded /bin/bash path takes a file out of the Windows-safe set.
*/
import { describe, expect, test } from 'bun:test';
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { scanBash32CaseInSubstitution } from './helpers/bash32-case-scan';
const ROOT = path.resolve(import.meta.dir, '..');
const PROBE = path.join(ROOT, 'bin/gstack-launch-probe.sh');
function trackedShellScripts(): string[] {
const files = spawnSync('git', ['ls-files', '-z'], { cwd: ROOT, encoding: 'utf8', timeout: 30_000 }).stdout.split('\0').filter(Boolean);
return files.filter(rel => {
if (/\.(md|ts|js|json|ya?ml|txt|tmpl)$/.test(rel)) return false;
const file = path.join(ROOT, rel);
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) return false;
const first = fs.readFileSync(file, 'utf8').slice(0, 200).split('\n')[0];
if (/^#!.*\b(zsh|python3?|node|bun|perl|ruby)\b/.test(first)) return false;
return rel.endsWith('.sh') || /^#!.*\b(bash|sh)\b/.test(first);
});
}
describe('bash 3.2: case patterns inside a substitution (#3601)', () => {
// Value: protects=the scanner flags exactly the shape bash 3.2 cannot parse; fails_when=it misses a bare pattern in $( ), one-line or multi-line,
// or flags a parenthesized pattern, a case outside a substitution, or quoted text; why_new=the scanner is new; seam=none
test('the scanner flags a bare pattern inside $( ) or <( ) and nothing else', () => {
const hits = (src: string) => scanBash32CaseInSubstitution(src).map(h => h.line);
expect(hits("x=$(case a in *[![:space:]]*) echo ok ;; esac)")).toEqual([1]);
expect(hits("x=$(\ncase a in\n (b) echo 1 ;;\n c) echo 2 ;;\nesac\n)")).toEqual([4]);
expect(hits("diff <(case a in b) echo ;; esac) /dev/null")).toEqual([1]);
expect(hits("x=$(case \"$v\" in (*[![:space:]]*) echo ok ;; (b|c) : ;; esac)")).toEqual([]);
expect(hits("case a in b) x=$(echo hi) ;; esac")).toEqual([]);
expect(hits("y=$(echo \"case a in b) no\")")).toEqual([]);
expect(hits("cat <<'EOF'\nx=$(case a in b) ;; esac)\nEOF\nz=$(echo ok)")).toEqual([]);
});
// Value: protects=every shell script gstack ships parses under macOS /bin/bash 3.2 (#3601);
// fails_when=a script puts a bare case pattern inside $( ); why_new=Linux CI runs bash 5, which accepts it; seam=none
test('no tracked shell script has a bare case pattern inside a substitution', () => {
const scripts = trackedShellScripts();
expect(scripts).toContain('bin/gstack-launch-probe.sh');
const hits = scripts.flatMap(rel => scanBash32CaseInSubstitution(fs.readFileSync(path.join(ROOT, rel), 'utf8')).map(h => `${rel}:${h.line}: ${h.text}`));
expect(hits).toEqual([]);
});
// Value: protects=the probe parses under macOS /bin/bash 3.2, so gstack-doctor can source it (#3601);
// fails_when=a case pattern inside $( ) loses its leading (, on a macOS runner only: Linux /bin/bash is 5.x, which parsed the old line,
// so the scan above is the Linux CI guard; why_new=test/gstack-launch-probe.test.ts runs PATH bash; seam=none
test.skipIf(!fs.existsSync('/bin/bash'))('bin/gstack-launch-probe.sh parses under /bin/bash (catches #3601 only where /bin/bash is 3.2, i.e. macOS)', () => {
const r = spawnSync('/bin/bash', ['-n', PROBE], { encoding: 'utf8', timeout: 30_000 });
expect(r.stderr).toBe('');
expect(r.status).toBe(0);
});
});