|
| 1 | +import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { checkBuildOutput } from './index'; |
| 8 | + |
| 9 | +describe('checkBuildOutput', () => { |
| 10 | + let testCwd: string; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + testCwd = await mkdtemp(path.join(tmpdir(), 'fastgpt-plugin-check-')); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(async () => { |
| 17 | + await rm(testCwd, { recursive: true, force: true }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('入口目录不存在时应优先报告 entry 错误', async () => { |
| 21 | + const missingEntry = path.join(testCwd, 'missing-plugin'); |
| 22 | + |
| 23 | + const result = await checkBuildOutput({ |
| 24 | + entry: missingEntry, |
| 25 | + output: './dist' |
| 26 | + }); |
| 27 | + |
| 28 | + expect(result.ok).toBe(false); |
| 29 | + expect(result.errors).toHaveLength(1); |
| 30 | + expect(result.errors[0]).toContain(`入口目录不存在: ${missingEntry}`); |
| 31 | + }); |
| 32 | + |
| 33 | + it('相对 output 应基于 entry 目录解析', async () => { |
| 34 | + const entryDir = path.join(testCwd, 'plugin'); |
| 35 | + await mkdir(entryDir, { recursive: true }); |
| 36 | + await writeFile(path.join(entryDir, 'index.ts'), 'export default {};\n', 'utf-8'); |
| 37 | + |
| 38 | + const result = await checkBuildOutput({ |
| 39 | + entry: entryDir, |
| 40 | + output: './dist' |
| 41 | + }); |
| 42 | + |
| 43 | + expect(result.ok).toBe(false); |
| 44 | + expect(result.errors).toEqual([ |
| 45 | + `构建产物目录不存在: ${path.join(entryDir, 'dist')}。请先运行 pnpm run build,或使用 --output 指向 dist 目录。` |
| 46 | + ]); |
| 47 | + }); |
| 48 | +}); |
0 commit comments