|
| 1 | +import plugin from '../../src'; |
| 2 | +import { loadSchema } from '../utils'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +describe('documentation plugin: plugin options', () => { |
| 9 | + it('cleanOutput deletes existing files in the output directory before generation', async () => { |
| 10 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'doc-plugin-')); |
| 11 | + // create a stale file that should be removed by cleanOutput |
| 12 | + fs.writeFileSync(path.join(tmpDir, 'stale.txt'), 'stale'); |
| 13 | + |
| 14 | + const model = await loadSchema(` |
| 15 | + model A { |
| 16 | + id String @id @default(cuid()) |
| 17 | + } |
| 18 | + `); |
| 19 | + |
| 20 | + await plugin.generate({ |
| 21 | + defaultOutputPath: tmpDir, |
| 22 | + model, |
| 23 | + pluginOptions: { cleanOutput: true, output: tmpDir }, |
| 24 | + schemaFile: 'schema.zmodel', |
| 25 | + }); |
| 26 | + |
| 27 | + // ensure output dir is prepopulated and removed by cleanOutput |
| 28 | + |
| 29 | + expect(fs.existsSync(path.join(tmpDir, 'stale.txt'))).toBe(false); |
| 30 | + expect(fs.existsSync(path.join(tmpDir, 'index.md'))).toBe(true); |
| 31 | + }); |
| 32 | + it("models with @@meta('doc:ignore', true) are treated as internal and excluded when includeInternalModels is false", async () => { |
| 33 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'doc-plugin-')); |
| 34 | + |
| 35 | + const schema = ` |
| 36 | + model Public { |
| 37 | + id String @id @default(cuid()) |
| 38 | + } |
| 39 | + |
| 40 | + model Internal { |
| 41 | + id String @id @default(cuid()) |
| 42 | + @@meta('doc:ignore', true) |
| 43 | + } |
| 44 | + `; |
| 45 | + |
| 46 | + const model = await loadSchema(schema); |
| 47 | + |
| 48 | + await plugin.generate({ |
| 49 | + defaultOutputPath: tmpDir, |
| 50 | + model, |
| 51 | + pluginOptions: { includeInternalModels: false, output: tmpDir }, |
| 52 | + schemaFile: 'schema.zmodel', |
| 53 | + }); |
| 54 | + |
| 55 | + // Public model should be present, internal model should be excluded |
| 56 | + expect(fs.existsSync(path.join(tmpDir, 'models', 'Public.md'))).toBe(true); |
| 57 | + expect(fs.existsSync(path.join(tmpDir, 'models', 'Internal.md'))).toBe( |
| 58 | + false, |
| 59 | + ); |
| 60 | + }); |
| 61 | +}); |
0 commit comments