|
| 1 | +import { readFileSync, rmSync, writeFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | + |
| 4 | +import { |
| 5 | + addDependents, |
| 6 | + checkModuleArtifact, |
| 7 | + checkPackages, |
| 8 | + enumerateModules, |
| 9 | + mapFilesToModules, |
| 10 | +} from '../../src/packaging/check'; |
| 11 | +import { resolveBundleArtifactPath, writeBundleArtifact } from '../../src/bundle/artifact'; |
| 12 | +import { PgpmPackage } from '../../src/core/class/pgpm'; |
| 13 | +import { TestFixture } from '../../test-utils'; |
| 14 | + |
| 15 | +const MODULES = ['my-first', 'my-second', 'my-third'] as const; |
| 16 | + |
| 17 | +/** |
| 18 | + * `pgpm package --check`: verify a committed bundle artifact still matches its |
| 19 | + * `deploy/`. Read + sha256 only — no DDL, no database — so these are pure, |
| 20 | + * DB-free unit tests over the fixture workspace. |
| 21 | + */ |
| 22 | +describe('package check (artifact drift verification)', () => { |
| 23 | + let fixture: TestFixture; |
| 24 | + let root: string; |
| 25 | + |
| 26 | + const moduleDir = (name: string): string => fixture.fixturePath('packages', name); |
| 27 | + |
| 28 | + const emitAll = async (): Promise<void> => { |
| 29 | + for (const name of MODULES) { |
| 30 | + const dir = moduleDir(name); |
| 31 | + const version = require(join(dir, 'package.json')).version as string; |
| 32 | + await writeBundleArtifact(dir, version); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + fixture = new TestFixture('sqitch', 'simple-w-tags'); |
| 38 | + root = fixture.fixturePath(); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + fixture.cleanup(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('reports no drift for a freshly packaged module', async () => { |
| 46 | + await emitAll(); |
| 47 | + expect(checkModuleArtifact(moduleDir('my-first'), 'my-first')).toBeNull(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('flags a missing artifact', () => { |
| 51 | + const drift = checkModuleArtifact(moduleDir('my-first'), 'my-first'); |
| 52 | + expect(drift?.reason).toBe('missing-artifact'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('flags an artifact whose deploy/ has changed (out of sync)', async () => { |
| 56 | + await emitAll(); |
| 57 | + const dir = moduleDir('my-first'); |
| 58 | + const deployFile = join(dir, 'deploy', 'schema_myfirstapp.sql'); |
| 59 | + writeFileSync(deployFile, `${readFileSync(deployFile, 'utf-8')}\n-- drift\n`); |
| 60 | + |
| 61 | + const drift = checkModuleArtifact(dir, 'my-first'); |
| 62 | + expect(drift?.reason).toBe('out-of-sync'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('flags a corrupt artifact archive', async () => { |
| 66 | + await emitAll(); |
| 67 | + writeFileSync(resolveBundleArtifactPath(moduleDir('my-second'))!, 'not-an-archive'); |
| 68 | + const drift = checkModuleArtifact(moduleDir('my-second'), 'my-second'); |
| 69 | + expect(drift?.reason).toBe('unreadable-artifact'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('checkPackages --all verifies every module and passes when in sync', async () => { |
| 73 | + await emitAll(); |
| 74 | + const result = await checkPackages({ cwd: root, all: true }); |
| 75 | + expect(new Set(result.checked)).toEqual(new Set(MODULES)); |
| 76 | + expect(result.drifted).toHaveLength(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('checkPackages --all fails fast on the first drift by default', async () => { |
| 80 | + await emitAll(); |
| 81 | + // Break two modules; fail-fast should report exactly one. |
| 82 | + rmSync(resolveBundleArtifactPath(moduleDir('my-first'))!); |
| 83 | + rmSync(resolveBundleArtifactPath(moduleDir('my-second'))!); |
| 84 | + const result = await checkPackages({ cwd: root, all: true }); |
| 85 | + expect(result.drifted).toHaveLength(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it('checkPackages --all --no-fail-fast reports every drifted module', async () => { |
| 89 | + await emitAll(); |
| 90 | + rmSync(resolveBundleArtifactPath(moduleDir('my-first'))!); |
| 91 | + rmSync(resolveBundleArtifactPath(moduleDir('my-second'))!); |
| 92 | + const result = await checkPackages({ cwd: root, all: true, failFast: false }); |
| 93 | + expect(new Set(result.drifted.map((d) => d.name))).toEqual( |
| 94 | + new Set(['my-first', 'my-second']) |
| 95 | + ); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('package check (workspace mapping helpers)', () => { |
| 100 | + let fixture: TestFixture; |
| 101 | + let pkg: PgpmPackage; |
| 102 | + |
| 103 | + beforeEach(() => { |
| 104 | + fixture = new TestFixture('sqitch', 'simple-w-tags'); |
| 105 | + pkg = new PgpmPackage(fixture.fixturePath()); |
| 106 | + }); |
| 107 | + |
| 108 | + afterEach(() => { |
| 109 | + fixture.cleanup(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('maps a changed deploy file to its owning module', () => { |
| 113 | + const modules = enumerateModules(pkg); |
| 114 | + const changed = [fixture.fixturePath('packages', 'my-second', 'deploy', 'anything.sql')]; |
| 115 | + expect(mapFilesToModules(changed, modules)).toEqual(['my-second']); |
| 116 | + }); |
| 117 | + |
| 118 | + it('ignores files outside any module', () => { |
| 119 | + const modules = enumerateModules(pkg); |
| 120 | + expect(mapFilesToModules([fixture.fixturePath('README.md')], modules)).toEqual([]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('expands to transitive dependents', () => { |
| 124 | + // my-third requires my-second requires my-first. |
| 125 | + const names = new Set(['my-first']); |
| 126 | + addDependents(names, pkg); |
| 127 | + expect(names).toEqual(new Set(['my-first', 'my-second', 'my-third'])); |
| 128 | + }); |
| 129 | +}); |
0 commit comments