about summary refs log tree commit diff stats
path: root/tree-sitter/dsk/dsk-cli/src/commands/test.ts
blob: 694acc724539b26a95b29bab6d417a8ba589c884 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * 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 <regex>', 'Only run tests whose descriptions match the regex')
    .option('--cwd <dir>', '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;
}