Skip to content

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

TypeScript

test(cli): theme build fixtures live in the OS temp directory, not inside packages/cli (#9892)

Christopher Chedeau3ae8349fe6cf
diff --git a/packages/cli/api/theme/build/build.icon-entry-variants.test.mjs b/packages/cli/api/theme/build/build.icon-entry-variants.test.mjs
index b6c1437060..f7e25c938d 100644
--- a/packages/cli/api/theme/build/build.icon-entry-variants.test.mjs
+++ b/packages/cli/api/theme/build/build.icon-entry-variants.test.mjs
@@ -17,11 +17,17 @@ import {execFileSync} from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import {themeBuild} from './build.mjs';
+import {
+ makeLinkedFixture,
+ removeLinkedFixture,
+} from '../../../test-utils/linked-fixture.mjs';
it('passes an inherited entry only its defining theme variants in a built child, as at runtime', async () => {
- const tmpDir = fs.mkdtempSync(
- path.resolve(import.meta.dirname, '../../..', '.tmp-icon-entry-variants-'),
- );
+ const tmpDir = makeLinkedFixture('astryx-icon-entry-variants-', [
+ '@astryxdesign',
+ 'react',
+ 'react-dom',
+ ]);
try {
for (const directory of ['base', 'child', 'unlisted']) {
fs.mkdirSync(path.join(tmpDir, directory));
@@ -129,6 +135,6 @@ process.stdout.write(JSON.stringify(result));`,
});
expect(result.unlistedBuilt).toEqual(result.unlistedRuntime);
} finally {
- fs.rmSync(tmpDir, {recursive: true, force: true});
+ removeLinkedFixture(tmpDir);
}
});
diff --git a/packages/cli/api/theme/build/build.icon-function-entries.test.mjs b/packages/cli/api/theme/build/build.icon-function-entries.test.mjs
index a8ac19d725..a1cb84f835 100644
--- a/packages/cli/api/theme/build/build.icon-function-entries.test.mjs
+++ b/packages/cli/api/theme/build/build.icon-function-entries.test.mjs
@@ -20,19 +20,26 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import ts from 'typescript';
import {themeBuild, themeBuildFamily} from './build.mjs';
+import {
+ makeLinkedFixture,
+ removeLinkedFixture,
+} from '../../../test-utils/linked-fixture.mjs';
const SIZES = ['xsm', 'sm', 'md', 'lg'];
let tmpDir;
beforeEach(() => {
- // Under the CLI package, so core, React, and TypeScript resolve both in the
- // theme loader and in the native artifact checks.
- tmpDir = fs.mkdtempSync(
- path.resolve(import.meta.dirname, '../../..', '.tmp-icon-functions-'),
- );
+ // Core, React and TypeScript resolve both in the theme loader and in the
+ // native artifact checks.
+ tmpDir = makeLinkedFixture('astryx-icon-functions-', [
+ '@astryxdesign',
+ 'react',
+ 'react-dom',
+ 'typescript',
+ ]);
});
afterEach(() => {
- fs.rmSync(tmpDir, {recursive: true, force: true});
+ removeLinkedFixture(tmpDir);
});
/** @param {string} file @param {string} source */
diff --git a/packages/cli/api/theme/build/build.icon-lineage.test.mjs b/packages/cli/api/theme/build/build.icon-lineage.test.mjs
index 0e53e52a74..c41588e843 100644
--- a/packages/cli/api/theme/build/build.icon-lineage.test.mjs
+++ b/packages/cli/api/theme/build/build.icon-lineage.test.mjs
@@ -15,11 +15,16 @@ import {execFileSync} from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import {themeBuild} from './build.mjs';
+import {
+ makeLinkedFixture,
+ removeLinkedFixture,
+} from '../../../test-utils/linked-fixture.mjs';
it('preserves merged icon lineage through a built child with colliding import names', async () => {
- const tmpDir = fs.mkdtempSync(
- path.resolve(import.meta.dirname, '../../..', '.tmp-icon-lineage-'),
- );
+ const tmpDir = makeLinkedFixture('astryx-icon-lineage-', [
+ '@astryxdesign',
+ 'react',
+ ]);
try {
for (const directory of ['base', 'child']) {
fs.mkdirSync(path.join(tmpDir, directory));
@@ -112,6 +117,6 @@ for (const [key, label] of Object.entries(expected)) {
{cwd: tmpDir, stdio: 'pipe', timeout: 10000},
);
} finally {
- fs.rmSync(tmpDir, {recursive: true, force: true});
+ removeLinkedFixture(tmpDir);
}
});
diff --git a/packages/cli/api/theme/build/build.icon-preservation.test.mjs b/packages/cli/api/theme/build/build.icon-preservation.test.mjs
index 4d51e91672..940292e1f5 100644
--- a/packages/cli/api/theme/build/build.icon-preservation.test.mjs
+++ b/packages/cli/api/theme/build/build.icon-preservation.test.mjs
@@ -16,17 +16,22 @@ import {execFileSync} from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import {themeBuild} from './build.mjs';
+import {
+ makeLinkedFixture,
+ removeLinkedFixture,
+} from '../../../test-utils/linked-fixture.mjs';
let tmpDir;
beforeEach(() => {
- // Keeping fixtures under the CLI package makes real core/React imports
- // resolve in both the theme loader and the native Node artifact check.
- tmpDir = fs.mkdtempSync(
- path.resolve(import.meta.dirname, '../../..', '.tmp-icon-preservation-'),
- );
+ // Real core and React imports resolve in both the theme loader and the
+ // native Node artifact check.
+ tmpDir = makeLinkedFixture('astryx-icon-preservation-', [
+ '@astryxdesign',
+ 'react',
+ ]);
});
afterEach(() => {
- fs.rmSync(tmpDir, {recursive: true, force: true});
+ removeLinkedFixture(tmpDir);
});
function write(file, source) {
diff --git a/packages/cli/api/theme/build/build.test.mjs b/packages/cli/api/theme/build/build.test.mjs
index b548855a5f..3fbf369759 100644
--- a/packages/cli/api/theme/build/build.test.mjs
+++ b/packages/cli/api/theme/build/build.test.mjs
@@ -20,6 +20,10 @@ import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
+import {
+ makeLinkedFixture,
+ removeLinkedFixture,
+} from '../../../test-utils/linked-fixture.mjs';
import {
generateThemeRulesSplit as mockGenerateThemeRulesSplit,
generateOnMediaCSS as mockGenerateOnMediaCSS,
@@ -896,16 +900,13 @@ describe('themeBuild() — icon registry detection', () => {
describe('themeBuild() — extends', () => {
// These fixtures `import {defineTheme} from '@astryxdesign/core/theme'` the
- // way a real theme file does, so they have to sit somewhere that specifier
- // resolves — an OS temp dir has no node_modules above it.
+ // way a real theme file does, so each links the CLI's packages.
let extDir;
beforeEach(() => {
- extDir = fs.mkdtempSync(
- path.join(path.resolve(import.meta.dirname, '../../..'), '.tmp-extends-'),
- );
+ extDir = makeLinkedFixture('astryx-extends-', ['@astryxdesign']);
});
afterEach(() => {
- fs.rmSync(extDir, {recursive: true, force: true});
+ removeLinkedFixture(extDir);
});
/**
diff --git a/packages/cli/clients/cli/commands/build-theme.ascii-output.test.mjs b/packages/cli/clients/cli/commands/build-theme.ascii-output.test.mjs
index ea3a79f40b..1c624281a0 100644
--- a/packages/cli/clients/cli/commands/build-theme.ascii-output.test.mjs
+++ b/packages/cli/clients/cli/commands/build-theme.ascii-output.test.mjs
@@ -16,9 +16,10 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import {ensureCoreBuilt} from './ensure-core-built.mjs';
import {runCli} from '../../../test-utils/run-cli.mjs';
-
-// Under the CLI package so fixtures resolve `@astryxdesign/core/theme`.
-const CLI_ROOT = path.resolve(import.meta.dirname, '../../..');
+import {
+ makeLinkedFixture,
+ removeLinkedFixture,
+} from '../../../test-utils/linked-fixture.mjs';
const OCEAN = `import {defineTheme} from '@astryxdesign/core/theme';
export const oceanTheme = defineTheme({
@@ -48,14 +49,14 @@ beforeAll(() => {
}, 200_000);
beforeEach(() => {
- dir = fs.mkdtempSync(path.join(CLI_ROOT, '.tmp-theme-ascii-'));
+ dir = makeLinkedFixture('astryx-theme-ascii-', ['@astryxdesign']);
fs.writeFileSync(path.join(dir, 'package.json'), '{"type":"module"}\n');
fs.writeFileSync(path.join(dir, 'ocean.mjs'), OCEAN);
fs.writeFileSync(path.join(dir, 'ocean-calm.mjs'), CALM);
});
afterEach(() => {
- fs.rmSync(dir, {recursive: true, force: true});
+ removeLinkedFixture(dir);
});
/** @param {{stdout: string, stderr: string}} result */
diff --git a/packages/cli/clients/cli/commands/build-theme.variants.test.mjs b/packages/cli/clients/cli/commands/build-theme.variants.test.mjs
index d0ea6f8e71..50fe65b1cc 100644
--- a/packages/cli/clients/cli/commands/build-theme.variants.test.mjs
+++ b/packages/cli/clients/cli/commands/build-theme.variants.test.mjs
@@ -29,13 +29,18 @@ import {describe, it, expect, beforeAll, beforeEach, afterEach} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
-import {fileURLToPath} from 'node:url';
import {execFileSync} from 'node:child_process';
+import {createRequire} from 'node:module';
import {ensureCoreBuilt} from './ensure-core-built.mjs';
import {runCli} from '../../../test-utils/run-cli.mjs';
+import {
+ makeLinkedFixture,
+ removeLinkedFixture,
+} from '../../../test-utils/linked-fixture.mjs';
-const HERE = path.dirname(fileURLToPath(import.meta.url));
-const CLI_ROOT = path.resolve(HERE, '../../..');
+const TSC = createRequire(import.meta.url).resolve('typescript/bin/tsc');
+// A consumer project type-checks against core and React's types.
+const CONSUMER_PACKAGES = ['@astryxdesign', 'react', '@types/react'];
function writeTheme(dir, contents) {
fs.mkdirSync(dir, {recursive: true});
@@ -207,12 +212,10 @@ describe('theme build custom-variant augmentations', () => {
expect(variantsDts).toContain("'editorial': true;");
expect(variantsDts).toContain("'shared': true;");
- const projectDir = path.join(
- CLI_ROOT,
- `.tmp-surface-heading-consumer-${process.pid}`,
+ const projectDir = makeLinkedFixture(
+ 'astryx-surface-heading-consumer-',
+ CONSUMER_PACKAGES,
);
- fs.rmSync(projectDir, {recursive: true, force: true});
- fs.mkdirSync(projectDir);
fs.copyFileSync(
path.join(tmpDir, 'variants-theme.d.ts'),
path.join(projectDir, 'variants-theme.d.ts'),
@@ -254,12 +257,12 @@ describe('theme build custom-variant augmentations', () => {
try {
execFileSync(
- 'pnpm',
- ['exec', 'tsc', '--project', 'tsconfig.json', '--noEmit'],
+ process.execPath,
+ [TSC, '--project', 'tsconfig.json', '--noEmit'],
{cwd: projectDir, stdio: 'pipe'},
);
} finally {
- fs.rmSync(projectDir, {recursive: true, force: true});
+ removeLinkedFixture(projectDir);
}
});
@@ -438,12 +441,10 @@ describe('theme build custom-variant augmentations', () => {
);
expect(result.code).toBe(0);
- const projectDir = path.join(
- CLI_ROOT,
- `.tmp-variant-consumer-${process.pid}`,
+ const projectDir = makeLinkedFixture(
+ 'astryx-variant-consumer-',
+ CONSUMER_PACKAGES,
);
- fs.rmSync(projectDir, {recursive: true, force: true});
- fs.mkdirSync(projectDir);
fs.copyFileSync(
path.join(tmpDir, 'variants-theme.d.ts'),
path.join(projectDir, 'variants-theme.d.ts'),
@@ -517,15 +518,15 @@ describe('theme build custom-variant augmentations', () => {
try {
execFileSync(
- 'pnpm',
- ['exec', 'tsc', '--project', 'tsconfig.json', '--noEmit'],
+ process.execPath,
+ [TSC, '--project', 'tsconfig.json', '--noEmit'],
{
cwd: projectDir,
stdio: 'pipe',
},
);
} finally {
- fs.rmSync(projectDir, {recursive: true, force: true});
+ removeLinkedFixture(projectDir);
}
});
diff --git a/packages/cli/test-utils/linked-fixture.mjs b/packages/cli/test-utils/linked-fixture.mjs
new file mode 100644
index 0000000000..42f445fef6
--- /dev/null
+++ b/packages/cli/test-utils/linked-fixture.mjs
@@ -0,0 +1,67 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file A test's fixture project in the OS temp directory, whose imports
+ * resolve to the CLI's own packages.
+ * @input A directory name prefix and the packages the fixture imports.
+ * @output The fixture's real path.
+ * @position Test support. A fixture under packages/cli lies in the tree that
+ * scanners walk, and it can vanish under a walk. Links beside the fixture
+ * resolve its imports as the CLI resolves them.
+ */
+import fs from 'node:fs';
+import {createRequire} from 'node:module';
+import os from 'node:os';
+import path from 'node:path';
+
+const CLI_ROOT = path.resolve(import.meta.dirname, '..');
+const require = createRequire(path.join(CLI_ROOT, 'package.json'));
+/** The roots makeLinkedFixture made and has not removed. */
+const made = new Set();
+
+/**
+ * Makes an empty fixture directory that resolves `packages`. The links sit in
+ * the directory's parent, so the directory holds only what the test writes.
+ * The parent's package.json gives the project a top and the CLI's module
+ * type, as the CLI's own package.json gives its files.
+ * @param {string} prefix
+ * @param {string[]} packages Package names. A scope the CLI holds, such as
+ * `@astryxdesign`, links whole.
+ * @returns {string}
+ */
+export function makeLinkedFixture(prefix, packages) {
+ // The real path counts, because the temp directory can be a link (macOS:
+ // /var to /private/var).
+ const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), prefix)));
+ made.add(root);
+ fs.writeFileSync(path.join(root, 'package.json'), '{"type": "module"}\n');
+ for (const name of packages) {
+ const link = path.join(root, 'node_modules', name);
+ fs.mkdirSync(path.dirname(link), {recursive: true});
+ fs.symlinkSync(packageDir(name), link, 'dir');
+ }
+ const dir = path.join(root, 'project');
+ fs.mkdirSync(dir);
+ return dir;
+}
+
+/**
+ * Removes a fixture that makeLinkedFixture made, with its links.
+ * @param {string} dir
+ */
+export function removeLinkedFixture(dir) {
+ const root = path.dirname(dir);
+ // A wrong argument must not remove the temp directory itself, so only a
+ // root that makeLinkedFixture made goes.
+ if (path.basename(dir) !== 'project' || !made.delete(root)) {
+ throw new Error(`not a linked fixture: ${dir}`);
+ }
+ fs.rmSync(root, {recursive: true, force: true});
+}
+
+/** @param {string} name */
+function packageDir(name) {
+ const own = path.join(CLI_ROOT, 'node_modules', name);
+ if (fs.existsSync(own)) return own;
+ return path.dirname(require.resolve(`${name}/package.json`));
+}
diff --git a/packages/cli/test-utils/linked-fixture.test.mjs b/packages/cli/test-utils/linked-fixture.test.mjs
new file mode 100644
index 0000000000..ce8c564ef5
--- /dev/null
+++ b/packages/cli/test-utils/linked-fixture.test.mjs
@@ -0,0 +1,70 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Tests for the linked fixture helper.
+ * @input Fixtures that makeLinkedFixture makes in the OS temp directory.
+ * @output Their place, their imports and their removal, and the refusal to
+ * remove a directory the helper did not make.
+ * @position Test support for the CLI's fixture projects.
+ */
+
+import * as fs from 'node:fs';
+import {createRequire} from 'node:module';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {describe, expect, it} from 'vitest';
+import {makeLinkedFixture, removeLinkedFixture} from './linked-fixture.mjs';
+
+describe('makeLinkedFixture', () => {
+ it('makes an empty project in the temp directory whose imports resolve as the CLI resolves them', () => {
+ const dir = makeLinkedFixture('astryx-linked-fixture-', [
+ '@astryxdesign',
+ 'react',
+ ]);
+ const root = path.dirname(dir);
+ try {
+ expect(path.dirname(root)).toBe(fs.realpathSync(os.tmpdir()));
+ expect(fs.readdirSync(dir)).toEqual([]);
+ expect(
+ JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')),
+ ).toEqual({type: 'module'});
+ const fromFixture = createRequire(path.join(dir, 'page.mjs'));
+ const fromCli = createRequire(import.meta.url);
+ expect(fs.realpathSync(fromFixture.resolve('react'))).toBe(
+ fs.realpathSync(fromCli.resolve('react')),
+ );
+ expect(
+ fs.realpathSync(path.join(root, 'node_modules/@astryxdesign/core')),
+ ).toBe(fs.realpathSync(path.join(import.meta.dirname, '../../core')));
+ } finally {
+ removeLinkedFixture(dir);
+ }
+ expect(fs.existsSync(root)).toBe(false);
+ });
+
+ it('refuses to remove a directory it did not make', () => {
+ const plain = fs.realpathSync(
+ fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-plain-')),
+ );
+ const lookalike = path.join(plain, 'project');
+ fs.mkdirSync(lookalike);
+ try {
+ expect(() => removeLinkedFixture(plain)).toThrow('not a linked fixture');
+ expect(() => removeLinkedFixture(lookalike)).toThrow(
+ 'not a linked fixture',
+ );
+ expect(() =>
+ removeLinkedFixture(path.join(os.tmpdir(), 'project')),
+ ).toThrow('not a linked fixture');
+ expect(fs.existsSync(lookalike)).toBe(true);
+ } finally {
+ fs.rmSync(plain, {recursive: true, force: true});
+ }
+ });
+
+ it('removes a fixture once', () => {
+ const dir = makeLinkedFixture('astryx-linked-fixture-', []);
+ removeLinkedFixture(dir);
+ expect(() => removeLinkedFixture(dir)).toThrow('not a linked fixture');
+ });
+});