Files
wekan/tests/sharpEveryPlatform.test.cjs
T
Lauri Ojansivu 943bfd9197 Make sharp load on every WeKan platform: add each bundle's own sharp package.
sharp makes the attachment thumbnails. Every bundle except amd64 is made
from the amd64 Linux bundle, and npm installed only @img/sharp-linux-x64
there. Reading the published v12.32 zips showed that every other bundle
carried that one package and nothing it could load: arm64, ppc64le, s390x,
riscv64, i386, armhf, armv7, armv6, mac-arm64 and mac-x64, as well as the
Docker images and snaps built from them. Thumbnails fell back to the
full-size image, and nothing failed.

releases/windows-native-packages.mjs becomes platform-native-packages.mjs,
<bundle> <win32|linux|darwin> <arch>. It removes the packages npm would not
install on that target (os, cpu, and on Linux libc), downloads
@img/sharp-<platform>-<arch> and its own libvips at the versions pinned,
checks each against the registry's sha512, and checks every native addon's
header for the target. sharp publishes nothing native for Linux i386 or
loong64, so there it uses @img/sharp-wasm32. That package is now a root
optional dependency in package.json, at sharp's own range. It was run
under Node 26 with no native package: it made a 512 px WebP thumbnail and
a GIF.

check-addon-arch.mjs now reads ELF (with byte order) and Mach-O (including
universal binaries) as well as PE, and follows npm's libc rule on Linux.

The script now runs in build-amd64, build-arm64 (in its arm64 container),
both macOS jobs, all three Windows jobs, and build-extra-arches through
install-node-for-arch.sh. There it runs with the target node's own
process.arch, so armv6/armv7/armhf are arm, ppc64le is ppc64 and i386 is
ia32. build-release-bundle.sh runs it for the local machine.

Against the published v12.32 zips, given their native addons, it added the
right sharp and libvips for all eleven Linux and macOS bundles, kept wasm32
for i386, and passed every bcrypt, argon2 and sharp binary. Checked the
other way, the s390x bundle was refused as ppc64.
tests/platformNativePackages.test.cjs covers the script with a mocked
registry (libvips, wasm fallback, wrong CPU, tampered tarball, path
traversal). tests/sharpEveryPlatform.test.cjs requires every job that makes
a bundle zip to run it for its own target, after the prune and before the
zip, and the wasm32 dependency to stay at sharp's version.

Thanks to xet7 !
2026-10-11 22:40:47 +03:00

128 lines
6.1 KiB
JavaScript

