const fs = require('node:fs'); const path = require('node:path'); const { spawn } = require('node:child_process'); const { verifyProtectedTree } = require('./check-protected-lib'); const root = path.resolve(__dirname, '..'); const temporaryRoot = fs.mkdtempSync(path.join(root, '.test-tmp-')); Object.assign(process.env, { TMPDIR: temporaryRoot, TMP: temporaryRoot, TEMP: temporaryRoot, PYTHONDONTWRITEBYTECODE: '1' }); function protect() { const result = verifyProtectedTree(root); if (!result.ok) throw new Error(`Protected library check failed:\n${result.errors.join('\n')}`); console.log(`Protected library: ${result.fileCount} files match the original baseline.`); } async function runPython() { const result = await new Promise((resolve) => { const child = spawn(process.env.TEST_PYTHON || 'python3', ['-B', '-m', 'unittest', 'discover', '-s', 'tests/python', '-v'], { cwd: root, env: process.env, stdio: ['ignore', 'pipe', 'pipe'] }); let output = '', settled = false; function finish(status, error) { if (settled) return; settled = true; clearTimeout(timer); resolve({ output, status, error }); } const timer = setTimeout(() => { child.kill('SIGKILL'); finish(null, new Error('Python suite exceeded its 60-second timeout.')); }, 60000); child.stdout.on('data', data => { output += data.toString(); }); child.stderr.on('data', data => { output += data.toString(); }); child.on('error', error => finish(null, error)); child.on('close', code => finish(code)); }); const count = Number(result.output.match(/Ran (\d+) tests?/)?.[1] || 0); if (result.error || result.status !== 0 || count === 0 || /skipped=([1-9]\d*)/.test(result.output)) { console.error(`Python downloader: FAILED${result.error ? ` (${result.error.message})` : ''}`); console.error(result.output); process.exitCode = 1; } else { console.log(`Python downloader: ${count} tests passed.`); } } // Direct registration executes the actual tests on supported Node versions and // also works in restricted runtimes that cannot fork Node's test runner. async function main() { protect(); await runPython(); const suites = fs.readdirSync(path.join(root, 'tests')).filter(name => name.endsWith('.test.js')).sort(); if (!suites.length) throw new Error('No Node test suites were found.'); for (const suite of suites) require(path.join(root, 'tests', suite)); } process.once('exit', () => { try { protect(); } catch (error) { console.error(error.message); process.exitCode = 1; } fs.rmSync(temporaryRoot, { recursive: true, force: true }); }); main().catch(error => { console.error(error.message); process.exitCode = 1; });