/** * expand -t comma-separated tab stops */ import { readFile } from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' import test from 'brittle' import b4a from 'b4a' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor async function loadBin(name) { const runtime = await readFile( path.join(__dirname, '../lib/runtime.js'), 'utf8' ) const body = await readFile( path.join(__dirname, `../src/${name}.js`), 'utf8' ) return new AsyncFunction( 'ctx', 'argv', `${runtime}\n${body}\nif (typeof run === 'function') return await run(ctx, argv)\n` ) } function createCtx() { const logs = [] return { b4a, exitCode: 0, vfs: { async stat() { return null } }, console: { log: (m) => logs.push(String(m)), error: (m) => logs.push(String(m)) }, _logs: logs } } test('expand -t 1,5 maps two leading tabs to five spaces before text', async (t) => { const run = await loadBin('expand') const ctx = createCtx() ctx.shellStdin = '\t\tfoo' await run(ctx, ['expand', '-t', '1,5']) t.is(ctx.exitCode, 0) t.is(ctx._logs.join('\n'), ' foo') }) test('expand -t 8 uniform matches single-stop width', async (t) => { const run = await loadBin('expand') const ctx = createCtx() ctx.shellStdin = '\tx' await run(ctx, ['expand', '-t', '8']) t.is(ctx._logs.join('\n'), ' x') })