124 lines
4.9 KiB
JavaScript
124 lines
4.9 KiB
JavaScript
#!/usr/bin/env node
|
|
import { spawn } from 'node:child_process'
|
|
import fs from 'node:fs'
|
|
import { availableParallelism } from 'node:os'
|
|
import path from 'node:path'
|
|
import { cachePath, REPO_ROOT, resolveStorageRoot } from './debug/paths.mjs'
|
|
import { createTemporaryFiles } from './debug/temporary-files.mjs'
|
|
|
|
const frontend = path.join(REPO_ROOT, 'frontend')
|
|
const goTrees = ['./cmd/...', './internal/...', './pkg/...', './scripts/...']
|
|
const hostInputs = [
|
|
'REIMS_GO_TEST_CTL', 'REIMS_GO_TEST_QEMU_IMG', 'REIMS_GO_TEST_OPENCORE', 'REIMS_GO_TEST_OFFLINE_IMG',
|
|
]
|
|
const usage = `Usage: node scripts/test.mjs <profile>
|
|
quick go ./internal/<package> [./pkg/<package> ...]
|
|
quick frontend | storage | tooling
|
|
ci
|
|
release
|
|
webview2 <windows-executable>
|
|
|
|
Set REIMS_TEST_JOBS to cap Go package concurrency (default: at most 4).`
|
|
|
|
function run(label, command, args, env, cwd = REPO_ROOT) {
|
|
console.log(`[test] ${label}`)
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(command, args, { cwd, env, stdio: 'inherit' })
|
|
child.once('error', reject)
|
|
child.once('close', (code, signal) => {
|
|
if (code === 0) resolve()
|
|
else reject(new Error(`${label} failed (${signal ?? `exit ${code}`})`))
|
|
})
|
|
})
|
|
}
|
|
|
|
function select(args) {
|
|
const [profile, area, ...packages] = args
|
|
if ((profile === 'ci' || profile === 'release') && args.length === 1) return { profile }
|
|
if (profile === 'webview2' && args.length === 2) return { profile, executable: path.resolve(area) }
|
|
if (profile !== 'quick') throw new Error(usage)
|
|
if (area === 'go' && packages.length > 0) {
|
|
for (const pkg of packages) {
|
|
if (!/^\.\/(?:[\w-]+\/)*[\w-]+$/.test(pkg)) {
|
|
throw new Error(`Invalid Go package ${pkg}. Use a path such as ./internal/config.`)
|
|
}
|
|
const directory = path.join(REPO_ROOT, pkg)
|
|
if (!fs.statSync(directory, { throwIfNoEntry: false })?.isDirectory()) {
|
|
throw new Error(`Go package directory does not exist: ${pkg}`)
|
|
}
|
|
}
|
|
return { profile, area, packages }
|
|
}
|
|
if (['frontend', 'storage', 'tooling'].includes(area) && packages.length === 0) return { profile, area }
|
|
throw new Error(usage)
|
|
}
|
|
|
|
async function main(args) {
|
|
if (args[0] === '--help' || args[0] === '-h') return console.log(usage)
|
|
const { profile, area, packages, executable } = select(args)
|
|
const jobs = Number(process.env.REIMS_TEST_JOBS ?? Math.min(4, availableParallelism()))
|
|
if (!Number.isSafeInteger(jobs) || jobs < 1) throw new Error('REIMS_TEST_JOBS must be a positive integer')
|
|
|
|
const root = resolveStorageRoot()
|
|
const files = createTemporaryFiles(root)
|
|
const directory = files.create(`test-${profile}`, process.pid)
|
|
const started = performance.now()
|
|
console.log(`[test] ${profile}${area ? ` ${area}` : ''} in ${directory}`)
|
|
try {
|
|
const env = {
|
|
...process.env,
|
|
TEMP: directory,
|
|
TMP: directory,
|
|
TMPDIR: directory,
|
|
GOTMPDIR: directory,
|
|
GOCACHE: cachePath(root, 'go-build'),
|
|
GOMAXPROCS: String(jobs),
|
|
REIMS_TMP_DIR: directory,
|
|
REIMS_GO_DATA_ROOT: path.join(directory, 'data'),
|
|
REIMS_GO_LOCATION_FILE: path.join(directory, 'location.json'),
|
|
REIMS_GO_FIRST_RUN: '',
|
|
REIMS_CTL_CONFIG_DIR: path.join(directory, 'ctl-config'),
|
|
REIMS_CTL_DATA_DIR: path.join(directory, 'ctl-data'),
|
|
}
|
|
for (const key of hostInputs) env[key] = ''
|
|
|
|
const go = (targets, flags = []) => run('Go', 'go', ['test', '-timeout=3m', `-p=${jobs}`, ...flags, ...targets], env)
|
|
const front = (script) => run(`Frontend ${script}`, process.execPath, ['--run', script], env, frontend)
|
|
const tooling = () => run('Tooling', process.execPath, ['--test', 'scripts/debug/debug.test.mjs'], env)
|
|
|
|
if (profile === 'webview2') {
|
|
if (process.platform !== 'win32') throw new Error('WebView2 smoke requires Windows')
|
|
await run('WebView2 first launch', 'powershell.exe', [
|
|
'-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', 'scripts/smoke-webview2.ps1', executable,
|
|
], env)
|
|
} else if (profile === 'quick') {
|
|
if (area === 'go') await go(packages)
|
|
if (area === 'storage') {
|
|
await go(['./internal/config', './internal/dataroot', './internal/datamigrate'])
|
|
await go(['./internal/plugin'], ['-run=^TestStateStore'])
|
|
}
|
|
if (area === 'frontend') await front('test')
|
|
if (area === 'tooling') await tooling()
|
|
} else {
|
|
await front('test')
|
|
await front('typecheck')
|
|
const flags = profile === 'release' ? ['-count=1', '-shuffle=on', '-tags=desktop,production'] : []
|
|
await go(goTrees, ['-vet=all', ...flags])
|
|
await tooling()
|
|
}
|
|
|
|
files.close(directory)
|
|
console.log(`[test] PASS in ${((performance.now() - started) / 1000).toFixed(2)}s`)
|
|
} catch (error) {
|
|
console.error(`[test] Evidence kept in ${directory}`)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
try {
|
|
await main(process.argv.slice(2))
|
|
} catch (error) {
|
|
console.error(`[test] ${error.message}`)
|
|
process.exitCode = 1
|
|
}
|