'use strict';
// Guard: every WeKan bundle carries a sharp it can load.
// Run: node tests/sharpEveryPlatform.test.cjs
//
// sharp makes the attachment thumbnails (server/lib/imageThumbnail.js). Every
// bundle except amd64 is made from the amd64 Linux bundle, so in v12.32 every
// one of them - arm64, ppc64le, s390x, riscv64, i386, armhf, armv7, armv6,
// mac-arm64, mac-x64, win64, win-arm64, and the Docker images and snaps built
// from them - had only @img/sharp-linux-x64 and showed full-size images in
// place of thumbnails. Nothing failed; it was found by reading the zips.
//
// So the guard is about the PIPELINE, not one job: every job that makes a
// bundle zip must run releases/platform-native-packages.mjs for its own
// platform and CPU, after the last step that deletes files and before the zip,
// and the WebAssembly fallback (for Linux i386 and loong64, where sharp
// publishes no binary) must be in package.json at sharp's own version.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const ROOT = path.join(__dirname, '..');
const read = (rel) => fs.readFileSync(path.join(ROOT, rel), 'utf8');
const code = (text) => text.split('\n').filter((l) => !/^\s*#/.test(l)).join('\n');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
const workflow = read('.github/workflows/release-all.yml');
function jobs(text) {
const out = {};
const starts = [...text.matchAll(/\n ([a-z0-9-]+):\n/g)].map((m) => ({ name: m[1], at: m.index }));
starts.forEach((s, i) => {
out[s.name] = code(text.slice(s.at, i + 1 < starts.length ? starts[i + 1].at : undefined));
});
return out;
}
const all = jobs(workflow);
const SCRIPT = 'platform-native-packages.mjs';
console.log('sharpEveryPlatform:');
// The jobs that make a bundle zip, and what each must name. build-extra-arches
// runs it inside the target container (install-node-for-arch.sh), where the
// target node's own process.arch is the name sharp looks up.
const EXPECTED = {
'build-amd64': /platform-native-packages\.mjs" \\\n\s*"\$GITHUB_WORKSPACE\/\.build\/bundle" linux x64/,
'build-arm64': /platform-native-packages\.mjs \/bundle linux arm64/,
'build-mac-arm64': /platform-native-packages\.mjs" \.\.\/\.\. darwin arm64/,
'build-mac-x64': /platform-native-packages\.mjs" \.\.\/\.\. darwin x64/,
'build-win64': /platform-native-packages\.mjs" bundle win32 x64/,
'build-win-arm64': /platform-native-packages\.mjs" bundle win32 arm64/,
'build-win32': /platform-native-packages\.mjs" bundle win32 ia32/,
'build-extra-arches': /install-node-for-arch\.sh/,
};
test('every job that makes a bundle zip is one of the expected ones', () => {
const zipping = Object.entries(all)
.filter(([, body]) => /- name: Create .* zip\b/.test(body))
.map(([name]) => name)
.sort();
assert.deepStrictEqual(zipping, Object.keys(EXPECTED).sort(),
'a new bundle job must be added here, with the platform/arch it gives platform-native-packages.mjs');
});
test('each bundle job runs platform-native-packages.mjs for its own target', () => {
for (const [name, re] of Object.entries(EXPECTED)) {
assert.ok(re.test(all[name]), `${name} must run ${SCRIPT} for its own platform and CPU`);
}
});
test('it runs after everything that deletes files, and before the zip', () => {
for (const name of Object.keys(EXPECTED)) {
if (name === 'build-extra-arches') continue;
const body = all[name];
const at = body.indexOf(SCRIPT);
const prune = body.lastIndexOf('prune-unreachable-npm.mjs', at);
assert.ok(prune !== -1 && prune < at, `${name}: after prune-unreachable-npm`);
assert.ok(body.lastIndexOf('bundle-trim.mjs', at) !== -1, `${name}: after bundle-trim`);
const zip = body.search(/- name: Create .* zip\b/);
assert.ok(at < zip, `${name}: before the zip is made`);
}
const install = code(read('releases/install-node-for-arch.sh'));
const at = install.indexOf(`${SCRIPT}" /bundle linux "$(node -p process.arch)"`);
assert.ok(at !== -1, 'install-node-for-arch.sh names the target node\'s own process.arch');
assert.ok(install.indexOf('prune-unreachable-npm.mjs') < at, 'install-node-for-arch.sh: after the prune');
assert.ok(/install-node-for-arch\.sh/.test(all['build-extra-arches']), 'build-extra-arches runs it');
});
test('the local release build runs the same step for its own machine', () => {
const local = code(read('releases/build-release-bundle.sh'));
assert.ok(/platform-native-packages\.mjs" "\$BUNDLE" \\\n\s*"\$\(node -p process\.platform\)" "\$\(node -p process\.arch\)"/.test(local));
});
test('the WebAssembly fallback is a dependency, at sharp\'s own version', () => {
const pkg = JSON.parse(read('package.json'));
assert.ok(pkg.dependencies.sharp, 'sharp is a dependency');
assert.strictEqual(pkg.optionalDependencies?.['@img/sharp-wasm32'], pkg.dependencies.sharp,
'@img/sharp-wasm32 uses the same range as sharp, so they move together');
const lock = JSON.parse(read('package-lock.json'));
assert.strictEqual(lock.packages['']?.optionalDependencies?.['@img/sharp-wasm32'], pkg.dependencies.sharp,
'package-lock.json records it as a root optional dependency');
const sharp = lock.packages['node_modules/sharp'];
const wasm = lock.packages['node_modules/@img/sharp-wasm32'];
assert.ok(sharp && wasm, 'both are locked');
assert.strictEqual(wasm.version, sharp.version, 'and locked to the same version');
assert.ok(lock.packages['node_modules/@emnapi/runtime'], 'with its @emnapi/runtime');
});
// Negative: nothing else may install a sharp platform package by hand, or the
// version could drift from the one sharp pins.
test('no other release script or job installs a sharp platform package itself', () => {
const files = ['.github/workflows/release-all.yml', 'Dockerfile',
...fs.readdirSync(path.join(ROOT, 'releases')).filter((f) => /\.(sh|mjs|py)$/.test(f)).map((f) => `releases/${f}`)];
for (const f of files) {
if (f === `releases/${SCRIPT}`) continue;
const body = code(read(f));
assert.ok(!/npm (?:i|install|pack)\b[^\n]*@img\/sharp-/.test(body), `${f} installs an @img/sharp- package itself`);
}
});
console.log(`\nsharpEveryPlatform: all ${passed} tests passed`);