Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(cli): six CLI test files leave nothing in the system temp directory (#9468)

Christopher Chedeaudfa7ae023e0d
diff --git a/packages/cli/api/search/_adapter.test.mjs b/packages/cli/api/search/_adapter.test.mjs
index 3e8d095a47..87566e30fa 100644
--- a/packages/cli/api/search/_adapter.test.mjs
+++ b/packages/cli/api/search/_adapter.test.mjs
@@ -14,7 +14,7 @@
* undo.
*/
-import {describe, it, expect, vi} from 'vitest';
+import {afterAll, beforeAll, describe, it, expect, vi} from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
@@ -31,11 +31,26 @@ const SLOW = 60_000;
/** A folder with only a package.json: a docs search there reads the CLI's docs. */
function scratchProject() {
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-gather-reuse-'));
+ const dir = fs.mkdtempSync(path.join(TEMP_ROOT, 'astryx-gather-reuse-'));
fs.writeFileSync(path.join(dir, 'package.json'), '{"name":"app"}');
return dir;
}
+/**
+ * Every temporary folder these tests make lives here, removed once they
+ * finish. It is made in beforeAll, not when the module loads: Vitest skips
+ * both hooks when a run filters out every test here.
+ */
+let TEMP_ROOT = '';
+beforeAll(() => {
+ TEMP_ROOT = fs.mkdtempSync(
+ path.join(os.tmpdir(), 'astryx-search-adapter-test-'),
+ );
+});
+afterAll(() => {
+ fs.rmSync(TEMP_ROOT, {recursive: true, force: true});
+});
+
describe('gatherCandidatesReused', () => {
it(
'reuses the gathered candidates while the inputs are unchanged',
diff --git a/packages/cli/clients/cli/lib/manifest.test.mjs b/packages/cli/clients/cli/lib/manifest.test.mjs
index bd9103a6fb..0f576164c9 100644
--- a/packages/cli/clients/cli/lib/manifest.test.mjs
+++ b/packages/cli/clients/cli/lib/manifest.test.mjs
@@ -18,11 +18,24 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {fileURLToPath, pathToFileURL} from 'node:url';
-import {describe, it, expect} from 'vitest';
+import {afterAll, beforeAll, describe, it, expect} from 'vitest';
import {program, JSON_SUPPORTED} from '../index.mjs';
import {buildManifest} from './manifest.mjs';
import {runCli} from '../../../test-utils/run-cli.mjs';
+/**
+ * Every temporary folder these tests make lives here, removed once they
+ * finish. It is made in beforeAll, not when the module loads: Vitest skips
+ * both hooks when a run filters out every test here.
+ */
+let TEMP_ROOT = '';
+beforeAll(() => {
+ TEMP_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-manifest-test-'));
+});
+afterAll(() => {
+ fs.rmSync(TEMP_ROOT, {recursive: true, force: true});
+});
+
const HERE = path.dirname(fileURLToPath(import.meta.url));
/**
@@ -144,7 +157,7 @@ describe('manifest: drift guards', () => {
});
it('lists upgrade.registry, which `upgrade --registry --json` emits', async () => {
- const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-manifest-'));
+ const cwd = fs.mkdtempSync(path.join(TEMP_ROOT, 'astryx-manifest-'));
fs.writeFileSync(path.join(cwd, 'package.json'), '{"name":"app","version":"1.0.0"}');
const {status, stdout} = await runCli(['--json', 'upgrade', '--registry'], {cwd});
expect(status).toBe(0);
@@ -153,7 +166,7 @@ describe('manifest: drift guards', () => {
});
it('lists theme.list for theme add, which lists themes when given no slug', async () => {
- const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-manifest-'));
+ const cwd = fs.mkdtempSync(path.join(TEMP_ROOT, 'astryx-manifest-'));
fs.writeFileSync(path.join(cwd, 'package.json'), '{"name":"app","version":"1.0.0"}');
const {status, stdout} = await runCli(['--json', 'theme', 'add'], {cwd});
expect(status).toBe(0);
diff --git a/packages/cli/foundation/fs/read-record.test.mjs b/packages/cli/foundation/fs/read-record.test.mjs
index 3ab2e7c2eb..79a1fd16d6 100644
--- a/packages/cli/foundation/fs/read-record.test.mjs
+++ b/packages/cli/foundation/fs/read-record.test.mjs
@@ -5,7 +5,7 @@
* any changed since, including a change made while the operation ran.
*/
-import {describe, it, expect} from 'vitest';
+import {afterAll, beforeAll, describe, it, expect} from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
@@ -16,8 +16,23 @@ import {
unchangedSince,
} from './read-record.mjs';
+/**
+ * Every temporary folder these tests make lives here, removed once they
+ * finish. It is made in beforeAll, not when the module loads: Vitest skips
+ * both hooks when a run filters out every test here.
+ */
+let TEMP_ROOT = '';
+beforeAll(() => {
+ TEMP_ROOT = fs.mkdtempSync(
+ path.join(os.tmpdir(), 'astryx-read-record-test-'),
+ );
+});
+afterAll(() => {
+ fs.rmSync(TEMP_ROOT, {recursive: true, force: true});
+});
+
function scratch() {
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-read-record-'));
+ const dir = fs.mkdtempSync(path.join(TEMP_ROOT, 'astryx-read-record-'));
const file = path.join(dir, 'doc.txt');
fs.writeFileSync(file, 'one');
return {dir, file};
diff --git a/packages/cli/foundation/response/response-types.doc.test.mjs b/packages/cli/foundation/response/response-types.doc.test.mjs
index 6012159aab..dc51159076 100644
--- a/packages/cli/foundation/response/response-types.doc.test.mjs
+++ b/packages/cli/foundation/response/response-types.doc.test.mjs
@@ -9,11 +9,26 @@
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
-import {describe, it, expect} from 'vitest';
+import {afterAll, beforeAll, describe, it, expect} from 'vitest';
import {doc} from './response-types.doc.mjs';
import {runCli} from '../../test-utils/run-cli.mjs';
import {linkThisCliAsProjectCli} from '../../test-utils/release-fixtures.mjs';
+/**
+ * Every temporary folder these tests make lives here, removed once they
+ * finish. It is made in beforeAll, not when the module loads: Vitest skips
+ * both hooks when a run filters out every test here.
+ */
+let TEMP_ROOT = '';
+beforeAll(() => {
+ TEMP_ROOT = fs.mkdtempSync(
+ path.join(os.tmpdir(), 'astryx-response-types-test-'),
+ );
+});
+afterAll(() => {
+ fs.rmSync(TEMP_ROOT, {recursive: true, force: true});
+});
+
/** @param {string} type */
const describedAs = type =>
doc.members.find(m => m.value === type)?.description ?? '';
@@ -174,7 +189,7 @@ describe('response-types EnumDoc names every field', () => {
it('theme.palette.generate, including receipt provenance', async () => {
const cwd = fs.mkdtempSync(
- path.join(os.tmpdir(), 'astryx-response-types-palette-'),
+ path.join(TEMP_ROOT, 'astryx-response-types-palette-'),
);
fs.writeFileSync(
path.join(cwd, 'palette.config.json'),
@@ -216,9 +231,7 @@ describe('response-types EnumDoc names every field', () => {
});
it('theme.build, including notices', async () => {
- const cwd = fs.mkdtempSync(
- path.join(os.tmpdir(), 'astryx-response-types-'),
- );
+ const cwd = fs.mkdtempSync(path.join(TEMP_ROOT, 'astryx-response-types-'));
fs.writeFileSync(
path.join(cwd, 'package.json'),
'{"name":"app","version":"1.0.0"}',
diff --git a/packages/cli/test/global-local.test.mjs b/packages/cli/test/global-local.test.mjs
index 20f0de8868..6ba7986cb9 100644
--- a/packages/cli/test/global-local.test.mjs
+++ b/packages/cli/test/global-local.test.mjs
@@ -47,7 +47,22 @@ const OFFLINE = 'http://127.0.0.1:9/';
beforeAll(() => {
packedCli();
}, 120_000);
-afterEach(cleanupFixtures);
+/**
+ * Every private computer the running test made, settled before cleanup.
+ * @type {ReturnType<typeof computer>[]}
+ */
+const computers = [];
+// The hook's own limit is above the wait's 60 s, so a stuck step fails with
+// the message below and its folders are still removed.
+afterEach(async () => {
+ const busy = await settleBackgroundSteps(computers.splice(0));
+ cleanupFixtures();
+ if (busy.length > 0) {
+ throw new Error(
+ `A background step was still writing into ${busy.join(', ')} 60 s after the test: it is stuck.`,
+ );
+ }
+}, 90_000);
afterAll(() => {
fs.rmSync(path.dirname(packedCli()), {recursive: true, force: true});
});
@@ -68,13 +83,53 @@ function computer(options = {}) {
if (POSIX && npm.prefix.length > 0) {
fs.symlinkSync(npm.prefix[0], path.join(nodeBin, 'npm'));
}
- return {
+ const pc = {
home,
cache,
/** The `astryx` cache folder global mode uses. */
astryxCache: path.join(cache, 'astryx'),
path: [...(options.path ?? []), nodeBin],
};
+ computers.push(pc);
+ return pc;
+}
+
+/**
+ * Wait until no background step a run started still writes into these
+ * computers' home folders: one that outlived its test would put the removed
+ * home folder back, left behind in the system temp directory. A step is done
+ * once it has released the state file's locks and, for the computer-level
+ * agent text, recorded the release it wrote. The CLI starts each step
+ * detached and keeps no handle to it, so this reads what the step records.
+ * @param {ReturnType<typeof computer>[]} pcs
+ * @returns {Promise<string[]>} the home folders a step still writes into
+ * after 60 s
+ */
+async function settleBackgroundSteps(pcs) {
+ const deadline = Date.now() + 60_000;
+ /** @type {string[]} */
+ const busy = [];
+ for (const pc of pcs) {
+ const stateDir = path.join(pc.home, '.config', 'astryx');
+ const writing = () => {
+ if (
+ fs.existsSync(path.join(stateDir, '.lock-check')) ||
+ fs.existsSync(path.join(stateDir, '.lock-state'))
+ ) {
+ return true;
+ }
+ const {agentText} = readState(pc);
+ return (
+ agentText?.attemptFor !== undefined &&
+ agentText.writtenBy !== agentText.attemptFor
+ );
+ };
+ while (writing() && Date.now() < deadline) {
+ await new Promise(done => setTimeout(done, 50));
+ }
+ if (writing()) busy.push(pc.home);
+ }
+ return busy;
}
/**
@@ -438,6 +493,8 @@ describe.skipIf(!POSIX)(
globalState(pc, {
latest: '9.0.7',
checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
firstRunAt: Date.now() - 2 * 24 * 60 * 60_000,
});
const app = makeProject({core: '9.0.1'});
@@ -508,6 +565,8 @@ describe.skipIf(!POSIX)(
globalState(pc, {
latest: '9.0.7',
checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
firstRunAt: Date.now() - 2 * 24 * 60 * 60_000,
});
writeJson(path.join(pc.home, 'package.json'), {
@@ -555,6 +614,8 @@ describe.skipIf(!POSIX)(
globalState(pc, {
latest: '9.0.7',
checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
firstRunAt: Date.now() - 2 * 24 * 60 * 60_000,
});
const tool = tempDir('tool');
@@ -586,6 +647,8 @@ describe.skipIf(!POSIX)(
globalState(pc, {
latest: '9.0.7',
checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
firstRunAt: Date.now() - 2 * 24 * 60 * 60_000,
});
const root = makeProject({core: '9.0.1'});
@@ -660,6 +723,8 @@ describe.skipIf(!POSIX)(
globalState(pc2, {
latest: '9.0.7',
checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
firstRunAt: Date.now() - 2 * 24 * 60 * 60_000,
});
const app = makeProject({core: '9.0.1'});
@@ -1269,6 +1334,8 @@ describe.skipIf(!POSIX)(
globalState(pc, {
latest: '9.0.5',
checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
firstRunAt: Date.now() - 2 * 24 * 60 * 60_000,
});
const dir = makeProject({core: '9.0.0'});
@@ -1579,7 +1646,13 @@ describe.skipIf(!POSIX)(
// A failed command prints no notice.
const pc = computer();
const g = globalCli('npm', '9.0.0', {root: pc.home});
- globalState(pc, {latest: '9.0.5', checkOkAt: Date.now(), firstRunAt: 0});
+ globalState(pc, {
+ latest: '9.0.5',
+ checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
+ firstRunAt: 0,
+ });
const failed = run(g.bin, ['no-such-command'], {
cwd: tempDir('empty'),
pc,
@@ -2072,7 +2145,13 @@ describe.skipIf(!POSIX)(
fs.symlinkSync(cli.bin, path.join(prefix, 'bin', 'astryx'));
pc.path.unshift(path.join(prefix, 'bin'));
acmeWidgets(path.join(prefix, 'lib'), {topic: 'acme-prefix'});
- globalState(pc, {latest: '9.0.5', checkOkAt: Date.now(), firstRunAt: 0});
+ globalState(pc, {
+ latest: '9.0.5',
+ checkOkAt: Date.now(),
+ // Checked just now, so no run starts an update check.
+ checkedAt: Date.now(),
+ firstRunAt: 0,
+ });
const empty = tempDir('empty');
const docs = run(cli.bin, ['docs', '--json'], {cwd: empty, pc});
expect(
diff --git a/packages/cli/test/readme-prose.test.mjs b/packages/cli/test/readme-prose.test.mjs
index 4715d228b9..069ed9d1b6 100644
--- a/packages/cli/test/readme-prose.test.mjs
+++ b/packages/cli/test/readme-prose.test.mjs
@@ -9,11 +9,26 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {describe, it, expect} from 'vitest';
+import {afterAll, beforeAll, describe, it, expect} from 'vitest';
import {program, JSON_SUPPORTED} from '../clients/cli/index.mjs';
import {buildManifest} from '../clients/cli/lib/manifest.mjs';
import {runCli} from '../test-utils/run-cli.mjs';
+/**
+ * Every temporary folder these tests make lives here, removed once they
+ * finish. It is made in beforeAll, not when the module loads: Vitest skips
+ * both hooks when a run filters out every test here.
+ */
+let TEMP_ROOT = '';
+beforeAll(() => {
+ TEMP_ROOT = fs.mkdtempSync(
+ path.join(os.tmpdir(), 'astryx-readme-prose-test-'),
+ );
+});
+afterAll(() => {
+ fs.rmSync(TEMP_ROOT, {recursive: true, force: true});
+});
+
const README = fs
.readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), '../README.md'), 'utf8')
.replace(/<!-- BEGIN GENERATED: (\S+) -->[\s\S]*?<!-- END GENERATED: \1 -->/g, '');
@@ -98,7 +113,7 @@ describe('README envelope shapes', () => {
});
it('real envelopes carry exactly the documented keys', async () => {
- const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-readme-'));
+ const cwd = fs.mkdtempSync(path.join(TEMP_ROOT, 'astryx-readme-'));
const success = JSON.parse((await runCli(['--json', 'docs'], {cwd})).stdout);
const failure = JSON.parse((await runCli(['--json', 'docs', 'no-such-topic'], {cwd})).stdout);
for (const [kind, envelope] of /** @type {const} */ ([