An open source design system that's fully customizable and agent ready
TypeScript
test(cli): remove packed-CLI fixtures once per run, not in file hooks (#10269)
Christopher Chedeauf31026bf4b8a
diff --git a/packages/cli/api/theme/build/build.packed-old-core.test.mjs b/packages/cli/api/theme/build/build.packed-old-core.test.mjsindex 560b5cd74a..7eec08224a 100644--- a/packages/cli/api/theme/build/build.packed-old-core.test.mjs+++ b/packages/cli/api/theme/build/build.packed-old-core.test.mjs@@ -37,6 +37,12 @@ import * as path from 'node:path';import {fileURLToPath} from 'node:url';import {findInstalledPackage} from '../../../foundation/fs/paths.mjs';+import {+ cliTarball,+ releasePackedCli,+ runFolder,+ tarballFiles,+} from '../../../test-utils/global-local-fixtures.mjs';import {CLI_VERSION} from '../../../test-utils/release-fixtures.mjs';const HERE = path.dirname(fileURLToPath(import.meta.url));@@ -45,8 +51,69 @@ const CLI_PKG = path.join(REPO_ROOT, 'packages/cli');const CORE_DIST = path.join(REPO_ROOT, 'packages/core/dist');const CORE_THEME_ENTRY = path.join(CORE_DIST, 'theme/index.js');-/** The CLI's published file set (packages/cli package.json `files`). */-const PACKED_DIRS = ['clients', 'api', 'assets', 'authoring', 'foundation'];+/**+ * Published asset folders that theme build never reads: page templates, the+ * docs corpus and icon data, three quarters of the CLI's files. Each is one+ * link to the package's own folder. Every published path still resolves, and+ * the fixture writes and deletes only the files the CLI can run.+ */+const LINKED_ASSET_FOLDERS = [+ 'assets/docs',+ 'assets/icons',+ 'assets/templates',+];++/** @param {string} file - A path in the CLI package, with `/` separators. */+function inLinkedFolder(file) {+ return LINKED_ASSET_FOLDERS.some(folder => file.startsWith(`${folder}/`));+}++/**+ * Put the folder `from` at `to` as one link. Where the system refuses the+ * link, copy the folder instead.+ * @param {string} from+ * @param {string} to+ * @param {(target: string, at: string, type: 'junction') => void} [link]+ */+function linkFolder(from, to, link = fs.symlinkSync) {+ fs.mkdirSync(path.dirname(to), {recursive: true});+ try {+ // A junction needs no special right on Windows; elsewhere the type is+ // ignored and this is a plain symbolic link.+ link(from, to, 'junction');+ } catch (error) {+ if (/** @type {NodeJS.ErrnoException} */ (error).code !== 'EPERM') {+ throw error;+ }+ fs.cpSync(from, to, {recursive: true});+ }+}++/**+ * Delete a fixture. `rmSync` removes a link and never follows it, so the+ * folders that the asset links point to stay whole.+ * @param {string} dir+ */+function removeFixture(dir) {+ fs.rmSync(dir, {recursive: true, force: true});+}++/**+ * The regular files under `dir` by their path below it, links not followed.+ * @param {string} dir+ * @param {string} [rel]+ * @returns {string[]}+ */+function filesHeld(dir, rel = '') {+ return fs.readdirSync(dir, {withFileTypes: true}).flatMap(entry => {+ const entryRel = rel ? `${rel}/${entry.name}` : entry.name;+ if (entry.isDirectory()) {+ return filesHeld(path.join(dir, entry.name), entryRel);+ }+ return entry.isFile() ? [entryRel] : [];+ });+}+/** Runtime dependencies the packed CLI resolves from its install root. */const RUNTIME_DEPS = ['@babel/parser',@@ -426,26 +493,31 @@ beforeAll(() => {'packages/core must be built before this suite (vitest.global-setup.node.mjs)',).toBe(true);- root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-packed-old-core-'));+ // In a run's folder, the global teardown removes the fixture with+ // everything else, even when this worker stops before its afterAll.+ root = fs.mkdtempSync(+ path.join(runFolder() ?? os.tmpdir(), 'astryx-packed-old-core-'),+ );consumer = path.join(root, 'app');fs.mkdirSync(consumer, {recursive: true});const modules = path.join(root, 'node_modules');- // The packed CLI, as published.+ // The packed CLI, as published: the files `npm pack` puts in its tarball.const cli = path.join(modules, '@astryxdesign/cli');- fs.mkdirSync(cli, {recursive: true});- for (const dir of PACKED_DIRS) {- fs.cpSync(path.join(CLI_PKG, dir), path.join(cli, dir), {recursive: true});+ /** @type {Set<string>} */+ const made = new Set();+ for (const file of tarballFiles(cliTarball())) {+ if (inLinkedFolder(file)) continue;+ const target = path.join(cli, file);+ if (!made.has(path.dirname(target))) {+ fs.mkdirSync(path.dirname(target), {recursive: true});+ made.add(path.dirname(target));+ }+ fs.copyFileSync(path.join(CLI_PKG, file), target);+ }+ for (const folder of LINKED_ASSET_FOLDERS) {+ linkFolder(path.join(CLI_PKG, folder), path.join(cli, folder));}- fs.mkdirSync(path.join(cli, 'scripts'), {recursive: true});- fs.copyFileSync(- path.join(CLI_PKG, 'scripts/postinstall.mjs'),- path.join(cli, 'scripts/postinstall.mjs'),- );- fs.copyFileSync(- path.join(CLI_PKG, 'package.json'),- path.join(cli, 'package.json'),- );// Its runtime dependencies, resolved from the install root like a real one.// Found from the CLI package rather than the repo root: only a hoisted@@ -1105,7 +1177,78 @@ export const authorThrowsTheme = defineTheme({}, 120_000);afterAll(() => {- if (root) fs.rmSync(root, {recursive: true, force: true});+ if (root && !runFolder()) removeFixture(root);+ releasePackedCli();+});++describe('the packed CLI fixture', () => {+ it("holds the CLI's packed files, three asset folders as links", () => {+ const cli = path.join(root, 'node_modules/@astryxdesign/cli');+ const published = tarballFiles(cliTarball());+ const unresolved = published.filter(+ file => !fs.existsSync(path.join(cli, file)),+ );+ expect(unresolved).toEqual([]);+ expect(+ filesHeld(cli)+ .filter(file => !inLinkedFolder(file))+ .sort(),+ ).toEqual(published.filter(file => !inLinkedFolder(file)).sort());+ for (const folder of LINKED_ASSET_FOLDERS) {+ const at = path.join(cli, folder);+ if (fs.lstatSync(at).isSymbolicLink()) {+ expect(fs.realpathSync(at)).toBe(+ fs.realpathSync(path.join(CLI_PKG, folder)),+ );+ } else {+ expect(filesHeld(at).sort()).toEqual(+ filesHeld(path.join(CLI_PKG, folder)).sort(),+ );+ }+ }+ });++ it('deletes the asset links and never what they point to', () => {+ const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-links-'));+ try {+ const pkg = path.join(scratch, 'package');+ const fixture = path.join(scratch, 'fixture');+ for (const folder of LINKED_ASSET_FOLDERS) {+ fs.mkdirSync(path.join(pkg, folder), {recursive: true});+ fs.writeFileSync(path.join(pkg, folder, 'planted.txt'), folder);+ linkFolder(path.join(pkg, folder), path.join(fixture, folder));+ }+ removeFixture(fixture);+ expect(fs.existsSync(fixture)).toBe(false);+ for (const folder of LINKED_ASSET_FOLDERS) {+ const planted = path.join(pkg, folder, 'planted.txt');+ expect(fs.readFileSync(planted, 'utf8')).toBe(folder);+ }+ } finally {+ fs.rmSync(scratch, {recursive: true, force: true});+ }+ });++ it('copies an asset folder where the system refuses a link', () => {+ const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-links-'));+ try {+ const from = path.join(scratch, 'package/assets/docs');+ const to = path.join(scratch, 'fixture/assets/docs');+ fs.mkdirSync(from, {recursive: true});+ fs.writeFileSync(path.join(from, 'planted.txt'), 'docs');+ linkFolder(from, to, () => {+ throw Object.assign(new Error('operation not permitted'), {+ code: 'EPERM',+ });+ });+ expect(fs.lstatSync(to).isDirectory()).toBe(true);+ expect(fs.readFileSync(path.join(to, 'planted.txt'), 'utf8')).toBe(+ 'docs',+ );+ } finally {+ fs.rmSync(scratch, {recursive: true, force: true});+ }+ });});describe('packed CLI against a core that predates adaptations', () => {diff --git a/packages/cli/test-utils/global-local-fixtures.mjs b/packages/cli/test-utils/global-local-fixtures.mjsindex 7c62f3a7e2..7b163cb8f4 100644--- a/packages/cli/test-utils/global-local-fixtures.mjs+++ b/packages/cli/test-utils/global-local-fixtures.mjs@@ -10,6 +10,11 @@* once for the run (`npm pack`), or packs one itself when no run did.* `extractTarball` unpacks it with no external tool, so the copies hold* exactly the published files.+ * - `cliTarball()` is that tarball, and `tarballFiles(file)` lists the files+ * a tarball holds, the published file set, without unpacking it.+ * - `releasePackedCli()` removes what this process unpacked or packed, unless+ * it sits in the run's folder, which the global teardown removes.+ * `runFolder()` is that folder, for a suite's own fixture.* - `placeCli(dir, version)` puts a copy at `dir`, labeled `version`: every* file is a hard link to the unpacked package except a rewritten* package.json, and its `node_modules` links to this package's installed@@ -25,7 +30,8 @@* from inside this process, the way the cross-platform smoke workflow does,* with no network.*- * @position packages/cli/test-utils — used by test/global-local.test.mjs.+ * @position packages/cli/test-utils — used by test/global-local.test.mjs,+ * and by the theme build test of a packed CLI with an older Core.*/import * as fs from 'node:fs';@@ -44,12 +50,12 @@ import {installCore, tempDir, writeJson} from './release-fixtures.mjs';export const CLI_DIR = fileURLToPath(new URL('..', import.meta.url));/**- * Unpack a `.tgz` written by `npm pack` into `dest`, dropping its leading- * `package/` folder.+ * Each entry of a `.tgz` written by `npm pack`: its path below the leading+ * `package/` folder, its tar type flag, and its contents.* @param {string} file- * @param {string} dest+ * @returns {Generator<{rel: string, type: string, body: Buffer}>}*/-export function extractTarball(file, dest) {+function* tarballEntries(file) {const buf = zlib.gunzipSync(fs.readFileSync(file));let off = 0;/** @type {string | null} */@@ -82,22 +88,85 @@ export function extractTarball(file, dest) {longName = null;const rel = name.replace(/^[^/]+\//u, '');if (!rel || rel.split('/').includes('..')) continue;- const target = path.join(dest, ...rel.split('/'));- if (type === '5') {- fs.mkdirSync(target, {recursive: true});- } else if (type === '0' || type === '\0') {- fs.mkdirSync(path.dirname(target), {recursive: true});- fs.writeFileSync(target, body);- if (/\.(?:mjs|cjs|js)$/u.test(rel) && rel.includes('/bin/')) {- fs.chmodSync(target, 0o755);- }+ yield {rel, type, body};+ }+}++/**+ * Unpack a `.tgz` written by `npm pack` into `dest`, dropping its leading+ * `package/` folder.+ * @param {string} file+ * @param {string} dest+ */+export function extractTarball(file, dest) {+ for (const entry of tarballEntries(file)) writeEntry(dest, entry);+}++/**+ * Write one tarball entry under `dest`: a folder, or a file with its+ * contents. A script under a `bin` folder is made executable.+ * @param {string} dest+ * @param {{rel: string, type: string, body: Buffer}} entry+ */+function writeEntry(dest, {rel, type, body}) {+ const target = path.join(dest, ...rel.split('/'));+ if (type === '5') {+ fs.mkdirSync(target, {recursive: true});+ } else if (type === '0' || type === '\0') {+ fs.mkdirSync(path.dirname(target), {recursive: true});+ fs.writeFileSync(target, body);+ if (/\.(?:mjs|cjs|js)$/u.test(rel) && rel.includes('/bin/')) {+ fs.chmodSync(target, 0o755);}}}+/**+ * The files a `.tgz` written by `npm pack` holds, by their path below its+ * leading `package/` folder: the package's published file set.+ * @param {string} file+ * @returns {string[]}+ */+export function tarballFiles(file) {+ return [...tarballEntries(file)]+ .filter(({type}) => type === '0' || type === '\0')+ .map(({rel}) => rel);+}+/** @type {string | null} */let packed = null;+/** @type {string | null} */+let cliTarballFile = null;++/** The folder this process packed its own tarball in, when no run did. */+let ownTarballDir = '';++/**+ * The folder the node project's global setup made for this run, or null when+ * nothing provided one. Its teardown removes the folder once, after every+ * suite, with each unpacked CLI, pooled copy and suite fixture inside it.+ * @returns {string | null}+ */+export function runFolder() {+ let dir;+ try {+ dir = inject('astryxPackedCliRunDir');+ } catch {+ return null;+ }+ return typeof dir === 'string' && fs.existsSync(dir) ? dir : null;+}++/**+ * Whether the unpacked package `dir` sits in the run's folder.+ * @param {string} dir+ */+function inRunFolder(dir) {+ const run = runFolder();+ return Boolean(run && path.dirname(path.dirname(dir)) === run);+}+/*** The tarball the node project's global setup packed for this run, or null* when nothing provided one, as in another test config.@@ -113,6 +182,25 @@ function providedTarball() {return typeof tarball === 'string' && fs.existsSync(tarball) ? tarball : null;}+/**+ * This CLI's tarball: the one the node project's global setup packed for the+ * run, or one packed once per process when no run packed it.+ * @returns {string} the `.tgz` file+ */+export function cliTarball() {+ if (cliTarballFile) return cliTarballFile;+ const provided = providedTarball();+ if (provided) {+ cliTarballFile = provided;+ } else {+ ownTarballDir = fs.mkdtempSync(+ path.join(os.tmpdir(), 'astryx-cli-tarball-'),+ );+ cliTarballFile = packCli(ownTarballDir);+ }+ return cliTarballFile;+}+/*** This CLI, packed once per run (or once per process when no run packed it)* and unpacked once per process.@@ -120,8 +208,10 @@ function providedTarball() {*/export function packedCli() {if (packed) return packed;- const out = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-packed-cli-'));- packedTarball = providedTarball() ?? packCli(out);+ const out = fs.mkdtempSync(+ path.join(runFolder() ?? os.tmpdir(), 'astryx-packed-cli-'),+ );+ packedTarball = cliTarball();const dir = path.join(out, 'package');extractTarball(packedTarball, dir);packed = dir;@@ -129,6 +219,25 @@ export function packedCli() {return dir;}+/**+ * Remove the CLI this process unpacked, with the copies given back to its+ * pool, and a tarball it packed itself. Leave what sits in the run's folder:+ * the node project's global teardown removes that once, after every suite.+ */+export function releasePackedCli() {+ if (packed && !inRunFolder(packed)) {+ fs.rmSync(path.dirname(packed), {recursive: true, force: true});+ pool.clear();+ packed = null;+ packedRecord = null;+ }+ if (ownTarballDir) {+ fs.rmSync(ownTarballDir, {recursive: true, force: true});+ ownTarballDir = '';+ cliTarballFile = null;+ }+}+/** The tarball {@link packedCli} made, to unpack again. */let packedTarball = '';@@ -190,13 +299,55 @@ function treeRecord(dir, {contents = false} = {}) {* @returns {string | null}*/function firstChange(before, after) {+ return changedPaths(before, after)[0] ?? null;+}++/**+ * Every path whose entry differs between two records, in the order+ * {@link firstChange} finds the first.+ * @param {Map<string, string>} before+ * @param {Map<string, string>} after+ * @returns {string[]}+ */+function changedPaths(before, after) {+ const changed = [];for (const [rel, entry] of before) {- if (after.get(rel) !== entry) return rel;+ if (after.get(rel) !== entry) changed.push(rel);}for (const rel of after.keys()) {- if (!before.has(rel)) return rel;+ if (!before.has(rel)) changed.push(rel);+ }+ return changed;+}++/**+ * Put each of `paths` below `dir` back as the tarball has it. What is there+ * is removed, and the tarball's entries at or below each path are unpacked+ * again. A path the tarball does not have stays removed.+ * @param {string} dir+ * @param {string} tarball+ * @param {string[]} paths+ */+function restorePaths(dir, tarball, paths) {+ const wanted = new Set(paths);+ // The package folder itself changed: no single entry restores it.+ if (wanted.has('.')) {+ fs.rmSync(dir, {recursive: true, force: true});+ extractTarball(tarball, dir);+ return;+ }+ for (const rel of paths) {+ fs.rmSync(path.join(dir, ...rel.split('/')), {+ recursive: true,+ force: true,+ });+ }+ for (const entry of tarballEntries(tarball)) {+ const parts = entry.rel.split('/');+ if (parts.some((_, i) => wanted.has(parts.slice(0, i + 1).join('/')))) {+ writeEntry(dir, entry);+ }}- return null;}/**@@ -286,26 +437,32 @@ let pooledCount = 0;* still use one.* @param {{keep?: boolean}} [options]* @throws {Error} when a test wrote into a file every copy shares. The pool is- * then emptied, and the package is unpacked again from its tarball.+ * then emptied, and the changed files are unpacked again from the tarball.*/export function recyclePlacedCopies({keep = true} = {}) {const copies = placed.splice(0);if (packed && packedRecord) {- const changed = firstChange(+ const changed = changedPaths(packedRecord,treeRecord(packed, {contents: true}),);- if (changed) {+ if (changed.length > 0) {+ // Each pooled copy links the changed files, so none is used again. The+ // run's teardown removes their folders; outside a run, they go now.pool.clear();- fs.rmSync(path.join(path.dirname(packed), 'pool'), {- recursive: true,- force: true,- });- fs.rmSync(packed, {recursive: true, force: true});- extractTarball(packedTarball, packed);+ if (!inRunFolder(packed)) {+ fs.rmSync(path.join(path.dirname(packed), 'pool'), {+ recursive: true,+ force: true,+ });+ }+ // Only the changed paths are unpacked again. Removing and unpacking+ // the whole package rewrites thousands of files, which a busy machine+ // can make outlast the test.+ restorePaths(packed, packedTarball, changed);packedRecord = treeRecord(packed, {contents: true});throw new Error(- `A test changed ${changed}, which every placed CLI copy shares. Unlink a copy's file before you write it, as placeCli does for package.json.`,+ `A test changed ${changed[0]}, which every placed CLI copy shares. Unlink a copy's file before you write it, as placeCli does for package.json.`,);}}diff --git a/packages/cli/test-utils/global-local-fixtures.test.mjs b/packages/cli/test-utils/global-local-fixtures.test.mjsindex 38c5b61619..7fc33c20e7 100644--- a/packages/cli/test-utils/global-local-fixtures.test.mjs+++ b/packages/cli/test-utils/global-local-fixtures.test.mjs@@ -8,7 +8,15 @@* fresh unpack.*/-import {afterAll, afterEach, describe, expect, it} from 'vitest';+import {+ afterAll,+ afterEach,+ beforeAll,+ describe,+ expect,+ inject,+ it,+} from 'vitest';import * as fs from 'node:fs';import * as path from 'node:path';import {@@ -16,9 +24,15 @@ import {packedCli,placeCli,recyclePlacedCopies,+ releasePackedCli,} from './global-local-fixtures.mjs';import {cleanupFixtures, tempDir} from './release-fixtures.mjs';+// Unpacking the CLI writes thousands of files. It is the suite's setup, as+// in global-local.test.mjs, not part of whichever test places a copy first.+beforeAll(() => {+ packedCli();+}, 120_000);afterEach(() => {try {recyclePlacedCopies();@@ -27,7 +41,29 @@ afterEach(() => {}});afterAll(() => {- fs.rmSync(path.dirname(packedCli()), {recursive: true, force: true});+ releasePackedCli();+});++/** The folder the node project's global setup made for this run, if any. */+const runFolder = (() => {+ try {+ return inject('astryxPackedCliRunDir');+ } catch {+ return undefined;+ }+})();++describe("the run's folder", () => {+ it.runIf(typeof runFolder === 'string')(+ 'holds the unpacked CLI, which a suite leaves for the global teardown',+ () => {+ const unpacked = packedCli();+ expect(path.dirname(path.dirname(unpacked))).toBe(runFolder);+ releasePackedCli();+ expect(fs.existsSync(unpacked)).toBe(true);+ expect(packedCli()).toBe(unpacked);+ },+ );});/**diff --git a/packages/cli/test-utils/run-folder.mjs b/packages/cli/test-utils/run-folder.mjsnew file mode 100644index 0000000000..16026ee370--- /dev/null+++ b/packages/cli/test-utils/run-folder.mjs@@ -0,0 +1,79 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Removal of a node-project run's folder outside every time limit.+ * @input A folder the node project's global setup made for one run.+ * @output `trashFolder(dir)` moves the folder aside and removes it in a+ * detached child; `sweepTrash(parent, before)` removes trash that an+ * earlier run's child left behind.+ * @position Used by vitest.global-setup.node.mjs. The suites that unpack the+ * packed CLI keep thousands of files in the run's folder. Vitest gives+ * `close()` only 10 s, so the teardown never waits on their removal.+ */++import {spawn} from 'node:child_process';+import * as fs from 'node:fs';+import * as path from 'node:path';++/** A trash folder's name: the run folder's name and the time it moved. */+const TRASH_NAME = /^astryx-cli-tarball-.+\.trash-(\d+)$/u;++/**+ * Remove `target` in a detached child that outlives this process.+ * @param {string} target+ * @returns {import('node:child_process').ChildProcess}+ */+function removeInBackground(target) {+ const child = spawn(+ process.execPath,+ [+ '-e',+ "require('node:fs').rmSync(process.argv[1], {recursive: true, force: true, maxRetries: 3})",+ target,+ ],+ {+ detached: true,+ stdio: 'ignore',+ windowsHide: true,+ // A debugger flag in NODE_OPTIONS would otherwise attach to the child.+ env: {...process.env, NODE_OPTIONS: ''},+ },+ );+ child.unref();+ return child;+}++/**+ * Move `dir` aside on its own file system, which takes no time, and remove+ * it in a detached child. Where the move fails, as with an open handle on+ * Windows, the child removes `dir` where it is.+ * @param {string} dir+ * @returns {import('node:child_process').ChildProcess | null} the child, or+ * null when there is no `dir`+ */+export function trashFolder(dir) {+ if (!fs.existsSync(dir)) return null;+ const trash = `${dir}.trash-${Date.now()}`;+ try {+ fs.renameSync(dir, trash);+ } catch {+ return removeInBackground(dir);+ }+ return removeInBackground(trash);+}++/**+ * Remove the trash folders in `parent` that moved before `before`, as when+ * an earlier run's child was stopped. A later run's trash is left to its own+ * child.+ * @param {string} parent+ * @param {number} before - a time in milliseconds since the epoch+ * @returns {import('node:child_process').ChildProcess[]} one child per folder+ */+export function sweepTrash(parent, before) {+ return fs.readdirSync(parent).flatMap(entry => {+ const moved = TRASH_NAME.exec(entry);+ if (!moved || Number(moved[1]) >= before) return [];+ return [removeInBackground(path.join(parent, entry))];+ });+}diff --git a/packages/cli/test-utils/run-folder.test.mjs b/packages/cli/test-utils/run-folder.test.mjsnew file mode 100644index 0000000000..7cd0c20e65--- /dev/null+++ b/packages/cli/test-utils/run-folder.test.mjs@@ -0,0 +1,83 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Verifies that a run's folder leaves its place at once. A detached+ * child then removes it and never follows a link. A later run sweeps the+ * trash that a stopped child left behind.+ */++import {afterEach, describe, expect, it} from 'vitest';+import {once} from 'node:events';+import * as fs from 'node:fs';+import * as path from 'node:path';+import {cleanupFixtures, tempDir} from './release-fixtures.mjs';+import {sweepTrash, trashFolder} from './run-folder.mjs';++afterEach(() => {+ cleanupFixtures();+});++/**+ * @param {string} dir+ * @param {number} count+ */+function plant(dir, count) {+ fs.mkdirSync(dir, {recursive: true});+ for (let i = 0; i < count; i += 1) {+ fs.writeFileSync(path.join(dir, `file-${i}`), '');+ }+}++describe('trashFolder', () => {+ it('moves the run folder away before it returns, and its child removes it', async () => {+ const parent = tempDir('run-parent');+ const run = path.join(parent, 'astryx-cli-tarball-run');+ plant(path.join(run, 'astryx-packed-cli-1', 'package'), 50);+ const child = trashFolder(run);+ expect(fs.existsSync(run)).toBe(false);+ if (!child) throw new Error('trashFolder started no child');+ const [code] = await once(child, 'exit');+ expect(code).toBe(0);+ expect(fs.readdirSync(parent)).toEqual([]);+ });++ it('removes a link in the run folder and never what it points to', async () => {+ const parent = tempDir('run-parent');+ const kept = path.join(tempDir('package'), 'assets', 'templates');+ plant(kept, 1);+ const run = path.join(parent, 'astryx-cli-tarball-run');+ fs.mkdirSync(path.join(run, 'assets'), {recursive: true});+ fs.symlinkSync(kept, path.join(run, 'assets', 'templates'), 'junction');+ const child = trashFolder(run);+ if (!child) throw new Error('trashFolder started no child');+ await once(child, 'exit');+ expect(fs.readdirSync(parent)).toEqual([]);+ expect(fs.readdirSync(kept)).toEqual(['file-0']);+ });++ it('starts nothing when the folder is already gone', () => {+ const gone = path.join(tempDir('run-parent'), 'astryx-cli-tarball-gone');+ expect(trashFolder(gone)).toBeNull();+ });+});++describe('sweepTrash', () => {+ it("removes trash that an earlier run left, and keeps a later run's", async () => {+ const parent = tempDir('sweep');+ for (const name of [+ 'astryx-cli-tarball-a.trash-1000',+ 'astryx-cli-tarball-b.trash-3000',+ 'astryx-cli-tarball-c',+ 'another-tool.trash-1000',+ ]) {+ plant(path.join(parent, name), 3);+ }+ const children = sweepTrash(parent, 2000);+ await Promise.all(children.map(child => once(child, 'exit')));+ expect(fs.readdirSync(parent).sort()).toEqual([+ 'another-tool.trash-1000',+ 'astryx-cli-tarball-b.trash-3000',+ 'astryx-cli-tarball-c',+ ]);+ });+});diff --git a/packages/cli/test/global-local.test.mjs b/packages/cli/test/global-local.test.mjsindex 820effa4f5..b33f1d004d 100644--- a/packages/cli/test/global-local.test.mjs+++ b/packages/cli/test/global-local.test.mjs@@ -22,6 +22,7 @@ import {packedCli,placeCli,recyclePlacedCopies,+ releasePackedCli,seedPackedRelease,startRegistry,} from '../test-utils/global-local-fixtures.mjs';@@ -69,7 +70,7 @@ afterEach(async () => {}}, 90_000);afterAll(() => {- fs.rmSync(path.dirname(packedCli()), {recursive: true, force: true});+ releasePackedCli();});/**diff --git a/vitest.global-setup.node.mjs b/vitest.global-setup.node.mjsindex 621944edcf..efaac8a4f3 100644--- a/vitest.global-setup.node.mjs+++ b/vitest.global-setup.node.mjs@@ -13,6 +13,8 @@* checkout. The suites that place CLI copies unpack one shared tarball. Run* these prerequisites here, serially and before the test workers, rather* than depending on an unrelated build job or local generated files.+ * Those suites unpack and keep their copies in this run's folder, which the+ * teardown removes.** SYNC: When modified, update this header.*/@@ -27,6 +29,21 @@ import {registerTestRun,} from './packages/cli/clients/cli/commands/ensure-core-built.mjs';import {packCli} from './packages/cli/test-utils/pack-cli.mjs';+import {+ sweepTrash,+ trashFolder,+} from './packages/cli/test-utils/run-folder.mjs';++/**+ * Trash what an earlier run left in `packDir`. Its workers have stopped, and+ * the next run unpacks again.+ * @param {string} packDir+ */+function trashUnpackedClis(packDir) {+ for (const entry of fs.readdirSync(packDir)) {+ if (!entry.endsWith('.tgz')) trashFolder(path.join(packDir, entry));+ }+}/** @param {import('vitest/node').TestProject} project */export default async function setup(project) {@@ -51,6 +68,9 @@ export default async function setup(project) {maxBuffer: 32 * 1024 * 1024,},);+ // An earlier run's child may have been stopped before it removed that+ // run's folder.+ sweepTrash(os.tmpdir(), Date.now());// `npm pack` costs about 5 s of CPU. Packing once here keeps that out of// every suite that places CLI copies, and out of their timed tests. A watch// session packs again before a rerun of this project's tests, so they see@@ -58,14 +78,23 @@ export default async function setup(project) {const packDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-cli-tarball-'),);+ // The suites unpack the tarball and keep their placed copies in this+ // folder, thousands of files each. The teardown removes them once, after+ // every suite, so no suite's own hook spends that time on a busy machine.+ project.provide('astryxPackedCliRunDir', packDir);const pack = () =>project.provide('astryxPackedCliTarball', packCli(packDir));pack();project.onTestsRerun(specs => {- if (specs.some(spec => spec.project.name === project.name)) pack();+ if (specs.some(spec => spec.project.name === project.name)) {+ trashUnpackedClis(packDir);+ pack();+ }});return () => {- fs.rmSync(packDir, {recursive: true, force: true});+ // Vitest gives close() 10 s, and the folder holds thousands of files.+ // It moves aside at once, and a detached child removes it.+ trashFolder(packDir);endRun();};} catch (error) {