|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'bun:test' |
| 2 | +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { dirname, resolve } from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | + |
| 6 | +describe('doctor', () => { |
| 7 | + const tmpRoot = resolve(process.cwd(), 'test/tmp/doctor') |
| 8 | + const tsconfigPath = resolve(tmpRoot, 'tsconfig.json') |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + if (existsSync(tmpRoot)) |
| 12 | + rmSync(tmpRoot, { recursive: true }) |
| 13 | + mkdirSync(tmpRoot, { recursive: true, mode: 0o777 }) |
| 14 | + }) |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + if (existsSync(tmpRoot)) |
| 18 | + rmSync(tmpRoot, { recursive: true }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should report required changes without --fix', async () => { |
| 22 | + // minimal tsconfig missing plugin/paths |
| 23 | + writeFileSync(tsconfigPath, JSON.stringify({ compilerOptions: {} }, null, 2)) |
| 24 | + |
| 25 | + const proc = Bun.spawn([ |
| 26 | + './bunfig', |
| 27 | + 'doctor', |
| 28 | + '--tsconfig', |
| 29 | + tsconfigPath, |
| 30 | + ], { |
| 31 | + cwd: process.cwd(), |
| 32 | + }) |
| 33 | + |
| 34 | + const output = await new Response(proc.stdout).text() |
| 35 | + await proc.exited |
| 36 | + |
| 37 | + expect(output).toContain('Added bunfig TS plugin') |
| 38 | + expect(output).toContain('Changes needed') |
| 39 | + |
| 40 | + // file should remain unchanged without --fix |
| 41 | + const content = readFileSync(tsconfigPath, 'utf8') |
| 42 | + expect(content).not.toContain('bunfig/ts-plugin') |
| 43 | + }) |
| 44 | + |
| 45 | + it('should apply fixes with --fix', async () => { |
| 46 | + // ensure parent exists |
| 47 | + if (!existsSync(dirname(tsconfigPath))) |
| 48 | + mkdirSync(dirname(tsconfigPath), { recursive: true, mode: 0o777 }) |
| 49 | + |
| 50 | + writeFileSync(tsconfigPath, JSON.stringify({ compilerOptions: {} }, null, 2)) |
| 51 | + |
| 52 | + const proc = Bun.spawn([ |
| 53 | + './bunfig', |
| 54 | + 'doctor', |
| 55 | + '--tsconfig', |
| 56 | + tsconfigPath, |
| 57 | + '--fix', |
| 58 | + ], { |
| 59 | + cwd: process.cwd(), |
| 60 | + }) |
| 61 | + |
| 62 | + const output = await new Response(proc.stdout).text() |
| 63 | + await proc.exited |
| 64 | + |
| 65 | + expect(output).toContain('Updated') |
| 66 | + |
| 67 | + const content = JSON.parse(readFileSync(tsconfigPath, 'utf8')) as any |
| 68 | + const plugins = content.compilerOptions?.plugins || [] |
| 69 | + const hasPlugin = plugins.some((p: any) => p?.name === 'bunfig/ts-plugin') |
| 70 | + expect(hasPlugin).toBe(true) |
| 71 | + |
| 72 | + if (existsSync(resolve(process.cwd(), 'packages/bunfig/src'))) { |
| 73 | + // In monorepo, paths should be ensured |
| 74 | + const paths = content.compilerOptions?.paths || {} |
| 75 | + expect(paths.bunfig).toBeTruthy() |
| 76 | + expect(paths['bunfig/*']).toBeTruthy() |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + it('should be a no-op when already configured', async () => { |
| 81 | + const preconfigured = { |
| 82 | + compilerOptions: { |
| 83 | + plugins: [{ name: 'bunfig/ts-plugin' }], |
| 84 | + baseUrl: '.', |
| 85 | + paths: { |
| 86 | + 'bunfig': ['packages/bunfig/src'], |
| 87 | + 'bunfig/*': ['packages/bunfig/src/*'], |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + writeFileSync(tsconfigPath, JSON.stringify(preconfigured, null, 2)) |
| 92 | + |
| 93 | + const proc = Bun.spawn([ |
| 94 | + './bunfig', |
| 95 | + 'doctor', |
| 96 | + '--tsconfig', |
| 97 | + tsconfigPath, |
| 98 | + ], { |
| 99 | + cwd: process.cwd(), |
| 100 | + }) |
| 101 | + |
| 102 | + const output = await new Response(proc.stdout).text() |
| 103 | + await proc.exited |
| 104 | + |
| 105 | + expect(output).toContain('Looks good! No changes needed.') |
| 106 | + }) |
| 107 | +}) |
0 commit comments