import fs from 'fs' import os from 'os' import path from 'path' import { spawnSync } from 'child_process' describe('scripts/docker-entrypoint.sh', () => { const entrypoint = fs.readFileSync( path.join(process.cwd(), 'scripts/docker-entrypoint.sh'), 'utf8' ) it('数据库迁移失败时必须停止容器启动', () => { expect(entrypoint).toContain('npx prisma migrate deploy') expect(entrypoint).toContain('数据库迁移失败') expect(entrypoint).toMatch(/数据库迁移失败[\s\S]*exit 1/) expect(entrypoint).not.toContain('数据库迁移失败,尝试继续启动') }) it('迁移命令真实失败时不会继续启动 server.js', () => { if (process.platform !== 'linux') { return } const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'flnxi-entrypoint-')) const binDir = path.join(tmpDir, 'bin') fs.mkdirSync(binDir) fs.writeFileSync( path.join(binDir, 'npx'), [ '#!/bin/sh', 'if [ "$1 $2 $3" = "prisma db execute" ]; then', ' exit 0', 'fi', 'if [ "$1 $2 $3" = "prisma migrate deploy" ]; then', ' exit 42', 'fi', 'exit 1', '', ].join('\n'), { mode: 0o755 } ) fs.writeFileSync( path.join(binDir, 'node'), [ '#!/bin/sh', `echo started > "${path.join(tmpDir, 'server-started').replace(/\\/g, '/')}"`, 'exit 0', '', ].join('\n'), { mode: 0o755 } ) const scriptPath = path.join(process.cwd(), 'scripts/docker-entrypoint.sh') const result = spawnSync('sh', [scriptPath], { cwd: tmpDir, env: { ...process.env, PATH: `${binDir}${path.delimiter}${process.env.PATH || ''}`, }, encoding: 'utf8', }) expect(result.status).toBe(1) expect(`${result.stdout}\n${result.stderr}`).toContain('数据库迁移失败') expect(fs.existsSync(path.join(tmpDir, 'server-started'))).toBe(false) }) })