diff --git a/packages/bun-plugin/src/__tests__/order-independent-build.helper.ts b/packages/bun-plugin/src/__tests__/order-independent-build.helper.ts index 5c81cc5bd..27ffd9936 100644 --- a/packages/bun-plugin/src/__tests__/order-independent-build.helper.ts +++ b/packages/bun-plugin/src/__tests__/order-independent-build.helper.ts @@ -1,13 +1,15 @@ import assert from 'node:assert/strict' +import { realpathSync } from 'node:fs' import { basename, join, relative, resolve } from 'node:path' import type { BunPlugin, OnLoadCallback, PluginBuilder } from 'bun' const [fixture, schedule] = process.argv.slice(2) assert(fixture && (schedule === 'AB' || schedule === 'BA')) -process.chdir(fixture) +// The parent spawns this process inside the fixture; the plugin resolves df +// from that cwd at import time, so never change directory here. +assert.equal(realpathSync.native(process.cwd()), realpathSync.native(fixture)) -// Import only after entering the isolated fixture: the plugin resolves df at import time. const { DevupUI } = await import('../plugin') const { getUnreservedClassSlotCount } = await import('@devup-ui/wasm') const a = join(fixture, 'src/A.tsx') diff --git a/packages/next-plugin/src/__tests__/atom-hoist-harness.ts b/packages/next-plugin/src/__tests__/atom-hoist-harness.ts index 4b562fd3d..89438f54a 100644 --- a/packages/next-plugin/src/__tests__/atom-hoist-harness.ts +++ b/packages/next-plugin/src/__tests__/atom-hoist-harness.ts @@ -29,7 +29,6 @@ import assert from 'node:assert/strict'; import { mkdirSync, writeFileSync, readFileSync, rmSync } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; const root = ${JSON.stringify(root)}; -const original = process.cwd(); const sources = { 'src/shared.tsx': 'import { Box } from "@devup-ui/react"; export const Shared = () => ;', 'src/app/a/page.tsx': 'import { Box } from "@devup-ui/react"; import { Shared } from "../../shared"; export default function A() { return <>; }', @@ -46,8 +45,6 @@ mkdirSync(cssDir, { recursive: true }); writeFileSync(join(root, 'devup.json'), '{}'); writeFileSync(join(root, 'tsconfig.json'), '{}'); writeFileSync(join(root, 'package.json'), '{"name":"atom-hoist-fixture","private":true}'); -process.chdir(root); -try { ${setup} const outputs = {}; for (const file of ${reverse} ? [...files].reverse() : files) { @@ -115,13 +112,11 @@ ${setup} sheets: Object.fromEntries(Object.entries(sheets).sort().map(([name, css]) => [name, clean(css)])), })); await finish(); -} finally { - process.chdir(original); -} process.exit(0); `, ], - { encoding: 'utf8', timeout: 30_000, env: process.env }, + // The fixture is the cwd from the start, as for a real build. + { cwd: root, encoding: 'utf8', timeout: 30_000, env: process.env }, ).trim() try { return [run(false), run(true), run(false)] as const diff --git a/packages/next-plugin/src/__tests__/deterministic-numbering.test.ts b/packages/next-plugin/src/__tests__/deterministic-numbering.test.ts index b18a334c8..dd4517319 100644 --- a/packages/next-plugin/src/__tests__/deterministic-numbering.test.ts +++ b/packages/next-plugin/src/__tests__/deterministic-numbering.test.ts @@ -29,8 +29,8 @@ const schemes: Record (path: string) => string> = { } describe('class and file numbers do not depend on the order files are seen', () => { + // Every path below is built from root; the process cwd is never changed. let root: string - const original = process.cwd() beforeAll(() => { root = mkdtempSync(join(tmpdir(), 'devup-ui-numbering-order-')) @@ -38,12 +38,10 @@ describe('class and file numbers do not depend on the order files are seen', () mkdirSync(dirname(join(root, path)), { recursive: true }) writeFileSync(join(root, path), code) } - process.chdir(root) }) afterAll(() => { wasm.resetBuildState() - process.chdir(original) rmSync(root, { recursive: true, force: true }) }) diff --git a/packages/next-plugin/src/__tests__/imported-styled.test.ts b/packages/next-plugin/src/__tests__/imported-styled.test.ts index aeebafb53..a6be26526 100644 --- a/packages/next-plugin/src/__tests__/imported-styled.test.ts +++ b/packages/next-plugin/src/__tests__/imported-styled.test.ts @@ -11,15 +11,14 @@ import { dirname, join } from 'node:path' import { pathToFileURL } from 'node:url' import * as wasm from '@devup-ui/wasm' -import { afterEach, beforeEach, describe, expect, it } from 'bun:test' +import { afterEach, beforeEach, describe, expect, it, spyOn } from 'bun:test' import { withModuleResolver } from '../wasm' import { assertImportedStyledRendering } from './imported-styled-render' -const originalCwd = process.cwd() - describe('styled components another module defines', () => { let root: string + let cwd: ReturnType const write = (path: string, code: string) => { mkdirSync(dirname(join(root, path)), { recursive: true }) @@ -58,12 +57,14 @@ describe('styled components another module defines', () => { 'node_modules/@acme/ui/src/index.js', "import styled from '@emotion/styled'\nexport const Card = styled.section`border: 13px solid;`\n", ) - process.chdir(root) + // The Next resolver names files relative to the project cwd; point it at + // the fixture without changing the directory of the whole test process. + cwd = spyOn(process, 'cwd').mockReturnValue(root) withModuleResolver(wasm) }) afterEach(() => { - process.chdir(originalCwd) + cwd.mockRestore() rmSync(root, { recursive: true, force: true }) }) diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 639d7cfe3..a91d3b66d 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -1,5 +1,6 @@ import { spawnSync } from 'node:child_process' import * as fs from 'node:fs' +import { tmpdir } from 'node:os' import { join, resolve } from 'node:path' import type { StaticImportGraph } from '@devup-ui/plugin-utils' @@ -254,14 +255,16 @@ describe('DevupUINextPlugin', () => { }) it('prewarms and serves actual default Turbo extraction through native state', () => { + // The child starts inside the fixture, as Next does, instead of changing + // its directory after the plugin modules have loaded. + const root = fs.mkdtempSync(join(tmpdir(), 'devup-next-native-')) const child = spawnSync( process.execPath, [ '-e', ` import assert from 'node:assert/strict'; - import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; - import { tmpdir } from 'node:os'; + import { mkdirSync, realpathSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { DevupUI } from ${JSON.stringify(new URL('../plugin.ts', import.meta.url).href)}; import loader from ${JSON.stringify(new URL('../loader.ts', import.meta.url).href)}; @@ -270,7 +273,8 @@ describe('DevupUINextPlugin', () => { import { loadCompiler } from ${JSON.stringify(new URL('../../../plugin-utils/src/index.ts', import.meta.url).href)}; const native = loadCompiler('native'), wasm = loadCompiler('wasm'); const primedWasm = loadWasm('wasm'); // Resolve the old loader dependency at its installed location. - const cwd = process.cwd(), root = mkdtempSync(join(tmpdir(), 'devup-next-native-')); + const root = ${JSON.stringify(root)}; + assert.equal(realpathSync.native(process.cwd()), realpathSync.native(root)); try { mkdirSync(join(root, 'src/app'), { recursive: true }); writeFileSync(join(root, 'tsconfig.json'), JSON.stringify({ compilerOptions: { jsx: 'react-jsx' }, include: ['src'] })); @@ -278,7 +282,6 @@ describe('DevupUINextPlugin', () => { const source = "import {Box} from '@devup-ui/react'; import {tone} from '../tokens'; export default function Page(){return ;}"; const resourcePath = join(root, 'src/app/page.tsx'); writeFileSync(resourcePath, source); - process.chdir(root); native.resetStateForTesting(); wasm.resetStateForTesting(); withModuleResolver(primedWasm); // Match normal fixed-cwd supplier resolution after fixture setup. const before = { native: native.getCss(null, false), wasm: wasm.getCss(null, false) }; @@ -297,17 +300,18 @@ describe('DevupUINextPlugin', () => { } finally { await flushCoordinatorWrites(); resetCoordinator(); native.resetStateForTesting(); wasm.resetStateForTesting(); - process.chdir(cwd); rmSync(root, { recursive: true, force: true }); } `, ], { + cwd: root, encoding: 'utf8', timeout: 12000, env: { ...process.env, NODE_ENV: 'production', TURBOPACK: '1' }, windowsHide: true, }, ) + fs.rmSync(root, { recursive: true, force: true }) console.info(child.stdout) expect(child.error).toBeUndefined() expect(child.signal).toBeNull() diff --git a/packages/vite-plugin/src/__tests__/css-http-native.ts b/packages/vite-plugin/src/__tests__/css-http-native.ts index 97929102b..fcc8086b5 100644 --- a/packages/vite-plugin/src/__tests__/css-http-native.ts +++ b/packages/vite-plugin/src/__tests__/css-http-native.ts @@ -7,13 +7,15 @@ import { createServer } from 'vite' import { DevupUI } from '../plugin' -// This helper is bundled into an owned temp directory, then run only by Node. +// This helper is bundled into an owned temp directory, then run only by Node +// with the project below as its cwd, so it never changes directory itself. const root = join( realpathSync(dirname(fileURLToPath(import.meta.url))), 'project', ) const accept = process.argv[2] assert(accept === 'text/css' || accept === '*/*') +assert.equal(realpathSync.native(process.cwd()), realpathSync.native(root)) mkdirSync(join(root, 'src'), { recursive: true }) writeFileSync(join(root, 'devup.json'), '{}') writeFileSync(join(root, 'package.json'), '{"type":"module"}') @@ -25,7 +27,6 @@ writeFileSync( join(root, 'src/main.js'), "import { globalCss } from '@devup-ui/react'; globalCss({ body: { outlineStyle: 'dashed' } });", ) -process.chdir(root) // Given: actual extraction and Vite with its default strict filesystem policy. const server = await createServer({ diff --git a/packages/vite-plugin/src/__tests__/css-http-representation.test.ts b/packages/vite-plugin/src/__tests__/css-http-representation.test.ts index dcdb51c62..5a57f8e13 100644 --- a/packages/vite-plugin/src/__tests__/css-http-representation.test.ts +++ b/packages/vite-plugin/src/__tests__/css-http-representation.test.ts @@ -1,6 +1,12 @@ import assert from 'node:assert/strict' import { spawnSync } from 'node:child_process' -import { mkdtempSync, realpathSync, rmSync, symlinkSync } from 'node:fs' +import { + mkdirSync, + mkdtempSync, + realpathSync, + rmSync, + symlinkSync, +} from 'node:fs' import { tmpdir } from 'node:os' import { join, resolve } from 'node:path' @@ -36,11 +42,14 @@ it.each(['text/css', '*/*'])( }) assert(bundle.success, bundle.logs.map(String).join('\n')) - // When: native Node owns Vite and HTTP, avoiding Bun's Windows fs identity. + // When: native Node owns Vite and HTTP, avoiding Bun's Windows fs identity, + // and starts inside the project the probe serves. + const project = join(root, 'project') + mkdirSync(project) const result = spawnSync( 'node', [owner, '45000', join(root, 'probe.mjs'), accept], - { encoding: 'utf8', timeout: 60000, windowsHide: true }, + { cwd: project, encoding: 'utf8', timeout: 60000, windowsHide: true }, ) // Then: all real HTTP assertions passed and the owned process tree drained.