T-2: tests
This commit is contained in:
parent
c2db2d7495
commit
fb7e786010
1 changed files with 108 additions and 1 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||||
import { tmpdir } from 'node:os';
|
import { tmpdir } from 'node:os';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||||
|
|
@ -278,7 +278,114 @@ describe('loadMigrationConfig (real initialization path, isolated subprocess)',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('config loader .env isolation used by runInitInIsolatedSubprocess', () => {
|
||||||
|
// Arrange a parent dir whose .env would flip the dev target to QA PostgreSQL and leak a sentinel,
|
||||||
|
// then load config from a child dir with or without its own empty .env.
|
||||||
|
function loadFromChild(childHasEmptyEnv: boolean): {
|
||||||
|
exitCode: number;
|
||||||
|
stdout: string;
|
||||||
|
stderr: string;
|
||||||
|
} {
|
||||||
|
const parentDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-parent-'));
|
||||||
|
const childDir = join(parentDir, 'child');
|
||||||
|
try {
|
||||||
|
mkdirSync(childDir);
|
||||||
|
writeFileSync(
|
||||||
|
join(parentDir, '.env'),
|
||||||
|
[
|
||||||
|
'DB_MIGRATE_ENV_LEAK_SENTINEL=leaked',
|
||||||
|
'POSTGRES_PORT=5432',
|
||||||
|
'POSTGRES_DATABASE=trading_bot',
|
||||||
|
'DATABASE_POSTGRES_PORT=5432',
|
||||||
|
'DATABASE_POSTGRES_DATABASE=trading_bot',
|
||||||
|
].join('\n'),
|
||||||
|
'utf8'
|
||||||
|
);
|
||||||
|
if (childHasEmptyEnv) {
|
||||||
|
writeFileSync(join(childDir, '.env'), '', 'utf8');
|
||||||
|
}
|
||||||
|
const script = `import { loadMigrationConfig, resolveMigrationTarget } from ${JSON.stringify(dbMigrateModuleUrl)};
|
||||||
|
const config = loadMigrationConfig();
|
||||||
|
const resolved = resolveMigrationTarget(config, process.env);
|
||||||
|
console.log(JSON.stringify({ sentinel: process.env.DB_MIGRATE_ENV_LEAK_SENTINEL ?? null, targetLabel: resolved.targetLabel }));`;
|
||||||
|
const result = Bun.spawnSync([process.execPath, '-e', script], {
|
||||||
|
cwd: childDir,
|
||||||
|
env: {
|
||||||
|
PATH: process.env.PATH ?? '',
|
||||||
|
HOME: childDir,
|
||||||
|
NODE_ENV: 'development',
|
||||||
|
POSTGRES_HOST: 'localhost',
|
||||||
|
DATABASE_POSTGRES_HOST: 'localhost',
|
||||||
|
POSTGRES_USERNAME: 'dev_user',
|
||||||
|
POSTGRES_PASSWORD: 'dev_pass',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
exitCode: result.exitCode,
|
||||||
|
stdout: new TextDecoder().decode(result.stdout).trim(),
|
||||||
|
stderr: new TextDecoder().decode(result.stderr).trim(),
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
rmSync(parentDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test('an empty .env in the working directory stops the loader from reading a parent .env', () => {
|
||||||
|
const result = loadFromChild(true);
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(0);
|
||||||
|
const parsed = JSON.parse(result.stdout) as { sentinel: string | null; targetLabel: string };
|
||||||
|
expect(parsed.sentinel).toBeNull();
|
||||||
|
// Defaults (no parent override) resolve to the isolated dev database, not QA.
|
||||||
|
expect(parsed.targetLabel).not.toBe('localhost:5432/trading_bot');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without the empty .env the parent .env leaks in and the QA target is rejected', () => {
|
||||||
|
const result = loadFromChild(false);
|
||||||
|
|
||||||
|
expect(result.exitCode).not.toBe(0);
|
||||||
|
expect(result.stderr).toMatch(/QA PostgreSQL/i);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('db-migrate CLI subprocess', () => {
|
describe('db-migrate CLI subprocess', () => {
|
||||||
|
test(
|
||||||
|
'exits non-zero with a clear message when the dev PostgreSQL target is unreachable',
|
||||||
|
() => {
|
||||||
|
const isolatedDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-unreachable-'));
|
||||||
|
try {
|
||||||
|
writeFileSync(join(isolatedDir, '.env'), '', 'utf8');
|
||||||
|
const result = Bun.spawnSync(
|
||||||
|
[process.execPath, 'run', join(root, 'scripts/db-migrate.ts'), '--track', 'master'],
|
||||||
|
{
|
||||||
|
cwd: isolatedDir,
|
||||||
|
env: {
|
||||||
|
PATH: process.env.PATH ?? '',
|
||||||
|
HOME: isolatedDir,
|
||||||
|
NODE_ENV: 'development',
|
||||||
|
POSTGRES_HOST: '127.0.0.1',
|
||||||
|
POSTGRES_PORT: '1',
|
||||||
|
POSTGRES_DATABASE: 'trading_bot_dev',
|
||||||
|
DATABASE_POSTGRES_HOST: '127.0.0.1',
|
||||||
|
DATABASE_POSTGRES_PORT: '1',
|
||||||
|
DATABASE_POSTGRES_DATABASE: 'trading_bot_dev',
|
||||||
|
},
|
||||||
|
timeout: 30000,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const stdout = new TextDecoder().decode(result.stdout);
|
||||||
|
const stderr = new TextDecoder().decode(result.stderr);
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(1);
|
||||||
|
expect(stderr).toMatch(/db:migrate failed: .*connect/i);
|
||||||
|
expect(stdout).not.toMatch(/Applied \d+ migration/);
|
||||||
|
} finally {
|
||||||
|
rmSync(isolatedDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
40000
|
||||||
|
);
|
||||||
|
|
||||||
test('exits non-zero with valid tracks on stderr for --track bogus without connecting to DB', () => {
|
test('exits non-zero with valid tracks on stderr for --track bogus without connecting to DB', () => {
|
||||||
const result = Bun.spawnSync(
|
const result = Bun.spawnSync(
|
||||||
[process.execPath, 'run', 'scripts/db-migrate.ts', '--track', 'bogus'],
|
[process.execPath, 'run', 'scripts/db-migrate.ts', '--track', 'bogus'],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue