/** * Test Command - Wrapper around `tree-sitter test` * * Streams test output directly to the console. */ import { Command } from 'commander'; import { execa } from 'execa'; import chalk from 'chalk'; import { existsSync } from 'fs'; /** * Create the test command */ export function createTestCommand(): Command { const testCommand = new Command('test'); testCommand .description('Run tree-sitter tests and stream output') .allowExcessArguments(true) .option('-u, --update', 'Update expected outputs (snapshots)') .option('-f, --filter ', 'Only run tests whose descriptions match the regex') .option('--cwd ', 'Run tests with a different working directory') .option('-v, --verbose', 'Show verbose output (passes through to tree-sitter)') .argument('[patterns...]', 'Optional test patterns (filenames or test names)') .action(async (patterns: string[], options) => { if (!existsSync('grammar.js')) { console.error(chalk.red('โŒ No grammar.js found. Are you in a DSL project directory?')); process.exit(1); } try { console.log(chalk.blue('๐Ÿงช Running tests...')); const args = ['test']; if (options.update) args.push('-u'); if (options.filter) args.push('-f', String(options.filter)); if (options.verbose) args.push('--verbose'); args.push(...patterns); await execa('tree-sitter', args, { stdio: 'inherit', cwd: options.cwd || process.cwd() }); } catch (error: any) { const message = error?.message || error || 'Unknown error'; console.error(chalk.red('โŒ Tests failed:'), message); process.exit(1); } }); return testCommand; }