|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { expect, test } from 'rstack/test'; |
| 5 | +import { normalizeFmtConfig } from '../../src/fmt/config.ts'; |
| 6 | +import { discoverFmtFiles } from '../../src/fmt/discovery.ts'; |
| 7 | +import type { FmtConfig } from '../../src/fmt/types.ts'; |
| 8 | + |
| 9 | +const withProject = async (callback: (rootPath: string) => Promise<void>): Promise<void> => { |
| 10 | + const rootPath = mkdtempSync(path.join(tmpdir(), 'rstack fmt ')); |
| 11 | + |
| 12 | + try { |
| 13 | + await callback(rootPath); |
| 14 | + } finally { |
| 15 | + rmSync(rootPath, { force: true, recursive: true }); |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +const writeProjectFile = (rootPath: string, filePath: string, content = ''): string => { |
| 20 | + const absolutePath = path.join(rootPath, filePath); |
| 21 | + mkdirSync(path.dirname(absolutePath), { recursive: true }); |
| 22 | + writeFileSync(absolutePath, content); |
| 23 | + return absolutePath; |
| 24 | +}; |
| 25 | + |
| 26 | +const discover = async (cwd: string, patterns?: string[], config?: FmtConfig, configRoot = cwd) => |
| 27 | + discoverFmtFiles({ |
| 28 | + cwd, |
| 29 | + patterns, |
| 30 | + config: normalizeFmtConfig(config, configRoot), |
| 31 | + }); |
| 32 | + |
| 33 | +const relativePaths = (rootPath: string, files: Awaited<ReturnType<typeof discover>>): string[] => |
| 34 | + files.map((file) => path.relative(rootPath, file.path)); |
| 35 | + |
| 36 | +test('applies config ignore patterns to discovered and explicit files', async () => { |
| 37 | + await withProject(async (rootPath) => { |
| 38 | + const keepPath = writeProjectFile(rootPath, 'generated/keep.ts'); |
| 39 | + const blockedPath = writeProjectFile(rootPath, 'generated/blocked.ts'); |
| 40 | + writeProjectFile(rootPath, 'src/index.ts'); |
| 41 | + const config = { ignorePatterns: ['generated/blocked.ts'] }; |
| 42 | + |
| 43 | + const discoveredFiles = await discover(rootPath, undefined, config); |
| 44 | + const explicitFiles = await discover(rootPath, [keepPath, blockedPath], config); |
| 45 | + |
| 46 | + expect(relativePaths(rootPath, discoveredFiles)).toEqual([ |
| 47 | + path.join('generated', 'keep.ts'), |
| 48 | + path.join('src', 'index.ts'), |
| 49 | + ]); |
| 50 | + expect(relativePaths(rootPath, explicitFiles)).toEqual([path.join('generated', 'keep.ts')]); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +test('applies config ignore patterns outside the config root', async () => { |
| 55 | + await withProject(async (rootPath) => { |
| 56 | + const configRoot = path.join(rootPath, 'project'); |
| 57 | + const filePath = writeProjectFile(rootPath, 'shared/index.ts'); |
| 58 | + mkdirSync(configRoot); |
| 59 | + |
| 60 | + await expect( |
| 61 | + discover(configRoot, [filePath], { ignorePatterns: ['../shared/*.ts'] }, configRoot), |
| 62 | + ).resolves.toEqual([]); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +test('resolves parsers and accepts unknown extensions with an explicit parser', async () => { |
| 67 | + await withProject(async (rootPath) => { |
| 68 | + writeProjectFile(rootPath, 'index.ts'); |
| 69 | + writeProjectFile(rootPath, 'source.custom'); |
| 70 | + writeProjectFile(rootPath, 'unknown.extension'); |
| 71 | + |
| 72 | + const inferredFiles = await discover(rootPath); |
| 73 | + const configuredFiles = await discover(rootPath, ['source.custom'], { parser: 'babel' }); |
| 74 | + |
| 75 | + expect(relativePaths(rootPath, inferredFiles)).toEqual(['index.ts']); |
| 76 | + expect(inferredFiles[0].options.parser).toBe('typescript'); |
| 77 | + expect(configuredFiles[0].options).toMatchObject({ |
| 78 | + filepath: path.join(rootPath, 'source.custom'), |
| 79 | + parser: 'babel', |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments