An open source design system that's fully customizable and agent ready
TypeScript
test(cli): render, codemod and doctor fixtures live in the OS temp directory (#9899)
Christopher Chedeau4d84e2d1b272
diff --git a/packages/cli/api/render/render.test.mjs b/packages/cli/api/render/render.test.mjsindex 5e9d24f353..f87de5b584 100644--- a/packages/cli/api/render/render.test.mjs+++ b/packages/cli/api/render/render.test.mjs@@ -6,11 +6,13 @@*/import * as fs from 'node:fs';+import * as os from 'node:os';import * as path from 'node:path';import {fileURLToPath} from 'node:url';import {afterAll, beforeAll, describe, expect, it} from 'vitest';import {render} from './render.mjs';import {runCli} from '../../test-utils/run-cli.mjs';+import {makeLinkedFixture, removeLinkedFixture} from '../../test-utils/linked-fixture.mjs';const CLI_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');@@ -66,7 +68,7 @@ export default function Page() {`;beforeAll(async () => {- dir = fs.mkdtempSync(path.join(CLI_DIR, '.astryx-render-test-'));+ dir = makeLinkedFixture('astryx-render-test-', ['@astryxdesign', '@stylexjs', 'react', 'react-dom']);put('dot.png', PNG);put('page.tsx', PAGE);put('plain.html', '<!doctype html><html><body><h1>Hello</h1><p class="lead">Hi</p></body></html>');@@ -75,7 +77,7 @@ beforeAll(async () => {}, 60_000);afterAll(() => {- fs.rmSync(dir, {recursive: true, force: true});+ removeLinkedFixture(dir);});describe('render(): page to one HTML file', () => {@@ -951,7 +953,7 @@ describe('render(): every write stays inside the current folder', () => {expect(await codeOf(() => render('page.html', {cwd: dir, reverse: true, out: '../escaped.tsx'}))).toBe('ERR_PATH_TRAVERSAL',);- const outside = fs.mkdtempSync(path.join(CLI_DIR, '.astryx-render-outside-'));+ const outside = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-render-outside-')));try {fs.symlinkSync(outside, path.join(dir, 'link-out'));expect(diff --git a/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/authoring-migration.tsc.test.mjs b/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/authoring-migration.tsc.test.mjsindex 34e4940b7b..ce2dd99c3a 100644--- a/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/authoring-migration.tsc.test.mjs+++ b/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/authoring-migration.tsc.test.mjs@@ -7,9 +7,9 @@* is that migrated consumer code still type-checks: the new* `@astryxdesign/cli/authoring` path must resolve and export the types, and the* unwrapped objects must still satisfy them. This runs both codemods over- * type-annotated fixtures, writes the result inside `packages/cli` (so- * `@astryxdesign/cli` self-resolves and `@astryxdesign/core` resolves via its- * deps), and runs the real `tsc` over it.+ * type-annotated fixtures, writes the result to a temporary project that links+ * `@astryxdesign/cli` and `@astryxdesign/core`, and runs the real `tsc` over+ * it.** A negative control asserts the PRE-migration source fails `tsc` (the old* `@astryxdesign/core/authoring|config` paths and the `create*` factories are@@ -23,6 +23,10 @@ import * as path from 'node:path';import unwrap from '../unwrap-authoring-factories.mjs';import migrate from '../migrate-authoring-imports.mjs';+import {+ makeLinkedFixture,+ removeLinkedFixture,+} from '../../../../../test-utils/linked-fixture.mjs';const CLI_DIR = path.resolve(import.meta.dirname, '../../../../..'); // packages/cliconst REPO_ROOT = path.resolve(CLI_DIR, '../..');@@ -82,7 +86,7 @@ export default createCodemod({title: 'Rename', transform});const TSCONFIG = JSON.stringify({- extends: '../../../../tsconfig.json',+ extends: path.join(REPO_ROOT, 'tsconfig.json'),// allowJs mirrors the CLI's own tsconfigs: the authoring `.ts` barrel// re-exports the parser values from `.mjs`, which need to be readable.compilerOptions: {noEmit: true, skipLibCheck: true, allowJs: true, checkJs: false},@@ -94,7 +98,13 @@ const TSCONFIG = JSON.stringify(let dir;beforeAll(async () => {- dir = fs.mkdtempSync(path.join(CLI_DIR, '.codemod-tsc-'));+ // The root tsconfig names the `node` and jest-dom types.+ dir = makeLinkedFixture('astryx-codemod-tsc-', [+ '@astryxdesign/cli',+ '@astryxdesign/core',+ '@types/node',+ '@testing-library/jest-dom',+ ]);fs.mkdirSync(path.join(dir, 'pre'));fs.mkdirSync(path.join(dir, 'post'));for (const [name, src] of Object.entries(FIXTURES)) {@@ -106,7 +116,7 @@ beforeAll(async () => {});afterAll(() => {- if (dir) fs.rmSync(dir, {recursive: true, force: true});+ if (dir) removeLinkedFixture(dir);});/** Run the real tsc on a fixture dir; return {ok, output}. */diff --git a/packages/cli/clients/cli/commands/doctor.test.mjs b/packages/cli/clients/cli/commands/doctor.test.mjsindex 68ea55652c..71c7535441 100644--- a/packages/cli/clients/cli/commands/doctor.test.mjs+++ b/packages/cli/clients/cli/commands/doctor.test.mjs@@ -4,7 +4,6 @@ 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 {fileURLToPath} from 'node:url';import {Command} from 'commander';import {registerDoctor} from './doctor.mjs';@@ -118,11 +117,8 @@ describe('doctor — individual checks', () => {});it('config: PASS when a valid astryx.config.mjs loads', async () => {- // Vitest intercepts dynamic import() through Vite's resolver, which can't- // serve a file written to an arbitrary tmp path at runtime. Write the- // fixture inside the package tree so Vite can resolve it, then clean up.- const fixtureDir = fs.mkdtempSync(- path.join(path.dirname(fileURLToPath(import.meta.url)), '__doctor_cfg_'),+ const fixtureDir = fs.realpathSync(+ fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-doctor-config-')),);const configPath = path.join(fixtureDir, 'astryx.config.mjs');try {@@ -138,8 +134,8 @@ describe('doctor — individual checks', () => {});it('config: FAIL when astryx.config.mjs throws on import', async () => {- const fixtureDir = fs.mkdtempSync(- path.join(path.dirname(fileURLToPath(import.meta.url)), '__doctor_cfg_'),+ const fixtureDir = fs.realpathSync(+ fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-doctor-config-')),);const configPath = path.join(fixtureDir, 'astryx.config.mjs');try {@@ -153,8 +149,8 @@ describe('doctor — individual checks', () => {});it('config: FAIL when default export is not an object', async () => {- const fixtureDir = fs.mkdtempSync(- path.join(path.dirname(fileURLToPath(import.meta.url)), '__doctor_cfg_'),+ const fixtureDir = fs.realpathSync(+ fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-doctor-config-')),);const configPath = path.join(fixtureDir, 'astryx.config.mjs');try {diff --git a/packages/cli/foundation/render/render.test.mjs b/packages/cli/foundation/render/render.test.mjsindex 235fca3a16..20d2878a0e 100644--- a/packages/cli/foundation/render/render.test.mjs+++ b/packages/cli/foundation/render/render.test.mjs@@ -6,13 +6,13 @@*/import * as fs from 'node:fs';-import {fileURLToPath} from 'node:url';import * as path from 'node:path';import {describe, expect, it} from 'vitest';import {classesIn, cutCss, layerOrderOf, requiredClasses} from './css-cut.mjs';import {readSourceBlock, sourceBlock} from './source-block.mjs';import {isAstryxMarkup, parseHtml} from './html-reader.mjs';import {renderPage} from './render-page.mjs';+import {makeLinkedFixture, removeLinkedFixture} from '../../test-utils/linked-fixture.mjs';describe('requiredClasses with grouped selectors', () => {it('keeps a rule whose :is() or :where() alternatives are not all used', async () => {@@ -103,15 +103,13 @@ describe('the HTML reader', () => {describe('renderPage time limit', () => {it('stops a page that never finishes rendering', async () => {- const dir = fs.mkdtempSync(- path.join(path.dirname(fileURLToPath(import.meta.url)), '../../.astryx-render-loop-'),- );+ const dir = makeLinkedFixture('astryx-render-loop-', ['react', 'react-dom']);try {const file = path.join(dir, 'loop.jsx');fs.writeFileSync(file, 'export default function Page() { for (;;) {} }\n');await expect(renderPage(file, {timeoutMs: 1500})).rejects.toMatchObject({code: 'ERR_RENDER_TIMEOUT'});} finally {- fs.rmSync(dir, {recursive: true, force: true});+ removeLinkedFixture(dir);}}, 30_000);});diff --git a/packages/cli/test-utils/linked-fixture.mjs b/packages/cli/test-utils/linked-fixture.mjsindex 42f445fef6..2e30d2e5ee 100644--- a/packages/cli/test-utils/linked-fixture.mjs+++ b/packages/cli/test-utils/linked-fixture.mjs@@ -16,6 +16,7 @@ import path from 'node:path';const CLI_ROOT = path.resolve(import.meta.dirname, '..');const require = createRequire(path.join(CLI_ROOT, 'package.json'));+const CLI_PACKAGE = require('./package.json').name;/** The roots makeLinkedFixture made and has not removed. */const made = new Set();@@ -61,6 +62,7 @@ export function removeLinkedFixture(dir) {/** @param {string} name */function packageDir(name) {+ if (name === CLI_PACKAGE) return CLI_ROOT;const own = path.join(CLI_ROOT, 'node_modules', name);if (fs.existsSync(own)) return own;return path.dirname(require.resolve(`${name}/package.json`));