Files
RuneStone/tests/unit/release/buildInstaller.spec.ts
T
2026-11-03 00:23:07 -07:00

132 lines
8.1 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, readFile, writeFile, copyFile, readdir, rm, access } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { buildCommands, describePlan, parseArguments, planInstallerBuild, runInstallerBuild, versionFiles } from '../../../scripts/buildInstaller.mjs';
import { nextHotfix, releaseVersion } from '../../../scripts/releaseVersion.mjs';
const repository = path.resolve('.');
const exists = (file: string) => access(file).then(() => true, () => false);
// A copy of the files that the installer script reads.
async function fixture() {
const root = await mkdtemp(path.join(tmpdir(), 'runestone-installer-build-'));
for (const file of [...versionFiles, 'src-tauri/tauri.windows.conf.json', 'src-tauri/windows/hooks.nsh']) {
await mkdir(path.dirname(path.join(root, file)), { recursive: true });
await copyFile(path.join(repository, file), path.join(root, file));
}
const before = await Promise.all(versionFiles.map(file => readFile(path.join(root, file), 'utf8')));
const current = releaseVersion(JSON.parse(before[0]).version);
return { root, before, current, next: releaseVersion(nextHotfix(current.version)) };
}
const contents = (root: string) => Promise.all(versionFiles.map(file => readFile(path.join(root, file), 'utf8')));
async function withFixture(body: (setup: Awaited<ReturnType<typeof fixture>>) => Promise<void>) {
const setup = await fixture();
try { await body(setup); } finally { await rm(setup.root, { recursive: true, force: true }); }
}
test('the installer script reads --version and --dry-run', () => {
assert.deepEqual(parseArguments([]), { version: undefined, dryRun: false });
assert.deepEqual(parseArguments(['--version', '0.1.44a', '--dry-run']), { version: '0.1.44a', dryRun: true });
assert.deepEqual(parseArguments(['--dry-run', '--version', '0.1.45']), { version: '0.1.45', dryRun: true });
for (const args of [['--version'], ['0.1.45'], ['--version', '0.1.45', '--version', '0.1.46'], ['--force']]) assert.throws(() => parseArguments(args));
});
test('a completed build sets the version fields only after the installer exists', () => withFixture(async ({ root, before, next }) => {
const plan = await planInstallerBuild(root, next.version);
assert.deepEqual(await contents(root), before, 'Planning must not change a file.');
assert.deepEqual(plan.override, { version: next.toolchain });
const plain = describePlan(plan);
assert.match(plain, new RegExp(`Configuration override: \\{"version":"${next.toolchain.replace('+', '\\+')}"\\}`));
assert.match(plain, /compile: .* build --no-bundle --config .* -- --locked/);
const staging = path.join(root, 'output/attempt');
const overridePath = path.join(staging, 'tauri.version.json');
const calls: { command: string, args: string[], files: string[] }[] = [];
const installer = await runInstallerBuild(plan, async (command: string, args: string[]) => {
calls.push({ command, args, files: await contents(root) });
if (args.includes('bundle')) {
assert.deepEqual(JSON.parse(await readFile(overridePath, 'utf8')), { version: next.toolchain });
await mkdir(path.dirname(plan.artifacts.bundled), { recursive: true });
await writeFile(plan.artifacts.bundled, 'setup');
}
}, staging);
assert.deepEqual(calls.map(({ command, args }) => ({ command, args })),
buildCommands(plan, overridePath).map(({ command, args }) => ({ command, args })));
for (const call of calls) {
// Tauri, Cargo, and the bundler get the version from the override. The front end gets it from plan.env.
assert.deepEqual(call.files, before, 'No version file changes during the build.');
assert.deepEqual(plan.env, { RUNESTONE_VERSION: next.version });
}
assert.equal(installer, path.join(root, 'releases', next.version, `RuneStone_${next.version}_x64-setup.exe`));
assert.equal(await readFile(installer, 'utf8'), 'setup');
assert.equal(await exists(plan.artifacts.bundled), false);
assert.equal(await exists(plan.artifacts.renamed), false);
const metadata = JSON.parse(await readFile(plan.artifacts.metadata, 'utf8'));
assert.deepEqual([metadata.version, metadata.toolchainVersion, metadata.windowsVersion, metadata.bytes], [next.version, next.toolchain, next.windows, 5]);
const after = await contents(root);
assert.deepEqual(after, plan.updated);
assert.equal(JSON.parse(after[0]).version, next.version);
assert.equal(JSON.parse(after[1]).version, next.toolchain);
assert.match(after[2], new RegExp(`^version = "${next.toolchain.replace('+', '\\+')}"`, 'm'));
assert.match(after[2], new RegExp(`\\[package\\.metadata\\.runestone\\]\\r?\\nversion = "${next.version}"`));
assert.match(after[3], new RegExp(`name = "runestone"\\r?\\nversion = "${next.toolchain.replace('+', '\\+')}"`));
}));
test('a failed build leaves the version fields unchanged', () => withFixture(async ({ root, before, next }) => {
for (const failure of ['compile', 'version-resource', 'bundle', 'missing setup file', 'oversized setup file']) {
const plan = await planInstallerBuild(root, next.version);
if (failure === 'oversized setup file') plan.maxSetupBytes = 4;
const staging = path.join(root, 'output', failure.replaceAll(' ', '-'));
const steps = buildCommands(plan, path.join(staging, 'tauri.version.json'));
let index = 0;
const attempt = runInstallerBuild(plan, async () => {
const step = steps[index++].step;
if (step === failure) throw Error('The build command failed with exit code 1');
// A setup file over the size limit, as with a bundled browser engine.
if (step === 'bundle' && failure === 'oversized setup file') {
await mkdir(path.dirname(plan.artifacts.bundled), { recursive: true });
await writeFile(plan.artifacts.bundled, 'setup');
}
}, staging);
await assert.rejects(attempt, failure === 'oversized setup file' ? /more than the limit/ : Error);
assert.deepEqual(await contents(root), before, 'A failure during ' + failure + ' must restore the version fields.');
assert.equal(await exists(plan.artifacts.release), false);
assert.equal(await exists(plan.artifacts.metadata), false);
assert.ok((await readdir(staging)).includes('package.json.before'));
}
}));
test('the plan stops before a build that breaks the release rules', () => withFixture(async ({ root, before, current, next }) => {
const tauriConfig = path.join(root, 'src-tauri/tauri.conf.json');
const windowsConfig = path.join(root, 'src-tauri/tauri.windows.conf.json');
const windowsBefore = await readFile(windowsConfig, 'utf8');
await assert.rejects(planInstallerBuild(root, '0.1.44-rc1'), /release version such as/);
await writeFile(tauriConfig, before[1].replace(`"version": "${current.toolchain}"`, '"version": "9.9.9"'));
await assert.rejects(planInstallerBuild(root, next.version), /do not match/);
await writeFile(tauriConfig, before[1]);
const windows = JSON.parse(windowsBefore);
windows.bundle.windows.allowDowngrades = true;
await writeFile(windowsConfig, JSON.stringify(windows));
await assert.rejects(planInstallerBuild(root, next.version), /Downgrade protection/);
windows.bundle.windows.allowDowngrades = false;
delete windows.bundle.windows.nsis.installerHooks;
await writeFile(windowsConfig, JSON.stringify(windows));
await assert.rejects(planInstallerBuild(root, next.version), /installer hooks/);
await writeFile(windowsConfig, windowsBefore);
const plan = await planInstallerBuild(root, next.version);
await mkdir(plan.releaseDir, { recursive: true });
await writeFile(plan.artifacts.release, 'existing');
await assert.rejects(planInstallerBuild(root, next.version), /already exists/);
await rm(plan.releaseDir, { recursive: true });
// The browser engine is a separate download. The installer must not bundle it.
const config = JSON.parse(before[1]);
config.bundle.resources = { 'browser-engine/': 'browser-engine/' };
await writeFile(tauriConfig, JSON.stringify(config));
await assert.rejects(planInstallerBuild(root, next.version), /must not bundle the browser engine/);
await writeFile(tauriConfig, before[1]);
assert.deepEqual(await contents(root), before);
}));