diff --git a/editor/tests/helpers/user-folders.cjs b/editor/tests/helpers/user-folders.cjs new file mode 100644 index 00000000..7fa3e9a7 --- /dev/null +++ b/editor/tests/helpers/user-folders.cjs @@ -0,0 +1,27 @@ +'use strict'; +// The per-user folders the legacy-import code reads and writes, pointed into a test's own folder. +// +// `RtpManager.downloadFolder` keeps a downloaded or unpacked RTP under XDG_DATA_HOME on Linux, under HOME on macOS +// and under APPDATA on Windows. Tests that redirected only HOME and XDG_DATA_HOME unpacked their fixture RTPs into the +// real `%APPDATA%\RPG Reactor\RTP` on every Windows run, where the editor then took them for installed RTPs. +const path = require('node:path'); + +/** + * Sets HOME, XDG_DATA_HOME (`/`) and APPDATA inside `root`, and clears the two RTP path variables, so + * nothing is read from or written to the user's own folders. Returns the function that puts every one back. + */ +function userFoldersIn(root, data = 'data') { + const set = { + HOME: path.join(root, 'home'), + XDG_DATA_HOME: path.join(root, data), + APPDATA: path.join(root, 'appdata'), + RPG2K_RTP_PATH: undefined, + RPG2K3_RTP_PATH: undefined, + }; + const saved = {}; + const put = (key, value) => { if (value === undefined) delete process.env[key]; else process.env[key] = value; }; + for (const [key, value] of Object.entries(set)) { saved[key] = process.env[key]; put(key, value); } + return () => { for (const [key, value] of Object.entries(saved)) put(key, value); }; +} + +module.exports = { userFoldersIn }; diff --git a/editor/tests/legacy-rtp-archives.test.cjs b/editor/tests/legacy-rtp-archives.test.cjs index f2e4f934..3a887e0b 100644 --- a/editor/tests/legacy-rtp-archives.test.cjs +++ b/editor/tests/legacy-rtp-archives.test.cjs @@ -14,6 +14,7 @@ const legacy = path.join(__dirname, '..', 'src', 'legacy'); const Rar = require(path.join(legacy, 'Rar2.js')); const RM = require(path.join(legacy, 'RtpManager.js')); const I = require(path.join(legacy, 'LegacyImporter.js')); +const { userFoldersIn } = require('./helpers/user-folders.cjs'); /** Bits, most significant first, as RAR reads them. */ function BitWriter() { this.bytes = []; this.acc = 0; this.n = 0; } @@ -79,13 +80,14 @@ test('RAR 3 and RAR 5 are refused with a way forward, not misread', () => { test('an RTP is found inside an archive\'s folders, and its fonts come along', () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'rr-arch-')); try { - const env = { HOME: root, XDG_DATA_HOME: path.join(root, 'data') }; - const saved = { HOME: process.env.HOME, XDG_DATA_HOME: process.env.XDG_DATA_HOME }; - Object.assign(process.env, env); + const restore = userFoldersIn(root); try { + // Where a download goes on this platform, inside root: XDG_DATA_HOME/rtp/2000, or APPDATA's on Windows. + const downloads = RM.downloadFolder('RPG Maker 2000'); + assert.ok(downloads.startsWith(root), 'the RTP folder is the test\'s own, not the user\'s'); const buf = rarArchive([['Data/RTP/CharSet/Chara1.png', 'c', 0x35], ['Data/RTP/ChipSet/Basis.png', 'b', 0x35], ['Data/RTP/ChipSet/Inner.png', 'i', 0x35], ['Data/RTP/ChipSet/Outline.png', 'o', 0x35], ['Data/RTP/Readme.txt', 'r', 0x35], ['Data/RM2000.fon', 'f', 0x30], ['Presetup/Gins.ini', 'g', 0x30]]); const result = RM.unpack(buf); - assert.equal(result.folder, path.join(root, 'data', 'rtp', '2000-donmiguel'), 'Don Miguel\'s RTP beside the official one'); + assert.equal(result.folder, downloads + '-donmiguel', 'Don Miguel\'s RTP beside the official one'); assert.deepEqual(fs.readdirSync(result.folder).sort(), ['CharSet', 'ChipSet', 'RM2000.fon', 'rpg-reactor-rtp.json']); // a zip with the RTP at its top, and a zip around an installer const zip = (entries) => { @@ -100,9 +102,9 @@ test('an RTP is found inside an archive\'s folders, and its fonts come along', ( return Buffer.concat([...locals, central, Buffer.from([0x50, 0x4b, 5, 6, 0, 0, 0, 0]), u16(entries.length), u16(entries.length), u32(central.length), u32(offset), u16(0)]); }; const plain = RM.unpack(zip([['My RTP/CharSet/Actor1.png', 'a'], ['My RTP/ChipSet/World.png', 'w'], ['My RTP/ChipSet/Exterior.png', 'e'], ['My RTP/ChipSet/Interior.png', 'i'], ['My RTP/ChipSet/Dungeon.png', 'd'], ['My RTP/ChipSet/Ship.png', 's']])); - assert.equal(plain.folder, path.join(root, 'data', 'rtp', '2000'), 'the official names go where a download goes'); + assert.equal(plain.folder, downloads, 'the official names go where a download goes'); assert.throws(() => RM.unpack(zip([['readme.txt', 'nothing here']])), /no RTP inside/); - } finally { for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } } + } finally { restore(); } } finally { fs.rmSync(root, { recursive: true, force: true }); } }); function u32(n) { const b = Buffer.alloc(4); b.writeUInt32LE(n >>> 0); return b; } @@ -110,8 +112,7 @@ function u16(n) { const b = Buffer.alloc(2); b.writeUInt16LE(n); return b; } test('a folder leads to its RTP: itself, one inside (not a game\'s), or an installer in it', async () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'rr-pick-')); - const saved = process.env.XDG_DATA_HOME; - process.env.XDG_DATA_HOME = path.join(root, 'data'); + const restore = userFoldersIn(root); try { const mk = (...parts) => fs.mkdirSync(path.join(root, ...parts), { recursive: true }); mk('RPG Maker 2000', 'Projects', 'Game', 'CharSet'); mk('RPG Maker 2000', 'Projects', 'Game', 'ChipSet'); @@ -127,19 +128,17 @@ test('a folder leads to its RTP: itself, one inside (not a game\'s), or an insta mk('Empty'); await assert.rejects(RM.rtpFrom(path.join(root, 'Empty')), /no CharSet and ChipSet folders/); } finally { - if (saved === undefined) delete process.env.XDG_DATA_HOME; else process.env.XDG_DATA_HOME = saved; + restore(); fs.rmSync(root, { recursive: true, force: true }); } }); test('of the RTPs on hand, a game gets the one answering to most of its names, and either RTP answers to both', () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'rr-rank-')); - const saved = { HOME: process.env.HOME, XDG_DATA_HOME: process.env.XDG_DATA_HOME, RPG2K_RTP_PATH: process.env.RPG2K_RTP_PATH, RPG2K3_RTP_PATH: process.env.RPG2K3_RTP_PATH }; - Object.assign(process.env, { HOME: path.join(root, 'home'), XDG_DATA_HOME: path.join(root, 'data') }); - delete process.env.RPG2K_RTP_PATH; delete process.env.RPG2K3_RTP_PATH; + const restore = userFoldersIn(root); try { const put = (dir, folder, names) => { fs.mkdirSync(path.join(dir, folder), { recursive: true }); for (const n of names) fs.writeFileSync(path.join(dir, folder, n + '.png'), n); }; - const official = path.join(root, 'data', 'rtp', '2000'), dm = official + '-donmiguel'; + const official = RM.downloadFolder('RPG Maker 2000'), dm = official + '-donmiguel'; put(official, 'ChipSet', ['World', 'Exterior', 'Interior', 'Dungeon', 'Ship']); put(official, 'CharSet', ['Actor1']); put(dm, 'ChipSet', ['Basis', 'Outline', 'Inner', 'Dungeon', 'Ship', 'ChipSet1']); put(dm, 'CharSet', ['Chara1', 'Chubby2']); const game = { ChipSet: ['Basis', 'ChipSet1'], CharSet: ['Chubby2'] }; @@ -152,7 +151,7 @@ test('of the RTPs on hand, a game gets the one answering to most of its names, a assert.equal(fs.readFileSync(path.join(view.folder, 'CharSet', 'Actor1.png'), 'utf8'), 'Chara1'); view.cleanup(); } finally { - for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } + restore(); fs.rmSync(root, { recursive: true, force: true }); } }); diff --git a/editor/tests/legacy-rtp-title.test.cjs b/editor/tests/legacy-rtp-title.test.cjs index 4e10067d..5c6a2ec1 100644 --- a/editor/tests/legacy-rtp-title.test.cjs +++ b/editor/tests/legacy-rtp-title.test.cjs @@ -13,6 +13,8 @@ const editorRoot = path.join(__dirname, '..'); const runtime = path.join(editorRoot, '..', 'runtime'); const I = require(path.join(editorRoot, 'src', 'legacy', 'LegacyImporter.js')); const K = require(path.join(editorRoot, 'src', 'legacy', 'LegacyConvert.js')); +const RM = require(path.join(editorRoot, 'src', 'legacy', 'RtpManager.js')); +const { userFoldersIn } = require('./helpers/user-folders.cjs'); function rtpFolder(dir) { fs.mkdirSync(path.join(dir, 'CharSet'), { recursive: true }); @@ -22,23 +24,21 @@ function rtpFolder(dir) { test('an RTP kept beside the game, or in EasyRPG\'s folder, is found without being named', () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'rr-rtp-')); - const saved = { HOME: process.env.HOME, XDG_DATA_HOME: process.env.XDG_DATA_HOME, RPG2K_RTP_PATH: process.env.RPG2K_RTP_PATH, RPG2K3_RTP_PATH: process.env.RPG2K3_RTP_PATH }; + const restore = userFoldersIn(root, 'xdg'); try { - process.env.HOME = path.join(root, 'home'); - process.env.XDG_DATA_HOME = path.join(root, 'xdg'); - delete process.env.RPG2K_RTP_PATH; delete process.env.RPG2K3_RTP_PATH; const game = path.join(root, 'games', 'Some Game'); fs.mkdirSync(game, { recursive: true }); assert.equal(I.findRtp('RPG Maker 2000', { source: game }), null); const beside = rtpFolder(path.join(root, 'games', 'RTP')); assert.equal(I.findRtp('RPG Maker 2000', { source: game }), beside); fs.rmSync(beside, { recursive: true }); - const easyrpg = rtpFolder(path.join(root, 'xdg', 'rtp', '2003')); + // EasyRPG's folder on Linux, the RPG Reactor folder in APPDATA on Windows: where a downloaded RTP goes. + const easyrpg = rtpFolder(RM.downloadFolder('RPG Maker 2003')); assert.equal(I.findRtp('RPG Maker 2003', { source: game }), easyrpg); assert.equal(I.findRtp('RPG Maker 2003', { source: game, rtpPath: rtpFolder(path.join(root, 'named')) }), path.join(root, 'named'), 'a named folder comes first'); assert.ok(I.rtpCandidates('RPG Maker 2000', {}).some(p => /ASCII.RPG2000.RTP$/.test(p)), 'the installers\' own folders are still looked in'); } finally { - for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } + restore(); fs.rmSync(root, { recursive: true, force: true }); } }); @@ -152,10 +152,8 @@ test('a window with no width or height tiles no back', () => { test('the RTP is chosen afresh for each game: a folder picked in the dialog is a place to look, and clearing means none', () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'rr-known-')); - const saved = { HOME: process.env.HOME, XDG_DATA_HOME: process.env.XDG_DATA_HOME, RPG2K_RTP_PATH: process.env.RPG2K_RTP_PATH, RPG2K3_RTP_PATH: process.env.RPG2K3_RTP_PATH }; + const restore = userFoldersIn(root, 'xdg'); try { - process.env.HOME = path.join(root, 'home'); process.env.XDG_DATA_HOME = path.join(root, 'xdg'); - delete process.env.RPG2K_RTP_PATH; delete process.env.RPG2K3_RTP_PATH; const game = path.join(root, 'games', 'Game'); fs.mkdirSync(game, { recursive: true }); const used = rtpFolder(path.join(root, 'elsewhere', 'My RTP')); @@ -167,7 +165,7 @@ test('the RTP is chosen afresh for each game: a folder picked in the dialog is a assert.match(dialog, /rtpMode = 'auto';\n\s+updateRtp\(\);/, 'picking a game finds its RTP afresh'); assert.match(dialog, /noRtp: rtpMode === 'none'/); } finally { - for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } + restore(); fs.rmSync(root, { recursive: true, force: true }); } }); diff --git a/editor/tests/run-tier.cjs b/editor/tests/run-tier.cjs index d8d433cf..de5c7760 100644 --- a/editor/tests/run-tier.cjs +++ b/editor/tests/run-tier.cjs @@ -16,6 +16,7 @@ */ 'use strict'; const fs = require('node:fs'), os = require('node:os'), path = require('node:path'), crypto = require('node:crypto'), { spawnSync } = require('node:child_process'); +const { pathToFileURL } = require('node:url'); const Graph = require('./helpers/test-graph.cjs'), Inputs = require('./helpers/test-inputs.cjs'); const here = __dirname, editor = path.dirname(here); @@ -160,19 +161,50 @@ function main(argv) { // Every run records each file's seconds (times-last.json, merged into times.json after): what each file costs on // this machine, kept current without a separate measuring run; --measure also writes them where asked. const measure = option('measure'), lastTimes = path.join(dir, 'times-last.json'); - flags.push('--test-reporter=' + path.join(here, 'helpers', 'file-times-reporter.mjs'), '--test-reporter-destination=' + (measure ? path.resolve(measure) : lastTimes), '--test-reporter=spec', '--test-reporter-destination=stdout'); + // The reporter is loaded as an ES module, so it is named by a file URL: a bare Windows path (`D:\…`) reads as a URL + // whose scheme is the drive letter, and Node refuses it (ERR_UNSUPPORTED_ESM_URL_SCHEME). + const timesTo = measure ? path.resolve(measure) : lastTimes; + flags.push('--test-reporter=' + pathToFileURL(path.join(here, 'helpers', 'file-times-reporter.mjs')).href, '--test-reporter-destination=' + timesTo, '--test-reporter=spec', '--test-reporter-destination=stdout'); // Each test process reports as it ends, so what passed is kept even if this run never finishes. const facts = plan ? plan.facts : (({ nodes, salt }) => allTestFiles().map(file => Graph.describe(nodes, file, salt)))(graphOf()); fs.writeFileSync(path.join(dir, 'run.json'), JSON.stringify(Object.fromEntries(facts.map(fact => [fact.file, fact.fingerprint])))); // How many files run at once, for the tests that hold code to a time budget (`helpers/timing.cjs`). const env = { ...process.env, RR_TEST_PEAKS: path.join(dir, 'run.jsonl'), RR_TEST_WORKERS: String(option('concurrency') || workers) }; flags.unshift('--require=' + path.join(here, 'helpers', 'peak-probe.cjs')); - const run = spawnSync(process.execPath, [...flags, ...passed, ...files], { cwd: editor, stdio: 'inherit', env }); - if (run.error) throw run.error; + // One process for the whole list where its command line fits; batches run one after another where it does not + // (Windows allows 32,767 characters, which about 700 test files fill). Each batch's passes go to the same journal, + // read once at the end; its times are merged as it ends, since the next batch writes over the file. + let status = 0; + const times = {}; + for (const batch of commandBatches([process.execPath, ...flags, ...passed], files)) { + const run = spawnSync(process.execPath, [...flags, ...passed, ...batch], { cwd: editor, stdio: 'inherit', env }); + if (run.error) throw run.error; + try { Object.assign(times, JSON.parse(fs.readFileSync(timesTo, 'utf8'))); } catch (error) { /* the batch wrote none */ } + keepTimes(dir, timesTo); + if (!status) status = run.status === null ? 1 : run.status; + } + if (measure) fs.writeFileSync(timesTo, JSON.stringify(times, null, 1)); loadPasses(dir); - keepTimes(dir, measure ? path.resolve(measure) : lastTimes); - return run.status === null ? 1 : run.status; + return status; +} + +/** The longest command line a process may be started with: Windows' CreateProcess limit, with room for quoting. */ +const COMMAND_LINE_LIMIT = process.platform === 'win32' ? 30000 : Infinity; + +/** `files` cut into runs of one command each, in order, every one fitting COMMAND_LINE_LIMIT with `fixed` before it. */ +function commandBatches(fixed, files, limit = COMMAND_LINE_LIMIT) { + const length = args => args.reduce((sum, arg) => sum + arg.length + 3, 0); + const room = limit - length(fixed), batches = []; + let batch = [], used = 0; + for (const file of files) { + const cost = file.length + 3; + if (batch.length && used + cost > room) { batches.push(batch); batch = []; used = 0; } + batch.push(file); + used += cost; + } + if (batch.length) batches.push(batch); + return batches; } if (require.main === module) process.exit(main(process.argv.slice(2))); -module.exports = { allTestFiles, heavyList, tier, reaching, owed, unchangedSince, loadInputs }; +module.exports = { allTestFiles, heavyList, tier, reaching, owed, unchangedSince, loadInputs, commandBatches }; diff --git a/editor/tests/test-tiers.test.cjs b/editor/tests/test-tiers.test.cjs index aee549c4..5bf9ba64 100644 --- a/editor/tests/test-tiers.test.cjs +++ b/editor/tests/test-tiers.test.cjs @@ -102,3 +102,20 @@ test('a test that started a worker is judged by its own reads and by what the wo // A script that reads what names can't reach (a folder listed) has no print: such a test always runs. assert.equal(Graph.workerPrint(nodes, ['editor/tests/test-tiers.test.cjs']), null); }); + +test('a file list too long for one command line is cut into runs that each fit, in order and losing none', () => { + const { commandBatches } = require('./run-tier.cjs'); + const files = Array.from({ length: 1046 }, (_, i) => `tests/some-fairly-long-test-name-${i}.test.cjs`); + const fixed = ['C:\Program Files\nodejs\node.exe', '--test', '--test-concurrency=16']; + const batches = commandBatches(fixed, files, 30000); + assert.ok(batches.length > 1, 'more than one run'); + assert.deepEqual(batches.flat(), files, 'every file once, in the order given'); + for (const batch of batches) assert.ok([...fixed, ...batch].reduce((sum, arg) => sum + arg.length + 3, 0) <= 30000); + assert.deepEqual(commandBatches(fixed, files, Infinity), [files], 'one run where there is no limit'); + assert.deepEqual(commandBatches(fixed, []), [], 'nothing to run, no run'); +}); + +test('the time reporter is named by a file URL, which Node loads on Windows too', () => { + const source = require('node:fs').readFileSync(path.join(__dirname, 'run-tier.cjs'), 'utf8'); + assert.match(source, /'--test-reporter=' \+ pathToFileURL\(path\.join\(here, 'helpers', 'file-times-reporter\.mjs'\)\)\.href/); +});