|
| 1 | +import { expect, test } from 'rstack/test'; |
| 2 | +import { fmtHelpMessage, parseFmtCLIArgs } from '../../src/fmt/cli.ts'; |
| 3 | + |
| 4 | +test('uses write mode by default', () => { |
| 5 | + expect(parseFmtCLIArgs([])).toEqual({ |
| 6 | + mode: 'write', |
| 7 | + patterns: [], |
| 8 | + help: false, |
| 9 | + }); |
| 10 | +}); |
| 11 | + |
| 12 | +test.each([ |
| 13 | + ['--write', 'write'], |
| 14 | + ['--check', 'check'], |
| 15 | + ['--list-different', 'list-different'], |
| 16 | + ['--listDifferent', 'list-different'], |
| 17 | +] as const)('parses %s mode', (option, mode) => { |
| 18 | + expect(parseFmtCLIArgs([option])).toEqual({ |
| 19 | + mode, |
| 20 | + patterns: [], |
| 21 | + help: false, |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +test('preserves file paths and globs', () => { |
| 26 | + const patterns = ['src/file with spaces.ts', 'src/**/*.{js,ts}', '!src/generated/**']; |
| 27 | + |
| 28 | + expect(parseFmtCLIArgs([patterns[0], '--check', ...patterns.slice(1)])).toEqual({ |
| 29 | + mode: 'check', |
| 30 | + patterns, |
| 31 | + help: false, |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +test('treats arguments after the terminator as paths', () => { |
| 36 | + expect(parseFmtCLIArgs(['--check', '--', '--write', '--help'])).toEqual({ |
| 37 | + mode: 'check', |
| 38 | + patterns: ['--write', '--help'], |
| 39 | + help: false, |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +test.each(['--help', '-h'])('parses %s', (option) => { |
| 44 | + expect(parseFmtCLIArgs([option]).help).toBe(true); |
| 45 | +}); |
| 46 | + |
| 47 | +test('provides command help', () => { |
| 48 | + expect(fmtHelpMessage).toContain('Usage:\n $ rs fmt [options] [files/globs...]'); |
| 49 | + expect(fmtHelpMessage).toContain('--write'); |
| 50 | + expect(fmtHelpMessage).toContain('--check'); |
| 51 | + expect(fmtHelpMessage).toContain('--list-different'); |
| 52 | + expect(fmtHelpMessage).toContain('-h, --help'); |
| 53 | +}); |
| 54 | + |
| 55 | +test.each([ |
| 56 | + ['--write', '--check'], |
| 57 | + ['--write', '--list-different'], |
| 58 | + ['--write', '--listDifferent'], |
| 59 | + ['--check', '--list-different'], |
| 60 | + ['--write', '--check', '--list-different'], |
| 61 | +])('rejects conflicting modes: %s', (...args) => { |
| 62 | + expect(() => parseFmtCLIArgs(args)).toThrow( |
| 63 | + 'The --write, --check, and --list-different options cannot be used together.', |
| 64 | + ); |
| 65 | +}); |
| 66 | + |
| 67 | +test.each(['--unknown', '--no-cache', '--no-parallel', '--parallel-workers'])( |
| 68 | + 'rejects unsupported option %s', |
| 69 | + (option) => { |
| 70 | + expect(() => parseFmtCLIArgs([option])).toThrow(); |
| 71 | + }, |
| 72 | +); |
0 commit comments