/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @oncall memory_lab */ /** * The test suite never writes into the user's own records directory or * findings index: every test script imports `test-isolation.mjs` first, every * other `node` command of the package's test scripts `--import`s it, and the * tests that record the most, run under a HOME with a Meta CLI on the PATH, * leave that HOME untouched. * * npm run build-pkg && node scripts/check-test-isolation.mjs */ import './test-isolation.mjs'; import assert from 'assert/strict'; import {spawnSync} from 'child_process'; import fs from 'fs'; import {test} from 'node:test'; import os from 'os'; import path from 'path'; import {fileURLToPath} from 'url'; const here = path.dirname(fileURLToPath(import.meta.url)); const ISOLATION = "import './test-isolation.mjs';"; test('every test script imports the isolation before anything else', () => { const missing = fs .readdirSync(here) .filter(f => /^check-.*\.mjs$/.test(f)) .filter(f => { const imports = fs .readFileSync(path.join(here, f), 'utf8') .split('\n') .filter(l => l.startsWith('import ')); return imports[0] !== ISOLATION; }); assert.deepEqual(missing, []); }); /** The `node` commands of the `test-*` scripts that do not load the isolation. */ function bareNodeCommands(scripts) { return Object.entries(scripts) .filter(([name]) => name.startsWith('test-')) .flatMap(([, cmd]) => cmd.split(/&&|\|\||[;|&\n]/)) .map(c => c .trim() .replace(/^[({\s]+/, '') .replace(/^(?:\w+=\S*\s+)+/, ''), ) .filter(c => /^node(?:\s|$)/.test(c)) .filter( c => !/^node scripts\/check-[\w-]+\.mjs(?:\s|$)/.test(c) && !/^node --import \.\/scripts\/test-isolation\.mjs\s/.test(c) && !/^node scripts\/sync-tool-catalog\.mjs(?:\s|$)/.test(c), ); } test('every other node command of the test scripts imports it', () => { const pkg = JSON.parse( fs.readFileSync(path.join(here, '..', 'package.json'), 'utf8'), ); assert.deepEqual(bareNodeCommands(pkg.scripts), []); }); test('a node command chained after a check script by any shell operator is still checked', () => { const x = 'node dist/x.test.js'; for (const sep of ['&&', ';', '||', '|', '&', '\n']) { assert.deepEqual( bareNodeCommands({'test-a': `node scripts/check-a.mjs ${sep} ${x}`}), [x], JSON.stringify(sep), ); } assert.deepEqual( bareNodeCommands({ 'test-b': 'node dist/x.test.js --import ./scripts/test-isolation.mjs', 'test-c': 'FOO=1 node dist/y.test.js', 'test-d': 'node --import ./scripts/test-isolation.mjs --test dist/z.js', build: 'node dist/w.js', }), [ 'node dist/x.test.js --import ./scripts/test-isolation.mjs', 'node dist/y.test.js', ], ); }); test('the recording tests leave HOME alone, with a Meta CLI on the PATH', () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), 'memlab-real-home-')); const bin = path.join(home, 'bin'); fs.mkdirSync(bin); fs.writeFileSync(path.join(bin, 'meta'), '#!/bin/sh\nexit 0\n'); fs.chmodSync(path.join(bin, 'meta'), 0o755); const env = { ...process.env, HOME: home, PATH: `${bin}${path.delimiter}${process.env.PATH ?? ''}`, }; delete env.MEMLAB_TEST_HOME; for (const script of [ 'check-record-upload.mjs', 'check-session-record.mjs', 'check-memlab-cli-record.mjs', ]) { const run = spawnSync(process.execPath, [path.join(here, script)], { env, encoding: 'utf8', }); assert.equal(run.status, 0, `${script}: ${run.stdout}${run.stderr}`); } assert.deepEqual(fs.readdirSync(home), ['bin']); fs.rmSync(home, {recursive: true, force: true}); }); test('a hidden meta keeps the rest of its PATH directory, and the HOME is removed at exit', () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'memlab-shared-bin-')); for (const name of ['meta', 'tool']) { fs.writeFileSync(path.join(dir, name), '#!/bin/sh\necho ran\n'); fs.chmodSync(path.join(dir, name), 0o755); } const probe = [ "import './scripts/test-isolation.mjs';", "import {execFileSync} from 'child_process';", "import fs from 'fs';", "import path from 'path';", 'const dirs = process.env.PATH.split(path.delimiter);', 'const has = n => dirs.some(d => fs.existsSync(path.join(d, n)));', "console.log(JSON.stringify({home: process.env.HOME, meta: has('meta'), tool: execFileSync('tool').toString().trim()}));", ].join('\n'); const env = { ...process.env, PATH: `${dir}${path.delimiter}/bin${path.delimiter}/usr/bin`, }; delete env.MEMLAB_TEST_HOME; const run = spawnSync( process.execPath, ['--input-type=module', '-e', probe], {env, encoding: 'utf8', cwd: path.join(here, '..')}, ); assert.equal(run.status, 0, run.stderr); const seen = JSON.parse(run.stdout); assert.equal(seen.meta, false); assert.equal(seen.tool, 'ran'); assert.equal( fs.existsSync(seen.home), false, 'the test HOME was left behind', ); fs.rmSync(dir, {recursive: true, force: true}); });