|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { FnGenerator } from '../src'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Regression test: run FnGenerator against the brasilia repo's own |
| 7 | + * `functions/` and `templates/`, output to a temp dir, and assert it |
| 8 | + * is byte-identical to the brasilia repo's checked-in `generated/` |
| 9 | + * (which is produced by the legacy `scripts/generate.ts`). |
| 10 | + */ |
| 11 | + |
| 12 | +const ROOT = path.resolve(__dirname, '..', '..', '..'); |
| 13 | +const FUNCTIONS_DIR = path.join(ROOT, 'functions'); |
| 14 | +const TEMPLATES_DIR = path.join(ROOT, 'templates'); |
| 15 | +const BASELINE_GENERATED = path.join(ROOT, 'generated'); |
| 16 | +const BASELINE_SKAFFOLD = path.join(ROOT, 'skaffold.yaml'); |
| 17 | + |
| 18 | +const walk = (dir: string): { files: string[]; symlinks: string[] } => { |
| 19 | + const files: string[] = []; |
| 20 | + const symlinks: string[] = []; |
| 21 | + const recurse = (current: string, base: string): void => { |
| 22 | + const entries = fs.readdirSync(current); |
| 23 | + for (const entry of entries) { |
| 24 | + // Skip pnpm-installed dirs that aren't generator output. |
| 25 | + if (entry === 'node_modules' || entry === 'dist') continue; |
| 26 | + const full = path.join(current, entry); |
| 27 | + const rel = base ? path.join(base, entry) : entry; |
| 28 | + const stat = fs.lstatSync(full); |
| 29 | + if (stat.isSymbolicLink()) { |
| 30 | + symlinks.push(rel); |
| 31 | + } else if (stat.isDirectory()) { |
| 32 | + recurse(full, rel); |
| 33 | + } else { |
| 34 | + files.push(rel); |
| 35 | + } |
| 36 | + } |
| 37 | + }; |
| 38 | + recurse(dir, ''); |
| 39 | + return { files, symlinks }; |
| 40 | +}; |
| 41 | + |
| 42 | +describe('FnGenerator snapshot vs scripts/generate.ts', () => { |
| 43 | + let tmpDir: string; |
| 44 | + let tmpRoot: string; |
| 45 | + |
| 46 | + beforeAll(() => { |
| 47 | + // The generator writes skaffold.yaml at the rootDir, so we mirror the |
| 48 | + // brasilia layout into a tmp tree (functions/, templates/ symlinked to |
| 49 | + // the real ones; outputDir in tmpDir; rootDir = tmpRoot). |
| 50 | + tmpRoot = fs.mkdtempSync(path.join(require('os').tmpdir(), 'fn-gen-test-')); |
| 51 | + tmpDir = path.join(tmpRoot, 'generated'); |
| 52 | + |
| 53 | + fs.symlinkSync(FUNCTIONS_DIR, path.join(tmpRoot, 'functions')); |
| 54 | + fs.symlinkSync(TEMPLATES_DIR, path.join(tmpRoot, 'templates')); |
| 55 | + |
| 56 | + const gen = new FnGenerator({ rootDir: tmpRoot }); |
| 57 | + gen.generate(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterAll(() => { |
| 61 | + fs.rmSync(tmpRoot, { recursive: true, force: true }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('produces the same files (byte-identical)', () => { |
| 65 | + const baseline = walk(BASELINE_GENERATED); |
| 66 | + const actual = walk(tmpDir); |
| 67 | + expect([...actual.files].sort()).toEqual([...baseline.files].sort()); |
| 68 | + for (const rel of baseline.files) { |
| 69 | + const a = fs.readFileSync(path.join(tmpDir, rel), 'utf-8'); |
| 70 | + const b = fs.readFileSync(path.join(BASELINE_GENERATED, rel), 'utf-8'); |
| 71 | + expect({ file: rel, content: a }).toEqual({ file: rel, content: b }); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it('produces the same symlinks (same relative target)', () => { |
| 76 | + const baseline = walk(BASELINE_GENERATED); |
| 77 | + const actual = walk(tmpDir); |
| 78 | + expect([...actual.symlinks].sort()).toEqual([...baseline.symlinks].sort()); |
| 79 | + for (const rel of baseline.symlinks) { |
| 80 | + const a = fs.readlinkSync(path.join(tmpDir, rel)); |
| 81 | + const b = fs.readlinkSync(path.join(BASELINE_GENERATED, rel)); |
| 82 | + expect({ symlink: rel, target: a }).toEqual({ symlink: rel, target: b }); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('produces an identical skaffold.yaml', () => { |
| 87 | + const a = fs.readFileSync(path.join(tmpRoot, 'skaffold.yaml'), 'utf-8'); |
| 88 | + const b = fs.readFileSync(BASELINE_SKAFFOLD, 'utf-8'); |
| 89 | + expect(a).toBe(b); |
| 90 | + }); |
| 91 | +}); |
0 commit comments