Something went wrong. Try again.
experiments in a post-browser web
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186'use strict';// Offline tests for scripts/gh-setup.sh. Synthetic token, temp HOME, stub commands only.const { test, before, after } = require('node:test');const assert = require('node:assert/strict');const fs = require('node:fs');const os = require('node:os');const path = require('node:path');const { spawnSync, spawn } = require('node:child_process');
const SCRIPT = path.join(__dirname, 'gh-setup.sh');const TOKEN = 'github_pat_synthetic_0123456789abcdef';
let root;let bin;
before(() => { root = fs.mkdtempSync(path.join(os.tmpdir(), 'gh-setup-test-')); bin = path.join(root, 'bin'); fs.mkdirSync(bin); // Stub gh: prints the env it sees to a record file; fails on anything but `repo view`. fs.writeFileSync( path.join(bin, 'gh'), `#!/bin/bashif [[ "$1 $2" != "repo view" ]]; then echo "unexpected gh subcommand: $*" >&2; exit 99; fiecho "$GH_TOKEN" > "$STUB_RECORD.token"echo "$GH_CONFIG_DIR" > "$STUB_RECORD.dir"echo "$*" > "$STUB_RECORD.argv"ls -A "$GH_CONFIG_DIR" > "$STUB_RECORD.dircontents"stat -c %a "$GH_CONFIG_DIR" > "$STUB_RECORD.dirmode"exit "\${STUB_EXIT:-0}"`, { mode: 0o755 }, ); fs.writeFileSync( path.join(bin, 'reader'), `#!/bin/bashcat > "$STUB_RECORD.stdin"`, { mode: 0o755 }, ); fs.writeFileSync( path.join(bin, 'sleeper'), `#!/bin/bashecho "$GH_CONFIG_DIR" > "$STUB_RECORD.dir"echo started > "$STUB_RECORD.started"sleep 30 &wait`, { mode: 0o755 }, );});
after(() => fs.rmSync(root, { recursive: true, force: true }));
function makeEnv(name, { token = TOKEN, fileMode = 0o600, dirMode = 0o700, kind = 'file' } = {}) { const home = path.join(root, name); const tmp = path.join(home, 'tmp'); const folder = path.join(home, '.config', 'peek-ci'); fs.mkdirSync(tmp, { recursive: true }); fs.mkdirSync(folder, { recursive: true }); fs.chmodSync(folder, dirMode); const file = path.join(folder, 'github-token'); if (kind === 'file') { fs.writeFileSync(file, token); fs.chmodSync(file, fileMode); } else if (kind === 'symlink') { const real = path.join(home, 'real-token'); fs.writeFileSync(real, token, { mode: 0o600 }); fs.symlinkSync(real, file); } const record = path.join(home, 'record'); const env = { PATH: `${bin}:/usr/bin:/bin`, HOME: home, TMPDIR: tmp, STUB_RECORD: record, }; return { home, tmp, record, env };}
function run(ctx, args, extraEnv = {}) { return spawnSync('bash', [SCRIPT, ...args], { env: { ...ctx.env, ...extraEnv }, encoding: 'utf8', });}
const read = (p) => fs.readFileSync(p, 'utf8').trim();const leftovers = (ctx) => fs.readdirSync(ctx.tmp);
test('runs command with GH_TOKEN and a fresh empty 0700 GH_CONFIG_DIR, removed afterwards', () => { const ctx = makeEnv('ok'); const r = run(ctx, ['--', 'gh', 'repo', 'view']); assert.equal(r.status, 0, r.stderr); assert.equal(read(`${ctx.record}.token`), TOKEN); const dir = read(`${ctx.record}.dir`); assert.ok(dir.startsWith(ctx.tmp + path.sep), 'config dir is under temp dir'); assert.equal(read(`${ctx.record}.dircontents`), ''); assert.equal(read(`${ctx.record}.dirmode`), '700'); assert.equal(fs.existsSync(dir), false); assert.deepEqual(leftovers(ctx), []); // real HOME's gh config untouched assert.equal(fs.existsSync(path.join(ctx.home, '.config', 'gh')), false);});
test('token never appears in stdout, stderr or argv', () => { const ctx = makeEnv('leak'); const r = run(ctx, ['--', 'gh', 'repo', 'view']); assert.ok(!r.stdout.includes(TOKEN)); assert.ok(!r.stderr.includes(TOKEN)); assert.ok(!read(`${ctx.record}.argv`).includes(TOKEN));});
const refusals = { 'missing file': { kind: 'none' }, 'empty file': { token: '' }, symlink: { kind: 'symlink' }, 'world-readable file': { fileMode: 0o644 }, '0700 dir but 0644 file': { fileMode: 0o644, dirMode: 0o700 }, 'group-readable file': { fileMode: 0o640 }, 'loose directory': { dirMode: 0o755 },};for (const [name, opts] of Object.entries(refusals)) { test(`refuses ${name} without running the command`, () => { const ctx = makeEnv(`refuse-${name.replace(/\W+/g, '-')}`, opts); const r = run(ctx, ['--', 'gh', 'repo', 'view']); assert.notEqual(r.status, 0); assert.equal(fs.existsSync(`${ctx.record}.token`), false, 'command must not run'); assert.ok(r.stderr.includes('github-token') || r.stderr.includes('peek-ci'), r.stderr); assert.ok(!r.stderr.includes(TOKEN) && !r.stdout.includes(TOKEN)); assert.deepEqual(leftovers(ctx), []); });}
test('refuses when no command is given', () => { const ctx = makeEnv('nocmd'); assert.notEqual(run(ctx, []).status, 0); assert.notEqual(run(ctx, ['--']).status, 0);});
test('refuses gh auth', () => { const ctx = makeEnv('auth'); const r = run(ctx, ['--', 'gh', 'auth', 'login']); assert.notEqual(r.status, 0); assert.equal(fs.existsSync(`${ctx.record}.token`), false);});
test('exit status is the command own status', () => { const ctx = makeEnv('status'); assert.equal(run(ctx, ['--', 'gh', 'repo', 'view']).status, 0); const bad = run(ctx, ['--', 'gh', 'repo', 'view'], { STUB_EXIT: '7' }); assert.equal(bad.status, 7); assert.deepEqual(leftovers(ctx), []); // stub fails on unexpected subcommands (no real gh can be reached) assert.equal(run(ctx, ['--', 'gh', 'api', 'user']).status, 99); assert.deepEqual(leftovers(ctx), []);});
test('the command receives the caller stdin', () => { const ctx = makeEnv('stdin'); const r = spawnSync('bash', [SCRIPT, '--', 'reader'], { env: ctx.env, encoding: 'utf8', input: 'piped-value\n', }); assert.equal(r.status, 0, r.stderr); assert.equal(read(`${ctx.record}.stdin`), 'piped-value');});
test('temp dir is removed after SIGINT', async () => { const ctx = makeEnv('sigint'); const child = spawn('bash', [SCRIPT, '--', 'sleeper'], { env: ctx.env, stdio: 'ignore' }); const exited = new Promise((resolve) => child.on('exit', (code, sig) => resolve({ code, sig }))); const started = `${ctx.record}.started`; for (let i = 0; i < 100 && !fs.existsSync(started); i++) { await new Promise((r) => setTimeout(r, 50)); } assert.ok(fs.existsSync(started), 'command started'); const dir = read(`${ctx.record}.dir`); assert.ok(fs.existsSync(dir)); child.kill('SIGINT'); await exited; assert.equal(fs.existsSync(dir), false); assert.deepEqual(leftovers(ctx), []);});