|
| 1 | +import { expect, rs, test } from 'rstack/test'; |
| 2 | +import { normalizeFmtConfig } from '../../src/fmt/config.ts'; |
| 3 | +import { discoverFmtFiles } from '../../src/fmt/discovery.ts'; |
| 4 | +import type { FmtConfig } from '../../src/fmt/types.ts'; |
| 5 | +import { withTempProject, writeProjectFile } from './helpers.ts'; |
| 6 | + |
| 7 | +const mocks = rs.hoisted(() => ({ |
| 8 | + pluginResolverRoots: [] as string[], |
| 9 | +})); |
| 10 | + |
| 11 | +rs.mock('../../src/fmt/plugins.ts', () => ({ |
| 12 | + createFmtPluginResolver: (rootPath: string) => { |
| 13 | + mocks.pluginResolverRoots.push(rootPath); |
| 14 | + return <T>(options: T): T => options; |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +const discover = (cwd: string, config: FmtConfig) => |
| 19 | + discoverFmtFiles({ |
| 20 | + cwd, |
| 21 | + patterns: ['index.ts'], |
| 22 | + config: normalizeFmtConfig(config, cwd), |
| 23 | + }); |
| 24 | + |
| 25 | +test('loads the plugin resolver only when a discovered file uses plugins', async () => { |
| 26 | + await withTempProject(async (rootPath) => { |
| 27 | + writeProjectFile(rootPath, 'index.ts'); |
| 28 | + |
| 29 | + await discover(rootPath, { |
| 30 | + overrides: [{ files: '*.md', options: { plugins: ['missing-plugin'] } }], |
| 31 | + }); |
| 32 | + expect(mocks.pluginResolverRoots).toEqual([]); |
| 33 | + |
| 34 | + const files = await discover(rootPath, { |
| 35 | + overrides: [{ files: '*.ts', options: { plugins: ['prettier-plugin-fixture'] } }], |
| 36 | + }); |
| 37 | + expect(mocks.pluginResolverRoots).toEqual([rootPath]); |
| 38 | + expect(files[0].options.plugins).toEqual(['prettier-plugin-fixture']); |
| 39 | + }); |
| 40 | +}); |
0 commit comments