'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/bash if [[ "$1 $2" != "repo view" ]]; then echo "unexpected gh subcommand: $*" >&2; exit 99; fi echo "$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/bash cat > "$STUB_RECORD.stdin" `, { mode: 0o755 }, ); fs.writeFileSync( path.join(bin, 'sleeper'), `#!/bin/bash echo "$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), []); });