An open source design system that's fully customizable and agent ready
TypeScript
test(cli): the node project packs the CLI once for every suite (#9935)
Christopher Chedeau4f5257f659a0
diff --git a/packages/cli/test-utils/global-local-fixtures.mjs b/packages/cli/test-utils/global-local-fixtures.mjsindex d89409f86e..7c62f3a7e2 100644--- a/packages/cli/test-utils/global-local-fixtures.mjs+++ b/packages/cli/test-utils/global-local-fixtures.mjs@@ -6,7 +6,8 @@* in every install layout the table names, and a package registry that runs* inside the test process.*- * - `packedCli()` packs this package once per process (`npm pack`), and+ * - `packedCli()` unpacks the tarball the node project's global setup packed+ * 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.* - `placeCli(dir, version)` puts a copy at `dir`, labeled `version`: every@@ -33,9 +34,10 @@ import * as os from 'node:os';import * as path from 'node:path';import * as zlib from 'node:zlib';import {createHash} from 'node:crypto';-import {spawnSync} from 'node:child_process';import {fileURLToPath} from 'node:url';+import {inject} from 'vitest';import {releaseTree} from '../foundation/release/cache.mjs';+import {packCli} from './pack-cli.mjs';import {installCore, tempDir, writeJson} from './release-fixtures.mjs';/** This CLI package's folder. */@@ -97,23 +99,30 @@ export function extractTarball(file, dest) {let packed = null;/**- * This CLI, packed once per process and unpacked.+ * The tarball the node project's global setup packed for this run, or null+ * when nothing provided one, as in another test config.+ * @returns {string | null}+ */+function providedTarball() {+ let tarball;+ try {+ tarball = inject('astryxPackedCliTarball');+ } catch {+ return null;+ }+ return typeof tarball === 'string' && fs.existsSync(tarball) ? tarball : null;+}++/**+ * This CLI, packed once per run (or once per process when no run packed it)+ * and unpacked once per process.* @returns {string} the unpacked package folder*/export function packedCli() {if (packed) return packed;const out = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-packed-cli-'));- const result = spawnSync(- process.platform === 'win32' ? 'npm.cmd' : 'npm',- ['pack', '--ignore-scripts', '--json', '--pack-destination', out],- {cwd: CLI_DIR, encoding: 'utf8', shell: process.platform === 'win32'},- );- if (result.status !== 0) {- throw new Error(`npm pack failed: ${result.stderr}`);- }- const [{filename}] = JSON.parse(result.stdout);+ packedTarball = providedTarball() ?? packCli(out);const dir = path.join(out, 'package');- packedTarball = path.join(out, filename);extractTarball(packedTarball, dir);packed = dir;packedRecord = treeRecord(dir, {contents: true});diff --git a/packages/cli/test-utils/pack-cli.mjs b/packages/cli/test-utils/pack-cli.mjsnew file mode 100644index 0000000000..bc8ad49d9f--- /dev/null+++ b/packages/cli/test-utils/pack-cli.mjs@@ -0,0 +1,37 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Packs this CLI the way it is published, for tests that place copies+ * of it.+ * @input packages/cli as it is on disk.+ * @output `packCli(dir)`: the path of the `.tgz` that `npm pack` writes to+ * `dir`.+ * @position Used by vitest.global-setup.node.mjs, which packs once for a+ * whole node-project run, and by global-local-fixtures.mjs when no run+ * provided a tarball.+ */++import {spawnSync} from 'node:child_process';+import * as path from 'node:path';+import {fileURLToPath} from 'node:url';++/** This CLI package's folder. */+const CLI_DIR = fileURLToPath(new URL('..', import.meta.url));++/**+ * Pack this CLI into `dir` with `npm pack`, as a release is packed.+ * @param {string} dir an existing folder for the tarball+ * @returns {string} the tarball's path+ */+export function packCli(dir) {+ const result = spawnSync(+ process.platform === 'win32' ? 'npm.cmd' : 'npm',+ ['pack', '--ignore-scripts', '--json', '--pack-destination', dir],+ {cwd: CLI_DIR, encoding: 'utf8', shell: process.platform === 'win32'},+ );+ if (result.status !== 0) {+ throw new Error(`npm pack failed: ${result.stderr}`);+ }+ const [{filename}] = JSON.parse(result.stdout);+ return path.join(dir, filename);+}diff --git a/vitest.global-setup.node.mjs b/vitest.global-setup.node.mjsindex 8c9dbf89ff..2066bc87b7 100644--- a/vitest.global-setup.node.mjs+++ b/vitest.global-setup.node.mjs@@ -3,22 +3,28 @@/*** @file globalSetup for the `node` test project.* @input Shared ensureCoreBuilt helper and authored Sandbox template descriptors.- * @output Builds Core and regenerates the Sandbox template registry and route- * wrappers once, before any test worker forks.+ * @output Builds Core, regenerates the Sandbox template registry and route+ * wrappers, and packs the CLI once, before any test worker forks.* @position Referenced by vitest.config.ts's `node` project. The build-theme* suites need a compiled Core; the Sandbox route contract imports generated* template metadata and scans ignored wrappers that do not exist in a fresh- * checkout. Run both prerequisites here, serially and before the test workers,- * rather than depending on an unrelated build job or local generated files.+ * 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.** SYNC: When modified, update this header.*/import {execFileSync} from 'node:child_process';+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';import {fileURLToPath} from 'node:url';import {ensureCoreBuilt} from './packages/cli/clients/cli/commands/ensure-core-built.mjs';+import {packCli} from './packages/cli/test-utils/pack-cli.mjs';-export default function setup() {+/** @param {import('vitest/node').TestProject} project */+export default function setup(project) {ensureCoreBuilt();// The ignored registry and ~700 physical page wrappers are build outputs,// not fixtures to check in. The route-manifest test must see the same inputs@@ -31,4 +37,16 @@ export default function setup() {maxBuffer: 32 * 1024 * 1024,},);+ // `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+ // CLI edits. A rerun of other projects' tests does not wait for it.+ const packDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-cli-tarball-'));+ const pack = () =>+ project.provide('astryxPackedCliTarball', packCli(packDir));+ pack();+ project.onTestsRerun(specs => {+ if (specs.some(spec => spec.project.name === project.name)) pack();+ });+ return () => fs.rmSync(packDir, {recursive: true, force: true});}